We are making a bluetooth application for a vehicle.

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_HeartRate_IDB0XA1 by ST

Committer:
Technicus
Date:
Mon Jul 17 17:16:11 2017 +0000
Revision:
28:a56710056f4d
Parent:
27:49f379daad68
Child:
29:e7d4922a4620
Mr. Baddeley and I accomplished a list of things to do.  We have yet to figure out an interrupt scheme and EEPROM reading writing.

Who changed what in which revision?

UserRevisionLine numberNew 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
Technicus 28:a56710056f4d 22 int alarm_activated = 0;
Technicus 28:a56710056f4d 23 int alarm_on = 0;
Technicus 28:a56710056f4d 24 int alarm_counter = 0;
bobbaddeley 26:3a1d82a26a83 25
Technicus 28:a56710056f4d 26 DigitalOut led_0(PC_6);
Technicus 28:a56710056f4d 27 DigitalOut led_1(PC_7);
bobbaddeley 26:3a1d82a26a83 28 DigitalOut buzzer(PC_8);
Technicus 28:a56710056f4d 29 DigitalOut rumblr(PC_9);;
apalmieri 13:227a0149b677 30 DigitalOut led1(LED1, 1);
screamer 0:eb7f02ad28a7 31
Technicus 28:a56710056f4d 32 const static char DEVICE_NAME[] = "HeckBike!";
bobbaddeley 24:d352571b1c1f 33 static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE,
bobbaddeley 23:a31b178e2263 34 0x8EC5};//made up UUID for the Heck bike
apalmieri 13:227a0149b677 35
bobbaddeley 26:3a1d82a26a83 36 #define on 0xFFFF
bobbaddeley 26:3a1d82a26a83 37 #define off 0
bobbaddeley 26:3a1d82a26a83 38
Technicus 28:a56710056f4d 39 #define buttonMask 0x003F
bobbaddeley 26:3a1d82a26a83 40
bobbaddeley 26:3a1d82a26a83 41 PortIn sixButtons(PortC, buttonMask);
bobbaddeley 26:3a1d82a26a83 42
bobbaddeley 26:3a1d82a26a83 43 unsigned char byteOut = 0;
Technicus 28:a56710056f4d 44 unsigned char prevByteOut = 0;
bobbaddeley 26:3a1d82a26a83 45 unsigned char byteIn = 0x08;
bobbaddeley 26:3a1d82a26a83 46
bobbaddeley 23:a31b178e2263 47 uint8_t inputs = 0x00;
bobbaddeley 23:a31b178e2263 48 uint8_t outputs = 0x00;
bobbaddeley 26:3a1d82a26a83 49 uint8_t old_outputs = 0x00;
bobbaddeley 23:a31b178e2263 50 uint32_t timestamp = 0x00;
bobbaddeley 23:a31b178e2263 51
screamer 0:eb7f02ad28a7 52 static volatile bool triggerSensorPolling = false;
screamer 0:eb7f02ad28a7 53
apalmieri 2:bc0c0d442a24 54 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
screamer 0:eb7f02ad28a7 55 {
apalmieri 13:227a0149b677 56 (void)params;
apalmieri 13:227a0149b677 57 BLE::Instance().gap().startAdvertising(); // restart advertising
screamer 0:eb7f02ad28a7 58 }
screamer 0:eb7f02ad28a7 59
screamer 0:eb7f02ad28a7 60 void periodicCallback(void)
screamer 0:eb7f02ad28a7 61 {
screamer 0:eb7f02ad28a7 62 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
screamer 0:eb7f02ad28a7 63 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
screamer 0:eb7f02ad28a7 64 * heavy-weight sensor polling from the main thread. */
screamer 0:eb7f02ad28a7 65 triggerSensorPolling = true;
screamer 0:eb7f02ad28a7 66 }
screamer 0:eb7f02ad28a7 67
apalmieri 13:227a0149b677 68 void onBleInitError(BLE &ble, ble_error_t error)
apalmieri 13:227a0149b677 69 {
apalmieri 13:227a0149b677 70 (void)ble;
apalmieri 13:227a0149b677 71 (void)error;
apalmieri 13:227a0149b677 72 /* Initialization error handling should go here */
apalmieri 13:227a0149b677 73 }
apalmieri 13:227a0149b677 74
Technicus 22:985657fc70c8 75 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
bobbaddeley 24:d352571b1c1f 76 //if (params->handle == BikeService.outputs.getValueAttribute().getHandle()) {
bobbaddeley 24:d352571b1c1f 77 //BOB Should update the outputs.
bobbaddeley 24:d352571b1c1f 78 outputs = *(params->data);
bobbaddeley 24:d352571b1c1f 79 //}
Technicus 22:985657fc70c8 80 }
Technicus 22:985657fc70c8 81
bobbaddeley 26:3a1d82a26a83 82
apalmieri 13:227a0149b677 83 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
screamer 0:eb7f02ad28a7 84 {
apalmieri 13:227a0149b677 85 BLE& ble = params->ble;
apalmieri 13:227a0149b677 86 ble_error_t error = params->error;
screamer 0:eb7f02ad28a7 87
apalmieri 13:227a0149b677 88 if (error != BLE_ERROR_NONE) {
apalmieri 13:227a0149b677 89 onBleInitError(ble, error);
apalmieri 13:227a0149b677 90 return;
apalmieri 13:227a0149b677 91 }
apalmieri 13:227a0149b677 92
apalmieri 13:227a0149b677 93 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
apalmieri 13:227a0149b677 94 return;
apalmieri 13:227a0149b677 95 }
apalmieri 13:227a0149b677 96
screamer 0:eb7f02ad28a7 97 ble.gap().onDisconnection(disconnectionCallback);
screamer 0:eb7f02ad28a7 98
Technicus 22:985657fc70c8 99 ble.gattServer().onDataWritten(onDataWrittenCallback);
screamer 0:eb7f02ad28a7 100 /* Setup primary service. */
bobbaddeley 24:d352571b1c1f 101 BikeService bikeService(ble);
bobbaddeley 23:a31b178e2263 102 /* Setup battery service. */
bobbaddeley 23:a31b178e2263 103 BatteryService btService(ble,100);
screamer 0:eb7f02ad28a7 104 /* Setup advertising. */
screamer 0:eb7f02ad28a7 105 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
screamer 0:eb7f02ad28a7 106 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
bobbaddeley 23:a31b178e2263 107 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_CYCLING);
screamer 0:eb7f02ad28a7 108 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
screamer 0:eb7f02ad28a7 109 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
screamer 0:eb7f02ad28a7 110 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
screamer 0:eb7f02ad28a7 111 ble.gap().startAdvertising();
screamer 0:eb7f02ad28a7 112
screamer 0:eb7f02ad28a7 113 // infinite loop
apalmieri 13:227a0149b677 114 while (true) {
screamer 0:eb7f02ad28a7 115 // check for trigger from periodicCallback()
screamer 0:eb7f02ad28a7 116 if (triggerSensorPolling && ble.getGapState().connected) {
Technicus 28:a56710056f4d 117 if (outputs & 0x01){
Technicus 28:a56710056f4d 118 led_0 = 1;
Technicus 28:a56710056f4d 119 }
Technicus 28:a56710056f4d 120 if ((outputs & 0x01) == 0){
Technicus 28:a56710056f4d 121 led_0 = 0;
Technicus 28:a56710056f4d 122 }
Technicus 28:a56710056f4d 123 if (outputs & 0x02){
Technicus 28:a56710056f4d 124 led_1 = 1;
Technicus 28:a56710056f4d 125 }
Technicus 28:a56710056f4d 126 if ((outputs & 0x02) == 0){
Technicus 28:a56710056f4d 127 led_1 = 0;
Technicus 28:a56710056f4d 128 }
Technicus 28:a56710056f4d 129 if (outputs & 0x04){
Technicus 28:a56710056f4d 130 buzzer = 1;
Technicus 28:a56710056f4d 131 }
Technicus 28:a56710056f4d 132 if ((outputs & 0x04) == 0){
Technicus 28:a56710056f4d 133 buzzer = 0;
bobbaddeley 26:3a1d82a26a83 134 }
Technicus 28:a56710056f4d 135 if (outputs & 0x08){
Technicus 28:a56710056f4d 136 rumblr = 1;
Technicus 28:a56710056f4d 137 }
Technicus 28:a56710056f4d 138 if ((outputs & 0x08) == 0){
Technicus 28:a56710056f4d 139 rumblr = 0;
Technicus 28:a56710056f4d 140 }
Technicus 28:a56710056f4d 141 if (outputs & 0x10){
Technicus 28:a56710056f4d 142 alarm_activated = 1;
bobbaddeley 26:3a1d82a26a83 143 }
Technicus 28:a56710056f4d 144 if ((outputs & 0x10) == 0){
Technicus 28:a56710056f4d 145 alarm_activated = 0;
Technicus 28:a56710056f4d 146 }
Technicus 28:a56710056f4d 147 if (outputs & 0x20){
Technicus 28:a56710056f4d 148 alarm_on = 1;
Technicus 28:a56710056f4d 149 }
Technicus 28:a56710056f4d 150 if ((outputs & 0x20) == 0){
Technicus 28:a56710056f4d 151 alarm_on = 0;
bobbaddeley 26:3a1d82a26a83 152 }
bobbaddeley 26:3a1d82a26a83 153
screamer 0:eb7f02ad28a7 154 triggerSensorPolling = false;
bobbaddeley 26:3a1d82a26a83 155
bobbaddeley 26:3a1d82a26a83 156 byteOut = sixButtons & buttonMask;
Technicus 28:a56710056f4d 157 if ((byteOut & 0x01) && alarm_activated==1) {
Technicus 28:a56710056f4d 158 alarm_on = 1;
Technicus 28:a56710056f4d 159 }
Technicus 28:a56710056f4d 160 if (byteOut != prevByteOut){
Technicus 28:a56710056f4d 161 // update the ble with the inputs
Technicus 28:a56710056f4d 162 bikeService.updateInputs(byteOut);
Technicus 28:a56710056f4d 163 bikeService.updateTimestamp();
Technicus 28:a56710056f4d 164 }
Technicus 28:a56710056f4d 165 prevByteOut = byteOut;
screamer 0:eb7f02ad28a7 166 } else {
screamer 0:eb7f02ad28a7 167 ble.waitForEvent(); // low power wait for event
screamer 0:eb7f02ad28a7 168 }
bobbaddeley 26:3a1d82a26a83 169
Technicus 28:a56710056f4d 170 if (alarm_activated && alarm_on) { //Flag set to sound the alarm?
bobbaddeley 26:3a1d82a26a83 171
Technicus 28:a56710056f4d 172 if (alarm_counter++ < 500) { //Beep it on and off
bobbaddeley 26:3a1d82a26a83 173 buzzer = 1;
Technicus 28:a56710056f4d 174 led_0 = 1;
Technicus 28:a56710056f4d 175 led_1 = 1;
Technicus 28:a56710056f4d 176 rumblr = 1;
bobbaddeley 26:3a1d82a26a83 177 }
bobbaddeley 26:3a1d82a26a83 178 else {
Technicus 28:a56710056f4d 179 if (alarm_counter > 1000){alarm_counter = 0;}
bobbaddeley 26:3a1d82a26a83 180 buzzer = 0;
Technicus 28:a56710056f4d 181 led_0 = 0;
Technicus 28:a56710056f4d 182 led_1 = 0;
Technicus 28:a56710056f4d 183 rumblr = 0;
bobbaddeley 26:3a1d82a26a83 184 }
Technicus 28:a56710056f4d 185 }
screamer 0:eb7f02ad28a7 186 }
screamer 0:eb7f02ad28a7 187 }
apalmieri 13:227a0149b677 188
apalmieri 13:227a0149b677 189 int main(void)
apalmieri 13:227a0149b677 190 {
apalmieri 13:227a0149b677 191 Ticker ticker;
apalmieri 13:227a0149b677 192 ticker.attach(periodicCallback, 1); // blink LED every second
apalmieri 13:227a0149b677 193
apalmieri 13:227a0149b677 194 BLE::Instance().init(bleInitComplete);
apalmieri 13:227a0149b677 195 }
apalmieri 14:f715c13eb84f 196