We are making a bluetooth application for a vehicle.
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@27:49f379daad68, 2017-06-27 (annotated)
- Committer:
- benheck
- Date:
- Tue Jun 27 19:44:12 2017 +0000
- Revision:
- 27:49f379daad68
- Parent:
- 26:3a1d82a26a83
- Child:
- 28:a56710056f4d
Testing...;
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 | |
bobbaddeley | 26:3a1d82a26a83 | 22 | int alarm = 0; |
bobbaddeley | 26:3a1d82a26a83 | 23 | |
bobbaddeley | 26:3a1d82a26a83 | 24 | DigitalOut buzzer(PC_8); |
apalmieri | 13:227a0149b677 | 25 | DigitalOut led1(LED1, 1); |
screamer | 0:eb7f02ad28a7 | 26 | |
bobbaddeley | 23:a31b178e2263 | 27 | const static char DEVICE_NAME[] = "HeckBike"; |
bobbaddeley | 24:d352571b1c1f | 28 | static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE, |
bobbaddeley | 23:a31b178e2263 | 29 | 0x8EC5};//made up UUID for the Heck bike |
apalmieri | 13:227a0149b677 | 30 | |
bobbaddeley | 26:3a1d82a26a83 | 31 | #define on 0xFFFF |
bobbaddeley | 26:3a1d82a26a83 | 32 | #define off 0 |
bobbaddeley | 26:3a1d82a26a83 | 33 | |
bobbaddeley | 26:3a1d82a26a83 | 34 | #define buttonMask 0x006F |
bobbaddeley | 26:3a1d82a26a83 | 35 | |
bobbaddeley | 26:3a1d82a26a83 | 36 | PortIn sixButtons(PortC, buttonMask); |
bobbaddeley | 26:3a1d82a26a83 | 37 | |
bobbaddeley | 26:3a1d82a26a83 | 38 | unsigned char byteOut = 0; |
bobbaddeley | 26:3a1d82a26a83 | 39 | |
bobbaddeley | 26:3a1d82a26a83 | 40 | unsigned char byteIn = 0x08; |
bobbaddeley | 26:3a1d82a26a83 | 41 | |
bobbaddeley | 23:a31b178e2263 | 42 | uint8_t inputs = 0x00; |
bobbaddeley | 23:a31b178e2263 | 43 | uint8_t outputs = 0x00; |
bobbaddeley | 26:3a1d82a26a83 | 44 | uint8_t old_outputs = 0x00; |
bobbaddeley | 23:a31b178e2263 | 45 | uint32_t timestamp = 0x00; |
bobbaddeley | 23:a31b178e2263 | 46 | |
screamer | 0:eb7f02ad28a7 | 47 | static volatile bool triggerSensorPolling = false; |
screamer | 0:eb7f02ad28a7 | 48 | |
apalmieri | 2:bc0c0d442a24 | 49 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
screamer | 0:eb7f02ad28a7 | 50 | { |
apalmieri | 13:227a0149b677 | 51 | (void)params; |
apalmieri | 13:227a0149b677 | 52 | BLE::Instance().gap().startAdvertising(); // restart advertising |
screamer | 0:eb7f02ad28a7 | 53 | } |
screamer | 0:eb7f02ad28a7 | 54 | |
screamer | 0:eb7f02ad28a7 | 55 | void periodicCallback(void) |
screamer | 0:eb7f02ad28a7 | 56 | { |
screamer | 0:eb7f02ad28a7 | 57 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
screamer | 0:eb7f02ad28a7 | 58 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
screamer | 0:eb7f02ad28a7 | 59 | * heavy-weight sensor polling from the main thread. */ |
screamer | 0:eb7f02ad28a7 | 60 | triggerSensorPolling = true; |
screamer | 0:eb7f02ad28a7 | 61 | } |
screamer | 0:eb7f02ad28a7 | 62 | |
apalmieri | 13:227a0149b677 | 63 | void onBleInitError(BLE &ble, ble_error_t error) |
apalmieri | 13:227a0149b677 | 64 | { |
apalmieri | 13:227a0149b677 | 65 | (void)ble; |
apalmieri | 13:227a0149b677 | 66 | (void)error; |
apalmieri | 13:227a0149b677 | 67 | /* Initialization error handling should go here */ |
apalmieri | 13:227a0149b677 | 68 | } |
apalmieri | 13:227a0149b677 | 69 | |
Technicus | 22:985657fc70c8 | 70 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
bobbaddeley | 24:d352571b1c1f | 71 | //if (params->handle == BikeService.outputs.getValueAttribute().getHandle()) { |
bobbaddeley | 24:d352571b1c1f | 72 | //BOB Should update the outputs. |
bobbaddeley | 24:d352571b1c1f | 73 | outputs = *(params->data); |
bobbaddeley | 24:d352571b1c1f | 74 | //} |
Technicus | 22:985657fc70c8 | 75 | } |
Technicus | 22:985657fc70c8 | 76 | |
bobbaddeley | 26:3a1d82a26a83 | 77 | |
apalmieri | 13:227a0149b677 | 78 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
screamer | 0:eb7f02ad28a7 | 79 | { |
apalmieri | 13:227a0149b677 | 80 | BLE& ble = params->ble; |
apalmieri | 13:227a0149b677 | 81 | ble_error_t error = params->error; |
screamer | 0:eb7f02ad28a7 | 82 | |
apalmieri | 13:227a0149b677 | 83 | if (error != BLE_ERROR_NONE) { |
apalmieri | 13:227a0149b677 | 84 | onBleInitError(ble, error); |
apalmieri | 13:227a0149b677 | 85 | return; |
apalmieri | 13:227a0149b677 | 86 | } |
apalmieri | 13:227a0149b677 | 87 | |
apalmieri | 13:227a0149b677 | 88 | if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
apalmieri | 13:227a0149b677 | 89 | return; |
apalmieri | 13:227a0149b677 | 90 | } |
apalmieri | 13:227a0149b677 | 91 | |
screamer | 0:eb7f02ad28a7 | 92 | ble.gap().onDisconnection(disconnectionCallback); |
screamer | 0:eb7f02ad28a7 | 93 | |
Technicus | 22:985657fc70c8 | 94 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
screamer | 0:eb7f02ad28a7 | 95 | /* Setup primary service. */ |
bobbaddeley | 24:d352571b1c1f | 96 | BikeService bikeService(ble); |
bobbaddeley | 23:a31b178e2263 | 97 | /* Setup battery service. */ |
bobbaddeley | 23:a31b178e2263 | 98 | BatteryService btService(ble,100); |
screamer | 0:eb7f02ad28a7 | 99 | /* Setup advertising. */ |
screamer | 0:eb7f02ad28a7 | 100 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
screamer | 0:eb7f02ad28a7 | 101 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
bobbaddeley | 23:a31b178e2263 | 102 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_CYCLING); |
screamer | 0:eb7f02ad28a7 | 103 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
screamer | 0:eb7f02ad28a7 | 104 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
screamer | 0:eb7f02ad28a7 | 105 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
screamer | 0:eb7f02ad28a7 | 106 | ble.gap().startAdvertising(); |
screamer | 0:eb7f02ad28a7 | 107 | |
screamer | 0:eb7f02ad28a7 | 108 | // infinite loop |
apalmieri | 13:227a0149b677 | 109 | while (true) { |
screamer | 0:eb7f02ad28a7 | 110 | // check for trigger from periodicCallback() |
screamer | 0:eb7f02ad28a7 | 111 | if (triggerSensorPolling && ble.getGapState().connected) { |
bobbaddeley | 26:3a1d82a26a83 | 112 | if (outputs & 0x80){ |
bobbaddeley | 26:3a1d82a26a83 | 113 | if (alarm == 0) { |
bobbaddeley | 26:3a1d82a26a83 | 114 | alarm = 1; |
bobbaddeley | 26:3a1d82a26a83 | 115 | } |
bobbaddeley | 26:3a1d82a26a83 | 116 | |
bobbaddeley | 26:3a1d82a26a83 | 117 | } |
bobbaddeley | 26:3a1d82a26a83 | 118 | else { |
bobbaddeley | 26:3a1d82a26a83 | 119 | alarm = 0; |
bobbaddeley | 26:3a1d82a26a83 | 120 | buzzer = 0; |
bobbaddeley | 26:3a1d82a26a83 | 121 | } |
bobbaddeley | 26:3a1d82a26a83 | 122 | |
screamer | 0:eb7f02ad28a7 | 123 | triggerSensorPolling = false; |
bobbaddeley | 26:3a1d82a26a83 | 124 | |
bobbaddeley | 26:3a1d82a26a83 | 125 | byteOut = sixButtons & buttonMask; |
screamer | 0:eb7f02ad28a7 | 126 | |
bobbaddeley | 24:d352571b1c1f | 127 | // update the ble with the inputs |
bobbaddeley | 26:3a1d82a26a83 | 128 | bikeService.updateInputs(byteOut); |
bobbaddeley | 25:cb54da929d9a | 129 | bikeService.updateTimestamp(); |
screamer | 0:eb7f02ad28a7 | 130 | } else { |
screamer | 0:eb7f02ad28a7 | 131 | ble.waitForEvent(); // low power wait for event |
screamer | 0:eb7f02ad28a7 | 132 | } |
bobbaddeley | 26:3a1d82a26a83 | 133 | |
benheck | 27:49f379daad68 | 134 | if (alarm) { //Flag set to sound the alarm? |
bobbaddeley | 26:3a1d82a26a83 | 135 | |
benheck | 27:49f379daad68 | 136 | if (alarm++ < 500) { //Beep it on and off |
bobbaddeley | 26:3a1d82a26a83 | 137 | buzzer = 1; |
bobbaddeley | 26:3a1d82a26a83 | 138 | } |
bobbaddeley | 26:3a1d82a26a83 | 139 | else { |
bobbaddeley | 26:3a1d82a26a83 | 140 | buzzer = 0; |
bobbaddeley | 26:3a1d82a26a83 | 141 | } |
bobbaddeley | 26:3a1d82a26a83 | 142 | |
bobbaddeley | 26:3a1d82a26a83 | 143 | if (alarm > 1000) { |
bobbaddeley | 26:3a1d82a26a83 | 144 | alarm = 1; |
bobbaddeley | 26:3a1d82a26a83 | 145 | } |
bobbaddeley | 26:3a1d82a26a83 | 146 | |
bobbaddeley | 26:3a1d82a26a83 | 147 | } |
bobbaddeley | 26:3a1d82a26a83 | 148 | |
bobbaddeley | 26:3a1d82a26a83 | 149 | |
screamer | 0:eb7f02ad28a7 | 150 | } |
screamer | 0:eb7f02ad28a7 | 151 | } |
apalmieri | 13:227a0149b677 | 152 | |
apalmieri | 13:227a0149b677 | 153 | int main(void) |
apalmieri | 13:227a0149b677 | 154 | { |
apalmieri | 13:227a0149b677 | 155 | Ticker ticker; |
apalmieri | 13:227a0149b677 | 156 | ticker.attach(periodicCallback, 1); // blink LED every second |
apalmieri | 13:227a0149b677 | 157 | |
apalmieri | 13:227a0149b677 | 158 | BLE::Instance().init(bleInitComplete); |
apalmieri | 13:227a0149b677 | 159 | } |
apalmieri | 14:f715c13eb84f | 160 |