heart rate monitor

Dependencies:   BLE_API GroveEarbudSensor mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

Revision:
71:c86b4658af70
Parent:
67:b2d2dee347c0
Child:
72:cc9b1d195d9f
diff -r 0a19690e8d23 -r c86b4658af70 main.cpp
--- a/main.cpp	Tue Aug 11 21:58:13 2015 +0000
+++ b/main.cpp	Sat Aug 29 17:49:14 2015 +0000
@@ -19,15 +19,22 @@
 #include "ble/services/HeartRateService.h"
 #include "ble/services/BatteryService.h"
 #include "ble/services/DeviceInformationService.h"
-
+// Grove Earbud Sensor include
+#include "GroveEarbudSensor.h"
 BLE  ble;
 DigitalOut led1(LED1);
+// Our sensor as an InterruptIn
+InterruptIn sensor(P0_0);
 
 const static char     DEVICE_NAME[]        = "HRM1";
 static const uint16_t uuid16_list[]        = {GattService::UUID_HEART_RATE_SERVICE,
                                               GattService::UUID_DEVICE_INFORMATION_SERVICE};
 static volatile bool  triggerSensorPolling = false;
 
+// callback for receiving heartrate values
+void heartrateCallback(float heartrate,void *data) {
+    printf("Callback: heartrate = %.1f\r\n",heartrate);
+}
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
     ble.gap().startAdvertising(); // restart advertising
@@ -53,6 +60,7 @@
 
     /* Setup primary service. */
     uint8_t hrmCounter = 100; // init HRM to 100bps
+   
     HeartRateService hrService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
 
     /* Setup auxiliary service. */
@@ -75,14 +83,37 @@
 
             // Do blocking calls or whatever is necessary for sensor polling.
             // In our case, we simply update the HRM measurement.
-            hrmCounter++;
+           // hrmCounter++;
 
             //  100 <= HRM bps <=175
-            if (hrmCounter == 175) {
+            /*if (hrmCounter == 175) {
                 hrmCounter = 100;
-            }
+            }*/
+            // announce
+            printf("Grove Earbud Sensor - Montoring Heart Rate\r\n");
+    
+             // allocate the earbud sensor
+             printf("Allocating earbud sensor instance...\r\n");
+             GroveEarbudSensor earbud(&sensor); 
+    
+            // register our callback function
+             printf("registering callback...\r\n");
+            earbud.registerCallback(heartrateCallback);
+    
+            /* begin main loop
+            printf("Beginning main loop...\r\n");
+             while (true) {
+            // blink... 
+            led1 = !led1; 
+             wait(0.5);*/
+        
+             // we can also call directly 
+             printf("Direct: heartrate = %.1f\r\n", earbud.getHeartRate());
+            hrmCounter=earbud.getHeartRate();
+    
 
             // update bps
+            //hrService.updateHeartRate(hrmCounter);  
             hrService.updateHeartRate(hrmCounter);
         } else {
             ble.waitForEvent(); // low power wait for event