We are making a bluetooth application for a vehicle.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@24:d352571b1c1f, 2017-06-27 (annotated)
- Committer:
- bobbaddeley
- Date:
- Tue Jun 27 17:29:28 2017 +0000
- Revision:
- 24:d352571b1c1f
- Parent:
- 23:a31b178e2263
- Child:
- 25:cb54da929d9a
Much of the BLE code required.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:eb7f02ad28a7 | 1 | /* mbed Microcontroller Library |
screamer | 0:eb7f02ad28a7 | 2 | * Copyright (c) 2006-2015 ARM Limited |
screamer | 0:eb7f02ad28a7 | 3 | * |
screamer | 0:eb7f02ad28a7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
screamer | 0:eb7f02ad28a7 | 5 | * you may not use this file except in compliance with the License. |
screamer | 0:eb7f02ad28a7 | 6 | * You may obtain a copy of the License at |
screamer | 0:eb7f02ad28a7 | 7 | * |
screamer | 0:eb7f02ad28a7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
screamer | 0:eb7f02ad28a7 | 9 | * |
screamer | 0:eb7f02ad28a7 | 10 | * Unless required by applicable law or agreed to in writing, software |
screamer | 0:eb7f02ad28a7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
screamer | 0:eb7f02ad28a7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
screamer | 0:eb7f02ad28a7 | 13 | * See the License for the specific language governing permissions and |
screamer | 0:eb7f02ad28a7 | 14 | * limitations under the License. |
screamer | 0:eb7f02ad28a7 | 15 | */ |
screamer | 0:eb7f02ad28a7 | 16 | |
screamer | 0:eb7f02ad28a7 | 17 | #include "mbed.h" |
screamer | 0:eb7f02ad28a7 | 18 | #include "ble/BLE.h" |
bobbaddeley | 23:a31b178e2263 | 19 | #include "bike_service.h" |
bobbaddeley | 23:a31b178e2263 | 20 | #include "ble/services/BatteryService.h" |
screamer | 0:eb7f02ad28a7 | 21 | |
apalmieri | 13:227a0149b677 | 22 | DigitalOut led1(LED1, 1); |
screamer | 0:eb7f02ad28a7 | 23 | |
bobbaddeley | 23:a31b178e2263 | 24 | const static char DEVICE_NAME[] = "HeckBike"; |
bobbaddeley | 24:d352571b1c1f | 25 | static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE, |
bobbaddeley | 23:a31b178e2263 | 26 | 0x8EC5};//made up UUID for the Heck bike |
apalmieri | 13:227a0149b677 | 27 | |
bobbaddeley | 23:a31b178e2263 | 28 | uint8_t inputs = 0x00; |
bobbaddeley | 23:a31b178e2263 | 29 | uint8_t outputs = 0x00; |
bobbaddeley | 23:a31b178e2263 | 30 | uint32_t timestamp = 0x00; |
bobbaddeley | 23:a31b178e2263 | 31 | |
screamer | 0:eb7f02ad28a7 | 32 | static volatile bool triggerSensorPolling = false; |
screamer | 0:eb7f02ad28a7 | 33 | |
apalmieri | 2:bc0c0d442a24 | 34 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
screamer | 0:eb7f02ad28a7 | 35 | { |
apalmieri | 13:227a0149b677 | 36 | (void)params; |
apalmieri | 13:227a0149b677 | 37 | BLE::Instance().gap().startAdvertising(); // restart advertising |
screamer | 0:eb7f02ad28a7 | 38 | } |
screamer | 0:eb7f02ad28a7 | 39 | |
screamer | 0:eb7f02ad28a7 | 40 | void periodicCallback(void) |
screamer | 0:eb7f02ad28a7 | 41 | { |
screamer | 0:eb7f02ad28a7 | 42 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
screamer | 0:eb7f02ad28a7 | 43 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
screamer | 0:eb7f02ad28a7 | 44 | * heavy-weight sensor polling from the main thread. */ |
screamer | 0:eb7f02ad28a7 | 45 | triggerSensorPolling = true; |
screamer | 0:eb7f02ad28a7 | 46 | } |
screamer | 0:eb7f02ad28a7 | 47 | |
apalmieri | 13:227a0149b677 | 48 | void onBleInitError(BLE &ble, ble_error_t error) |
apalmieri | 13:227a0149b677 | 49 | { |
apalmieri | 13:227a0149b677 | 50 | (void)ble; |
apalmieri | 13:227a0149b677 | 51 | (void)error; |
apalmieri | 13:227a0149b677 | 52 | /* Initialization error handling should go here */ |
apalmieri | 13:227a0149b677 | 53 | } |
apalmieri | 13:227a0149b677 | 54 | |
Technicus | 22:985657fc70c8 | 55 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
bobbaddeley | 24:d352571b1c1f | 56 | //if (params->handle == BikeService.outputs.getValueAttribute().getHandle()) { |
bobbaddeley | 24:d352571b1c1f | 57 | //BOB Should update the outputs. |
bobbaddeley | 24:d352571b1c1f | 58 | outputs = *(params->data); |
bobbaddeley | 24:d352571b1c1f | 59 | //} |
Technicus | 22:985657fc70c8 | 60 | } |
Technicus | 22:985657fc70c8 | 61 | |
apalmieri | 13:227a0149b677 | 62 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
screamer | 0:eb7f02ad28a7 | 63 | { |
apalmieri | 13:227a0149b677 | 64 | BLE& ble = params->ble; |
apalmieri | 13:227a0149b677 | 65 | ble_error_t error = params->error; |
screamer | 0:eb7f02ad28a7 | 66 | |
apalmieri | 13:227a0149b677 | 67 | if (error != BLE_ERROR_NONE) { |
apalmieri | 13:227a0149b677 | 68 | onBleInitError(ble, error); |
apalmieri | 13:227a0149b677 | 69 | return; |
apalmieri | 13:227a0149b677 | 70 | } |
apalmieri | 13:227a0149b677 | 71 | |
apalmieri | 13:227a0149b677 | 72 | if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
apalmieri | 13:227a0149b677 | 73 | return; |
apalmieri | 13:227a0149b677 | 74 | } |
apalmieri | 13:227a0149b677 | 75 | |
screamer | 0:eb7f02ad28a7 | 76 | ble.gap().onDisconnection(disconnectionCallback); |
screamer | 0:eb7f02ad28a7 | 77 | |
Technicus | 22:985657fc70c8 | 78 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
screamer | 0:eb7f02ad28a7 | 79 | /* Setup primary service. */ |
bobbaddeley | 24:d352571b1c1f | 80 | BikeService bikeService(ble); |
bobbaddeley | 23:a31b178e2263 | 81 | /* Setup battery service. */ |
bobbaddeley | 23:a31b178e2263 | 82 | BatteryService btService(ble,100); |
screamer | 0:eb7f02ad28a7 | 83 | /* Setup advertising. */ |
screamer | 0:eb7f02ad28a7 | 84 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
screamer | 0:eb7f02ad28a7 | 85 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
bobbaddeley | 23:a31b178e2263 | 86 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_CYCLING); |
screamer | 0:eb7f02ad28a7 | 87 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
screamer | 0:eb7f02ad28a7 | 88 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
screamer | 0:eb7f02ad28a7 | 89 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
screamer | 0:eb7f02ad28a7 | 90 | ble.gap().startAdvertising(); |
screamer | 0:eb7f02ad28a7 | 91 | |
screamer | 0:eb7f02ad28a7 | 92 | // infinite loop |
apalmieri | 13:227a0149b677 | 93 | while (true) { |
screamer | 0:eb7f02ad28a7 | 94 | // check for trigger from periodicCallback() |
screamer | 0:eb7f02ad28a7 | 95 | if (triggerSensorPolling && ble.getGapState().connected) { |
screamer | 0:eb7f02ad28a7 | 96 | triggerSensorPolling = false; |
screamer | 0:eb7f02ad28a7 | 97 | |
bobbaddeley | 24:d352571b1c1f | 98 | // let's update our fake inputs. |
bobbaddeley | 24:d352571b1c1f | 99 | inputs++; |
bobbaddeley | 24:d352571b1c1f | 100 | if (inputs == 100) { |
bobbaddeley | 24:d352571b1c1f | 101 | inputs = 0; |
screamer | 0:eb7f02ad28a7 | 102 | } |
screamer | 0:eb7f02ad28a7 | 103 | |
bobbaddeley | 24:d352571b1c1f | 104 | // update the ble with the inputs |
bobbaddeley | 24:d352571b1c1f | 105 | bikeService.updateInputs(inputs); |
screamer | 0:eb7f02ad28a7 | 106 | } else { |
screamer | 0:eb7f02ad28a7 | 107 | ble.waitForEvent(); // low power wait for event |
screamer | 0:eb7f02ad28a7 | 108 | } |
screamer | 0:eb7f02ad28a7 | 109 | } |
screamer | 0:eb7f02ad28a7 | 110 | } |
apalmieri | 13:227a0149b677 | 111 | |
apalmieri | 13:227a0149b677 | 112 | int main(void) |
apalmieri | 13:227a0149b677 | 113 | { |
apalmieri | 13:227a0149b677 | 114 | Ticker ticker; |
apalmieri | 13:227a0149b677 | 115 | ticker.attach(periodicCallback, 1); // blink LED every second |
apalmieri | 13:227a0149b677 | 116 | |
apalmieri | 13:227a0149b677 | 117 | BLE::Instance().init(bleInitComplete); |
apalmieri | 13:227a0149b677 | 118 | } |
apalmieri | 14:f715c13eb84f | 119 |