HRV -> Mood

Dependencies:   MAX30101 Hexi_KW40Z Hexi_OLED_SSD1351

Files at this revision

API Documentation at this revision

Comitter:
jeannie9809
Date:
Sun Mar 17 06:55:42 2019 +0000
Parent:
13:45c2837ab056
Child:
15:5d291cd60879
Commit message:
we did it!

Changed in this revision

8cee5929f4d8/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/8cee5929f4d8/main.cpp	Sat Mar 16 22:34:14 2019 +0000
+++ b/8cee5929f4d8/main.cpp	Sun Mar 17 06:55:42 2019 +0000
@@ -4,6 +4,10 @@
 #include "string.h"
 #include "Hexi_OLED_SSD1351.h"
 #include "Hexi_KW40Z.h"
+
+#include "OLED_types.h"
+#include "OpenSans_Font.h"
+
 #include <math.h>
 #include <vector>
 using namespace std;
@@ -17,6 +21,8 @@
 void StopHaptic(void const *n);
 void txTask(void);
 
+DigitalOut blueLed(LED3,1); // i added this
+
 DigitalOut pwr1v8(PTA29);
 DigitalOut pwr3v3b(PTC13);
 DigitalOut pwr15v(PTB12);
@@ -38,7 +44,11 @@
 int mask_ppg = 0;
 uint32_t count = 0;
 uint32_t num;
-uint8_t testsignal = 60;
+// uint8_t testsignal = 60;
+
+uint8_t battery = 0;
+
+char text[20];  /* Text Buffer */
 
 // I added this
 const int num_samples = 500; // for 30 sec
@@ -48,6 +58,7 @@
 double HF_LF_n;
 int SDNN_n = 0;
 bool ready = false;
+bool ready_to_send = false;
 
 void StartHaptic(void)
 {
@@ -75,12 +86,22 @@
 {
 
     while (true) {
+        
+        while(ready_to_send == false) {
+            Thread::wait(100); // wait until has something to send
+        }
+
         UpdateSensorData();
+        ready_to_send = false;
+        printf("Sending!");
 
         /*Notify Hexiwear App that it is running Sensor Tag mode*/
         kw40z_device.SendSetApplicationMode(GUI_CURRENT_APP_SENSOR_TAG);
+        
+        kw40z_device.SendBatteryLevel(battery);
+        
         //send heartrate
-        kw40z_device.SendHeartRate(testsignal);
+//        kw40z_device.SendHeartRate(testsignal);
         /*The following is sending dummy data over BLE. Replace with real data*/
 
         /*Send Battery Level for 20%
@@ -108,7 +129,8 @@
 }
 void UpdateSensorData(void)
 {
-    testsignal+=1;
+//    testsignal+=1;
+    battery = valence_arousal;
     /*battery -= 5;
     if(battery < 5) battery = 100;
 
@@ -408,27 +430,40 @@
         HF_LF = HF/LF;
         printf("HF/LF = %.2f    \n", HF_LF);
         
-        if(SDNN_n < 1.0) {
+        if(SDNN_n < 1) {
             SDNN_n = SDNN;
             HF_LF_n = HF_LF;
             valence_arousal = 0;
         }
         else {
-            if(SDNN > SDNN_n) {
-                if(HF_LF > HF_LF_n)
-                    valence_arousal = 0;
-                else
-                    valence_arousal = 3;
-            }
-            else {
-                if(HF_LF > HF_LF_n)
-                    valence_arousal = 1;
-                else
-                    valence_arousal = 2;
-            }
+            // mapping SDNN, HF/LF to valence, arousal
+            int comp_sdnn = SDNN-SDNN_n;
+            if(comp_sdnn > 20)
+                valence_arousal = 50;
+            else if(comp_sdnn > 5)
+                valence_arousal = 40;
+            else if(comp_sdnn > -5)
+                valence_arousal = 30;
+            else if(comp_sdnn > -20)
+                valence_arousal = 20;
+            else
+                valence_arousal = 10;
+            
+            double comp_hf_lf = HF_LF/HF_LF_n;
+            if(comp_hf_lf > 5.0)
+                valence_arousal += 5;
+            else if(comp_hf_lf > 1.5)
+                valence_arousal += 4;
+            else if(comp_hf_lf > 0.9)
+                valence_arousal += 3;
+            else if(comp_hf_lf > 0.7)
+                valence_arousal += 2;
+            else
+                valence_arousal += 1;
+            printf("valence_arousal = %d   ", valence_arousal);
+            ready_to_send = true;
         }
         
-        
         ready = false; // last line
 
     }
@@ -455,7 +490,7 @@
     interruptStatus.all = 0xFF;
     hr.enableInterrupts(interruptStatus);
 
-    char text[20];  /* Text Buffer */
+    
     oled_text_properties_t textProperties = {0};
     oled.GetTextProperties(&textProperties);
     /* Turn on the backlight of the OLED Display */