Heart Rate Monitor example for the BLE API using nRF51822 native mode drivers

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Fork of BLE_HeartRate by Bluetooth Low Energy

Revision:
72:99c283dfe28d
Parent:
71:469dbde1a238
Child:
73:49b6090478e2
--- a/main.cpp	Tue Sep 29 09:49:50 2015 +0000
+++ b/main.cpp	Mon Nov 02 09:49:57 2015 +0000
@@ -28,6 +28,11 @@
                                               GattService::UUID_DEVICE_INFORMATION_SERVICE};
 static volatile bool  triggerSensorPolling = false;
 
+uint8_t hrmCounter = 100; // init HRM to 100bps
+
+HeartRateService         *hrService;
+DeviceInformationService *deviceInfo;
+
 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
 {
     ble.gap().startAdvertising(); // restart advertising
@@ -42,21 +47,19 @@
     triggerSensorPolling = true;
 }
 
-int main(void)
+void bleInitComplete(BLE &ble, ble_error_t error)
 {
-    led1 = 1;
-    Ticker ticker;
-    ticker.attach(periodicCallback, 1); // blink LED every second
+    if (error != BLE_ERROR_NONE) {
+        return;
+    }
 
-    ble.init();
     ble.gap().onDisconnection(disconnectionCallback);
 
     /* Setup primary service. */
-    uint8_t hrmCounter = 100; // init HRM to 100bps
-    HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
+    hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
 
     /* Setup auxiliary service. */
-    DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
+    deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
 
     /* Setup advertising. */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
@@ -66,6 +69,15 @@
     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     ble.gap().setAdvertisingInterval(1000); /* 1000ms */
     ble.gap().startAdvertising();
+}
+
+int main(void)
+{
+    led1 = 1;
+    Ticker ticker;
+    ticker.attach(periodicCallback, 1); // blink LED every second
+
+    ble.init(bleInitComplete);
 
     // infinite loop
     while (1) {
@@ -76,14 +88,11 @@
             // 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) {
+            if (hrmCounter == 175) { //  100 <= HRM bps <=175
                 hrmCounter = 100;
             }
 
-            // update bps
-            hrService.updateHeartRate(hrmCounter);
+            hrService->updateHeartRate(hrmCounter);
         } else {
             ble.waitForEvent(); // low power wait for event
         }