Solution for Bluetooth SIG hands-on training course

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

Dependents:   microbit

Fork of microbit-dal-bluetooth-mdw_starter by Martin Woolley

Committer:
bluetooth_mdw
Date:
Fri Mar 24 06:20:38 2017 +0000
Revision:
82:91e085d6ad72
Parent:
1:8aa5cdb4ab67
Removed comment re: writing initial values to the Attribute Table. Added comments to onDataWritten. Removed solution details.

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 IOPin Service.
Jonathan Austin 1:8aa5cdb4ab67 28 * Provides a BLE service to remotely read the state of the I/O Pin, and configure its behaviour.
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 "MicroBitIOPinService.h"
Jonathan Austin 1:8aa5cdb4ab67 34 #include "MicroBitFiber.h"
Jonathan Austin 1:8aa5cdb4ab67 35
Jonathan Austin 1:8aa5cdb4ab67 36 /**
Jonathan Austin 1:8aa5cdb4ab67 37 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 38 * Create a representation of the IOPinService
Jonathan Austin 1:8aa5cdb4ab67 39 * @param _ble The instance of a BLE device that we're running on.
Jonathan Austin 1:8aa5cdb4ab67 40 * @param _io An instance of MicroBitIO that this service will use to perform
Jonathan Austin 1:8aa5cdb4ab67 41 * I/O operations.
Jonathan Austin 1:8aa5cdb4ab67 42 */
Jonathan Austin 1:8aa5cdb4ab67 43 MicroBitIOPinService::MicroBitIOPinService(BLEDevice &_ble, MicroBitIO &_io) :
Jonathan Austin 1:8aa5cdb4ab67 44 ble(_ble), io(_io)
Jonathan Austin 1:8aa5cdb4ab67 45 {
Jonathan Austin 1:8aa5cdb4ab67 46 // Create the AD characteristic, that defines whether each pin is treated as analogue or digital
Jonathan Austin 1:8aa5cdb4ab67 47 GattCharacteristic ioPinServiceADCharacteristic(MicroBitIOPinServiceADConfigurationUUID, (uint8_t *)&ioPinServiceADCharacteristicBuffer, 0, sizeof(ioPinServiceADCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
Jonathan Austin 1:8aa5cdb4ab67 48
Jonathan Austin 1:8aa5cdb4ab67 49 // Create the IO characteristic, that defines whether each pin is treated as input or output
Jonathan Austin 1:8aa5cdb4ab67 50 GattCharacteristic ioPinServiceIOCharacteristic(MicroBitIOPinServiceIOConfigurationUUID, (uint8_t *)&ioPinServiceIOCharacteristicBuffer, 0, sizeof(ioPinServiceIOCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
Jonathan Austin 1:8aa5cdb4ab67 51
Jonathan Austin 1:8aa5cdb4ab67 52 // Create the Data characteristic, that allows the actual read and write operations.
Jonathan Austin 1:8aa5cdb4ab67 53 ioPinServiceDataCharacteristic = new GattCharacteristic(MicroBitIOPinServiceDataUUID, (uint8_t *)ioPinServiceDataCharacteristicBuffer, 0, sizeof(ioPinServiceDataCharacteristicBuffer), GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
Jonathan Austin 1:8aa5cdb4ab67 54
Jonathan Austin 1:8aa5cdb4ab67 55 ioPinServiceDataCharacteristic->setReadAuthorizationCallback(this, &MicroBitIOPinService::onDataRead);
Jonathan Austin 1:8aa5cdb4ab67 56
Jonathan Austin 1:8aa5cdb4ab67 57 ioPinServiceADCharacteristicBuffer = 0;
Jonathan Austin 1:8aa5cdb4ab67 58 ioPinServiceIOCharacteristicBuffer = 0;
Jonathan Austin 1:8aa5cdb4ab67 59 memset(ioPinServiceIOData, 0, sizeof(ioPinServiceIOData));
Jonathan Austin 1:8aa5cdb4ab67 60
Jonathan Austin 1:8aa5cdb4ab67 61 // Set default security requirements
Jonathan Austin 1:8aa5cdb4ab67 62 ioPinServiceADCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
Jonathan Austin 1:8aa5cdb4ab67 63 ioPinServiceIOCharacteristic.requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
Jonathan Austin 1:8aa5cdb4ab67 64 ioPinServiceDataCharacteristic->requireSecurity(SecurityManager::MICROBIT_BLE_SECURITY_LEVEL);
Jonathan Austin 1:8aa5cdb4ab67 65
Jonathan Austin 1:8aa5cdb4ab67 66 GattCharacteristic *characteristics[] = {&ioPinServiceADCharacteristic, &ioPinServiceIOCharacteristic, ioPinServiceDataCharacteristic};
Jonathan Austin 1:8aa5cdb4ab67 67 GattService service(MicroBitIOPinServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
Jonathan Austin 1:8aa5cdb4ab67 68
Jonathan Austin 1:8aa5cdb4ab67 69 ble.addService(service);
Jonathan Austin 1:8aa5cdb4ab67 70
Jonathan Austin 1:8aa5cdb4ab67 71 ioPinServiceADCharacteristicHandle = ioPinServiceADCharacteristic.getValueHandle();
Jonathan Austin 1:8aa5cdb4ab67 72 ioPinServiceIOCharacteristicHandle = ioPinServiceIOCharacteristic.getValueHandle();
Jonathan Austin 1:8aa5cdb4ab67 73
Jonathan Austin 1:8aa5cdb4ab67 74 ble.gattServer().write(ioPinServiceADCharacteristicHandle, (const uint8_t *)&ioPinServiceADCharacteristicBuffer, sizeof(ioPinServiceADCharacteristicBuffer));
Jonathan Austin 1:8aa5cdb4ab67 75 ble.gattServer().write(ioPinServiceIOCharacteristicHandle, (const uint8_t *)&ioPinServiceIOCharacteristicBuffer, sizeof(ioPinServiceIOCharacteristicBuffer));
Jonathan Austin 1:8aa5cdb4ab67 76
Jonathan Austin 1:8aa5cdb4ab67 77 ble.onDataWritten(this, &MicroBitIOPinService::onDataWritten);
Jonathan Austin 1:8aa5cdb4ab67 78 fiber_add_idle_component(this);
Jonathan Austin 1:8aa5cdb4ab67 79 }
Jonathan Austin 1:8aa5cdb4ab67 80
Jonathan Austin 1:8aa5cdb4ab67 81 /**
Jonathan Austin 1:8aa5cdb4ab67 82 * Determines if the given pin was configured as a digital pin by the BLE ADPinConfigurationCharacterisitic.
Jonathan Austin 1:8aa5cdb4ab67 83 *
Jonathan Austin 1:8aa5cdb4ab67 84 * @param i the enumeration of the pin to test
Jonathan Austin 1:8aa5cdb4ab67 85 * @return 1 if this pin is configured as digital, 0 otherwise
Jonathan Austin 1:8aa5cdb4ab67 86 */
Jonathan Austin 1:8aa5cdb4ab67 87 int MicroBitIOPinService::isDigital(int i)
Jonathan Austin 1:8aa5cdb4ab67 88 {
Jonathan Austin 1:8aa5cdb4ab67 89 return ((ioPinServiceADCharacteristicBuffer & (1 << i)) == 0);
Jonathan Austin 1:8aa5cdb4ab67 90 }
Jonathan Austin 1:8aa5cdb4ab67 91
Jonathan Austin 1:8aa5cdb4ab67 92 /**
Jonathan Austin 1:8aa5cdb4ab67 93 * Determines if the given pin was configured as an analog pin by the BLE ADPinConfigurationCharacterisitic.
Jonathan Austin 1:8aa5cdb4ab67 94 *
Jonathan Austin 1:8aa5cdb4ab67 95 * @param i the enumeration of the pin to test
Jonathan Austin 1:8aa5cdb4ab67 96 * @return 1 if this pin is configured as analog, 0 otherwise
Jonathan Austin 1:8aa5cdb4ab67 97 */
Jonathan Austin 1:8aa5cdb4ab67 98 int MicroBitIOPinService::isAnalog(int i)
Jonathan Austin 1:8aa5cdb4ab67 99 {
Jonathan Austin 1:8aa5cdb4ab67 100 return ((ioPinServiceADCharacteristicBuffer & (1 << i)) != 0);
Jonathan Austin 1:8aa5cdb4ab67 101 }
Jonathan Austin 1:8aa5cdb4ab67 102
Jonathan Austin 1:8aa5cdb4ab67 103 /**
Jonathan Austin 1:8aa5cdb4ab67 104 * Determines if the given pin was configured as an input by the BLE IOPinConfigurationCharacterisitic.
Jonathan Austin 1:8aa5cdb4ab67 105 *
Jonathan Austin 1:8aa5cdb4ab67 106 * @param i the enumeration of the pin to test
Jonathan Austin 1:8aa5cdb4ab67 107 * @return 1 if this pin is configured as an input, 0 otherwise
Jonathan Austin 1:8aa5cdb4ab67 108 */
Jonathan Austin 1:8aa5cdb4ab67 109 int MicroBitIOPinService::isInput(int i)
Jonathan Austin 1:8aa5cdb4ab67 110 {
Jonathan Austin 1:8aa5cdb4ab67 111 return ((ioPinServiceIOCharacteristicBuffer & (1 << i)) != 0);
Jonathan Austin 1:8aa5cdb4ab67 112 }
Jonathan Austin 1:8aa5cdb4ab67 113
Jonathan Austin 1:8aa5cdb4ab67 114 /**
Jonathan Austin 1:8aa5cdb4ab67 115 * Determines if the given pin was configured as output by the BLE IOPinConfigurationCharacterisitic.
Jonathan Austin 1:8aa5cdb4ab67 116 *
Jonathan Austin 1:8aa5cdb4ab67 117 * @param i the enumeration of the pin to test
Jonathan Austin 1:8aa5cdb4ab67 118 * @return 1 if this pin is configured as an output, 0 otherwise
Jonathan Austin 1:8aa5cdb4ab67 119 */
Jonathan Austin 1:8aa5cdb4ab67 120 int MicroBitIOPinService::isOutput(int i)
Jonathan Austin 1:8aa5cdb4ab67 121 {
Jonathan Austin 1:8aa5cdb4ab67 122 return ((ioPinServiceIOCharacteristicBuffer & (1 << i)) == 0);
Jonathan Austin 1:8aa5cdb4ab67 123 }
Jonathan Austin 1:8aa5cdb4ab67 124
Jonathan Austin 1:8aa5cdb4ab67 125 /**
Jonathan Austin 1:8aa5cdb4ab67 126 * Callback. Invoked when any of our attributes are written via BLE.
Jonathan Austin 1:8aa5cdb4ab67 127 */
Jonathan Austin 1:8aa5cdb4ab67 128 void MicroBitIOPinService::onDataWritten(const GattWriteCallbackParams *params)
Jonathan Austin 1:8aa5cdb4ab67 129 {
Jonathan Austin 1:8aa5cdb4ab67 130 // Check for writes to the IO configuration characteristic
Jonathan Austin 1:8aa5cdb4ab67 131 if (params->handle == ioPinServiceIOCharacteristicHandle && params->len >= sizeof(ioPinServiceIOCharacteristicBuffer))
Jonathan Austin 1:8aa5cdb4ab67 132 {
Jonathan Austin 1:8aa5cdb4ab67 133 uint32_t *value = (uint32_t *)params->data;
Jonathan Austin 1:8aa5cdb4ab67 134
Jonathan Austin 1:8aa5cdb4ab67 135 // Our IO configuration may be changing... read the new value, and push it back into the BLE stack.
Jonathan Austin 1:8aa5cdb4ab67 136 ioPinServiceIOCharacteristicBuffer = *value;
Jonathan Austin 1:8aa5cdb4ab67 137 ble.gattServer().write(ioPinServiceIOCharacteristicHandle, (const uint8_t *)&ioPinServiceIOCharacteristicBuffer, sizeof(ioPinServiceIOCharacteristicBuffer));
Jonathan Austin 1:8aa5cdb4ab67 138
Jonathan Austin 1:8aa5cdb4ab67 139 // Also, drop any selected pins into input mode, so we can pick up changes later
Jonathan Austin 1:8aa5cdb4ab67 140 for (int i=0; i < MICROBIT_IO_PIN_SERVICE_PINCOUNT; i++)
Jonathan Austin 1:8aa5cdb4ab67 141 {
Jonathan Austin 1:8aa5cdb4ab67 142 if(isDigital(i) && isInput(i))
Jonathan Austin 1:8aa5cdb4ab67 143 io.pin[i].getDigitalValue();
Jonathan Austin 1:8aa5cdb4ab67 144 //MicroBitIOPins[i]->getDigitalValue();
Jonathan Austin 1:8aa5cdb4ab67 145
Jonathan Austin 1:8aa5cdb4ab67 146 if(isAnalog(i) && isInput(i))
Jonathan Austin 1:8aa5cdb4ab67 147 io.pin[i].getAnalogValue();
Jonathan Austin 1:8aa5cdb4ab67 148 //MicroBitIOPins[i]->getAnalogValue();
Jonathan Austin 1:8aa5cdb4ab67 149 }
Jonathan Austin 1:8aa5cdb4ab67 150 }
Jonathan Austin 1:8aa5cdb4ab67 151
Jonathan Austin 1:8aa5cdb4ab67 152 // Check for writes to the IO configuration characteristic
Jonathan Austin 1:8aa5cdb4ab67 153 if (params->handle == ioPinServiceADCharacteristicHandle && params->len >= sizeof(ioPinServiceADCharacteristicBuffer))
Jonathan Austin 1:8aa5cdb4ab67 154 {
Jonathan Austin 1:8aa5cdb4ab67 155 uint32_t *value = (uint32_t *)params->data;
Jonathan Austin 1:8aa5cdb4ab67 156
Jonathan Austin 1:8aa5cdb4ab67 157 // Our IO configuration may be changing... read the new value, and push it back into the BLE stack.
Jonathan Austin 1:8aa5cdb4ab67 158 ioPinServiceADCharacteristicBuffer = *value;
Jonathan Austin 1:8aa5cdb4ab67 159 ble.gattServer().write(ioPinServiceADCharacteristicHandle, (const uint8_t *)&ioPinServiceADCharacteristicBuffer, sizeof(ioPinServiceADCharacteristicBuffer));
Jonathan Austin 1:8aa5cdb4ab67 160
Jonathan Austin 1:8aa5cdb4ab67 161 // Also, drop any selected pins into input mode, so we can pick up changes later
Jonathan Austin 1:8aa5cdb4ab67 162 for (int i=0; i < MICROBIT_IO_PIN_SERVICE_PINCOUNT; i++)
Jonathan Austin 1:8aa5cdb4ab67 163 {
Jonathan Austin 1:8aa5cdb4ab67 164 if(isDigital(i) && isInput(i))
Jonathan Austin 1:8aa5cdb4ab67 165 io.pin[i].getDigitalValue();
Jonathan Austin 1:8aa5cdb4ab67 166 //MicroBitIOPins[i]->getDigitalValue();
Jonathan Austin 1:8aa5cdb4ab67 167
Jonathan Austin 1:8aa5cdb4ab67 168 if(isAnalog(i) && isInput(i))
Jonathan Austin 1:8aa5cdb4ab67 169 io.pin[i].getAnalogValue();
Jonathan Austin 1:8aa5cdb4ab67 170 //MicroBitIOPins[i]->getAnalogValue();
Jonathan Austin 1:8aa5cdb4ab67 171 }
Jonathan Austin 1:8aa5cdb4ab67 172 }
Jonathan Austin 1:8aa5cdb4ab67 173
Jonathan Austin 1:8aa5cdb4ab67 174 if (params->handle == ioPinServiceDataCharacteristic->getValueHandle())
Jonathan Austin 1:8aa5cdb4ab67 175 {
Jonathan Austin 1:8aa5cdb4ab67 176 // We have some pin data to change...
Jonathan Austin 1:8aa5cdb4ab67 177 uint16_t len = params->len;
Jonathan Austin 1:8aa5cdb4ab67 178 IOData *data = (IOData *)params->data;
Jonathan Austin 1:8aa5cdb4ab67 179
Jonathan Austin 1:8aa5cdb4ab67 180 // There may be multiple write operaitons... take each in turn and update the pin values
Jonathan Austin 1:8aa5cdb4ab67 181 while (len >= sizeof(IOData))
Jonathan Austin 1:8aa5cdb4ab67 182 {
Jonathan Austin 1:8aa5cdb4ab67 183 if (isOutput(data->pin))
Jonathan Austin 1:8aa5cdb4ab67 184 {
Jonathan Austin 1:8aa5cdb4ab67 185 if (isDigital(data->pin))
Jonathan Austin 1:8aa5cdb4ab67 186 io.pin[data->pin].setDigitalValue(data->value);
Jonathan Austin 1:8aa5cdb4ab67 187 //MicroBitIOPins[data->pin]->setDigitalValue(data->value);
Jonathan Austin 1:8aa5cdb4ab67 188 else
Jonathan Austin 1:8aa5cdb4ab67 189 io.pin[data->pin].setAnalogValue(data->value*4);
Jonathan Austin 1:8aa5cdb4ab67 190 //MicroBitIOPins[data->pin]->setAnalogValue(data->value*4);
Jonathan Austin 1:8aa5cdb4ab67 191 }
Jonathan Austin 1:8aa5cdb4ab67 192
Jonathan Austin 1:8aa5cdb4ab67 193 data++;
Jonathan Austin 1:8aa5cdb4ab67 194 len -= sizeof(IOData);
Jonathan Austin 1:8aa5cdb4ab67 195 }
Jonathan Austin 1:8aa5cdb4ab67 196 }
Jonathan Austin 1:8aa5cdb4ab67 197 }
Jonathan Austin 1:8aa5cdb4ab67 198
Jonathan Austin 1:8aa5cdb4ab67 199 /**
Jonathan Austin 1:8aa5cdb4ab67 200 * Callback. invoked when the BLE data characteristic is read.
Jonathan Austin 1:8aa5cdb4ab67 201 *
Jonathan Austin 1:8aa5cdb4ab67 202 * Reads all the pins marked as inputs, and updates the data stored in the characteristic.
Jonathan Austin 1:8aa5cdb4ab67 203 */
Jonathan Austin 1:8aa5cdb4ab67 204 void MicroBitIOPinService::onDataRead(GattReadAuthCallbackParams *params)
Jonathan Austin 1:8aa5cdb4ab67 205 {
Jonathan Austin 1:8aa5cdb4ab67 206 if (params->handle == ioPinServiceDataCharacteristic->getValueHandle())
Jonathan Austin 1:8aa5cdb4ab67 207 {
Jonathan Austin 1:8aa5cdb4ab67 208
Jonathan Austin 1:8aa5cdb4ab67 209 // Scan through all pins that our BLE client may be listening for. If any have changed value, update the BLE characterisitc, and NOTIFY our client.
Jonathan Austin 1:8aa5cdb4ab67 210 int pairs = 0;
Jonathan Austin 1:8aa5cdb4ab67 211
Jonathan Austin 1:8aa5cdb4ab67 212 for (int i=0; i < MICROBIT_IO_PIN_SERVICE_PINCOUNT; i++)
Jonathan Austin 1:8aa5cdb4ab67 213 {
Jonathan Austin 1:8aa5cdb4ab67 214 if (isInput(i))
Jonathan Austin 1:8aa5cdb4ab67 215 {
Jonathan Austin 1:8aa5cdb4ab67 216 uint8_t value;
Jonathan Austin 1:8aa5cdb4ab67 217
Jonathan Austin 1:8aa5cdb4ab67 218 if (isDigital(i))
Jonathan Austin 1:8aa5cdb4ab67 219 value = io.pin[i].getDigitalValue();
Jonathan Austin 1:8aa5cdb4ab67 220 //value = MicroBitIOPins[i]->getDigitalValue();
Jonathan Austin 1:8aa5cdb4ab67 221 else
Jonathan Austin 1:8aa5cdb4ab67 222 value = io.pin[i].getAnalogValue();
Jonathan Austin 1:8aa5cdb4ab67 223 //value = MicroBitIOPins[i]->getAnalogValue();
Jonathan Austin 1:8aa5cdb4ab67 224
Jonathan Austin 1:8aa5cdb4ab67 225 ioPinServiceIOData[i] = value;
Jonathan Austin 1:8aa5cdb4ab67 226 ioPinServiceDataCharacteristicBuffer[pairs].pin = i;
Jonathan Austin 1:8aa5cdb4ab67 227 ioPinServiceDataCharacteristicBuffer[pairs].value = value;
Jonathan Austin 1:8aa5cdb4ab67 228
Jonathan Austin 1:8aa5cdb4ab67 229 pairs++;
Jonathan Austin 1:8aa5cdb4ab67 230
Jonathan Austin 1:8aa5cdb4ab67 231 if (pairs >= MICROBIT_IO_PIN_SERVICE_DATA_SIZE)
Jonathan Austin 1:8aa5cdb4ab67 232 break;
Jonathan Austin 1:8aa5cdb4ab67 233 }
Jonathan Austin 1:8aa5cdb4ab67 234 }
Jonathan Austin 1:8aa5cdb4ab67 235
Jonathan Austin 1:8aa5cdb4ab67 236 // If there's any data, issue a BLE notification.
Jonathan Austin 1:8aa5cdb4ab67 237 if (pairs > 0)
Jonathan Austin 1:8aa5cdb4ab67 238 ble.gattServer().notify(ioPinServiceDataCharacteristic->getValueHandle(), (uint8_t *)ioPinServiceDataCharacteristicBuffer, pairs * sizeof(IOData));
Jonathan Austin 1:8aa5cdb4ab67 239 }
Jonathan Austin 1:8aa5cdb4ab67 240 }
Jonathan Austin 1:8aa5cdb4ab67 241
Jonathan Austin 1:8aa5cdb4ab67 242
Jonathan Austin 1:8aa5cdb4ab67 243 /**
Jonathan Austin 1:8aa5cdb4ab67 244 * Periodic callback from MicroBit scheduler.
Jonathan Austin 1:8aa5cdb4ab67 245 *
Jonathan Austin 1:8aa5cdb4ab67 246 * Check if any of the pins we're watching need updating. Notify any connected
Jonathan Austin 1:8aa5cdb4ab67 247 * device with any changes.
Jonathan Austin 1:8aa5cdb4ab67 248 */
Jonathan Austin 1:8aa5cdb4ab67 249 void MicroBitIOPinService::idleTick()
Jonathan Austin 1:8aa5cdb4ab67 250 {
Jonathan Austin 1:8aa5cdb4ab67 251 // If we're not we're connected, then there's nothing to do...
Jonathan Austin 1:8aa5cdb4ab67 252 if (!ble.getGapState().connected)
Jonathan Austin 1:8aa5cdb4ab67 253 return;
Jonathan Austin 1:8aa5cdb4ab67 254
Jonathan Austin 1:8aa5cdb4ab67 255 // Scan through all pins that our BLE client may be listening for. If any have changed value, update the BLE characterisitc, and NOTIFY our client.
Jonathan Austin 1:8aa5cdb4ab67 256 int pairs = 0;
Jonathan Austin 1:8aa5cdb4ab67 257
Jonathan Austin 1:8aa5cdb4ab67 258 for (int i=0; i < MICROBIT_IO_PIN_SERVICE_PINCOUNT; i++)
Jonathan Austin 1:8aa5cdb4ab67 259 {
Jonathan Austin 1:8aa5cdb4ab67 260 if (isInput(i))
Jonathan Austin 1:8aa5cdb4ab67 261 {
Jonathan Austin 1:8aa5cdb4ab67 262 uint8_t value;
Jonathan Austin 1:8aa5cdb4ab67 263
Jonathan Austin 1:8aa5cdb4ab67 264 if (isDigital(i))
Jonathan Austin 1:8aa5cdb4ab67 265 value = io.pin[i].getDigitalValue();
Jonathan Austin 1:8aa5cdb4ab67 266 //value = MicroBitIOPins[i]->getDigitalValue();
Jonathan Austin 1:8aa5cdb4ab67 267 else
Jonathan Austin 1:8aa5cdb4ab67 268 value = io.pin[i].getAnalogValue();
Jonathan Austin 1:8aa5cdb4ab67 269 //value = MicroBitIOPins[i]->getAnalogValue();
Jonathan Austin 1:8aa5cdb4ab67 270
Jonathan Austin 1:8aa5cdb4ab67 271 // If the data has changed, send an update.
Jonathan Austin 1:8aa5cdb4ab67 272 if (value != ioPinServiceIOData[i])
Jonathan Austin 1:8aa5cdb4ab67 273 {
Jonathan Austin 1:8aa5cdb4ab67 274 ioPinServiceIOData[i] = value;
Jonathan Austin 1:8aa5cdb4ab67 275
Jonathan Austin 1:8aa5cdb4ab67 276 ioPinServiceDataCharacteristicBuffer[pairs].pin = i;
Jonathan Austin 1:8aa5cdb4ab67 277 ioPinServiceDataCharacteristicBuffer[pairs].value = value;
Jonathan Austin 1:8aa5cdb4ab67 278
Jonathan Austin 1:8aa5cdb4ab67 279 pairs++;
Jonathan Austin 1:8aa5cdb4ab67 280
Jonathan Austin 1:8aa5cdb4ab67 281 if (pairs >= MICROBIT_IO_PIN_SERVICE_DATA_SIZE)
Jonathan Austin 1:8aa5cdb4ab67 282 break;
Jonathan Austin 1:8aa5cdb4ab67 283 }
Jonathan Austin 1:8aa5cdb4ab67 284 }
Jonathan Austin 1:8aa5cdb4ab67 285 }
Jonathan Austin 1:8aa5cdb4ab67 286
Jonathan Austin 1:8aa5cdb4ab67 287 // If there were any changes, issue a BLE notification.
Jonathan Austin 1:8aa5cdb4ab67 288 if (pairs > 0)
Jonathan Austin 1:8aa5cdb4ab67 289 ble.gattServer().notify(ioPinServiceDataCharacteristic->getValueHandle(), (uint8_t *)ioPinServiceDataCharacteristicBuffer, pairs * sizeof(IOData));
Jonathan Austin 1:8aa5cdb4ab67 290 }
Jonathan Austin 1:8aa5cdb4ab67 291
Jonathan Austin 1:8aa5cdb4ab67 292 const uint8_t MicroBitIOPinServiceUUID[] = {
Jonathan Austin 1:8aa5cdb4ab67 293 0xe9,0x5d,0x12,0x7b,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
Jonathan Austin 1:8aa5cdb4ab67 294 };
Jonathan Austin 1:8aa5cdb4ab67 295
Jonathan Austin 1:8aa5cdb4ab67 296 const uint8_t MicroBitIOPinServiceIOConfigurationUUID[] = {
Jonathan Austin 1:8aa5cdb4ab67 297 0xe9,0x5d,0xb9,0xfe,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
Jonathan Austin 1:8aa5cdb4ab67 298 };
Jonathan Austin 1:8aa5cdb4ab67 299
Jonathan Austin 1:8aa5cdb4ab67 300 const uint8_t MicroBitIOPinServiceADConfigurationUUID[] = {
Jonathan Austin 1:8aa5cdb4ab67 301 0xe9,0x5d,0x58,0x99,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
Jonathan Austin 1:8aa5cdb4ab67 302 };
Jonathan Austin 1:8aa5cdb4ab67 303
Jonathan Austin 1:8aa5cdb4ab67 304 const uint8_t MicroBitIOPinServiceDataUUID[] = {
Jonathan Austin 1:8aa5cdb4ab67 305 0xe9,0x5d,0x8d,0x00,0x25,0x1d,0x47,0x0a,0xa0,0x62,0xfa,0x19,0x22,0xdf,0xa9,0xa8
Jonathan Austin 1:8aa5cdb4ab67 306 };
Jonathan Austin 1:8aa5cdb4ab67 307
Jonathan Austin 1:8aa5cdb4ab67 308 /*
Jonathan Austin 1:8aa5cdb4ab67 309 MicroBitPin * const MicroBitIOPins[] = {
Jonathan Austin 1:8aa5cdb4ab67 310 &uBit.io.P0,
Jonathan Austin 1:8aa5cdb4ab67 311 &uBit.io.P1,
Jonathan Austin 1:8aa5cdb4ab67 312 &uBit.io.P2,
Jonathan Austin 1:8aa5cdb4ab67 313 &uBit.io.P3,
Jonathan Austin 1:8aa5cdb4ab67 314 &uBit.io.P4,
Jonathan Austin 1:8aa5cdb4ab67 315 &uBit.io.P5,
Jonathan Austin 1:8aa5cdb4ab67 316 &uBit.io.P6,
Jonathan Austin 1:8aa5cdb4ab67 317 &uBit.io.P7,
Jonathan Austin 1:8aa5cdb4ab67 318 &uBit.io.P8,
Jonathan Austin 1:8aa5cdb4ab67 319 &uBit.io.P9,
Jonathan Austin 1:8aa5cdb4ab67 320 &uBit.io.P10,
Jonathan Austin 1:8aa5cdb4ab67 321 &uBit.io.P11,
Jonathan Austin 1:8aa5cdb4ab67 322 &uBit.io.P12,
Jonathan Austin 1:8aa5cdb4ab67 323 &uBit.io.P13,
Jonathan Austin 1:8aa5cdb4ab67 324 &uBit.io.P14,
Jonathan Austin 1:8aa5cdb4ab67 325 &uBit.io.P15,
Jonathan Austin 1:8aa5cdb4ab67 326 &uBit.io.P16,
Jonathan Austin 1:8aa5cdb4ab67 327 &uBit.io.P19,
Jonathan Austin 1:8aa5cdb4ab67 328 &uBit.io.P20
Jonathan Austin 1:8aa5cdb4ab67 329 };
Jonathan Austin 1:8aa5cdb4ab67 330 */