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
MicroBitLEDService.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 EVENT 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 the custom MicroBit LED Service. 00028 * Provides a BLE service to remotely read and write the state of the LED display. 00029 */ 00030 #include "MicroBitConfig.h" 00031 #include "ble/UUID.h" 00032 00033 #include "MicroBitLEDService.h" 00034 00035 /** 00036 * Constructor. 00037 * Create a representation of the LEDService 00038 * @param _ble The instance of a BLE device that we're running on. 00039 * @param _display An instance of MicroBitDisplay to interface with. 00040 */ 00041 MicroBitLEDService::MicroBitLEDService(BLEDevice &_ble, MicroBitDisplay &_display) : 00042 ble(_ble), display(_display), 00043 matrixCharacteristic(MicroBitLEDServiceMatrixUUID, (uint8_t *)&matrixCharacteristicBuffer, 0, sizeof(matrixCharacteristicBuffer), 00044 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ) 00045 { 00046 // Create the data structures that represent each of our characteristics in Soft Device. 00047 GattCharacteristic textCharacteristic(MicroBitLEDServiceTextUUID, (uint8_t *)textCharacteristicBuffer, 0, MICROBIT_BLE_MAXIMUM_SCROLLTEXT, 00048 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE); 00049 00050 GattCharacteristic scrollingSpeedCharacteristic(MicroBitLEDServiceScrollingSpeedUUID, (uint8_t *)&scrollingSpeedCharacteristicBuffer, 0, 00051 sizeof(scrollingSpeedCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); 00052 00053 // Initialise our characteristic values. 00054 memclr(matrixCharacteristicBuffer, sizeof(matrixCharacteristicBuffer)); 00055 textCharacteristicBuffer[0] = 0; 00056 scrollingSpeedCharacteristicBuffer = MICROBIT_DEFAULT_SCROLL_SPEED; 00057 00058 matrixCharacteristic.setReadAuthorizationCallback(this, &MicroBitLEDService::onDataRead); 00059 00060 // Set default security requirements 00061 matrixCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00062 textCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00063 scrollingSpeedCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL); 00064 00065 GattCharacteristic *characteristics[] = {&matrixCharacteristic, &textCharacteristic, &scrollingSpeedCharacteristic}; 00066 GattService service(MicroBitLEDServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); 00067 00068 ble.addService(service); 00069 00070 matrixCharacteristicHandle = matrixCharacteristic.getValueHandle(); 00071 textCharacteristicHandle = textCharacteristic.getValueHandle(); 00072 scrollingSpeedCharacteristicHandle = scrollingSpeedCharacteristic.getValueHandle(); 00073 00074 ble.gattServer().write(scrollingSpeedCharacteristicHandle, (const uint8_t *)&scrollingSpeedCharacteristicBuffer, sizeof(scrollingSpeedCharacteristicBuffer)); 00075 ble.gattServer().write(matrixCharacteristicHandle, (const uint8_t *)&matrixCharacteristicBuffer, sizeof(matrixCharacteristicBuffer)); 00076 00077 ble.onDataWritten(this, &MicroBitLEDService::onDataWritten); 00078 } 00079 00080 00081 /** 00082 * Callback. Invoked when any of our attributes are written via BLE. 00083 */ 00084 void MicroBitLEDService::onDataWritten(const GattWriteCallbackParams *params) 00085 { 00086 uint8_t *data = (uint8_t *)params->data; 00087 00088 if (params->handle == matrixCharacteristicHandle && params->len > 0 && params->len < 6) 00089 { 00090 for (int y=0; y<params->len; y++) 00091 for (int x=0; x<5; x++) 00092 display.image.setPixelValue(x, y, (data[y] & (0x01 << (4-x))) ? 255 : 0); 00093 } 00094 00095 else if (params->handle == textCharacteristicHandle) 00096 { 00097 // Create a ManagedString representation from the UTF8 data. 00098 // We do this explicitly to control the length (in case the string is not NULL terminated!) 00099 ManagedString s((char *)params->data, params->len); 00100 00101 // Start the string scrolling and we're done. 00102 display.scrollAsync(s, (int) scrollingSpeedCharacteristicBuffer); 00103 } 00104 00105 else if (params->handle == scrollingSpeedCharacteristicHandle && params->len >= sizeof(scrollingSpeedCharacteristicBuffer)) 00106 { 00107 // Read the speed requested, and store it locally. 00108 // We use this as the speed for all scroll operations subsquently initiated from BLE. 00109 scrollingSpeedCharacteristicBuffer = *((uint16_t *)params->data); 00110 } 00111 } 00112 00113 /** 00114 * Callback. Invoked when any of our attributes are read via BLE. 00115 */ 00116 void MicroBitLEDService::onDataRead(GattReadAuthCallbackParams *params) 00117 { 00118 if (params->handle == matrixCharacteristicHandle) 00119 { 00120 for (int y=0; y<5; y++) 00121 { 00122 matrixCharacteristicBuffer[y] = 0; 00123 00124 for (int x=0; x<5; x++) 00125 { 00126 if (display.image.getPixelValue(x, y)) 00127 matrixCharacteristicBuffer[y] |= 0x01 << (4-x); 00128 } 00129 } 00130 00131 ble.gattServer().write(matrixCharacteristicHandle, (const uint8_t *)&matrixCharacteristicBuffer, sizeof(matrixCharacteristicBuffer)); 00132 } 00133 } 00134 00135 00136 const uint8_t MicroBitLEDServiceUUID[] = { 00137 0xe9,0x5d,0xd9,0x1d,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00138 }; 00139 00140 const uint8_t MicroBitLEDServiceMatrixUUID[] = { 00141 0xe9,0x5d,0x7b,0x77,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00142 }; 00143 00144 const uint8_t MicroBitLEDServiceTextUUID[] = { 00145 0xe9,0x5d,0x93,0xee,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00146 }; 00147 00148 const uint8_t MicroBitLEDServiceScrollingSpeedUUID[] = { 00149 0xe9,0x5d,0x0d,0x2d,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8 00150 };
Generated on Tue Jul 12 2022 20:39:13 by 1.7.2