fork

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit

Fork of microbit-dal by Lancaster University

Committer:
jaafreitas
Date:
Wed Oct 04 18:52:09 2017 +0000
Revision:
74:7c635e51760e
Parent:
1:8aa5cdb4ab67
Fixed the analogRead bug where values greater than 255 were returned to bluetooth io pin service.; Config to allow open bluetooth connectivity.;

Who changed what in which revision?

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