Tests for BLE Uart Service
Fork of mbed-os-example-ble-HeartRate by
source/main.cpp@25:1bd8d5cc86ef, 2017-05-05 (annotated)
- Committer:
- Mathias Giacomuzzi
- Date:
- Fri May 05 11:24:34 2017 +0200
- Revision:
- 25:1bd8d5cc86ef
- Parent:
- 10:ac3615194d04
"update Example to working UART Service"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 1 | /* mbed Microcontroller Library |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 2 | * Copyright (c) 2006-2015 ARM Limited |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 3 | * |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 5 | * you may not use this file except in compliance with the License. |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 6 | * You may obtain a copy of the License at |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 7 | * |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 9 | * |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 10 | * Unless required by applicable law or agreed to in writing, software |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 13 | * See the License for the specific language governing permissions and |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 14 | * limitations under the License. |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 15 | */ |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 16 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 17 | #include <events/mbed_events.h> |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 18 | #include <mbed.h> |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 19 | #include "ble/BLE.h" |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 20 | #include "ble/Gap.h" |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 21 | #include "ble/services/UARTService.h" |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 22 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 23 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 24 | * it will have an impact on code-size and power consumption. */ |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 25 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 26 | #if NEED_CONSOLE_OUTPUT |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 27 | #define DEBUG(STR) { if (uartServicePtr) uartServicePtr->write(STR, strlen(STR)); } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 28 | #else |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 29 | #define DEBUG(...) /* nothing */ |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 30 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 31 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 32 | DigitalOut led1(LED1, 1); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 33 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 34 | const static char DEVICE_NAME[] = "BLE UART"; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 35 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 36 | static uint8_t hrmCounter = 100; // init HRM to 100bps |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 37 | static UARTService *uartServicePtr; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 38 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 39 | static EventQueue eventQueue( |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 40 | /* event count */ 16 * /* event size */ 32 |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 41 | ); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 42 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 43 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 44 | { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 45 | BLE::Instance().gap().startAdvertising(); // restart advertising |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 46 | } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 47 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 48 | void updateSensorValue() |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 49 | { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 50 | char buffer[5]; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 51 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 52 | // Do blocking calls or whatever is necessary for sensor polling. |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 53 | // In our case, we simply update the HRM measurement. |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 54 | hrmCounter++; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 55 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 56 | // 100 <= HRM bps <=175 |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 57 | if (hrmCounter == 175) { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 58 | hrmCounter = 100; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 59 | } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 60 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 61 | sniprintf(buffer, 5, "%d\n\r", hrmCounter); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 62 | DEBUG(buffer); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 63 | printf("update hrmCounter: %s\n\r", buffer); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 64 | } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 65 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 66 | void periodicCallback(void) |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 67 | { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 68 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 69 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 70 | if (BLE::Instance().getGapState().connected) { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 71 | eventQueue.call(updateSensorValue); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 72 | } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 73 | } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 74 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 75 | void onBleInitError(BLE &ble, ble_error_t error) |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 76 | { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 77 | (void)ble; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 78 | (void)error; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 79 | /* Initialization error handling should go here */ |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 80 | } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 81 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 82 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 83 | { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 84 | BLE& ble = params->ble; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 85 | ble_error_t error = params->error; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 86 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 87 | if (error != BLE_ERROR_NONE) { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 88 | onBleInitError(ble, error); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 89 | return; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 90 | } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 91 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 92 | if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 93 | return; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 94 | } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 95 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 96 | ble.gap().onDisconnection(disconnectionCallback); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 97 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 98 | /* Setup primary service. */ |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 99 | uartServicePtr = new UARTService(ble); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 100 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 101 | /* Setup advertising. */ |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 102 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 103 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 104 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 105 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 106 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed)); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 107 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 108 | ble.gap().setAdvertisingInterval(100); /* 100ms */ |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 109 | ble.gap().startAdvertising(); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 110 | } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 111 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 112 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 113 | BLE &ble = BLE::Instance(); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 114 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 115 | } |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 116 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 117 | int main() |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 118 | { |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 119 | eventQueue.call_every(100, periodicCallback); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 120 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 121 | BLE &ble = BLE::Instance(); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 122 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 123 | ble.init(bleInitComplete); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 124 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 125 | printf("Start up...\n\r"); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 126 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 127 | eventQueue.dispatch_forever(); |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 128 | |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 129 | return 0; |
Mathias Giacomuzzi |
25:1bd8d5cc86ef | 130 | } |