
Example to simulate photoplethysmography for heart rate and transmit via BLE
Dependencies: BLE_API X_NUCLEO_6180XA1 X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
main.cpp@0:eb7f02ad28a7, 2015-09-22 (annotated)
- Committer:
- screamer
- Date:
- Tue Sep 22 18:11:51 2015 +0000
- Revision:
- 0:eb7f02ad28a7
- Child:
- 1:11cd427bbc8b
Initial revision
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" |
screamer | 0:eb7f02ad28a7 | 19 | #include "ble/services/HeartRateService.h" |
screamer | 0:eb7f02ad28a7 | 20 | #include "ble/services/BatteryService.h" |
screamer | 0:eb7f02ad28a7 | 21 | #include "ble/services/DeviceInformationService.h" |
screamer | 0:eb7f02ad28a7 | 22 | |
screamer | 0:eb7f02ad28a7 | 23 | BLE ble; |
screamer | 0:eb7f02ad28a7 | 24 | DigitalOut led1(LED1); |
screamer | 0:eb7f02ad28a7 | 25 | |
screamer | 0:eb7f02ad28a7 | 26 | const static char DEVICE_NAME[] = "HRM1"; |
screamer | 0:eb7f02ad28a7 | 27 | static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, |
screamer | 0:eb7f02ad28a7 | 28 | GattService::UUID_DEVICE_INFORMATION_SERVICE}; |
screamer | 0:eb7f02ad28a7 | 29 | static volatile bool triggerSensorPolling = false; |
screamer | 0:eb7f02ad28a7 | 30 | |
screamer | 0:eb7f02ad28a7 | 31 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
screamer | 0:eb7f02ad28a7 | 32 | { |
screamer | 0:eb7f02ad28a7 | 33 | ble.gap().startAdvertising(); // restart advertising |
screamer | 0:eb7f02ad28a7 | 34 | } |
screamer | 0:eb7f02ad28a7 | 35 | |
screamer | 0:eb7f02ad28a7 | 36 | void periodicCallback(void) |
screamer | 0:eb7f02ad28a7 | 37 | { |
screamer | 0:eb7f02ad28a7 | 38 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
screamer | 0:eb7f02ad28a7 | 39 | |
screamer | 0:eb7f02ad28a7 | 40 | /* Note that the periodicCallback() executes in interrupt context, so it is safer to do |
screamer | 0:eb7f02ad28a7 | 41 | * heavy-weight sensor polling from the main thread. */ |
screamer | 0:eb7f02ad28a7 | 42 | triggerSensorPolling = true; |
screamer | 0:eb7f02ad28a7 | 43 | } |
screamer | 0:eb7f02ad28a7 | 44 | |
screamer | 0:eb7f02ad28a7 | 45 | int main(void) |
screamer | 0:eb7f02ad28a7 | 46 | { |
screamer | 0:eb7f02ad28a7 | 47 | led1 = 1; |
screamer | 0:eb7f02ad28a7 | 48 | Ticker ticker; |
screamer | 0:eb7f02ad28a7 | 49 | ticker.attach(periodicCallback, 1); // blink LED every second |
screamer | 0:eb7f02ad28a7 | 50 | |
screamer | 0:eb7f02ad28a7 | 51 | ble.init(); |
screamer | 0:eb7f02ad28a7 | 52 | ble.gap().onDisconnection(disconnectionCallback); |
screamer | 0:eb7f02ad28a7 | 53 | |
screamer | 0:eb7f02ad28a7 | 54 | /* Setup primary service. */ |
screamer | 0:eb7f02ad28a7 | 55 | uint8_t hrmCounter = 100; // init HRM to 100bps |
screamer | 0:eb7f02ad28a7 | 56 | HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); |
screamer | 0:eb7f02ad28a7 | 57 | |
screamer | 0:eb7f02ad28a7 | 58 | /* Setup auxiliary service. */ |
screamer | 0:eb7f02ad28a7 | 59 | DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
screamer | 0:eb7f02ad28a7 | 60 | |
screamer | 0:eb7f02ad28a7 | 61 | /* Setup advertising. */ |
screamer | 0:eb7f02ad28a7 | 62 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
screamer | 0:eb7f02ad28a7 | 63 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
screamer | 0:eb7f02ad28a7 | 64 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); |
screamer | 0:eb7f02ad28a7 | 65 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
screamer | 0:eb7f02ad28a7 | 66 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
screamer | 0:eb7f02ad28a7 | 67 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
screamer | 0:eb7f02ad28a7 | 68 | ble.gap().startAdvertising(); |
screamer | 0:eb7f02ad28a7 | 69 | |
screamer | 0:eb7f02ad28a7 | 70 | // infinite loop |
screamer | 0:eb7f02ad28a7 | 71 | while (1) { |
screamer | 0:eb7f02ad28a7 | 72 | // check for trigger from periodicCallback() |
screamer | 0:eb7f02ad28a7 | 73 | if (triggerSensorPolling && ble.getGapState().connected) { |
screamer | 0:eb7f02ad28a7 | 74 | triggerSensorPolling = false; |
screamer | 0:eb7f02ad28a7 | 75 | |
screamer | 0:eb7f02ad28a7 | 76 | // Do blocking calls or whatever is necessary for sensor polling. |
screamer | 0:eb7f02ad28a7 | 77 | // In our case, we simply update the HRM measurement. |
screamer | 0:eb7f02ad28a7 | 78 | hrmCounter++; |
screamer | 0:eb7f02ad28a7 | 79 | |
screamer | 0:eb7f02ad28a7 | 80 | // 100 <= HRM bps <=175 |
screamer | 0:eb7f02ad28a7 | 81 | if (hrmCounter == 175) { |
screamer | 0:eb7f02ad28a7 | 82 | hrmCounter = 100; |
screamer | 0:eb7f02ad28a7 | 83 | } |
screamer | 0:eb7f02ad28a7 | 84 | |
screamer | 0:eb7f02ad28a7 | 85 | // update bps |
screamer | 0:eb7f02ad28a7 | 86 | hrService.updateHeartRate(hrmCounter); |
screamer | 0:eb7f02ad28a7 | 87 | } else { |
screamer | 0:eb7f02ad28a7 | 88 | ble.waitForEvent(); // low power wait for event |
screamer | 0:eb7f02ad28a7 | 89 | } |
screamer | 0:eb7f02ad28a7 | 90 | } |
screamer | 0:eb7f02ad28a7 | 91 | } |