Added new Magnetometer Calibration characteristic to Magnetometer Service

This commit is contained in:
Martin Woolley 2017-05-30 10:17:08 +01:00
parent d5ee7d99b6
commit 621e328df1
2 changed files with 38 additions and 2 deletions

View file

@ -31,12 +31,17 @@ DEALINGS IN THE SOFTWARE.
#include "MicroBitCompass.h"
#include "EventModel.h"
#define COMPASS_CALIBRATION_STATUS_UNKNOWN 0
#define COMPASS_CALIBRATION_REQUESTED 1
#define COMPASS_CALIBRATION_COMPLETED_OK 2
#define COMPASS_CALIBRATION_COMPLETED_ERR 3
// UUIDs for our service and characteristics
extern const uint8_t MicroBitMagnetometerServiceUUID[];
extern const uint8_t MicroBitMagnetometerServiceDataUUID[];
extern const uint8_t MicroBitMagnetometerServiceBearingUUID[];
extern const uint8_t MicroBitMagnetometerServicePeriodUUID[];
extern const uint8_t MicroBitMagnetometerServiceCalibrationUUID[];
/**
* Class definition for the MicroBit BLE Magnetometer Service.
@ -81,11 +86,13 @@ class MicroBitMagnetometerService
int16_t magnetometerDataCharacteristicBuffer[3];
uint16_t magnetometerBearingCharacteristicBuffer;
uint16_t magnetometerPeriodCharacteristicBuffer;
uint8_t magnetometerCalibrationCharacteristicBuffer;
// Handles to access each characteristic when they are held by Soft Device.
GattAttribute::Handle_t magnetometerDataCharacteristicHandle;
GattAttribute::Handle_t magnetometerBearingCharacteristicHandle;
GattAttribute::Handle_t magnetometerPeriodCharacteristicHandle;
GattAttribute::Handle_t magnetometerCalibrationCharacteristicHandle;
};
#endif

View file

@ -52,19 +52,25 @@ MicroBitMagnetometerService::MicroBitMagnetometerService(BLEDevice &_ble, MicroB
sizeof(magnetometerPeriodCharacteristicBuffer),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
GattCharacteristic magnetometerCalibrationCharacteristic(MicroBitMagnetometerServiceCalibrationUUID, (uint8_t *)&magnetometerCalibrationCharacteristicBuffer, 0,
sizeof(magnetometerCalibrationCharacteristicBuffer),
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
// Initialise our characteristic values.
magnetometerDataCharacteristicBuffer[0] = 0;
magnetometerDataCharacteristicBuffer[1] = 0;
magnetometerDataCharacteristicBuffer[2] = 0;
magnetometerBearingCharacteristicBuffer = 0;
magnetometerPeriodCharacteristicBuffer = compass.getPeriod();
magnetometerCalibrationCharacteristicBuffer = 0;
// Set default security requirements
magnetometerDataCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
magnetometerBearingCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
magnetometerPeriodCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
magnetometerCalibrationCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
GattCharacteristic *characteristics[] = {&magnetometerDataCharacteristic, &magnetometerBearingCharacteristic, &magnetometerPeriodCharacteristic};
GattCharacteristic *characteristics[] = {&magnetometerDataCharacteristic, &magnetometerBearingCharacteristic, &magnetometerPeriodCharacteristic, &magnetometerCalibrationCharacteristic};
GattService service(MicroBitMagnetometerServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
ble.addService(service);
@ -72,10 +78,12 @@ MicroBitMagnetometerService::MicroBitMagnetometerService(BLEDevice &_ble, MicroB
magnetometerDataCharacteristicHandle = magnetometerDataCharacteristic.getValueHandle();
magnetometerBearingCharacteristicHandle = magnetometerBearingCharacteristic.getValueHandle();
magnetometerPeriodCharacteristicHandle = magnetometerPeriodCharacteristic.getValueHandle();
magnetometerCalibrationCharacteristicHandle = magnetometerCalibrationCharacteristic.getValueHandle();
ble.gattServer().notify(magnetometerDataCharacteristicHandle,(uint8_t *)magnetometerDataCharacteristicBuffer, sizeof(magnetometerDataCharacteristicBuffer));
ble.gattServer().notify(magnetometerBearingCharacteristicHandle,(uint8_t *)&magnetometerBearingCharacteristicBuffer, sizeof(magnetometerBearingCharacteristicBuffer));
ble.gattServer().write(magnetometerPeriodCharacteristicHandle, (const uint8_t *)&magnetometerPeriodCharacteristicBuffer, sizeof(magnetometerPeriodCharacteristicBuffer));
ble.gattServer().write(magnetometerCalibrationCharacteristicHandle, (const uint8_t *)&magnetometerCalibrationCharacteristicBuffer, sizeof(magnetometerCalibrationCharacteristicBuffer));
ble.onDataWritten(this, &MicroBitMagnetometerService::onDataWritten);
if (EventModel::defaultEventBus)
@ -94,6 +102,23 @@ void MicroBitMagnetometerService::onDataWritten(const GattWriteCallbackParams *p
{
magnetometerPeriodCharacteristicBuffer = *((uint16_t *)params->data);
MicroBitEvent evt(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CONFIG_NEEDED);
return;
}
if (params->handle == magnetometerCalibrationCharacteristicHandle && params->len >= sizeof(magnetometerCalibrationCharacteristicBuffer))
{
magnetometerCalibrationCharacteristicBuffer = *((uint8_t *)params->data);
if (magnetometerCalibrationCharacteristicBuffer == COMPASS_CALIBRATION_REQUESTED) {
int rc = compass.calibrate();
if (rc == MICROBIT_OK) {
magnetometerCalibrationCharacteristicBuffer = COMPASS_CALIBRATION_COMPLETED_OK;
} else {
magnetometerCalibrationCharacteristicBuffer = COMPASS_CALIBRATION_COMPLETED_ERR;
}
ble.gattServer().notify(magnetometerCalibrationCharacteristicHandle,(uint8_t *)&magnetometerCalibrationCharacteristicBuffer, sizeof(magnetometerCalibrationCharacteristicBuffer));
}
return;
}
}
@ -154,3 +179,7 @@ const uint8_t MicroBitMagnetometerServicePeriodUUID[] = {
const uint8_t MicroBitMagnetometerServiceBearingUUID[] = {
0xe9,0x5d,0x97,0x15,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
};
const uint8_t MicroBitMagnetometerServiceCalibrationUUID[] = {
0xE9,0x5D,0xB3,0x58,0x25,0x1D,0x47,0x0A,0xA0,0x62,0xFA,0x19,0x22,0xDF,0xA9,0xA8
};