BLE heart rate example, with yotta machinations.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_HeartRate by
Diff: main.cpp
- Revision:
- 55:3a7d497a3e03
- Parent:
- 53:06a74fd722b8
- Child:
- 56:83623419d5e4
--- a/main.cpp Tue Dec 09 08:54:37 2014 +0000 +++ b/main.cpp Mon Feb 09 04:50:03 2015 +0000 @@ -17,7 +17,6 @@ #include "mbed.h" #include "BLEDevice.h" #include "HeartRateService.h" -#include "BatteryService.h" #include "DeviceInformationService.h" /* Enable the following if you need to throttle the connection interval. This has @@ -29,9 +28,8 @@ BLEDevice ble; DigitalOut led1(LED1); -const static char DEVICE_NAME[] = "Nordic_HRM"; +const static char DEVICE_NAME[] = "HRM1"; static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, - GattService::UUID_BATTERY_SERVICE, GattService::UUID_DEVICE_INFORMATION_SERVICE}; static volatile bool triggerSensorPolling = false; @@ -40,28 +38,6 @@ ble.startAdvertising(); // restart advertising } -void onConnectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *params) -{ - #if UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL - /* Updating connection parameters can be attempted only after a connection has been - * established. Please note that the ble-Central is still the final arbiter for - * the effective parameters; the peripheral can only hope that the request is - * honored. Please also be mindful of the constraints that might be enforced by - * the BLE stack on the underlying controller.*/ - #define MIN_CONN_INTERVAL 250 /**< Minimum connection interval (250 ms) */ - #define MAX_CONN_INTERVAL 350 /**< Maximum connection interval (350 ms). */ - #define CONN_SUP_TIMEOUT 6000 /**< Connection supervisory timeout (6 seconds). */ - #define SLAVE_LATENCY 4 - - Gap::ConnectionParams_t gap_conn_params; - gap_conn_params.minConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(MIN_CONN_INTERVAL); - gap_conn_params.maxConnectionInterval = Gap::MSEC_TO_GAP_DURATION_UNITS(MAX_CONN_INTERVAL); - gap_conn_params.connectionSupervisionTimeout = Gap::MSEC_TO_GAP_DURATION_UNITS(CONN_SUP_TIMEOUT); - gap_conn_params.slaveLatency = SLAVE_LATENCY; - ble.updateConnectionParams(handle, &gap_conn_params); - #endif /* #if UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL */ -} - void periodicCallback(void) { led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ @@ -75,20 +51,16 @@ { led1 = 1; Ticker ticker; - ticker.attach(periodicCallback, 1); + ticker.attach(periodicCallback, 1); // blink LED every second ble.init(); ble.onDisconnection(disconnectionCallback); -#if UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL - ble.onConnection(onConnectionCallback); -#endif /* #if UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL */ /* Setup primary service. */ - uint8_t hrmCounter = 100; + uint8_t hrmCounter = 100; // init HRM to 100bps HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); - /* Setup auxiliary services. */ - BatteryService battery(ble); + /* Setup auxiliary service. */ DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); /* Setup advertising. */ @@ -100,20 +72,25 @@ ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000)); ble.startAdvertising(); - while (true) { + // infinite loop + while (1) { + // check for trigger from periodicCallback() if (triggerSensorPolling && ble.getGapState().connected) { triggerSensorPolling = false; - /* Do blocking calls or whatever is necessary for sensor polling. */ - /* In our case, we simply update the dummy HRM measurement. */ + // Do blocking calls or whatever is necessary for sensor polling. + // In our case, we simply update the HRM measurement. hrmCounter++; + + // 100 <= HRM bps <=175 if (hrmCounter == 175) { hrmCounter = 100; } - + + // update bps hrService.updateHeartRate(hrmCounter); } else { - ble.waitForEvent(); + ble.waitForEvent(); // low power wait for event } } }