Added magnetometer calibration characteristic to the Bluetooth Magnetometer Service

This commit is contained in:
Martin Woolley 2017-06-06 07:50:03 +01:00
parent 621e328df1
commit 66fd96b098
3 changed files with 18 additions and 9 deletions

View file

@ -78,6 +78,11 @@ class MicroBitMagnetometerService
*/
void samplePeriodUpdateNeeded(MicroBitEvent e);
/**
* Initiate compass calibration procedure
*/
void calibrateCompass(MicroBitEvent e);
// Bluetooth stack we're running on.
BLEDevice &ble;
MicroBitCompass &compass;

View file

@ -88,7 +88,7 @@ extern const MAG3110SampleRateConfig MAG3110SampleRate[];
#define MICROBIT_COMPASS_EVT_DATA_UPDATE 4
#define MICROBIT_COMPASS_EVT_CONFIG_NEEDED 5
#define MICROBIT_COMPASS_EVT_CALIBRATE 6
#define MICROBIT_COMPASS_EVT_CALIBRATION_NEEDED 7
/**
* Status Bits
*/

View file

@ -90,9 +90,20 @@ MicroBitMagnetometerService::MicroBitMagnetometerService(BLEDevice &_ble, MicroB
{
EventModel::defaultEventBus->listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_DATA_UPDATE, this, &MicroBitMagnetometerService::magnetometerUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE);
EventModel::defaultEventBus->listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CONFIG_NEEDED, this, &MicroBitMagnetometerService::samplePeriodUpdateNeeded);
EventModel::defaultEventBus->listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CALIBRATION_NEEDED, this, &MicroBitMagnetometerService::calibrateCompass);
}
}
void MicroBitMagnetometerService::calibrateCompass(MicroBitEvent) {
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));
}
/**
* Callback. Invoked when any of our attributes are written via BLE.
*/
@ -109,15 +120,8 @@ void MicroBitMagnetometerService::onDataWritten(const GattWriteCallbackParams *p
{
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));
MicroBitEvent evt(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CALIBRATION_NEEDED);
}
return;
}
}