heart modes, send, receive

Dependencies:   Terminal TextLCD mbed-rtos mbed

Fork of Heartnew by CIS541

Revision:
6:cc4fcc38b9f0
Parent:
5:8e3b9032ddfc
Child:
7:46aeded22784
--- a/heart.cpp	Wed Dec 02 02:09:34 2015 +0000
+++ b/heart.cpp	Wed Dec 02 04:03:46 2015 +0000
@@ -31,6 +31,7 @@
 bool test_result;
 const int prop_delay=15;
 int flag_key=0;
+Mutex heartmut, keymutex;
 
 int LRI=1500;
 int URI=600;
@@ -65,6 +66,7 @@
 
 int lri=normalModeLRI;
 int uri=normalModeURI;
+int flagr=0;
 
 typedef enum Modes {
     Test,
@@ -146,7 +148,7 @@
     VPaceReceive=1;
     wait(0.002);
     APaceReceive=0;
-    pulses++;
+    //pulses++;
     Vreceived=true;
     Areceived=false;
     if(mode == Test && flag==0) {
@@ -200,7 +202,9 @@
 void updateHeart()
 {
     int temp = 60/heartInterval;
+    heartmut.lock();
     heartRate = heartRate + temp;
+    heartmut.lock();
 }
 void HeartReceive(void const* args)
 {
@@ -218,7 +222,8 @@
 
 void HeartSend(void const* args)
 {
-    while(!pc.readable()) {
+    while(1) {
+        if(flagr==1){
         int r = rand() % 2;
         pc.printf("HS");
         while(t<minwait_V && mode!=Random && (mode==Test || mode==Manual));
@@ -231,46 +236,54 @@
             ASignalsend();
             pc.printf("ASignal");
             resett();
-        }
+        }}
     }
+    //keymutex.unlock();
+    // }
 }
 void HeartKeyBoardModeSwitch(void const* args)
 {
     while(1) {
-       // Thread::wait(0x02);
+        keymutex.lock();
         if((key_input=='r'||key_input=='R')&&flag_key==1) {
-        mode=Random;
-        pc.printf("Random");
-            HeartSend("s");
+            mode=Random;
+            pc.printf("Random");
+            flagr=1;
+                HeartSend("s");     
             flag_key=0;
         } else if((key_input=='m'||key_input=='M')&&flag_key) {
-        mode=Manual;
-        pc.printf("Manual");
+            mode=Manual;
+            pc.printf("Manual");
             flag_key=0;
+            
         } else if(mode==Manual&&(key_input=='v'||key_input=='V')&&flag_key==1) {
-        mode=Manual;
-        VSignalsend();
+            mode=Manual;
+            VSignalsend();
             pc.printf("Ventricular");
             flag_key=0;
+            
         } else if(mode==Manual&&flag_key==1&&(key_input=='a'||key_input=='A')) {
-        mode=Manual;
-        ASignalsend();
+            mode=Manual;
+            ASignalsend();
             pc.printf("Atrial");
-             flag_key=0;
+            flag_key=0;
         } else if((key_input=='o'||key_input=='O')&&flag_key==1) {
-        int a = 10 + rand() % 80;
+            int a = 10 + rand() % 80;
             mode=Observer;
             pc.printf("Observer and %u",a);
             heartInterval =a;
             HeartSend("s");
             flag_key=0;
+            
 
         } else if(flag_key==1&&(key_input=='t'||key_input=='T')) {
-        mode=Test;
-        (*testmode).signal_set(0x03);
+            mode=Test;
+            (*testmode).signal_set(0x03);
             pc.printf("Test");
-             flag_key=0;
+            flag_key=0;
+            
         }
+        keymutex.unlock();
     }
 }
 
@@ -288,21 +301,29 @@
 //            }
 //        }
         if(!Test) {
+            heartmut.lock();
             pc.printf("Avg Heart Rate: %02d bpm", heartRate);
+            heartmut.unlock();
             while(!Vreceived||!VSignal==0) {
                 updateHeart();
             }
             while(k<=heartInterval*1000);
             if(heartRate>high[mode]) {
                 sound_highAlarm();
+                heartmut.lock();
                 heartRate=0;
+                heartmut.unlock();
                 resetk();
             } else if(heartRate<low[mode]) {
                 sound_lowAlarm();
+                heartmut.lock();
                 heartRate=0;
+                heartmut.unlock();
                 resetk();
             } else if(heartRate  >= low[mode]  && heartRate  <= high[mode]) {
+                heartmut.lock();
                 heartRate=0;
+                heartmut.unlock();
                 resetk();
             }
 
@@ -470,7 +491,7 @@
 
 
 int main()
-{   
+{
     pc.baud(9600);
     VPace.rise(&VPacereceived);
     APace.rise(&APacereceived);
@@ -483,13 +504,15 @@
     Thread tmode(Testmode);
     testmode = &tmode;
 
-    while(1){
-    if(pc.readable()) {
-        key_input=pc.getc();                  //read uart buffer data and clear the interrupt flag
-        //initiate the serial thread to change the state of the timer
-        pc.printf("setting");
-        //(*testmode).signal_set(0x02);
-        flag_key=1;
-         pc.printf("set");
-    }}
+    while(1) {
+        if(pc.readable()) {
+            key_input=pc.getc();                  //read uart buffer data and clear the interrupt flag
+            //initiate the serial thread to change the state of the timer
+            pc.printf("setting");
+            flag_key=1;
+            if(key_input=='o'||key_input=='O'||key_input=='m'||key_input=='M'||key_input=='t'||key_input=='T')
+            flagr=0;
+            pc.printf("set");
+        }
+    }
 }
\ No newline at end of file