From 609cb6f2d8df9bee3c935ff878263a4fff1f9e0e Mon Sep 17 00:00:00 2001 From: Joe Finney Date: Wed, 9 Sep 2015 15:16:11 +0100 Subject: [PATCH] microbit: Added first draft of BLE Accelerometer service n.b. this code is as yet untested. --- inc/MicroBitAccelerometerService.h | 48 ++++++++++++ source/CMakeLists.txt | 1 + .../MicroBitAccelerometerService.cpp | 73 +++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 inc/MicroBitAccelerometerService.h create mode 100644 source/ble-services/MicroBitAccelerometerService.cpp diff --git a/inc/MicroBitAccelerometerService.h b/inc/MicroBitAccelerometerService.h new file mode 100644 index 0000000..65dc5d6 --- /dev/null +++ b/inc/MicroBitAccelerometerService.h @@ -0,0 +1,48 @@ +#ifndef MICROBIT_ACCELEROMETER_SERVICE_H +#define MICROBIT_ACCELEROMETER_SERVICE_H + +#include "MicroBit.h" + +// UUIDs for our service and characteristics +extern const uint8_t MicroBitAccelerometerServiceUUID[]; +extern const uint8_t MicroBitAccelerometerServiceDataUUID[]; +extern const uint8_t MicroBitAccelerometerServicePeriodUUID[]; + + +/** + * Class definition for a MicroBit BLE Accelerometer Service. + * Provides access to live accelerometer data via BLE, and provides basic configuration options. + */ +class MicroBitAccelerometerService +{ + public: + + /** + * Constructor. + * Create a representation of the AccelerometerService + * @param _ble The instance of a BLE device that we're running on. + */ + MicroBitAccelerometerService(BLEDevice &_ble); + + /** + * Callback. Invoked when any of our attributes are written via BLE. + */ + void onDataWritten(const GattWriteCallbackParams *params); + + private: + + // Bluetooth stack we're running on. + BLEDevice &ble; + + // memory for our 8 bit control characteristics. + uint16_t accelerometerDataCharacteristicBuffer[3]; + uint8_t accelerometerPeriodCharacteristicBuffer; + + // Handles to access each characteristic when they are held by Soft Device. + GattAttribute::Handle_t accelerometerDataCharacteristicHandle; + GattAttribute::Handle_t accelerometerPeriodCharacteristicHandle; +}; + + +#endif + diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index e902b52..8cd1472 100755 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -29,6 +29,7 @@ set(YOTTA_AUTO_MICROBIT-DAL_CPP_FILES "ble-services/MicroBitDFUService.cpp" "ble-services/MicroBitEventService.cpp" "ble-services/MicroBitLEDService.cpp" + "ble-services/MicroBitAccelerometerService.cpp" ) if (YOTTA_CFG_MICROBIT_CONFIGFILE) diff --git a/source/ble-services/MicroBitAccelerometerService.cpp b/source/ble-services/MicroBitAccelerometerService.cpp new file mode 100644 index 0000000..874a3a6 --- /dev/null +++ b/source/ble-services/MicroBitAccelerometerService.cpp @@ -0,0 +1,73 @@ +/** + * Class definition for the custom MicroBit Accelerometer Service. + * Provides a BLE service to remotely read the state of the accelerometer, and configure its behaviour. + */ + +#include "MicroBit.h" +#include "ble/UUID.h" + +#include "MicroBitAccelerometerService.h" + +/** + * Constructor. + * Create a representation of the AccelerometerService + * @param _ble The instance of a BLE device that we're running on. + */ +MicroBitAccelerometerService::MicroBitAccelerometerService(BLEDevice &_ble) : + ble(_ble) +{ + // Create the data structures that represent each of our characteristics in Soft Device. + GattCharacteristic accelerometerDataCharacteristic(MicroBitAccelerometerServiceDataUUID, (uint8_t *)accelerometerDataCharacteristicBuffer, 0, + sizeof(accelerometerDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); + + GattCharacteristic accelerometerPeriodCharacteristic(MicroBitAccelerometerServicePeriodUUID, (uint8_t *)accelerometerPeriodCharacteristicBuffer, 0, + sizeof(accelerometerPeriodCharacteristicBuffer), + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE); + + // Initialise our characteristic values. + accelerometerDataCharacteristicBuffer[0] = 0; + accelerometerDataCharacteristicBuffer[1] = 0; + accelerometerDataCharacteristicBuffer[2] = 0; + accelerometerPeriodCharacteristicBuffer = 0; + + GattCharacteristic *characteristics[] = {&accelerometerDataCharacteristic, &accelerometerPeriodCharacteristic}; + GattService service(MicroBitAccelerometerServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); + + ble.addService(service); + + accelerometerDataCharacteristicHandle = accelerometerDataCharacteristic.getValueHandle(); + accelerometerPeriodCharacteristicHandle = accelerometerPeriodCharacteristic.getValueHandle(); + + ble.updateCharacteristicValue(accelerometerDataCharacteristicHandle, (const uint8_t *)&accelerometerDataCharacteristicBuffer, sizeof(accelerometerDataCharacteristicBuffer)); + ble.updateCharacteristicValue(accelerometerPeriodCharacteristicHandle, (const uint8_t *)&accelerometerPeriodCharacteristicBuffer, sizeof(accelerometerPeriodCharacteristicBuffer)); + + ble.onDataWritten(this, &MicroBitAccelerometerService::onDataWritten); +} + + +/** + * Callback. Invoked when any of our attributes are written via BLE. + */ +void MicroBitAccelerometerService::onDataWritten(const GattWriteCallbackParams *params) +{ + if (params->handle == accelerometerPeriodCharacteristicHandle && params->len >= sizeof(accelerometerPeriodCharacteristicBuffer)) + { + accelerometerPeriodCharacteristicBuffer = *((uint8_t *)params->data); + uBit.accelerometer.setPeriod(accelerometerPeriodCharacteristicBuffer); + } +} + + +const uint8_t MicroBitAccelerometerServiceUUID[] = { + 0xe9,0x5d,0x07,0x53,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +}; + +const uint8_t MicroBitAccelerometerServiceDataUUID[] = { + 0xe9,0x5d,0xca,0x4b,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +}; + +const uint8_t MicroBitAccelerometerServicePeriodUUID[] = { + 0xe9,0x5d,0xfb,0x24,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +}; + +