Solution for Bluetooth SIG hands-on training course
Dependencies: BLE_API mbed-dev-bin nRF51822-bluetooth-mdw
Fork of microbit-dal-bluetooth-mdw_starter by
Revision 74:9771cd712730, committed 2016-12-27
- Comitter:
- bluetooth_mdw
- Date:
- Tue Dec 27 10:48:18 2016 +0000
- Parent:
- 73:eb91bba49623
- Child:
- 75:6f9e0accbd83
- Commit message:
- New Bluetooth animation service
Changed in this revision
--- a/inc/bluetooth/MicroBitAccelerometerService.h Wed Jul 13 14:32:54 2016 +0000 +++ b/inc/bluetooth/MicroBitAccelerometerService.h Tue Dec 27 10:48:18 2016 +0000 @@ -36,7 +36,6 @@ 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 Bluetooth, and provides basic configuration options.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/inc/bluetooth/MicroBitAnimationService.h Tue Dec 27 10:48:18 2016 +0000 @@ -0,0 +1,80 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 British Broadcasting Corporation. +This software is provided by Lancaster University by arrangement with the BBC. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO Animation SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +#ifndef MICROBIT_ANIMATION_SERVICE_H +#define MICROBIT_ANIMATION_SERVICE_H + +#include "MicroBitConfig.h" +#include "Animator.h" +#include "ble/BLE.h" + +// UUIDs for our service and characteristics +extern const uint8_t MicroBitAnimationServiceUUID[]; +// animation type: indicates the type of animation that should be executed by the microbit : R|W +extern const uint8_t MicroBitAnimationServiceAnimationTypeCharacteristicUUID[]; +// animation status: indicates whether or not an animation is currently in progress : R|N +extern const uint8_t MicroBitAnimationServiceAnimationStatusCharacteristicUUID[]; +// animation control: allows various types of control to be exercised (start|stop|faster|slower) : W +extern const uint8_t MicroBitAnimationServiceAnimationControlCharacteristicUUID[]; + +/** + * Class definition for a MicroBit BLE Animation Service. + */ +class MicroBitAnimationService +{ + public: + + /** + * Constructor. + * Create a representation of the AnimationService + * @param _ble The instance of a BLE device that we're running on. + */ + MicroBitAnimationService(BLEDevice &_ble); + + /** + * Callback. Invoked when a characteristic is written to + */ + void onDataWritten(const GattWriteCallbackParams *params); + + private: + + // Bluetooth stack we're running on. + BLEDevice &ble; + + // memory for our Animation characteristics. + uint8_t animation_type_buffer[1]; + uint8_t animation_status_buffer[1]; + uint8_t animation_control_buffer[1]; + + // handles on this service's characterisitics. + GattAttribute::Handle_t animationTypeCharacteristicHandle; + GattAttribute::Handle_t animationStatusCharacteristicHandle; + GattAttribute::Handle_t animationControlCharacteristicHandle; + + void animationStatusUpdate(MicroBitEvent e); + +}; + +#endif
--- a/inc/bluetooth/MicroBitBLEManager.h Wed Jul 13 14:32:54 2016 +0000 +++ b/inc/bluetooth/MicroBitBLEManager.h Tue Dec 27 10:48:18 2016 +0000 @@ -61,6 +61,8 @@ #include "MicroBitButton.h" #include "MicroBitStorage.h" +#include "MicroBitAnimationService.h" + #define MICROBIT_BLE_PAIR_REQUEST 0x01 #define MICROBIT_BLE_PAIR_COMPLETE 0x02 #define MICROBIT_BLE_PAIR_PASSCODE 0x04
--- a/inc/core/MicroBitConfig.h Wed Jul 13 14:32:54 2016 +0000 +++ b/inc/core/MicroBitConfig.h Tue Dec 27 10:48:18 2016 +0000 @@ -101,7 +101,7 @@ // For standard S110 builds, this should be word aligned and in the range 0x300 - 0x700. // Any unused memory will be automatically reclaimed as HEAP memory if both MICROBIT_HEAP_REUSE_SD and MICROBIT_HEAP_ALLOCATOR are enabled. #ifndef MICROBIT_SD_GATT_TABLE_SIZE -#define MICROBIT_SD_GATT_TABLE_SIZE 0x300 +#define MICROBIT_SD_GATT_TABLE_SIZE 0x500 #endif // @@ -187,7 +187,7 @@ // Open BLE links are not secure, but commonly used during the development of BLE services // Set '1' to disable all secuity #ifndef MICROBIT_BLE_OPEN -#define MICROBIT_BLE_OPEN 0 +#define MICROBIT_BLE_OPEN 1 #endif // Configure for open BLE operation if so configured
--- a/mbed-dev-bin.lib Wed Jul 13 14:32:54 2016 +0000 +++ b/mbed-dev-bin.lib Tue Dec 27 10:48:18 2016 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/Lancaster-University/code/mbed-dev-bin/#768173a57492 +https://developer.mbed.org/teams/Lancaster-University/code/mbed-dev-bin/#588aed8f3091
--- a/nRF51822.lib Wed Jul 13 14:32:54 2016 +0000 +++ b/nRF51822.lib Tue Dec 27 10:48:18 2016 +0000 @@ -1,1 +1,1 @@ -https://developer.mbed.org/teams/Lancaster-University/code/nRF51822/#b84f72a53341 +https://developer.mbed.org/teams/Lancaster-University/code/nRF51822/#f4b6d92d164f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/source/bluetooth/MicroBitAnimationService.cpp Tue Dec 27 10:48:18 2016 +0000 @@ -0,0 +1,146 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 British Broadcasting Corporation. +This software is provided by Lancaster University by arrangement with the BBC. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO Animation SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +/** + * Class definition for a MicroBit BLE Animation Service. + * Provides a BLE gateway onto an Animation Model. + */ + +#include "MicroBitConfig.h" +#include "MicroBitAnimationService.h" +#include "ble/UUID.h" +#include "Animator.h" + +/** + * Constructor. + * Create a representation of the AnimationService + * @param _ble The instance of a BLE device that we're running on. + */ +MicroBitAnimationService::MicroBitAnimationService(BLEDevice &_ble) : + ble(_ble) +{ + // UUID, valuePTR, length, max length, properties + GattCharacteristic animationTypeCharacteristic( + MicroBitAnimationServiceAnimationTypeCharacteristicUUID, + (uint8_t *)animation_type_buffer, + 1, + 1, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); + + GattCharacteristic animationStatusCharacteristic( + MicroBitAnimationServiceAnimationStatusCharacteristicUUID, + (uint8_t *)animation_status_buffer, + 1, + 1, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); + + GattCharacteristic animationControlCharacteristic( + MicroBitAnimationServiceAnimationControlCharacteristicUUID, + (uint8_t *)animation_control_buffer, + 1, + 1, + GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); + + animation_type_buffer[0] = 0x00; + animation_status_buffer[0] = 0x00; + animation_control_buffer[0] = 0x00; + + // Set default security requirements - optional + // animationTypeCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); + // animationStatusCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); + // animationControlCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); + + // create an array of our characteristics so we can pass them to the service when we create it + GattCharacteristic *characteristics[] = {&animationTypeCharacteristic, &animationStatusCharacteristic, &animationControlCharacteristic}; + + // create the animation service and specify its characteristics + GattService service(MicroBitAnimationServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); + + // add the service to the Bluetooth attribute table + ble.addService(service); + + // take a note of the handles of our new characteristics now that they're in the attribute table + animationTypeCharacteristicHandle = animationTypeCharacteristic.getValueHandle(); + animationStatusCharacteristicHandle = animationStatusCharacteristic.getValueHandle(); + animationControlCharacteristicHandle = animationControlCharacteristic.getValueHandle(); + + ble.gattServer().write(animationTypeCharacteristicHandle, (const uint8_t *)&animation_type_buffer, sizeof(animation_type_buffer)); + ble.gattServer().write(animationStatusCharacteristicHandle, (const uint8_t *)&animation_status_buffer, sizeof(animation_status_buffer)); + ble.gattServer().write(animationControlCharacteristicHandle, (const uint8_t *)&animation_control_buffer, sizeof(animation_control_buffer)); + + // register a callback function for when a characteristic is written to + ble.onDataWritten(this, &MicroBitAnimationService::onDataWritten); + + // subscribe to animation status events so they can be notified to the connected client + if (EventModel::defaultEventBus) + EventModel::defaultEventBus->listen(ANIMATION_STATUS_EVENT, MICROBIT_EVT_ANY, this, &MicroBitAnimationService::animationStatusUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE); +} + + +/** + * Callback. Invoked when any of our attributes are written via BLE. + */ +void MicroBitAnimationService::onDataWritten(const GattWriteCallbackParams *params) +{ + + if (params->handle == animationTypeCharacteristicHandle && params->len >= 1) { + uint8_t *value = (uint8_t *)params->data; + uint16_t event_value = static_cast<uint16_t>(value[0]); + MicroBitEvent evt(ANIMATION_TYPE_EVENT, event_value); + return; + } + + if (params->handle == animationControlCharacteristicHandle && params->len >= 1) { + uint8_t *value = (uint8_t *)params->data; + uint16_t event_value = static_cast<uint16_t>(value[0]); + MicroBitEvent evt(ANIMATION_CONTROL_EVENT, event_value); + return; + } +} + +void MicroBitAnimationService::animationStatusUpdate(MicroBitEvent e) +{ + if (ble.getGapState().connected) + { + animation_status_buffer[0] = e.value; + ble.gattServer().notify(animationStatusCharacteristicHandle,(uint8_t *)animation_status_buffer, 1); + } +} + +const uint8_t MicroBitAnimationServiceUUID[] = { + 0xe9,0x5d,0x71,0x70,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +}; + +const uint8_t MicroBitAnimationServiceAnimationTypeCharacteristicUUID[] = { + 0xe9,0x5d,0xC3,0x06,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +}; + +const uint8_t MicroBitAnimationServiceAnimationStatusCharacteristicUUID[] = { + 0xe9,0x5d,0x45,0x92,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +}; + +const uint8_t MicroBitAnimationServiceAnimationControlCharacteristicUUID[] = { + 0xe9,0x5d,0xb8,0x4c,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 +};