mbed code for UberVest health monitoring

Dependencies:   BLE_API mbed nRF51822

Revision:
4:851d3f52c344
Parent:
3:183823979708
Child:
5:40fcfb34e48f
--- a/main.cpp	Thu Oct 22 10:22:59 2015 +0000
+++ b/main.cpp	Sat Oct 24 21:05:58 2015 +0000
@@ -18,10 +18,16 @@
 #include "BLE.h"
 #include "UberVestService.h"
 
-BLE         ble;
-DigitalOut  connectionLed(LED1);
+// Outputs
+DigitalOut connectionLed(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+
+// Inputs
 InterruptIn button(BUTTON1);
-AnalogIn ecg(p1);
+AnalogIn    ecg(p1);
+
+BLE     ble;
 
 const static char     DEVICE_NAME[] = "UberVest";
 static const uint16_t uuid16_list[] = {UberVestService::UBER_VEST_SERVICE_UUID};
@@ -31,8 +37,10 @@
     PRESSED,
     IDLE
 };
+
 static uint8_t buttonState = IDLE;
 static int8_t ecgValue;
+static volatile bool sample = false;
 
 UberVestService *uberVestServicePtr;
 
@@ -61,6 +69,12 @@
     connectionLed = 0;
 }
 
+void sampleTimer(void)
+{
+    led2 = !led2;
+    sample = true;
+}
+    
 int main(void)
 {
     button.fall(buttonPressedCallback);
@@ -72,6 +86,9 @@
 
     UberVestService uberVestService(ble, false /* initial value for button pressed */, 0);
     uberVestServicePtr = &uberVestService;
+    
+    Ticker sampler;
+    sampler.attach(sampleTimer, 0.02);
 
     /* setup advertising */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
@@ -87,9 +104,14 @@
             buttonState = IDLE;
         }
         
-        ecgValue = (ecg.read() * 1024);
-        uberVestServicePtr->updateEcg(ecgValue);
-
-        ble.waitForEvent();
+        if (ble.getGapState().connected && sample) {
+            led3 = 0;
+            sample = false;
+            ecgValue = (ecg.read() * 1024);
+            uberVestServicePtr->updateEcg(ecgValue);
+        } else {
+            led3 = 1;
+            ble.waitForEvent();
+        }
     }
 }