Modify the BLE LED Control so that it toggles the LED on the other board only if you keep the button pressed
source/main.cpp@0:8c76e25a3cff, 2018-11-29 (annotated)
- Committer:
- vicara
- Date:
- Thu Nov 29 18:32:03 2018 +0000
- Revision:
- 0:8c76e25a3cff
Modify the BLE LED Control so that it; toggles the LED on the other board only if; you keep the button pressed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vicara | 0:8c76e25a3cff | 1 | /* mbed Microcontroller Library |
vicara | 0:8c76e25a3cff | 2 | * Copyright (c) 2006-2015 ARM Limited |
vicara | 0:8c76e25a3cff | 3 | * |
vicara | 0:8c76e25a3cff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
vicara | 0:8c76e25a3cff | 5 | * you may not use this file except in compliance with the License. |
vicara | 0:8c76e25a3cff | 6 | * You may obtain a copy of the License at |
vicara | 0:8c76e25a3cff | 7 | * |
vicara | 0:8c76e25a3cff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
vicara | 0:8c76e25a3cff | 9 | * |
vicara | 0:8c76e25a3cff | 10 | * Unless required by applicable law or agreed to in writing, software |
vicara | 0:8c76e25a3cff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
vicara | 0:8c76e25a3cff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
vicara | 0:8c76e25a3cff | 13 | * See the License for the specific language governing permissions and |
vicara | 0:8c76e25a3cff | 14 | * limitations under the License. |
vicara | 0:8c76e25a3cff | 15 | */ |
vicara | 0:8c76e25a3cff | 16 | |
vicara | 0:8c76e25a3cff | 17 | #include <events/mbed_events.h> |
vicara | 0:8c76e25a3cff | 18 | #include <mbed.h> |
vicara | 0:8c76e25a3cff | 19 | #include "ble/BLE.h" |
vicara | 0:8c76e25a3cff | 20 | #include "ble/DiscoveredCharacteristic.h" |
vicara | 0:8c76e25a3cff | 21 | #include "ble/DiscoveredService.h" |
vicara | 0:8c76e25a3cff | 22 | |
vicara | 0:8c76e25a3cff | 23 | DigitalOut alivenessLED(LED1, 1); |
vicara | 0:8c76e25a3cff | 24 | static DiscoveredCharacteristic ledCharacteristic; |
vicara | 0:8c76e25a3cff | 25 | static bool triggerLedCharacteristic; |
vicara | 0:8c76e25a3cff | 26 | static const char PEER_NAME[] = "LED_RED"; |
vicara | 0:8c76e25a3cff | 27 | InterruptIn button(USER_BUTTON); |
vicara | 0:8c76e25a3cff | 28 | static uint8_t toggledValue = 0x0; |
vicara | 0:8c76e25a3cff | 29 | |
vicara | 0:8c76e25a3cff | 30 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
vicara | 0:8c76e25a3cff | 31 | |
vicara | 0:8c76e25a3cff | 32 | void periodicCallback(void) { |
vicara | 0:8c76e25a3cff | 33 | alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */ |
vicara | 0:8c76e25a3cff | 34 | } |
vicara | 0:8c76e25a3cff | 35 | |
vicara | 0:8c76e25a3cff | 36 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
vicara | 0:8c76e25a3cff | 37 | // parse the advertising payload, looking for data type COMPLETE_LOCAL_NAME |
vicara | 0:8c76e25a3cff | 38 | // The advertising payload is a collection of key/value records where |
vicara | 0:8c76e25a3cff | 39 | // byte 0: length of the record excluding this byte |
vicara | 0:8c76e25a3cff | 40 | // byte 1: The key, it is the type of the data |
vicara | 0:8c76e25a3cff | 41 | // byte [2..N] The value. N is equal to byte0 - 1 |
vicara | 0:8c76e25a3cff | 42 | for (uint8_t i = 0; i < params->advertisingDataLen; ++i) { |
vicara | 0:8c76e25a3cff | 43 | |
vicara | 0:8c76e25a3cff | 44 | const uint8_t record_length = params->advertisingData[i]; |
vicara | 0:8c76e25a3cff | 45 | if (record_length == 0) { |
vicara | 0:8c76e25a3cff | 46 | continue; |
vicara | 0:8c76e25a3cff | 47 | } |
vicara | 0:8c76e25a3cff | 48 | const uint8_t type = params->advertisingData[i + 1]; |
vicara | 0:8c76e25a3cff | 49 | const uint8_t* value = params->advertisingData + i + 2; |
vicara | 0:8c76e25a3cff | 50 | const uint8_t value_length = record_length - 1; |
vicara | 0:8c76e25a3cff | 51 | |
vicara | 0:8c76e25a3cff | 52 | if(type == GapAdvertisingData::COMPLETE_LOCAL_NAME) { |
vicara | 0:8c76e25a3cff | 53 | if ((value_length == sizeof(PEER_NAME)) && (memcmp(value, PEER_NAME, value_length) == 0)) { |
vicara | 0:8c76e25a3cff | 54 | printf( |
vicara | 0:8c76e25a3cff | 55 | "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n", |
vicara | 0:8c76e25a3cff | 56 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], |
vicara | 0:8c76e25a3cff | 57 | params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type |
vicara | 0:8c76e25a3cff | 58 | ); |
vicara | 0:8c76e25a3cff | 59 | BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
vicara | 0:8c76e25a3cff | 60 | break; |
vicara | 0:8c76e25a3cff | 61 | } |
vicara | 0:8c76e25a3cff | 62 | } |
vicara | 0:8c76e25a3cff | 63 | i += record_length; |
vicara | 0:8c76e25a3cff | 64 | } |
vicara | 0:8c76e25a3cff | 65 | } |
vicara | 0:8c76e25a3cff | 66 | |
vicara | 0:8c76e25a3cff | 67 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
vicara | 0:8c76e25a3cff | 68 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
vicara | 0:8c76e25a3cff | 69 | printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
vicara | 0:8c76e25a3cff | 70 | } else { |
vicara | 0:8c76e25a3cff | 71 | printf("S UUID-"); |
vicara | 0:8c76e25a3cff | 72 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
vicara | 0:8c76e25a3cff | 73 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
vicara | 0:8c76e25a3cff | 74 | printf("%02x", longUUIDBytes[i]); |
vicara | 0:8c76e25a3cff | 75 | } |
vicara | 0:8c76e25a3cff | 76 | printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
vicara | 0:8c76e25a3cff | 77 | } |
vicara | 0:8c76e25a3cff | 78 | } |
vicara | 0:8c76e25a3cff | 79 | |
vicara | 0:8c76e25a3cff | 80 | void updateLedCharacteristic(void) { |
vicara | 0:8c76e25a3cff | 81 | if (!BLE::Instance().gattClient().isServiceDiscoveryActive()) { |
vicara | 0:8c76e25a3cff | 82 | ledCharacteristic.read(); |
vicara | 0:8c76e25a3cff | 83 | } |
vicara | 0:8c76e25a3cff | 84 | } |
vicara | 0:8c76e25a3cff | 85 | |
vicara | 0:8c76e25a3cff | 86 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
vicara | 0:8c76e25a3cff | 87 | printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast()); |
vicara | 0:8c76e25a3cff | 88 | if (characteristicP->getUUID().getShortUUID() == 0xa001) { /* !ALERT! Alter this filter to suit your device. */ |
vicara | 0:8c76e25a3cff | 89 | ledCharacteristic = *characteristicP; |
vicara | 0:8c76e25a3cff | 90 | triggerLedCharacteristic = true; |
vicara | 0:8c76e25a3cff | 91 | } |
vicara | 0:8c76e25a3cff | 92 | } |
vicara | 0:8c76e25a3cff | 93 | |
vicara | 0:8c76e25a3cff | 94 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
vicara | 0:8c76e25a3cff | 95 | printf("terminated SD for handle %u\r\n", connectionHandle); |
vicara | 0:8c76e25a3cff | 96 | if (triggerLedCharacteristic) { |
vicara | 0:8c76e25a3cff | 97 | triggerLedCharacteristic = false; |
vicara | 0:8c76e25a3cff | 98 | eventQueue.call(updateLedCharacteristic); |
vicara | 0:8c76e25a3cff | 99 | } |
vicara | 0:8c76e25a3cff | 100 | } |
vicara | 0:8c76e25a3cff | 101 | |
vicara | 0:8c76e25a3cff | 102 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
vicara | 0:8c76e25a3cff | 103 | if (params->role == Gap::CENTRAL) { |
vicara | 0:8c76e25a3cff | 104 | BLE &ble = BLE::Instance(); |
vicara | 0:8c76e25a3cff | 105 | ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
vicara | 0:8c76e25a3cff | 106 | ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001); |
vicara | 0:8c76e25a3cff | 107 | } |
vicara | 0:8c76e25a3cff | 108 | } |
vicara | 0:8c76e25a3cff | 109 | |
vicara | 0:8c76e25a3cff | 110 | void triggerToggledWrite(const GattReadCallbackParams *response) { |
vicara | 0:8c76e25a3cff | 111 | if (response->handle == ledCharacteristic.getValueHandle()) { |
vicara | 0:8c76e25a3cff | 112 | printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len); |
vicara | 0:8c76e25a3cff | 113 | for (unsigned index = 0; index < response->len; index++) { |
vicara | 0:8c76e25a3cff | 114 | printf("%c[%02x]", response->data[index], response->data[index]); |
vicara | 0:8c76e25a3cff | 115 | } |
vicara | 0:8c76e25a3cff | 116 | printf("\r\n"); |
vicara | 0:8c76e25a3cff | 117 | |
vicara | 0:8c76e25a3cff | 118 | ledCharacteristic.write(1, &toggledValue); |
vicara | 0:8c76e25a3cff | 119 | } |
vicara | 0:8c76e25a3cff | 120 | } |
vicara | 0:8c76e25a3cff | 121 | |
vicara | 0:8c76e25a3cff | 122 | void triggerRead(const GattWriteCallbackParams *response) { |
vicara | 0:8c76e25a3cff | 123 | if (response->handle == ledCharacteristic.getValueHandle()) { |
vicara | 0:8c76e25a3cff | 124 | ledCharacteristic.read(); |
vicara | 0:8c76e25a3cff | 125 | } |
vicara | 0:8c76e25a3cff | 126 | } |
vicara | 0:8c76e25a3cff | 127 | |
vicara | 0:8c76e25a3cff | 128 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) { |
vicara | 0:8c76e25a3cff | 129 | printf("disconnected\r\n"); |
vicara | 0:8c76e25a3cff | 130 | /* Start scanning and try to connect again */ |
vicara | 0:8c76e25a3cff | 131 | BLE::Instance().gap().startScan(advertisementCallback); |
vicara | 0:8c76e25a3cff | 132 | } |
vicara | 0:8c76e25a3cff | 133 | |
vicara | 0:8c76e25a3cff | 134 | void onBleInitError(BLE &ble, ble_error_t error) |
vicara | 0:8c76e25a3cff | 135 | { |
vicara | 0:8c76e25a3cff | 136 | /* Initialization error handling should go here */ |
vicara | 0:8c76e25a3cff | 137 | } |
vicara | 0:8c76e25a3cff | 138 | |
vicara | 0:8c76e25a3cff | 139 | void printMacAddress() |
vicara | 0:8c76e25a3cff | 140 | { |
vicara | 0:8c76e25a3cff | 141 | /* Print out device MAC address to the console*/ |
vicara | 0:8c76e25a3cff | 142 | Gap::AddressType_t addr_type; |
vicara | 0:8c76e25a3cff | 143 | Gap::Address_t address; |
vicara | 0:8c76e25a3cff | 144 | BLE::Instance().gap().getAddress(&addr_type, address); |
vicara | 0:8c76e25a3cff | 145 | printf("DEVICE MAC ADDRESS: "); |
vicara | 0:8c76e25a3cff | 146 | for (int i = 5; i >= 1; i--){ |
vicara | 0:8c76e25a3cff | 147 | printf("%02x:", address[i]); |
vicara | 0:8c76e25a3cff | 148 | } |
vicara | 0:8c76e25a3cff | 149 | printf("%02x\r\n", address[0]); |
vicara | 0:8c76e25a3cff | 150 | } |
vicara | 0:8c76e25a3cff | 151 | |
vicara | 0:8c76e25a3cff | 152 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
vicara | 0:8c76e25a3cff | 153 | { |
vicara | 0:8c76e25a3cff | 154 | BLE& ble = params->ble; |
vicara | 0:8c76e25a3cff | 155 | ble_error_t error = params->error; |
vicara | 0:8c76e25a3cff | 156 | |
vicara | 0:8c76e25a3cff | 157 | if (error != BLE_ERROR_NONE) { |
vicara | 0:8c76e25a3cff | 158 | /* In case of error, forward the error handling to onBleInitError */ |
vicara | 0:8c76e25a3cff | 159 | onBleInitError(ble, error); |
vicara | 0:8c76e25a3cff | 160 | return; |
vicara | 0:8c76e25a3cff | 161 | } |
vicara | 0:8c76e25a3cff | 162 | |
vicara | 0:8c76e25a3cff | 163 | /* Ensure that it is the default instance of BLE */ |
vicara | 0:8c76e25a3cff | 164 | if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
vicara | 0:8c76e25a3cff | 165 | return; |
vicara | 0:8c76e25a3cff | 166 | } |
vicara | 0:8c76e25a3cff | 167 | |
vicara | 0:8c76e25a3cff | 168 | ble.gap().onDisconnection(disconnectionCallback); |
vicara | 0:8c76e25a3cff | 169 | ble.gap().onConnection(connectionCallback); |
vicara | 0:8c76e25a3cff | 170 | |
vicara | 0:8c76e25a3cff | 171 | ble.gattClient().onDataRead(triggerToggledWrite); |
vicara | 0:8c76e25a3cff | 172 | ble.gattClient().onDataWrite(triggerRead); |
vicara | 0:8c76e25a3cff | 173 | |
vicara | 0:8c76e25a3cff | 174 | // scan interval: 400ms and scan window: 400ms. |
vicara | 0:8c76e25a3cff | 175 | // Every 400ms the device will scan for 400ms |
vicara | 0:8c76e25a3cff | 176 | // This means that the device will scan continuously. |
vicara | 0:8c76e25a3cff | 177 | ble.gap().setScanParams(400, 400); |
vicara | 0:8c76e25a3cff | 178 | ble.gap().startScan(advertisementCallback); |
vicara | 0:8c76e25a3cff | 179 | |
vicara | 0:8c76e25a3cff | 180 | printMacAddress(); |
vicara | 0:8c76e25a3cff | 181 | } |
vicara | 0:8c76e25a3cff | 182 | |
vicara | 0:8c76e25a3cff | 183 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
vicara | 0:8c76e25a3cff | 184 | BLE &ble = BLE::Instance(); |
vicara | 0:8c76e25a3cff | 185 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
vicara | 0:8c76e25a3cff | 186 | } |
vicara | 0:8c76e25a3cff | 187 | |
vicara | 0:8c76e25a3cff | 188 | void btn_on (){ |
vicara | 0:8c76e25a3cff | 189 | toggledValue = 0x1; |
vicara | 0:8c76e25a3cff | 190 | } |
vicara | 0:8c76e25a3cff | 191 | |
vicara | 0:8c76e25a3cff | 192 | void btn_off (){ |
vicara | 0:8c76e25a3cff | 193 | toggledValue = 0x0; |
vicara | 0:8c76e25a3cff | 194 | } |
vicara | 0:8c76e25a3cff | 195 | |
vicara | 0:8c76e25a3cff | 196 | int main() |
vicara | 0:8c76e25a3cff | 197 | { |
vicara | 0:8c76e25a3cff | 198 | triggerLedCharacteristic = false; |
vicara | 0:8c76e25a3cff | 199 | eventQueue.call_every(500, periodicCallback); |
vicara | 0:8c76e25a3cff | 200 | |
vicara | 0:8c76e25a3cff | 201 | button.fall(&btn_on); |
vicara | 0:8c76e25a3cff | 202 | button.rise(&btn_off); |
vicara | 0:8c76e25a3cff | 203 | |
vicara | 0:8c76e25a3cff | 204 | BLE &ble = BLE::Instance(); |
vicara | 0:8c76e25a3cff | 205 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
vicara | 0:8c76e25a3cff | 206 | ble.init(bleInitComplete); |
vicara | 0:8c76e25a3cff | 207 | |
vicara | 0:8c76e25a3cff | 208 | eventQueue.dispatch_forever(); |
vicara | 0:8c76e25a3cff | 209 | |
vicara | 0:8c76e25a3cff | 210 | return 0; |
vicara | 0:8c76e25a3cff | 211 | } |