HRV -> Mood

Dependencies:   MAX30101 Hexi_KW40Z Hexi_OLED_SSD1351

Revision:
9:83dcb7382516
Parent:
8:f5bd13e53c38
Child:
10:3ddf92687e34
--- a/8cee5929f4d8/main.cpp	Sat Mar 16 04:28:02 2019 +0000
+++ b/8cee5929f4d8/main.cpp	Sat Mar 16 05:38:01 2019 +0000
@@ -43,6 +43,7 @@
 int SDNN_n, SDNN;
 bool first_sample_set = true;
 double arousal, valence, HF_LF, HF_LF_n;
+bool ready = false;
 
 void StartHaptic(void)
 {
@@ -186,7 +187,7 @@
             uint16_t readBytes = 0;
 
             hr.readFIFO(MAX30101::OneLedChannel, data, readBytes);
-            printf("data length: %u \r\n",readBytes);
+//            printf("data length: %u \r\n",readBytes);
             //printf("data length: %u \r\n",data);
             for (uint16_t i = 0; i < readBytes; i += 3) {
                 uint8_t sample[4] = {0};
@@ -195,19 +196,25 @@
                 sample[2] = data[i];
 
                 num = *(uint32_t *) sample;
+                
                 if (num < 310000) {
                     ppg_single_sample = 0;
-                    printf("keep closer to your hand \r\n");
+//                    printf("keep closer to your hand \r\n");
                 } else {
 
                     //ppg_single_sample = 65;
                     ppg_single_sample = num;
-                    if(iter < num_samples)
-                        ppg[iter] = num;
-//                    printf("%d\r\n", ppg_single_sample); // I commented this out
+                    if(iter < num_samples && !ready) {
+                        ppg[iter] = num;                        
+//                        printf("%d   \n", ppg[iter]);
+                        if(iter == num_samples-1)
+                            ready = true;
+                    }
+
+//                    printf("%d   ", ppg_single_sample); // I commented this out
                     iter++;
                 }
-                printf("%d; %d\r\n", iter, ppg[iter-1]);
+//                printf("%d; %d\r\n", iter, ppg[iter-1]);
 
 
             }
@@ -226,46 +233,52 @@
     }
     */
     //ppg = temp_ppg;
+    
 }
 
 void interruptHandler()
 {
     evqueue.call(interruptHandlerQueued);
+//    interruptHandlerQueued();
+    if(ready) {
+    printf("\nStarting...  ");
+    
+    int i = 0;
+    int j = 0;
     
     // moving average
     double movave[num_samples];
     int avecap = 25;
-    int i = 0;
-    int j = 0;
     for(i = 0; i < (avecap-1)/2; i++) {
-        movave[i] = ppg[0];
+        movave[i] = (double)ppg[0];
         for(j = 1; j < 1+(avecap-1)/2; j++) {
-            movave[i] = movave[i] + ppg[j];
+            movave[i] = movave[i] + (double)ppg[j];
         }
         movave[i] = (double)(movave[i]/(i+(avecap-1)/2-1));
     }
     for(i = (num_samples-(avecap-1)/2); i < num_samples; i++) {
-        movave[i] = ppg[i-(avecap-1)/2-1];
+        movave[i] = (double)ppg[i-(avecap-1)/2-1];
         for(j = (i-(avecap-1)/2); j < num_samples; j++) {
-            movave[i] = movave[i] + ppg[j];
+            movave[i] = movave[i] + (double)ppg[j];
         }
         movave[i] = (double)(movave[i]/(num_samples-i+(avecap-1)/2));
     }
     for(i = (avecap-1)/2; i < (num_samples-(avecap-1)/2); i++) {
-        movave[i] = ppg[i-(avecap-1)/2-1];
+        movave[i] = (double)ppg[i-(avecap-1)/2-1];
         for(j = i-(avecap-1)/2; j < i+(avecap-1)/2; j++) {
-            movave[i] = movave[i] + ppg[j];
+            movave[i] = movave[i] + (double)ppg[j];
         }
         movave[i] = (double)(movave[i]/avecap);
     }
     // normalize ppg
     for(i = 0; i < num_samples; i++) {
-        ppg[i] = ppg[i] - movave[i];
+        ppg[i] = ppg[i] - (int)movave[i];
     }
     
     // smoothing curve
     for(i = 1; i < num_samples; i++) {
         ppg[i] = ppg[i] + ppg[i-1];
+        //printf("%d   ", ppg[i]);
     }
     
     // AMPD Algorithm
@@ -316,6 +329,8 @@
         index_r.push_back(index_of_max.at(i+1) - index_of_max.at(i));
         mean_inter_time = mean_inter_time + r.at(i);
     }
+    printf("num_max = %d    ", num_max);
+    //printf("r: %.2f %.2f %.2f   ", r.at(0), r.at(1), r.at(2));
     mean_inter_time = (double)(mean_inter_time/(num_max-1));
     
     // getting rid of outlier points in r
@@ -337,6 +352,11 @@
         SDNN_doub = SDNN_doub + (r.at(i)-mean_inter_time)*(r.at(i)-mean_inter_time);
     }
     SDNN = (int)(sqrt(SDNN_doub/(num_max-1))*1000);
+    printf("SDNN = %d\r\n", SDNN);
+    
+    ready = false;
+    }
+    
 }
 
 // main() runs in its own thread in the OS