Jan Jongboom
/
ble-devicelink-example
Example program for mbed Cloud Bluetooth Devicelink
source/main.cpp@0:cfbdc6a35374, 2017-05-01 (annotated)
- Committer:
- Jan Jongboom
- Date:
- Mon May 01 14:04:51 2017 +0200
- Revision:
- 0:cfbdc6a35374
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Jongboom |
0:cfbdc6a35374 | 1 | /* mbed Microcontroller Library |
Jan Jongboom |
0:cfbdc6a35374 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Jan Jongboom |
0:cfbdc6a35374 | 3 | * |
Jan Jongboom |
0:cfbdc6a35374 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Jan Jongboom |
0:cfbdc6a35374 | 5 | * you may not use this file except in compliance with the License. |
Jan Jongboom |
0:cfbdc6a35374 | 6 | * You may obtain a copy of the License at |
Jan Jongboom |
0:cfbdc6a35374 | 7 | * |
Jan Jongboom |
0:cfbdc6a35374 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jan Jongboom |
0:cfbdc6a35374 | 9 | * |
Jan Jongboom |
0:cfbdc6a35374 | 10 | * Unless required by applicable law or agreed to in writing, software |
Jan Jongboom |
0:cfbdc6a35374 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Jan Jongboom |
0:cfbdc6a35374 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Jan Jongboom |
0:cfbdc6a35374 | 13 | * See the License for the specific language governing permissions and |
Jan Jongboom |
0:cfbdc6a35374 | 14 | * limitations under the License. |
Jan Jongboom |
0:cfbdc6a35374 | 15 | */ |
Jan Jongboom |
0:cfbdc6a35374 | 16 | #include <events/mbed_events.h> |
Jan Jongboom |
0:cfbdc6a35374 | 17 | |
Jan Jongboom |
0:cfbdc6a35374 | 18 | #include <mbed.h> |
Jan Jongboom |
0:cfbdc6a35374 | 19 | #include "ble/BLE.h" |
Jan Jongboom |
0:cfbdc6a35374 | 20 | #include "ble/Gap.h" |
Jan Jongboom |
0:cfbdc6a35374 | 21 | #include "ButtonService.h" |
Jan Jongboom |
0:cfbdc6a35374 | 22 | #include "GroveLedService.h" |
Jan Jongboom |
0:cfbdc6a35374 | 23 | #include "ChainableLED.h" |
Jan Jongboom |
0:cfbdc6a35374 | 24 | |
Jan Jongboom |
0:cfbdc6a35374 | 25 | DigitalOut led1(LED1, 1); |
Jan Jongboom |
0:cfbdc6a35374 | 26 | InterruptIn button(BLE_BUTTON_PIN_NAME); |
Jan Jongboom |
0:cfbdc6a35374 | 27 | ChainableLED grove_led(D2, D3, 1); |
Jan Jongboom |
0:cfbdc6a35374 | 28 | |
Jan Jongboom |
0:cfbdc6a35374 | 29 | static EventQueue eventQueue( |
Jan Jongboom |
0:cfbdc6a35374 | 30 | /* event count */ 10 * /* event size */ 32 |
Jan Jongboom |
0:cfbdc6a35374 | 31 | ); |
Jan Jongboom |
0:cfbdc6a35374 | 32 | |
Jan Jongboom |
0:cfbdc6a35374 | 33 | const static char DEVICE_NAME[] = "DeviceLinkTest"; |
Jan Jongboom |
0:cfbdc6a35374 | 34 | static const uint16_t uuid16_list[] = {ButtonService::BUTTON_SERVICE_UUID, GroveLEDService::LED_SERVICE_UUID}; |
Jan Jongboom |
0:cfbdc6a35374 | 35 | |
Jan Jongboom |
0:cfbdc6a35374 | 36 | ButtonService *buttonServicePtr; |
Jan Jongboom |
0:cfbdc6a35374 | 37 | GroveLEDService *ledServicePtr; |
Jan Jongboom |
0:cfbdc6a35374 | 38 | |
Jan Jongboom |
0:cfbdc6a35374 | 39 | static uint8_t button_count = 0; |
Jan Jongboom |
0:cfbdc6a35374 | 40 | |
Jan Jongboom |
0:cfbdc6a35374 | 41 | void buttonPressedCallback(void) |
Jan Jongboom |
0:cfbdc6a35374 | 42 | { |
Jan Jongboom |
0:cfbdc6a35374 | 43 | eventQueue.call(Callback<void(uint8_t)>(buttonServicePtr, &ButtonService::updateButtonState), ++button_count); |
Jan Jongboom |
0:cfbdc6a35374 | 44 | } |
Jan Jongboom |
0:cfbdc6a35374 | 45 | |
Jan Jongboom |
0:cfbdc6a35374 | 46 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Jan Jongboom |
0:cfbdc6a35374 | 47 | { |
Jan Jongboom |
0:cfbdc6a35374 | 48 | BLE::Instance().gap().startAdvertising(); // restart advertising |
Jan Jongboom |
0:cfbdc6a35374 | 49 | } |
Jan Jongboom |
0:cfbdc6a35374 | 50 | |
Jan Jongboom |
0:cfbdc6a35374 | 51 | void blinkCallback(void) |
Jan Jongboom |
0:cfbdc6a35374 | 52 | { |
Jan Jongboom |
0:cfbdc6a35374 | 53 | led1 = !led1; /* Do blinky on LED1 to indicate system aliveness. */ |
Jan Jongboom |
0:cfbdc6a35374 | 54 | } |
Jan Jongboom |
0:cfbdc6a35374 | 55 | |
Jan Jongboom |
0:cfbdc6a35374 | 56 | void onBleInitError(BLE &ble, ble_error_t error) |
Jan Jongboom |
0:cfbdc6a35374 | 57 | { |
Jan Jongboom |
0:cfbdc6a35374 | 58 | /* Initialization error handling should go here */ |
Jan Jongboom |
0:cfbdc6a35374 | 59 | } |
Jan Jongboom |
0:cfbdc6a35374 | 60 | |
Jan Jongboom |
0:cfbdc6a35374 | 61 | /** |
Jan Jongboom |
0:cfbdc6a35374 | 62 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
Jan Jongboom |
0:cfbdc6a35374 | 63 | * |
Jan Jongboom |
0:cfbdc6a35374 | 64 | * @param[in] params |
Jan Jongboom |
0:cfbdc6a35374 | 65 | * Information about the characteristic being updated. |
Jan Jongboom |
0:cfbdc6a35374 | 66 | */ |
Jan Jongboom |
0:cfbdc6a35374 | 67 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
Jan Jongboom |
0:cfbdc6a35374 | 68 | if ((params->handle == ledServicePtr->getValueHandle()) && (params->len >= 3)) { |
Jan Jongboom |
0:cfbdc6a35374 | 69 | grove_led.setColorRGB(0, params->data[0], params->data[1], params->data[2]); |
Jan Jongboom |
0:cfbdc6a35374 | 70 | } |
Jan Jongboom |
0:cfbdc6a35374 | 71 | } |
Jan Jongboom |
0:cfbdc6a35374 | 72 | |
Jan Jongboom |
0:cfbdc6a35374 | 73 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
Jan Jongboom |
0:cfbdc6a35374 | 74 | { |
Jan Jongboom |
0:cfbdc6a35374 | 75 | BLE& ble = params->ble; |
Jan Jongboom |
0:cfbdc6a35374 | 76 | ble_error_t error = params->error; |
Jan Jongboom |
0:cfbdc6a35374 | 77 | |
Jan Jongboom |
0:cfbdc6a35374 | 78 | if (error != BLE_ERROR_NONE) { |
Jan Jongboom |
0:cfbdc6a35374 | 79 | /* In case of error, forward the error handling to onBleInitError */ |
Jan Jongboom |
0:cfbdc6a35374 | 80 | onBleInitError(ble, error); |
Jan Jongboom |
0:cfbdc6a35374 | 81 | return; |
Jan Jongboom |
0:cfbdc6a35374 | 82 | } |
Jan Jongboom |
0:cfbdc6a35374 | 83 | |
Jan Jongboom |
0:cfbdc6a35374 | 84 | /* Ensure that it is the default instance of BLE */ |
Jan Jongboom |
0:cfbdc6a35374 | 85 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
Jan Jongboom |
0:cfbdc6a35374 | 86 | return; |
Jan Jongboom |
0:cfbdc6a35374 | 87 | } |
Jan Jongboom |
0:cfbdc6a35374 | 88 | |
Jan Jongboom |
0:cfbdc6a35374 | 89 | ble.gap().onDisconnection(disconnectionCallback); |
Jan Jongboom |
0:cfbdc6a35374 | 90 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
Jan Jongboom |
0:cfbdc6a35374 | 91 | |
Jan Jongboom |
0:cfbdc6a35374 | 92 | button.fall(buttonPressedCallback); |
Jan Jongboom |
0:cfbdc6a35374 | 93 | |
Jan Jongboom |
0:cfbdc6a35374 | 94 | buttonServicePtr = new ButtonService(ble, 0); |
Jan Jongboom |
0:cfbdc6a35374 | 95 | ledServicePtr = new GroveLEDService(ble, 0xff0000); |
Jan Jongboom |
0:cfbdc6a35374 | 96 | |
Jan Jongboom |
0:cfbdc6a35374 | 97 | /* setup advertising */ |
Jan Jongboom |
0:cfbdc6a35374 | 98 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Jan Jongboom |
0:cfbdc6a35374 | 99 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
Jan Jongboom |
0:cfbdc6a35374 | 100 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Jan Jongboom |
0:cfbdc6a35374 | 101 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Jan Jongboom |
0:cfbdc6a35374 | 102 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
Jan Jongboom |
0:cfbdc6a35374 | 103 | ble.gap().startAdvertising(); |
Jan Jongboom |
0:cfbdc6a35374 | 104 | } |
Jan Jongboom |
0:cfbdc6a35374 | 105 | |
Jan Jongboom |
0:cfbdc6a35374 | 106 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
Jan Jongboom |
0:cfbdc6a35374 | 107 | BLE &ble = BLE::Instance(); |
Jan Jongboom |
0:cfbdc6a35374 | 108 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
Jan Jongboom |
0:cfbdc6a35374 | 109 | } |
Jan Jongboom |
0:cfbdc6a35374 | 110 | |
Jan Jongboom |
0:cfbdc6a35374 | 111 | int main() |
Jan Jongboom |
0:cfbdc6a35374 | 112 | { |
Jan Jongboom |
0:cfbdc6a35374 | 113 | grove_led.setColorRGB(0, 0xff, 0, 0); // red |
Jan Jongboom |
0:cfbdc6a35374 | 114 | |
Jan Jongboom |
0:cfbdc6a35374 | 115 | eventQueue.call_every(500, blinkCallback); |
Jan Jongboom |
0:cfbdc6a35374 | 116 | |
Jan Jongboom |
0:cfbdc6a35374 | 117 | BLE &ble = BLE::Instance(); |
Jan Jongboom |
0:cfbdc6a35374 | 118 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
Jan Jongboom |
0:cfbdc6a35374 | 119 | ble.init(bleInitComplete); |
Jan Jongboom |
0:cfbdc6a35374 | 120 | |
Jan Jongboom |
0:cfbdc6a35374 | 121 | eventQueue.dispatch_forever(); |
Jan Jongboom |
0:cfbdc6a35374 | 122 | |
Jan Jongboom |
0:cfbdc6a35374 | 123 | return 0; |
Jan Jongboom |
0:cfbdc6a35374 | 124 | } |