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
source/bluetooth/MicroBitAnimationService.cpp@81:58bd55de8fc6, 2017-02-06 (annotated)
- Committer:
- bluetooth_mdw
- Date:
- Mon Feb 06 09:22:57 2017 +0000
- Revision:
- 81:58bd55de8fc6
Added button B to display name
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bluetooth_mdw | 81:58bd55de8fc6 | 1 | /* |
bluetooth_mdw | 81:58bd55de8fc6 | 2 | The MIT License (MIT) |
bluetooth_mdw | 81:58bd55de8fc6 | 3 | |
bluetooth_mdw | 81:58bd55de8fc6 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
bluetooth_mdw | 81:58bd55de8fc6 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
bluetooth_mdw | 81:58bd55de8fc6 | 6 | |
bluetooth_mdw | 81:58bd55de8fc6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
bluetooth_mdw | 81:58bd55de8fc6 | 8 | copy of this software and associated documentation files (the "Software"), |
bluetooth_mdw | 81:58bd55de8fc6 | 9 | to deal in the Software without restriction, including without limitation |
bluetooth_mdw | 81:58bd55de8fc6 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
bluetooth_mdw | 81:58bd55de8fc6 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
bluetooth_mdw | 81:58bd55de8fc6 | 12 | Software is furnished to do so, subject to the following conditions: |
bluetooth_mdw | 81:58bd55de8fc6 | 13 | |
bluetooth_mdw | 81:58bd55de8fc6 | 14 | The above copyright notice and this permission notice shall be included in |
bluetooth_mdw | 81:58bd55de8fc6 | 15 | all copies or substantial portions of the Software. |
bluetooth_mdw | 81:58bd55de8fc6 | 16 | |
bluetooth_mdw | 81:58bd55de8fc6 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
bluetooth_mdw | 81:58bd55de8fc6 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
bluetooth_mdw | 81:58bd55de8fc6 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO Animation SHALL |
bluetooth_mdw | 81:58bd55de8fc6 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
bluetooth_mdw | 81:58bd55de8fc6 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
bluetooth_mdw | 81:58bd55de8fc6 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
bluetooth_mdw | 81:58bd55de8fc6 | 23 | DEALINGS IN THE SOFTWARE. |
bluetooth_mdw | 81:58bd55de8fc6 | 24 | */ |
bluetooth_mdw | 81:58bd55de8fc6 | 25 | |
bluetooth_mdw | 81:58bd55de8fc6 | 26 | /** |
bluetooth_mdw | 81:58bd55de8fc6 | 27 | * Class definition for a MicroBit BLE Animation Service. |
bluetooth_mdw | 81:58bd55de8fc6 | 28 | * Provides a BLE gateway onto an Animation Model. |
bluetooth_mdw | 81:58bd55de8fc6 | 29 | */ |
bluetooth_mdw | 81:58bd55de8fc6 | 30 | |
bluetooth_mdw | 81:58bd55de8fc6 | 31 | #include "MicroBitConfig.h" |
bluetooth_mdw | 81:58bd55de8fc6 | 32 | #include "MicroBitAnimationService.h" |
bluetooth_mdw | 81:58bd55de8fc6 | 33 | #include "ble/UUID.h" |
bluetooth_mdw | 81:58bd55de8fc6 | 34 | #include "Animator.h" |
bluetooth_mdw | 81:58bd55de8fc6 | 35 | |
bluetooth_mdw | 81:58bd55de8fc6 | 36 | /** |
bluetooth_mdw | 81:58bd55de8fc6 | 37 | * Constructor. |
bluetooth_mdw | 81:58bd55de8fc6 | 38 | * Create a representation of the AnimationService |
bluetooth_mdw | 81:58bd55de8fc6 | 39 | * @param _ble The instance of a BLE device that we're running on. |
bluetooth_mdw | 81:58bd55de8fc6 | 40 | */ |
bluetooth_mdw | 81:58bd55de8fc6 | 41 | MicroBitAnimationService::MicroBitAnimationService(BLEDevice &_ble) : |
bluetooth_mdw | 81:58bd55de8fc6 | 42 | ble(_ble) |
bluetooth_mdw | 81:58bd55de8fc6 | 43 | { |
bluetooth_mdw | 81:58bd55de8fc6 | 44 | // TODO: Three GattCharacteristic objects. UUID, valuePTR, length, max length, properties. |
bluetooth_mdw | 81:58bd55de8fc6 | 45 | |
bluetooth_mdw | 81:58bd55de8fc6 | 46 | // TODO: initialise characteristic value buffers |
bluetooth_mdw | 81:58bd55de8fc6 | 47 | |
bluetooth_mdw | 81:58bd55de8fc6 | 48 | // Set default security requirements - optional |
bluetooth_mdw | 81:58bd55de8fc6 | 49 | // animationTypeCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); |
bluetooth_mdw | 81:58bd55de8fc6 | 50 | // animationStatusCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); |
bluetooth_mdw | 81:58bd55de8fc6 | 51 | // animationControlCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); |
bluetooth_mdw | 81:58bd55de8fc6 | 52 | |
bluetooth_mdw | 81:58bd55de8fc6 | 53 | // TODO: create GattService containing the three characteristics |
bluetooth_mdw | 81:58bd55de8fc6 | 54 | // create an array of our characteristics so we can pass them to the service when we create it |
bluetooth_mdw | 81:58bd55de8fc6 | 55 | |
bluetooth_mdw | 81:58bd55de8fc6 | 56 | // create the animation service and specify its characteristics |
bluetooth_mdw | 81:58bd55de8fc6 | 57 | |
bluetooth_mdw | 81:58bd55de8fc6 | 58 | |
bluetooth_mdw | 81:58bd55de8fc6 | 59 | // TODO: add the service to the Bluetooth attribute table |
bluetooth_mdw | 81:58bd55de8fc6 | 60 | |
bluetooth_mdw | 81:58bd55de8fc6 | 61 | // TODO: take a note of the handles of our new characteristics now that they're in the attribute table |
bluetooth_mdw | 81:58bd55de8fc6 | 62 | |
bluetooth_mdw | 81:58bd55de8fc6 | 63 | // TODO: set the attribute table's characteristic values to the initialised buffer values |
bluetooth_mdw | 81:58bd55de8fc6 | 64 | |
bluetooth_mdw | 81:58bd55de8fc6 | 65 | // TODO: register a callback function for when a characteristic is written to |
bluetooth_mdw | 81:58bd55de8fc6 | 66 | |
bluetooth_mdw | 81:58bd55de8fc6 | 67 | // subscribe to animation status events so they can be notified to the connected client |
bluetooth_mdw | 81:58bd55de8fc6 | 68 | if (EventModel::defaultEventBus) |
bluetooth_mdw | 81:58bd55de8fc6 | 69 | EventModel::defaultEventBus->listen(ANIMATION_STATUS_EVENT, MICROBIT_EVT_ANY, this, &MicroBitAnimationService::animationStatusUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE); |
bluetooth_mdw | 81:58bd55de8fc6 | 70 | } |
bluetooth_mdw | 81:58bd55de8fc6 | 71 | |
bluetooth_mdw | 81:58bd55de8fc6 | 72 | |
bluetooth_mdw | 81:58bd55de8fc6 | 73 | /** |
bluetooth_mdw | 81:58bd55de8fc6 | 74 | * Callback. Invoked when any of our attributes are written via BLE. |
bluetooth_mdw | 81:58bd55de8fc6 | 75 | */ |
bluetooth_mdw | 81:58bd55de8fc6 | 76 | void MicroBitAnimationService::onDataWritten(const GattWriteCallbackParams *params) |
bluetooth_mdw | 81:58bd55de8fc6 | 77 | { |
bluetooth_mdw | 81:58bd55de8fc6 | 78 | |
bluetooth_mdw | 81:58bd55de8fc6 | 79 | } |
bluetooth_mdw | 81:58bd55de8fc6 | 80 | |
bluetooth_mdw | 81:58bd55de8fc6 | 81 | void MicroBitAnimationService::animationStatusUpdate(MicroBitEvent e) |
bluetooth_mdw | 81:58bd55de8fc6 | 82 | { |
bluetooth_mdw | 81:58bd55de8fc6 | 83 | // TODO: notify connected client of change to the animation status characteristic |
bluetooth_mdw | 81:58bd55de8fc6 | 84 | } |
bluetooth_mdw | 81:58bd55de8fc6 | 85 | |
bluetooth_mdw | 81:58bd55de8fc6 | 86 | const uint8_t MicroBitAnimationServiceUUID[] = { |
bluetooth_mdw | 81:58bd55de8fc6 | 87 | 0xe9,0x5d,0x71,0x70,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 |
bluetooth_mdw | 81:58bd55de8fc6 | 88 | }; |
bluetooth_mdw | 81:58bd55de8fc6 | 89 | |
bluetooth_mdw | 81:58bd55de8fc6 | 90 | const uint8_t MicroBitAnimationServiceAnimationTypeCharacteristicUUID[] = { |
bluetooth_mdw | 81:58bd55de8fc6 | 91 | 0xe9,0x5d,0xC3,0x06,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 |
bluetooth_mdw | 81:58bd55de8fc6 | 92 | }; |
bluetooth_mdw | 81:58bd55de8fc6 | 93 | |
bluetooth_mdw | 81:58bd55de8fc6 | 94 | const uint8_t MicroBitAnimationServiceAnimationStatusCharacteristicUUID[] = { |
bluetooth_mdw | 81:58bd55de8fc6 | 95 | 0xe9,0x5d,0x45,0x92,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 |
bluetooth_mdw | 81:58bd55de8fc6 | 96 | }; |
bluetooth_mdw | 81:58bd55de8fc6 | 97 | |
bluetooth_mdw | 81:58bd55de8fc6 | 98 | const uint8_t MicroBitAnimationServiceAnimationControlCharacteristicUUID[] = { |
bluetooth_mdw | 81:58bd55de8fc6 | 99 | 0xe9,0x5d,0xb8,0x4c,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 |
bluetooth_mdw | 81:58bd55de8fc6 | 100 | }; |
bluetooth_mdw | 81:58bd55de8fc6 | 101 |