Unudge / Mbed 2 deprecated BLE_HeartRate

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
Aeyrial
Date:
Mon Jun 05 09:13:27 2017 +0000
Revision:
80:a2573ce708e2
Parent:
75:8128a06c0a21
Child:
81:fd97c70abbd8
Initial BLE example; Works for Heart Rate on Panobike app; Seeking to add UARTService;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:87a7fc231fae 1 /* mbed Microcontroller Library
rgrover1 67:b2d2dee347c0 2 * Copyright (c) 2006-2015 ARM Limited
ktownsend 0:87a7fc231fae 3 *
ktownsend 0:87a7fc231fae 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 0:87a7fc231fae 5 * you may not use this file except in compliance with the License.
ktownsend 0:87a7fc231fae 6 * You may obtain a copy of the License at
ktownsend 0:87a7fc231fae 7 *
ktownsend 0:87a7fc231fae 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 0:87a7fc231fae 9 *
ktownsend 0:87a7fc231fae 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 0:87a7fc231fae 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 0:87a7fc231fae 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 0:87a7fc231fae 13 * See the License for the specific language governing permissions and
ktownsend 0:87a7fc231fae 14 * limitations under the License.
ktownsend 0:87a7fc231fae 15 */
Aeyrial 80:a2573ce708e2 16
ktownsend 0:87a7fc231fae 17
ktownsend 0:87a7fc231fae 18 #include "mbed.h"
rgrover1 67:b2d2dee347c0 19 #include "ble/BLE.h"
rgrover1 67:b2d2dee347c0 20 #include "ble/services/HeartRateService.h"
rgrover1 67:b2d2dee347c0 21 #include "ble/services/BatteryService.h"
rgrover1 67:b2d2dee347c0 22 #include "ble/services/DeviceInformationService.h"
Aeyrial 80:a2573ce708e2 23 #include "ble/services/UARTService.h" // Not explictly used, I have just added this in.
ktownsend 0:87a7fc231fae 24
Aeyrial 80:a2573ce708e2 25 //** From Simple Chat example **//
Aeyrial 80:a2573ce708e2 26 #define BLE_UUID_TXRX_SERVICE 0x0000 /**< The UUID of the Nordic UART Service. */
Aeyrial 80:a2573ce708e2 27 #define BLE_UUID_TX_CHARACTERISTIC 0x0002 /**< The UUID of the TX Characteristic. */
Aeyrial 80:a2573ce708e2 28 #define BLE_UUIDS_RX_CHARACTERISTIC 0x0003 /**< The UUID of the RX Characteristic. */
Aeyrial 80:a2573ce708e2 29
Aeyrial 80:a2573ce708e2 30 #define TXRX_BUF_LEN 20
Aeyrial 80:a2573ce708e2 31
Aeyrial 80:a2573ce708e2 32 /* SimpleChat */ BLE ble; // Commented out to prevent conflict
Aeyrial 80:a2573ce708e2 33 /* HeartRate */ /* BLE &ble = params->ble; // Defined in scope of "bleInitComplete"
Aeyrial 80:a2573ce708e2 34 * ble_error_t error = params->error; */
Aeyrial 80:a2573ce708e2 35
Aeyrial 80:a2573ce708e2 36 Serial pc(USBTX, USBRX); // USBTX, USBRX origin unclear
Aeyrial 80:a2573ce708e2 37
Aeyrial 80:a2573ce708e2 38 // The Nordic UART Service
Aeyrial 80:a2573ce708e2 39 static const uint8_t uart_base_uuid[] = {0x71, 0x3D, 0, 0, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
Aeyrial 80:a2573ce708e2 40 static const uint8_t uart_tx_uuid[] = {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
Aeyrial 80:a2573ce708e2 41 static const uint8_t uart_rx_uuid[] = {0x71, 0x3D, 0, 2, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};
Aeyrial 80:a2573ce708e2 42 static const uint8_t uart_base_uuid_rev[] = {0x1E, 0x94, 0x8D, 0xF1, 0x48, 0x31, 0x94, 0xBA, 0x75, 0x4C, 0x3E, 0x50, 0, 0, 0x3D, 0x71};
Aeyrial 80:a2573ce708e2 43
Aeyrial 80:a2573ce708e2 44 uint8_t txPayload[TXRX_BUF_LEN] = {0,};
Aeyrial 80:a2573ce708e2 45 uint8_t rxPayload[TXRX_BUF_LEN] = {0,};
Aeyrial 80:a2573ce708e2 46
Aeyrial 80:a2573ce708e2 47 static uint8_t rx_buf[TXRX_BUF_LEN];
Aeyrial 80:a2573ce708e2 48 static uint8_t rx_len=0;
Aeyrial 80:a2573ce708e2 49
Aeyrial 80:a2573ce708e2 50 GattCharacteristic txCharacteristic (uart_tx_uuid, txPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE);
Aeyrial 80:a2573ce708e2 51 GattCharacteristic rxCharacteristic (uart_rx_uuid, rxPayload, 1, TXRX_BUF_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
Aeyrial 80:a2573ce708e2 52 GattCharacteristic *uartChars[] = {&txCharacteristic, &rxCharacteristic};
Aeyrial 80:a2573ce708e2 53 GattService uartService(uart_base_uuid, uartChars, sizeof(uartChars) / sizeof(GattCharacteristic *));
Aeyrial 80:a2573ce708e2 54
Aeyrial 80:a2573ce708e2 55 void WrittenHandler(const GattWriteCallbackParams *Handler)
Aeyrial 80:a2573ce708e2 56 {
Aeyrial 80:a2573ce708e2 57 uint8_t buf[TXRX_BUF_LEN];
Aeyrial 80:a2573ce708e2 58 uint16_t bytesRead, index;
Aeyrial 80:a2573ce708e2 59
Aeyrial 80:a2573ce708e2 60 if (Handler->handle == txCharacteristic.getValueAttribute().getHandle())
Aeyrial 80:a2573ce708e2 61 {
Aeyrial 80:a2573ce708e2 62 ble.readCharacteristicValue(txCharacteristic.getValueAttribute().getHandle(), buf, &bytesRead);
Aeyrial 80:a2573ce708e2 63 memset(txPayload, 0, TXRX_BUF_LEN);
Aeyrial 80:a2573ce708e2 64 memcpy(txPayload, buf, TXRX_BUF_LEN);
Aeyrial 80:a2573ce708e2 65 pc.printf("WriteHandler \r\n");
Aeyrial 80:a2573ce708e2 66 pc.printf("Length: ");
Aeyrial 80:a2573ce708e2 67 pc.putc(bytesRead);
Aeyrial 80:a2573ce708e2 68 pc.printf("\r\n");
Aeyrial 80:a2573ce708e2 69 pc.printf("Data: ");
Aeyrial 80:a2573ce708e2 70 for(index=0; index<bytesRead; index++)
Aeyrial 80:a2573ce708e2 71 {
Aeyrial 80:a2573ce708e2 72 pc.putc(txPayload[index]);
Aeyrial 80:a2573ce708e2 73 }
Aeyrial 80:a2573ce708e2 74 pc.printf("\r\n");
Aeyrial 80:a2573ce708e2 75 }
Aeyrial 80:a2573ce708e2 76 }
Aeyrial 80:a2573ce708e2 77
Aeyrial 80:a2573ce708e2 78 void uartCB(void)
Aeyrial 80:a2573ce708e2 79 {
Aeyrial 80:a2573ce708e2 80 while(pc.readable())
Aeyrial 80:a2573ce708e2 81 {
Aeyrial 80:a2573ce708e2 82 rx_buf[rx_len++] = pc.getc();
Aeyrial 80:a2573ce708e2 83 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
Aeyrial 80:a2573ce708e2 84 {
Aeyrial 80:a2573ce708e2 85 ble.updateCharacteristicValue(rxCharacteristic.getValueAttribute().getHandle(), rx_buf, rx_len);
Aeyrial 80:a2573ce708e2 86 pc.printf("RecHandler \r\n");
Aeyrial 80:a2573ce708e2 87 pc.printf("Length: ");
Aeyrial 80:a2573ce708e2 88 pc.putc(rx_len);
Aeyrial 80:a2573ce708e2 89 pc.printf("\r\n");
Aeyrial 80:a2573ce708e2 90 rx_len = 0;
Aeyrial 80:a2573ce708e2 91 break;
Aeyrial 80:a2573ce708e2 92 }
Aeyrial 80:a2573ce708e2 93 }
Aeyrial 80:a2573ce708e2 94 }
Aeyrial 80:a2573ce708e2 95 //** From Heart Rate example **//
rgrover1 47:430545f41113 96 DigitalOut led1(LED1);
ktownsend 0:87a7fc231fae 97
Aeyrial 80:a2573ce708e2 98 const static char DEVICE_NAME[] = "Unudge_Prototype";
rgrover1 42:06ebef2e0e44 99 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
rgrover1 42:06ebef2e0e44 100 GattService::UUID_DEVICE_INFORMATION_SERVICE};
Aeyrial 80:a2573ce708e2 101
Aeyrial 80:a2573ce708e2 102 // Alternately to include the UART , I could use a different unit size ("uint128_list[]" rather than "uuid16_list[]")
Aeyrial 80:a2573ce708e2 103
rgrover1 39:6390604f904c 104 static volatile bool triggerSensorPolling = false;
Rohit Grover 36:ea2a1b4f51c1 105
rgrover1 72:99c283dfe28d 106 uint8_t hrmCounter = 100; // init HRM to 100bps
rgrover1 72:99c283dfe28d 107
Aeyrial 80:a2573ce708e2 108 // These services are classes: "HeartRateService" and "DeviceInformationService" are defined in the BLE_API library under classes
Aeyrial 80:a2573ce708e2 109 // The names of the objects are brought in as pointers
rgrover1 72:99c283dfe28d 110 HeartRateService *hrService;
rgrover1 72:99c283dfe28d 111 DeviceInformationService *deviceInfo;
rgrover1 72:99c283dfe28d 112
Aeyrial 80:a2573ce708e2 113 // Specifics not understood, purpose clear: When the device is disconnected,
Aeyrial 80:a2573ce708e2 114 // start advertising again immediately
rgrover1 71:469dbde1a238 115 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
ktownsend 0:87a7fc231fae 116 {
Aeyrial 80:a2573ce708e2 117 BLE::Instance(BLE::DEFAULT_INSTANCE).startAdvertising(); // restart advertising
Aeyrial 80:a2573ce708e2 118 // Alternate Content
Aeyrial 80:a2573ce708e2 119 /*pc.printf("Disconnected \r\n");
Aeyrial 80:a2573ce708e2 120 pc.printf("Restart advertising \r\n");
Aeyrial 80:a2573ce708e2 121 ble.startAdvertising();*/
rgrover1 7:daab8ba5139e 122 }
Rohit Grover 3:24e2b056d229 123
Aeyrial 80:a2573ce708e2 124 // This function switches the led ON/OFF and
Aeyrial 80:a2573ce708e2 125 // sets a flag value which is used in int main ()
Rohit Grover 11:1d9aafee4984 126 void periodicCallback(void)
Rohit Grover 11:1d9aafee4984 127 {
rgrover1 47:430545f41113 128 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
rgrover1 47:430545f41113 129
rgrover1 39:6390604f904c 130 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
rgrover1 39:6390604f904c 131 * heavy-weight sensor polling from the main thread. */
rgrover1 39:6390604f904c 132 triggerSensorPolling = true;
Aeyrial 80:a2573ce708e2 133
Aeyrial 80:a2573ce708e2 134 // ".available" is guesswork. I need something to check
Aeyrial 80:a2573ce708e2 135 // If there's any incoming message - onDataWritten?
Aeyrial 80:a2573ce708e2 136 /*
Aeyrial 80:a2573ce708e2 137 if(ble.getGapState().available) {
Aeyrial 80:a2573ce708e2 138 // I anticipate using a single char input to flag
Aeyrial 80:a2573ce708e2 139 // The intended purpose of the communication
Aeyrial 80:a2573ce708e2 140
Aeyrial 80:a2573ce708e2 141 // read in "someFlag" and store
Aeyrial 80:a2573ce708e2 142 string someFlag = /syntax for reading ble input/
Aeyrial 80:a2573ce708e2 143
Aeyrial 80:a2573ce708e2 144 switch(someFlag){
Aeyrial 80:a2573ce708e2 145 case 'S':
Aeyrial 80:a2573ce708e2 146 inBoundSettingChange = true;
Aeyrial 80:a2573ce708e2 147 break;
Aeyrial 80:a2573ce708e2 148 case 'R':
Aeyrial 80:a2573ce708e2 149 inBoundRhythm = true;
Aeyrial 80:a2573ce708e2 150 break;
Aeyrial 80:a2573ce708e2 151 default:
Aeyrial 80:a2573ce708e2 152 break;
Aeyrial 80:a2573ce708e2 153 }
Aeyrial 80:a2573ce708e2 154 //Flush someFlag
Aeyrial 80:a2573ce708e2 155 someFlag = '';
Aeyrial 80:a2573ce708e2 156 }
Aeyrial 80:a2573ce708e2 157 */
Rohit Grover 11:1d9aafee4984 158 }
Rohit Grover 11:1d9aafee4984 159
rgrover1 74:c9d58e7847c4 160 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
ktownsend 0:87a7fc231fae 161 {
Aeyrial 80:a2573ce708e2 162 // BLE is the class, &ble is the address of the declared object
Aeyrial 80:a2573ce708e2 163 // "params->" purpose unknown
rgrover1 74:c9d58e7847c4 164 BLE &ble = params->ble;
rgrover1 74:c9d58e7847c4 165 ble_error_t error = params->error;
rgrover1 74:c9d58e7847c4 166
rgrover1 72:99c283dfe28d 167 if (error != BLE_ERROR_NONE) {
rgrover1 72:99c283dfe28d 168 return;
rgrover1 72:99c283dfe28d 169 }
ktownsend 0:87a7fc231fae 170
Aeyrial 80:a2573ce708e2 171 ble.onDisconnection(disconnectionCallback);
ktownsend 0:87a7fc231fae 172
rgrover1 45:98c5a34b07a4 173 /* Setup primary service. */
rgrover1 72:99c283dfe28d 174 hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
rgrover1 45:98c5a34b07a4 175
mbedAustin 55:3a7d497a3e03 176 /* Setup auxiliary service. */
Aeyrial 80:a2573ce708e2 177 // 2nd parameter - the manufacturer's name // rev - revision
Aeyrial 80:a2573ce708e2 178 deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SerialNo#", "hw-rev1", "fw-rev1", "soft-rev1");
rgrover1 45:98c5a34b07a4 179
rgrover1 45:98c5a34b07a4 180 /* Setup advertising. */
Aeyrial 80:a2573ce708e2 181 // This line below is mandatory for bluetooth smart
Aeyrial 80:a2573ce708e2 182 // It indicates the device only supports Bluetooth Low Energy
Aeyrial 80:a2573ce708e2 183 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Aeyrial 80:a2573ce708e2 184 // I believe this advertises the service list
Aeyrial 80:a2573ce708e2 185 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
Aeyrial 80:a2573ce708e2 186
Aeyrial 80:a2573ce708e2 187 // I realise that this isn't efficient but I would rather keep the code as
Aeyrial 80:a2573ce708e2 188 // Is for now, so that it actually works. Hence why a 2nd "COMPLETE_LIST"
Aeyrial 80:a2573ce708e2 189 /* SimpleChat */ ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
Aeyrial 80:a2573ce708e2 190 (const uint8_t *)uart_base_uuid_rev, sizeof(uart_base_uuid));
Aeyrial 80:a2573ce708e2 191
Aeyrial 80:a2573ce708e2 192 ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
Aeyrial 80:a2573ce708e2 193 // Advertise the device name
Aeyrial 80:a2573ce708e2 194 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
Aeyrial 80:a2573ce708e2 195 // Appears in all examples I have seen
Aeyrial 80:a2573ce708e2 196 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Aeyrial 80:a2573ce708e2 197
Aeyrial 80:a2573ce708e2 198 // Setup Advertising Interval
Aeyrial 80:a2573ce708e2 199 ble.setAdvertisingInterval(1000); /* 1000ms */
Aeyrial 80:a2573ce708e2 200
Aeyrial 80:a2573ce708e2 201 /* SimpleChat */ ble.addService(uartService);
Aeyrial 80:a2573ce708e2 202 // Start Advertising
Aeyrial 80:a2573ce708e2 203 ble.startAdvertising();
Aeyrial 80:a2573ce708e2 204
Aeyrial 80:a2573ce708e2 205 /* SimpleChat */ pc.printf("Advertising Start \r\n");
rgrover1 72:99c283dfe28d 206 }
rgrover1 72:99c283dfe28d 207
rgrover1 72:99c283dfe28d 208 int main(void)
rgrover1 72:99c283dfe28d 209 {
Aeyrial 80:a2573ce708e2 210 pc.baud(9600);
Aeyrial 80:a2573ce708e2 211 pc.printf("Baud Rate set to 9600 \r\n");
Aeyrial 80:a2573ce708e2 212
Aeyrial 80:a2573ce708e2 213 pc.attach( uartCB , pc.RxIrq);
Aeyrial 80:a2573ce708e2 214
rgrover1 72:99c283dfe28d 215 led1 = 1;
rgrover1 72:99c283dfe28d 216 Ticker ticker;
rgrover1 72:99c283dfe28d 217 ticker.attach(periodicCallback, 1); // blink LED every second
rgrover1 72:99c283dfe28d 218
rgrover1 73:49b6090478e2 219 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
rgrover1 72:99c283dfe28d 220 ble.init(bleInitComplete);
Rohit Grover 3:24e2b056d229 221
rgrover1 73:49b6090478e2 222 /* SpinWait for initialization to complete. This is necessary because the
rgrover1 73:49b6090478e2 223 * BLE object is used in the main loop below. */
vcoubard 75:8128a06c0a21 224 while (ble.hasInitialized() == false) { /* spin loop */ }
rgrover1 73:49b6090478e2 225
Aeyrial 80:a2573ce708e2 226 /* SimpleChat */ // Places nothing in the while (1) {} loop
Aeyrial 80:a2573ce708e2 227 // It's relying on interrupts rather than
Aeyrial 80:a2573ce708e2 228 // a periodic callback via a Ticker
mbedAustin 55:3a7d497a3e03 229 // infinite loop
mbedAustin 55:3a7d497a3e03 230 while (1) {
mbedAustin 55:3a7d497a3e03 231 // check for trigger from periodicCallback()
rgrover1 50:477004d54431 232 if (triggerSensorPolling && ble.getGapState().connected) {
Rohit Grover 36:ea2a1b4f51c1 233 triggerSensorPolling = false;
Rohit Grover 36:ea2a1b4f51c1 234
mbedAustin 55:3a7d497a3e03 235 // Do blocking calls or whatever is necessary for sensor polling.
rgrover1 67:b2d2dee347c0 236 // In our case, we simply update the HRM measurement.
Rohit Grover 36:ea2a1b4f51c1 237 hrmCounter++;
rgrover1 72:99c283dfe28d 238 if (hrmCounter == 175) { // 100 <= HRM bps <=175
Rohit Grover 36:ea2a1b4f51c1 239 hrmCounter = 100;
Rohit Grover 36:ea2a1b4f51c1 240 }
rgrover1 72:99c283dfe28d 241 hrService->updateHeartRate(hrmCounter);
Aeyrial 80:a2573ce708e2 242 // Code relating to storing/playing inbound rhythm
Aeyrial 80:a2573ce708e2 243 // Into "Rhythm" Class (Yet to be defined)
Aeyrial 80:a2573ce708e2 244 /*} else if (inBoundRhythm && ble.getGapState().connected) {
Aeyrial 80:a2573ce708e2 245 inBoundRhythm = false;
Aeyrial 80:a2573ce708e2 246 Rhythm inRhythm;
Aeyrial 80:a2573ce708e2 247 inRythm += syntax for getting the incoming string
Aeyrial 80:a2573ce708e2 248 inRhythm.play(); // Create Member function which
Aeyrial 80:a2573ce708e2 249 // Plays Rhythms
Aeyrial 80:a2573ce708e2 250
Aeyrial 80:a2573ce708e2 251 */
Aeyrial 80:a2573ce708e2 252 // Code relating to storing/sending outbound rhythm
Aeyrial 80:a2573ce708e2 253 /*} else if (inBoundRhythm && ble.getGapState().connected) {
Aeyrial 80:a2573ce708e2 254 inBoundRhythm = false;
Aeyrial 80:a2573ce708e2 255 Rhythm inRhythm;
Aeyrial 80:a2573ce708e2 256 inRythm += syntax for getting the incoming string
Aeyrial 80:a2573ce708e2 257 inRhythm.play(); // Create Member function which
Aeyrial 80:a2573ce708e2 258 // Plays Rhythms
Aeyrial 80:a2573ce708e2 259
Aeyrial 80:a2573ce708e2 260 */
Aeyrial 80:a2573ce708e2 261
Aeyrial 80:a2573ce708e2 262 // How will I set inBoundRhythm = true; ?
Rohit Grover 36:ea2a1b4f51c1 263 } else {
mbedAustin 55:3a7d497a3e03 264 ble.waitForEvent(); // low power wait for event
Rohit Grover 36:ea2a1b4f51c1 265 }
ktownsend 0:87a7fc231fae 266 }
ktownsend 0:87a7fc231fae 267 }