
For example with Central/Peripheral: simple service to toggle a led via BLE
Dependencies: BLE_API mbed nRF51822
Fork of BLE_LED_PERIPHERAL by
main.cpp@1:222379aed289, 2017-10-24 (annotated)
- Committer:
- Alexgerni
- Date:
- Tue Oct 24 18:24:18 2017 +0000
- Revision:
- 1:222379aed289
- Parent:
- 0:8ae63d3cc188
For example with Central/Peripheral: simple service to toggle a led via BLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Alexgerni | 0:8ae63d3cc188 | 1 | /* mbed Microcontroller Library |
Alexgerni | 0:8ae63d3cc188 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Alexgerni | 0:8ae63d3cc188 | 3 | * |
Alexgerni | 0:8ae63d3cc188 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Alexgerni | 0:8ae63d3cc188 | 5 | * you may not use this file except in compliance with the License. |
Alexgerni | 0:8ae63d3cc188 | 6 | * You may obtain a copy of the License at |
Alexgerni | 0:8ae63d3cc188 | 7 | * |
Alexgerni | 0:8ae63d3cc188 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Alexgerni | 0:8ae63d3cc188 | 9 | * |
Alexgerni | 0:8ae63d3cc188 | 10 | * Unless required by applicable law or agreed to in writing, software |
Alexgerni | 0:8ae63d3cc188 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Alexgerni | 0:8ae63d3cc188 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Alexgerni | 0:8ae63d3cc188 | 13 | * See the License for the specific language governing permissions and |
Alexgerni | 0:8ae63d3cc188 | 14 | * limitations under the License. |
Alexgerni | 0:8ae63d3cc188 | 15 | */ |
Alexgerni | 0:8ae63d3cc188 | 16 | |
Alexgerni | 0:8ae63d3cc188 | 17 | #include "mbed.h" |
Alexgerni | 0:8ae63d3cc188 | 18 | #include "ble/BLE.h" |
Alexgerni | 0:8ae63d3cc188 | 19 | #include "LEDService.h" |
Alexgerni | 0:8ae63d3cc188 | 20 | |
Alexgerni | 1:222379aed289 | 21 | DigitalOut alivenessLED(p16, 0); |
Alexgerni | 1:222379aed289 | 22 | DigitalOut actuatedLED(p6, 0); |
Alexgerni | 0:8ae63d3cc188 | 23 | |
Alexgerni | 0:8ae63d3cc188 | 24 | Serial pc(p5, p4); |
Alexgerni | 0:8ae63d3cc188 | 25 | |
Alexgerni | 0:8ae63d3cc188 | 26 | const static char DEVICE_NAME[] = "LED"; |
Alexgerni | 0:8ae63d3cc188 | 27 | static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID}; |
Alexgerni | 0:8ae63d3cc188 | 28 | |
Alexgerni | 0:8ae63d3cc188 | 29 | uint8_t ts = 1; |
Alexgerni | 0:8ae63d3cc188 | 30 | |
Alexgerni | 0:8ae63d3cc188 | 31 | LEDService *ledServicePtr; |
Alexgerni | 0:8ae63d3cc188 | 32 | |
Alexgerni | 0:8ae63d3cc188 | 33 | Ticker ticker; |
Alexgerni | 1:222379aed289 | 34 | bool onConnection = false; |
Alexgerni | 0:8ae63d3cc188 | 35 | |
Alexgerni | 0:8ae63d3cc188 | 36 | void periodicCallback(void) |
Alexgerni | 0:8ae63d3cc188 | 37 | { |
Alexgerni | 1:222379aed289 | 38 | if(!onConnection){ |
Alexgerni | 1:222379aed289 | 39 | alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */ |
Alexgerni | 1:222379aed289 | 40 | actuatedLED = 1; |
Alexgerni | 1:222379aed289 | 41 | } |
Alexgerni | 0:8ae63d3cc188 | 42 | } |
Alexgerni | 0:8ae63d3cc188 | 43 | |
Alexgerni | 0:8ae63d3cc188 | 44 | /** |
Alexgerni | 0:8ae63d3cc188 | 45 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
Alexgerni | 0:8ae63d3cc188 | 46 | * |
Alexgerni | 0:8ae63d3cc188 | 47 | * @param[in] params |
Alexgerni | 0:8ae63d3cc188 | 48 | * Information about the characterisitc being updated. |
Alexgerni | 0:8ae63d3cc188 | 49 | */ |
Alexgerni | 0:8ae63d3cc188 | 50 | void onDataWrittenCallback(const GattWriteCallbackParams *params) |
Alexgerni | 0:8ae63d3cc188 | 51 | { |
Alexgerni | 0:8ae63d3cc188 | 52 | if ((params->handle == ledServicePtr->getValueHandle())) { |
Alexgerni | 0:8ae63d3cc188 | 53 | actuatedLED = *(params->data); |
Alexgerni | 1:222379aed289 | 54 | onConnection = true; |
Alexgerni | 1:222379aed289 | 55 | alivenessLED = 1; |
Alexgerni | 0:8ae63d3cc188 | 56 | } |
Alexgerni | 0:8ae63d3cc188 | 57 | } |
Alexgerni | 0:8ae63d3cc188 | 58 | |
Alexgerni | 0:8ae63d3cc188 | 59 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Alexgerni | 0:8ae63d3cc188 | 60 | { |
Alexgerni | 0:8ae63d3cc188 | 61 | BLE::Instance().gap().startAdvertising(); |
Alexgerni | 1:222379aed289 | 62 | onConnection = false; |
Alexgerni | 0:8ae63d3cc188 | 63 | } |
Alexgerni | 0:8ae63d3cc188 | 64 | |
Alexgerni | 0:8ae63d3cc188 | 65 | |
Alexgerni | 0:8ae63d3cc188 | 66 | /** |
Alexgerni | 0:8ae63d3cc188 | 67 | * This function is called when the ble initialization process has failed |
Alexgerni | 0:8ae63d3cc188 | 68 | */ |
Alexgerni | 0:8ae63d3cc188 | 69 | void onBleInitError(BLE &ble, ble_error_t error) |
Alexgerni | 0:8ae63d3cc188 | 70 | { |
Alexgerni | 0:8ae63d3cc188 | 71 | /* Initialization error handling should go here */ |
Alexgerni | 0:8ae63d3cc188 | 72 | } |
Alexgerni | 0:8ae63d3cc188 | 73 | |
Alexgerni | 0:8ae63d3cc188 | 74 | /** |
Alexgerni | 0:8ae63d3cc188 | 75 | * Callback triggered when the ble initialization process has finished |
Alexgerni | 0:8ae63d3cc188 | 76 | */ |
Alexgerni | 0:8ae63d3cc188 | 77 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
Alexgerni | 0:8ae63d3cc188 | 78 | { |
Alexgerni | 0:8ae63d3cc188 | 79 | BLE& ble = params->ble; |
Alexgerni | 0:8ae63d3cc188 | 80 | ble_error_t error = params->error; |
Alexgerni | 1:222379aed289 | 81 | |
Alexgerni | 0:8ae63d3cc188 | 82 | if (error != BLE_ERROR_NONE) { |
Alexgerni | 0:8ae63d3cc188 | 83 | /* In case of error, forward the error handling to onBleInitError */ |
Alexgerni | 0:8ae63d3cc188 | 84 | onBleInitError(ble, error); |
Alexgerni | 0:8ae63d3cc188 | 85 | return; |
Alexgerni | 0:8ae63d3cc188 | 86 | } |
Alexgerni | 0:8ae63d3cc188 | 87 | |
Alexgerni | 0:8ae63d3cc188 | 88 | /* Ensure that it is the default instance of BLE */ |
Alexgerni | 0:8ae63d3cc188 | 89 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
Alexgerni | 0:8ae63d3cc188 | 90 | return; |
Alexgerni | 0:8ae63d3cc188 | 91 | } |
Alexgerni | 0:8ae63d3cc188 | 92 | |
Alexgerni | 0:8ae63d3cc188 | 93 | ble.gap().onDisconnection(disconnectionCallback); |
Alexgerni | 0:8ae63d3cc188 | 94 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
Alexgerni | 0:8ae63d3cc188 | 95 | |
Alexgerni | 0:8ae63d3cc188 | 96 | bool initialValueForLEDCharacteristic = false; |
Alexgerni | 0:8ae63d3cc188 | 97 | ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic); |
Alexgerni | 0:8ae63d3cc188 | 98 | |
Alexgerni | 0:8ae63d3cc188 | 99 | /* setup advertising */ |
Alexgerni | 0:8ae63d3cc188 | 100 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Alexgerni | 0:8ae63d3cc188 | 101 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Alexgerni | 0:8ae63d3cc188 | 102 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Alexgerni | 0:8ae63d3cc188 | 103 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Alexgerni | 0:8ae63d3cc188 | 104 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
Alexgerni | 0:8ae63d3cc188 | 105 | ble.gap().startAdvertising(); |
Alexgerni | 0:8ae63d3cc188 | 106 | } |
Alexgerni | 0:8ae63d3cc188 | 107 | |
Alexgerni | 0:8ae63d3cc188 | 108 | |
Alexgerni | 0:8ae63d3cc188 | 109 | int main(void) |
Alexgerni | 0:8ae63d3cc188 | 110 | { |
Alexgerni | 0:8ae63d3cc188 | 111 | pc.baud(115200); |
Alexgerni | 0:8ae63d3cc188 | 112 | pc.printf("Initialization starts... \n"); |
Alexgerni | 0:8ae63d3cc188 | 113 | ticker.attach(periodicCallback, ts); /* Blink LED every second */ |
Alexgerni | 1:222379aed289 | 114 | actuatedLED = 1; |
Alexgerni | 0:8ae63d3cc188 | 115 | |
Alexgerni | 0:8ae63d3cc188 | 116 | BLE &ble = BLE::Instance(); |
Alexgerni | 0:8ae63d3cc188 | 117 | ble.init(bleInitComplete); |
Alexgerni | 0:8ae63d3cc188 | 118 | |
Alexgerni | 0:8ae63d3cc188 | 119 | /* SpinWait for initialization to complete. This is necessary because the |
Alexgerni | 0:8ae63d3cc188 | 120 | * BLE object is used in the main loop below. */ |
Alexgerni | 1:222379aed289 | 121 | //while (ble.hasInitialized() == false) { /* spin loop */ } |
Alexgerni | 0:8ae63d3cc188 | 122 | pc.printf("Initialization finished \n"); |
Alexgerni | 0:8ae63d3cc188 | 123 | |
Alexgerni | 0:8ae63d3cc188 | 124 | while (true) { |
Alexgerni | 0:8ae63d3cc188 | 125 | ble.waitForEvent(); |
Alexgerni | 0:8ae63d3cc188 | 126 | } |
Alexgerni | 0:8ae63d3cc188 | 127 | } |