KEN OGAMI / microbit-dal-acc

Dependencies:   BLE_API mbed-dev-bin nRF51822-bluetooth-mdw

Fork of microbit-dal by Martin Woolley

Committer:
bluetooth_kyo
Date:
Wed Nov 29 22:40:52 2017 +0000
Revision:
83:7e08ab5aba66
Parent:
82:91e085d6ad72
forking microbi-dal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bluetooth_mdw 74:9771cd712730 1 /*
bluetooth_mdw 74:9771cd712730 2 The MIT License (MIT)
bluetooth_mdw 82:91e085d6ad72 3
bluetooth_mdw 74:9771cd712730 4 Copyright (c) 2016 British Broadcasting Corporation.
bluetooth_mdw 74:9771cd712730 5 This software is provided by Lancaster University by arrangement with the BBC.
bluetooth_mdw 82:91e085d6ad72 6
bluetooth_mdw 74:9771cd712730 7 Permission is hereby granted, free of charge, to any person obtaining a
bluetooth_mdw 74:9771cd712730 8 copy of this software and associated documentation files (the "Software"),
bluetooth_mdw 74:9771cd712730 9 to deal in the Software without restriction, including without limitation
bluetooth_mdw 74:9771cd712730 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
bluetooth_mdw 74:9771cd712730 11 and/or sell copies of the Software, and to permit persons to whom the
bluetooth_mdw 74:9771cd712730 12 Software is furnished to do so, subject to the following conditions:
bluetooth_mdw 82:91e085d6ad72 13
bluetooth_mdw 74:9771cd712730 14 The above copyright notice and this permission notice shall be included in
bluetooth_mdw 74:9771cd712730 15 all copies or substantial portions of the Software.
bluetooth_mdw 82:91e085d6ad72 16
bluetooth_mdw 74:9771cd712730 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bluetooth_mdw 74:9771cd712730 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bluetooth_mdw 74:9771cd712730 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO Animation SHALL
bluetooth_mdw 74:9771cd712730 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bluetooth_mdw 74:9771cd712730 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
bluetooth_mdw 74:9771cd712730 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
bluetooth_mdw 74:9771cd712730 23 DEALINGS IN THE SOFTWARE.
bluetooth_mdw 74:9771cd712730 24 */
bluetooth_mdw 82:91e085d6ad72 25
bluetooth_mdw 74:9771cd712730 26 /**
bluetooth_mdw 74:9771cd712730 27 * Class definition for a MicroBit BLE Animation Service.
bluetooth_mdw 74:9771cd712730 28 * Provides a BLE gateway onto an Animation Model.
bluetooth_mdw 74:9771cd712730 29 */
bluetooth_mdw 82:91e085d6ad72 30
bluetooth_mdw 74:9771cd712730 31 #include "MicroBitConfig.h"
bluetooth_mdw 74:9771cd712730 32 #include "MicroBitAnimationService.h"
bluetooth_mdw 74:9771cd712730 33 #include "ble/UUID.h"
bluetooth_mdw 74:9771cd712730 34 #include "Animator.h"
bluetooth_mdw 82:91e085d6ad72 35
bluetooth_mdw 74:9771cd712730 36 /**
bluetooth_mdw 74:9771cd712730 37 * Constructor.
bluetooth_mdw 74:9771cd712730 38 * Create a representation of the AnimationService
bluetooth_mdw 74:9771cd712730 39 * @param _ble The instance of a BLE device that we're running on.
bluetooth_mdw 74:9771cd712730 40 */
bluetooth_mdw 74:9771cd712730 41 MicroBitAnimationService::MicroBitAnimationService(BLEDevice &_ble) :
bluetooth_mdw 74:9771cd712730 42 ble(_ble)
bluetooth_mdw 74:9771cd712730 43 {
bluetooth_mdw 82:91e085d6ad72 44 // TODO: Three GattCharacteristic objects. UUID, valuePTR, length, max length, properties.
bluetooth_kyo 83:7e08ab5aba66 45 GattCharacteristic animationTypeCharacteristic(
bluetooth_kyo 83:7e08ab5aba66 46 MicroBitAnimationServiceAnimationTypeCharacteristicUUID,
bluetooth_kyo 83:7e08ab5aba66 47 (uint8_t *)animation_type_buffer,
bluetooth_kyo 83:7e08ab5aba66 48 1,
bluetooth_kyo 83:7e08ab5aba66 49 1,
bluetooth_kyo 83:7e08ab5aba66 50 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
bluetooth_kyo 83:7e08ab5aba66 51
bluetooth_kyo 83:7e08ab5aba66 52 GattCharacteristic animationStatusCharacteristic(
bluetooth_kyo 83:7e08ab5aba66 53 MicroBitAnimationServiceAnimationStatusCharacteristicUUID,
bluetooth_kyo 83:7e08ab5aba66 54 (uint8_t *)animation_status_buffer,
bluetooth_kyo 83:7e08ab5aba66 55 1,
bluetooth_kyo 83:7e08ab5aba66 56 1,
bluetooth_kyo 83:7e08ab5aba66 57 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
bluetooth_kyo 83:7e08ab5aba66 58
bluetooth_kyo 83:7e08ab5aba66 59 GattCharacteristic animationControlCharacteristic(
bluetooth_kyo 83:7e08ab5aba66 60 MicroBitAnimationServiceAnimationControlCharacteristicUUID,
bluetooth_kyo 83:7e08ab5aba66 61 (uint8_t *)animation_control_buffer,
bluetooth_kyo 83:7e08ab5aba66 62 1,
bluetooth_kyo 83:7e08ab5aba66 63 1,
bluetooth_kyo 83:7e08ab5aba66 64 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
bluetooth_kyo 83:7e08ab5aba66 65
bluetooth_mdw 82:91e085d6ad72 66 // TODO: initialise characteristic value buffers
bluetooth_kyo 83:7e08ab5aba66 67 animation_type_buffer[0] = 0x00;
bluetooth_kyo 83:7e08ab5aba66 68 animation_status_buffer[0] = 0x00;
bluetooth_kyo 83:7e08ab5aba66 69 animation_control_buffer[0] = 0x00;
bluetooth_kyo 83:7e08ab5aba66 70
bluetooth_mdw 74:9771cd712730 71 // Set default security requirements - optional
bluetooth_mdw 74:9771cd712730 72 // animationTypeCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
bluetooth_mdw 74:9771cd712730 73 // animationStatusCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
bluetooth_mdw 74:9771cd712730 74 // animationControlCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
bluetooth_mdw 82:91e085d6ad72 75
bluetooth_mdw 82:91e085d6ad72 76 // TODO: create GattService containing the three characteristics
bluetooth_mdw 74:9771cd712730 77 // create an array of our characteristics so we can pass them to the service when we create it
bluetooth_kyo 83:7e08ab5aba66 78 GattCharacteristic *characteristics[] = {&animationTypeCharacteristic, &animationStatusCharacteristic, &animationControlCharacteristic};
bluetooth_mdw 74:9771cd712730 79
bluetooth_kyo 83:7e08ab5aba66 80 // create the animation service and specify its characteristics. 3rd arg is the number of characteristics.
bluetooth_kyo 83:7e08ab5aba66 81 GattService service(MicroBitAnimationServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
bluetooth_mdw 82:91e085d6ad72 82
bluetooth_mdw 82:91e085d6ad72 83
bluetooth_mdw 82:91e085d6ad72 84 // TODO: add the service to the Bluetooth attribute table
bluetooth_kyo 83:7e08ab5aba66 85 ble.addService(service);
bluetooth_kyo 83:7e08ab5aba66 86
bluetooth_mdw 82:91e085d6ad72 87 // TODO: take a note of the handles of our new characteristics now that they're in the attribute table
bluetooth_kyo 83:7e08ab5aba66 88 animationTypeCharacteristicHandle = animationTypeCharacteristic.getValueHandle();
bluetooth_kyo 83:7e08ab5aba66 89 animationStatusCharacteristicHandle = animationStatusCharacteristic.getValueHandle();
bluetooth_kyo 83:7e08ab5aba66 90 animationControlCharacteristicHandle = animationControlCharacteristic.getValueHandle();
bluetooth_kyo 83:7e08ab5aba66 91
bluetooth_mdw 82:91e085d6ad72 92 // TODO: register a callback function for when a characteristic is written to
bluetooth_kyo 83:7e08ab5aba66 93 ble.onDataWritten(this, &MicroBitAnimationService::onDataWritten);
bluetooth_kyo 83:7e08ab5aba66 94
bluetooth_mdw 74:9771cd712730 95 // subscribe to animation status events so they can be notified to the connected client
bluetooth_mdw 74:9771cd712730 96 if (EventModel::defaultEventBus)
bluetooth_mdw 74:9771cd712730 97 EventModel::defaultEventBus->listen(ANIMATION_STATUS_EVENT, MICROBIT_EVT_ANY, this, &MicroBitAnimationService::animationStatusUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE);
bluetooth_mdw 74:9771cd712730 98 }
bluetooth_mdw 82:91e085d6ad72 99
bluetooth_mdw 82:91e085d6ad72 100
bluetooth_mdw 74:9771cd712730 101 /**
bluetooth_kyo 83:7e08ab5aba66 102 * Callback. Invoked when any of our attributes are written to
bluetooth_mdw 74:9771cd712730 103 */
bluetooth_mdw 74:9771cd712730 104 void MicroBitAnimationService::onDataWritten(const GattWriteCallbackParams *params)
bluetooth_mdw 74:9771cd712730 105 {
bluetooth_kyo 83:7e08ab5aba66 106
bluetooth_mdw 82:91e085d6ad72 107 // TODO: handle write to animation type characteristic
bluetooth_kyo 83:7e08ab5aba66 108 if (params->handle == animationTypeCharacteristicHandle && params->len == 1) {
bluetooth_kyo 83:7e08ab5aba66 109 uint8_t *value = (uint8_t *)params->data;
bluetooth_kyo 83:7e08ab5aba66 110 uint16_t event_value = static_cast<uint16_t>(value[0]);
bluetooth_kyo 83:7e08ab5aba66 111 MicroBitEvent evt(ANIMATION_TYPE_EVENT, event_value);
bluetooth_kyo 83:7e08ab5aba66 112 return;
bluetooth_kyo 83:7e08ab5aba66 113 }
bluetooth_kyo 83:7e08ab5aba66 114
bluetooth_kyo 83:7e08ab5aba66 115 // TODO: handle write to animation control characteristic
bluetooth_kyo 83:7e08ab5aba66 116 if (params->handle == animationControlCharacteristicHandle && params->len == 1) {
bluetooth_kyo 83:7e08ab5aba66 117 uint8_t *value = (uint8_t *)params->data;
bluetooth_kyo 83:7e08ab5aba66 118 uint16_t event_value = static_cast<uint16_t>(value[0]);
bluetooth_kyo 83:7e08ab5aba66 119 MicroBitEvent evt(ANIMATION_CONTROL_EVENT, event_value);
bluetooth_kyo 83:7e08ab5aba66 120 return;
bluetooth_kyo 83:7e08ab5aba66 121 }
bluetooth_mdw 74:9771cd712730 122 }
bluetooth_mdw 82:91e085d6ad72 123
bluetooth_mdw 74:9771cd712730 124 void MicroBitAnimationService::animationStatusUpdate(MicroBitEvent e)
bluetooth_mdw 74:9771cd712730 125 {
bluetooth_mdw 82:91e085d6ad72 126 // TODO: notify connected client of change to the animation status characteristic
bluetooth_kyo 83:7e08ab5aba66 127 if (ble.getGapState().connected)
bluetooth_kyo 83:7e08ab5aba66 128 {
bluetooth_kyo 83:7e08ab5aba66 129 animation_status_buffer[0] = e.value;
bluetooth_kyo 83:7e08ab5aba66 130 ble.gattServer().notify(animationStatusCharacteristicHandle,(uint8_t *)animation_status_buffer, 1);
bluetooth_kyo 83:7e08ab5aba66 131 }
bluetooth_mdw 74:9771cd712730 132 }
bluetooth_mdw 82:91e085d6ad72 133
bluetooth_mdw 74:9771cd712730 134 const uint8_t MicroBitAnimationServiceUUID[] = {
bluetooth_mdw 74:9771cd712730 135 0xe9,0x5d,0x71,0x70,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
bluetooth_mdw 74:9771cd712730 136 };
bluetooth_mdw 82:91e085d6ad72 137
bluetooth_mdw 74:9771cd712730 138 const uint8_t MicroBitAnimationServiceAnimationTypeCharacteristicUUID[] = {
bluetooth_mdw 74:9771cd712730 139 0xe9,0x5d,0xC3,0x06,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
bluetooth_mdw 74:9771cd712730 140 };
bluetooth_mdw 82:91e085d6ad72 141
bluetooth_mdw 74:9771cd712730 142 const uint8_t MicroBitAnimationServiceAnimationStatusCharacteristicUUID[] = {
bluetooth_mdw 74:9771cd712730 143 0xe9,0x5d,0x45,0x92,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
bluetooth_mdw 74:9771cd712730 144 };
bluetooth_mdw 82:91e085d6ad72 145
bluetooth_mdw 74:9771cd712730 146 const uint8_t MicroBitAnimationServiceAnimationControlCharacteristicUUID[] = {
bluetooth_mdw 74:9771cd712730 147 0xe9,0x5d,0xb8,0x4c,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
bluetooth_mdw 82:91e085d6ad72 148 };
bluetooth_mdw 82:91e085d6ad72 149