Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API nRF51822-bluetooth-mdw
Fork of microbit-dal by
MicroBitAnimationService.cpp
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO Animation SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 /** 00027 * Class definition for a MicroBit BLE Animation Service. 00028 * Provides a BLE gateway onto an Animation Model. 00029 */ 00030 00031 #include "MicroBitConfig.h" 00032 #include "MicroBitAnimationService.h" 00033 #include "ble/UUID.h" 00034 #include "Animator.h" 00035 00036 /** 00037 * Constructor. 00038 * Create a representation of the AnimationService 00039 * @param _ble The instance of a BLE device that we're running on. 00040 */ 00041 MicroBitAnimationService::MicroBitAnimationService(BLEDevice &_ble) : 00042 ble(_ble) 00043 { 00044 // UUID, valuePTR, length, max length, properties 00045 GattCharacteristic animationTypeCharacteristic( 00046 MicroBitAnimationServiceAnimationTypeCharacteristicUUID, 00047 (uint8_t *)animation_type_buffer, 00048 1, 00049 1, 00050 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); 00051 00052 GattCharacteristic animationStatusCharacteristic( 00053 MicroBitAnimationServiceAnimationStatusCharacteristicUUID, 00054 (uint8_t *)animation_status_buffer, 00055 1, 00056 1, 00057 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); 00058 00059 GattCharacteristic animationControlCharacteristic( 00060 MicroBitAnimationServiceAnimationControlCharacteristicUUID, 00061 (uint8_t *)animation_control_buffer, 00062 1, 00063 1, 00064 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); 00065 00066 animation_type_buffer[0] = 0x00; 00067 animation_status_buffer[0] = 0x00; 00068 animation_control_buffer[0] = 0x00; 00069 00070 // Set default security requirements - optional 00071 // animationTypeCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00072 // animationStatusCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00073 // animationControlCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00074 00075 // create an array of our characteristics so we can pass them to the service when we create it 00076 GattCharacteristic *characteristics[] = {&animationTypeCharacteristic, &animationStatusCharacteristic, &animationControlCharacteristic}; 00077 00078 // create the animation service and specify its characteristics 00079 GattService service(MicroBitAnimationServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00080 00081 // add the service to the Bluetooth attribute table 00082 ble.addService(service); 00083 00084 // take a note of the handles of our new characteristics now that they're in the attribute table 00085 animationTypeCharacteristicHandle = animationTypeCharacteristic.getValueHandle(); 00086 animationStatusCharacteristicHandle = animationStatusCharacteristic.getValueHandle(); 00087 animationControlCharacteristicHandle = animationControlCharacteristic.getValueHandle(); 00088 00089 ble.gattServer().write(animationTypeCharacteristicHandle, (const uint8_t *)&animation_type_buffer, sizeof(animation_type_buffer)); 00090 ble.gattServer().write(animationStatusCharacteristicHandle, (const uint8_t *)&animation_status_buffer, sizeof(animation_status_buffer)); 00091 ble.gattServer().write(animationControlCharacteristicHandle, (const uint8_t *)&animation_control_buffer, sizeof(animation_control_buffer)); 00092 00093 // register a callback function for when a characteristic is written to 00094 ble.onDataWritten(this, &MicroBitAnimationService::onDataWritten); 00095 00096 // subscribe to animation status events so they can be notified to the connected client 00097 if (EventModel::defaultEventBus) 00098 EventModel::defaultEventBus->listen(ANIMATION_STATUS_EVENT, MICROBIT_EVT_ANY, this, &MicroBitAnimationService::animationStatusUpdate, MESSAGE_BUS_LISTENER_IMMEDIATE); 00099 } 00100 00101 00102 /** 00103 * Callback. Invoked when any of our attributes are written via BLE. 00104 */ 00105 void MicroBitAnimationService::onDataWritten(const GattWriteCallbackParams *params) 00106 { 00107 00108 if (params->handle == animationTypeCharacteristicHandle && params->len >= 1) { 00109 uint8_t *value = (uint8_t *)params->data; 00110 uint16_t event_value = static_cast<uint16_t>(value[0]); 00111 MicroBitEvent evt(ANIMATION_TYPE_EVENT, event_value); 00112 return; 00113 } 00114 00115 if (params->handle == animationControlCharacteristicHandle && params->len >= 1) { 00116 uint8_t *value = (uint8_t *)params->data; 00117 uint16_t event_value = static_cast<uint16_t>(value[0]); 00118 MicroBitEvent evt(ANIMATION_CONTROL_EVENT, event_value); 00119 return; 00120 } 00121 } 00122 00123 void MicroBitAnimationService::animationStatusUpdate(MicroBitEvent e) 00124 { 00125 if (ble.getGapState().connected) 00126 { 00127 animation_status_buffer[0] = e.value; 00128 ble.gattServer().notify(animationStatusCharacteristicHandle,(uint8_t *)animation_status_buffer, 1); 00129 } 00130 } 00131 00132 const uint8_t MicroBitAnimationServiceUUID[] = { 00133 0xe9,0x5d,0x71,0x70,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00134 }; 00135 00136 const uint8_t MicroBitAnimationServiceAnimationTypeCharacteristicUUID[] = { 00137 0xe9,0x5d,0xC3,0x06,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00138 }; 00139 00140 const uint8_t MicroBitAnimationServiceAnimationStatusCharacteristicUUID[] = { 00141 0xe9,0x5d,0x45,0x92,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00142 }; 00143 00144 const uint8_t MicroBitAnimationServiceAnimationControlCharacteristicUUID[] = { 00145 0xe9,0x5d,0xb8,0x4c,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00146 };
Generated on Tue Jul 12 2022 18:04:44 by
1.7.2
