Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /*
MrBedfordVan 0:b9164b348919 2 The MIT License (MIT)
MrBedfordVan 0:b9164b348919 3
MrBedfordVan 0:b9164b348919 4 Copyright (c) 2016 British Broadcasting Corporation.
MrBedfordVan 0:b9164b348919 5 This software is provided by Lancaster University by arrangement with the BBC.
MrBedfordVan 0:b9164b348919 6
MrBedfordVan 0:b9164b348919 7 Permission is hereby granted, free of charge, to any person obtaining a
MrBedfordVan 0:b9164b348919 8 copy of this software and associated documentation files (the "Software"),
MrBedfordVan 0:b9164b348919 9 to deal in the Software without restriction, including without limitation
MrBedfordVan 0:b9164b348919 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
MrBedfordVan 0:b9164b348919 11 and/or sell copies of the Software, and to permit persons to whom the
MrBedfordVan 0:b9164b348919 12 Software is furnished to do so, subject to the following conditions:
MrBedfordVan 0:b9164b348919 13
MrBedfordVan 0:b9164b348919 14 The above copyright notice and this permission notice shall be included in
MrBedfordVan 0:b9164b348919 15 all copies or substantial portions of the Software.
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
MrBedfordVan 0:b9164b348919 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
MrBedfordVan 0:b9164b348919 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
MrBedfordVan 0:b9164b348919 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
MrBedfordVan 0:b9164b348919 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
MrBedfordVan 0:b9164b348919 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
MrBedfordVan 0:b9164b348919 23 DEALINGS IN THE SOFTWARE.
MrBedfordVan 0:b9164b348919 24 */
MrBedfordVan 0:b9164b348919 25
MrBedfordVan 0:b9164b348919 26 /**
MrBedfordVan 0:b9164b348919 27 * Class definition for the custom MicroBit LED Service.
MrBedfordVan 0:b9164b348919 28 * Provides a BLE service to remotely read and write the state of the LED display.
MrBedfordVan 0:b9164b348919 29 */
MrBedfordVan 0:b9164b348919 30 #include "MicroBitConfig.h"
MrBedfordVan 0:b9164b348919 31 #include "ble/UUID.h"
MrBedfordVan 0:b9164b348919 32
MrBedfordVan 0:b9164b348919 33 #include "MicroBitLEDService.h"
MrBedfordVan 0:b9164b348919 34
MrBedfordVan 0:b9164b348919 35 /**
MrBedfordVan 0:b9164b348919 36 * Constructor.
MrBedfordVan 0:b9164b348919 37 * Create a representation of the LEDService
MrBedfordVan 0:b9164b348919 38 * @param _ble The instance of a BLE device that we're running on.
MrBedfordVan 0:b9164b348919 39 * @param _display An instance of MicroBitDisplay to interface with.
MrBedfordVan 0:b9164b348919 40 */
MrBedfordVan 0:b9164b348919 41 MicroBitLEDService::MicroBitLEDService(BLEDevice &_ble, MicroBitDisplay &_display) :
MrBedfordVan 0:b9164b348919 42 ble(_ble), display(_display),
MrBedfordVan 0:b9164b348919 43 matrixCharacteristic(MicroBitLEDServiceMatrixUUID, (uint8_t *)&matrixCharacteristicBuffer, 0, sizeof(matrixCharacteristicBuffer),
MrBedfordVan 0:b9164b348919 44 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)
MrBedfordVan 0:b9164b348919 45 {
MrBedfordVan 0:b9164b348919 46 // Create the data structures that represent each of our characteristics in Soft Device.
MrBedfordVan 0:b9164b348919 47 GattCharacteristic textCharacteristic(MicroBitLEDServiceTextUUID, (uint8_t *)textCharacteristicBuffer, 0, MICROBIT_BLE_MAXIMUM_SCROLLTEXT,
MrBedfordVan 0:b9164b348919 48 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
MrBedfordVan 0:b9164b348919 49
MrBedfordVan 0:b9164b348919 50 GattCharacteristic scrollingSpeedCharacteristic(MicroBitLEDServiceScrollingSpeedUUID, (uint8_t *)&scrollingSpeedCharacteristicBuffer, 0,
MrBedfordVan 0:b9164b348919 51 sizeof(scrollingSpeedCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
MrBedfordVan 0:b9164b348919 52
MrBedfordVan 0:b9164b348919 53 // Initialise our characteristic values.
MrBedfordVan 0:b9164b348919 54 memclr(matrixCharacteristicBuffer, sizeof(matrixCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 55 textCharacteristicBuffer[0] = 0;
MrBedfordVan 0:b9164b348919 56 scrollingSpeedCharacteristicBuffer = MICROBIT_DEFAULT_SCROLL_SPEED;
MrBedfordVan 0:b9164b348919 57
MrBedfordVan 0:b9164b348919 58 matrixCharacteristic.setReadAuthorizationCallback(this, &MicroBitLEDService::onDataRead);
MrBedfordVan 0:b9164b348919 59
MrBedfordVan 0:b9164b348919 60 // Set default security requirements
MrBedfordVan 0:b9164b348919 61 matrixCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
MrBedfordVan 0:b9164b348919 62 textCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
MrBedfordVan 0:b9164b348919 63 scrollingSpeedCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
MrBedfordVan 0:b9164b348919 64
MrBedfordVan 0:b9164b348919 65 GattCharacteristic *characteristics[] = {&matrixCharacteristic, &textCharacteristic, &scrollingSpeedCharacteristic};
MrBedfordVan 0:b9164b348919 66 GattService service(MicroBitLEDServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
MrBedfordVan 0:b9164b348919 67
MrBedfordVan 0:b9164b348919 68 ble.addService(service);
MrBedfordVan 0:b9164b348919 69
MrBedfordVan 0:b9164b348919 70 matrixCharacteristicHandle = matrixCharacteristic.getValueHandle();
MrBedfordVan 0:b9164b348919 71 textCharacteristicHandle = textCharacteristic.getValueHandle();
MrBedfordVan 0:b9164b348919 72 scrollingSpeedCharacteristicHandle = scrollingSpeedCharacteristic.getValueHandle();
MrBedfordVan 0:b9164b348919 73
MrBedfordVan 0:b9164b348919 74 ble.gattServer().write(scrollingSpeedCharacteristicHandle, (const uint8_t *)&scrollingSpeedCharacteristicBuffer, sizeof(scrollingSpeedCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 75 ble.gattServer().write(matrixCharacteristicHandle, (const uint8_t *)&matrixCharacteristicBuffer, sizeof(matrixCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 76
MrBedfordVan 0:b9164b348919 77 ble.onDataWritten(this, &MicroBitLEDService::onDataWritten);
MrBedfordVan 0:b9164b348919 78 }
MrBedfordVan 0:b9164b348919 79
MrBedfordVan 0:b9164b348919 80
MrBedfordVan 0:b9164b348919 81 /**
MrBedfordVan 0:b9164b348919 82 * Callback. Invoked when any of our attributes are written via BLE.
MrBedfordVan 0:b9164b348919 83 */
MrBedfordVan 0:b9164b348919 84 void MicroBitLEDService::onDataWritten(const GattWriteCallbackParams *params)
MrBedfordVan 0:b9164b348919 85 {
MrBedfordVan 0:b9164b348919 86 uint8_t *data = (uint8_t *)params->data;
MrBedfordVan 0:b9164b348919 87
MrBedfordVan 0:b9164b348919 88 if (params->handle == matrixCharacteristicHandle && params->len > 0 && params->len < 6)
MrBedfordVan 0:b9164b348919 89 {
MrBedfordVan 0:b9164b348919 90 for (int y=0; y<params->len; y++)
MrBedfordVan 0:b9164b348919 91 for (int x=0; x<5; x++)
MrBedfordVan 0:b9164b348919 92 display.image.setPixelValue(x, y, (data[y] & (0x01 << (4-x))) ? 255 : 0);
MrBedfordVan 0:b9164b348919 93 }
MrBedfordVan 0:b9164b348919 94
MrBedfordVan 0:b9164b348919 95 else if (params->handle == textCharacteristicHandle)
MrBedfordVan 0:b9164b348919 96 {
MrBedfordVan 0:b9164b348919 97 // Create a ManagedString representation from the UTF8 data.
MrBedfordVan 0:b9164b348919 98 // We do this explicitly to control the length (in case the string is not NULL terminated!)
MrBedfordVan 0:b9164b348919 99 ManagedString s((char *)params->data, params->len);
MrBedfordVan 0:b9164b348919 100
MrBedfordVan 0:b9164b348919 101 // Start the string scrolling and we're done.
MrBedfordVan 0:b9164b348919 102 display.scrollAsync(s, (int) scrollingSpeedCharacteristicBuffer);
MrBedfordVan 0:b9164b348919 103 }
MrBedfordVan 0:b9164b348919 104
MrBedfordVan 0:b9164b348919 105 else if (params->handle == scrollingSpeedCharacteristicHandle && params->len >= sizeof(scrollingSpeedCharacteristicBuffer))
MrBedfordVan 0:b9164b348919 106 {
MrBedfordVan 0:b9164b348919 107 // Read the speed requested, and store it locally.
MrBedfordVan 0:b9164b348919 108 // We use this as the speed for all scroll operations subsquently initiated from BLE.
MrBedfordVan 0:b9164b348919 109 scrollingSpeedCharacteristicBuffer = *((uint16_t *)params->data);
MrBedfordVan 0:b9164b348919 110 }
MrBedfordVan 0:b9164b348919 111 }
MrBedfordVan 0:b9164b348919 112
MrBedfordVan 0:b9164b348919 113 /**
MrBedfordVan 0:b9164b348919 114 * Callback. Invoked when any of our attributes are read via BLE.
MrBedfordVan 0:b9164b348919 115 */
MrBedfordVan 0:b9164b348919 116 void MicroBitLEDService::onDataRead(GattReadAuthCallbackParams *params)
MrBedfordVan 0:b9164b348919 117 {
MrBedfordVan 0:b9164b348919 118 if (params->handle == matrixCharacteristicHandle)
MrBedfordVan 0:b9164b348919 119 {
MrBedfordVan 0:b9164b348919 120 for (int y=0; y<5; y++)
MrBedfordVan 0:b9164b348919 121 {
MrBedfordVan 0:b9164b348919 122 matrixCharacteristicBuffer[y] = 0;
MrBedfordVan 0:b9164b348919 123
MrBedfordVan 0:b9164b348919 124 for (int x=0; x<5; x++)
MrBedfordVan 0:b9164b348919 125 {
MrBedfordVan 0:b9164b348919 126 if (display.image.getPixelValue(x, y))
MrBedfordVan 0:b9164b348919 127 matrixCharacteristicBuffer[y] |= 0x01 << (4-x);
MrBedfordVan 0:b9164b348919 128 }
MrBedfordVan 0:b9164b348919 129 }
MrBedfordVan 0:b9164b348919 130
MrBedfordVan 0:b9164b348919 131 ble.gattServer().write(matrixCharacteristicHandle, (const uint8_t *)&matrixCharacteristicBuffer, sizeof(matrixCharacteristicBuffer));
MrBedfordVan 0:b9164b348919 132 }
MrBedfordVan 0:b9164b348919 133 }
MrBedfordVan 0:b9164b348919 134
MrBedfordVan 0:b9164b348919 135
MrBedfordVan 0:b9164b348919 136 const uint8_t MicroBitLEDServiceUUID[] = {
MrBedfordVan 0:b9164b348919 137 0xe9,0x5d,0xd9,0x1d,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
MrBedfordVan 0:b9164b348919 138 };
MrBedfordVan 0:b9164b348919 139
MrBedfordVan 0:b9164b348919 140 const uint8_t MicroBitLEDServiceMatrixUUID[] = {
MrBedfordVan 0:b9164b348919 141 0xe9,0x5d,0x7b,0x77,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
MrBedfordVan 0:b9164b348919 142 };
MrBedfordVan 0:b9164b348919 143
MrBedfordVan 0:b9164b348919 144 const uint8_t MicroBitLEDServiceTextUUID[] = {
MrBedfordVan 0:b9164b348919 145 0xe9,0x5d,0x93,0xee,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
MrBedfordVan 0:b9164b348919 146 };
MrBedfordVan 0:b9164b348919 147
MrBedfordVan 0:b9164b348919 148 const uint8_t MicroBitLEDServiceScrollingSpeedUUID[] = {
MrBedfordVan 0:b9164b348919 149 0xe9,0x5d,0x0d,0x2d,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
MrBedfordVan 0:b9164b348919 150 };