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