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