test publish

Dependencies:   mbed GroveEarbudSensor

Revision:
12:a56f9fb318de
Child:
13:879d678baf64
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Application.cpp	Thu Apr 14 13:06:29 2016 +0000
@@ -0,0 +1,60 @@
+#include "Application.h"
+
+Application::Application(InterruptIn* earSensorPin, AnalogIn* GSRPin, PwmOut* speakerPin) : earbud(earSensorPin), speaker(speakerPin), GSRSens(GSRPin)
+{
+    thresholdHR = 0.0;
+    thresholdGSR = 0.0;
+}
+
+void Application::Init()
+{
+    printf("Initialization.. waiting 14s");
+    fflush(stdout);
+    wait(4); // Time for GSR measure to stabilize
+
+    float sumGSR, sumHR = 0.0;
+    for(int i = 0 ; i<NBGSRVALUE ; i++) {
+        sumGSR += GSRSens.getGSRValue();
+        sumHR += earbud.getHeartRate();
+        wait(0.5);
+
+    }
+    thresholdGSR = sumGSR/NBGSRVALUE;
+    thresholdHR = sumHR/NBGSRVALUE;
+
+}
+
+void Application::Run()
+{
+    MelodyGenerator melodyGen;
+    MajorScale scale1(SOL_4);
+    GypsyScale scale2(MI_4);
+    vector<Notes>* melo;
+    float heartRate, measureGSR;
+
+    while(true) {
+
+        heartRate = earbud.getHeartRate();
+        measureGSR = GSRSens.getGSRValue();
+        printf("\r\nHeartrate : %0.0f (nominal : %0.3f) \r\nGSR       : %0.4f (nominal : %0.4f) \r\n", heartRate, thresholdHR, measureGSR, thresholdGSR); // for debug purpose
+
+        if(heartRate <30) {
+            speaker.play(SILENCE);
+            heartRate = earbud.getHeartRate();
+            wait(1);
+        } else {
+            if (measureGSR < thresholdGSR) {
+                melo = melodyGen.getMeasure(scale1);
+                printf("\tMajor scale\r\n");
+            } else {
+                melo = melodyGen.getMeasure(scale2);
+                printf("\tGypsy scale\r\n");
+            }
+
+            for(vector<Notes>::iterator it = melo->begin(); it != melo->end(); ++it) {
+                speaker.play(*it);
+                wait( 0.3 - ((heartRate - thresholdHR) * 0.0015));
+            }
+        }
+    }
+}
\ No newline at end of file