Merge pull request #296 from smartyw/master

BLE Magnetometer service has new characteristic for requesting calilbration
This commit is contained in:
Joe Finney 2017-07-18 11:14:13 +01:00 committed by GitHub
commit f90ef0030d
3 changed files with 74 additions and 9 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.
@ -64,14 +69,24 @@ class MicroBitMagnetometerService
/**
* Magnetometer update callback
*/
void magnetometerUpdate(MicroBitEvent e);
void magnetometerUpdate();
/**
* Sample Period Change Needed callback.
* Reconfiguring the magnetometer can to a REALLY long time (sometimes even seconds to complete)
* So we do this in the background when necessary, through this event handler.
*/
void samplePeriodUpdateNeeded(MicroBitEvent e);
void samplePeriodUpdateNeeded();
/**
*calibrate compass
*/
void calibrateCompass();
/**
* Handle compass events such as calibration requests
*/
void compassEvents(MicroBitEvent e);
// Bluetooth stack we're running on.
BLEDevice &ble;
@ -81,11 +96,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

@ -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

@ -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,19 +78,47 @@ 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)
{
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_DATA_UPDATE, this, &MicroBitMagnetometerService::compassEvents, MESSAGE_BUS_LISTENER_IMMEDIATE);
EventModel::defaultEventBus->listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CONFIG_NEEDED, this, &MicroBitMagnetometerService::compassEvents);
EventModel::defaultEventBus->listen(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CALIBRATION_NEEDED, this, &MicroBitMagnetometerService::compassEvents);
}
}
void MicroBitMagnetometerService::calibrateCompass() {
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));
}
void MicroBitMagnetometerService::compassEvents(MicroBitEvent e) {
if (e.value == MICROBIT_COMPASS_EVT_DATA_UPDATE) {
magnetometerUpdate();
return;
}
if (e.value == MICROBIT_COMPASS_EVT_CONFIG_NEEDED) {
samplePeriodUpdateNeeded();
return;
}
if (e.value == MICROBIT_COMPASS_EVT_CALIBRATION_NEEDED) {
calibrateCompass();
return;
}
}
/**
* Callback. Invoked when any of our attributes are written via BLE.
*/
@ -94,13 +128,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) {
MicroBitEvent evt(MICROBIT_ID_COMPASS, MICROBIT_COMPASS_EVT_CALIBRATION_NEEDED);
}
return;
}
}
/**
* Magnetometer update callback
*/
void MicroBitMagnetometerService::magnetometerUpdate(MicroBitEvent)
void MicroBitMagnetometerService::magnetometerUpdate()
{
if (ble.getGapState().connected)
{
@ -125,7 +169,7 @@ void MicroBitMagnetometerService::magnetometerUpdate(MicroBitEvent)
* Reconfiguring the magnetometer can to a REALLY long time (sometimes even seconds to complete)
* So we do this in the background when necessary, through this event handler.
*/
void MicroBitMagnetometerService::samplePeriodUpdateNeeded(MicroBitEvent)
void MicroBitMagnetometerService::samplePeriodUpdateNeeded()
{
// Reconfigure the compass. This might take a while...
compass.setPeriod(magnetometerPeriodCharacteristicBuffer);
@ -154,3 +198,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
};