heart modes, send, receive

Dependencies:   Terminal TextLCD mbed-rtos mbed

Fork of Heart by CIS541

Revision:
4:a64a0fea5266
Parent:
3:b702a21b2990
Child:
5:8e3b9032ddfc
diff -r b702a21b2990 -r a64a0fea5266 heart.cpp
--- a/heart.cpp	Tue Dec 01 22:57:12 2015 +0000
+++ b/heart.cpp	Wed Dec 02 01:11:08 2015 +0000
@@ -28,7 +28,9 @@
 int heartInterval;
 char key_input;
 int testNumber;
-bool testResult;
+bool test_result;
+const int prop_delay=15;
+int flag_key=0;
 
 int LRI=1500;
 int URI=600;
@@ -40,6 +42,14 @@
 int high[4] = {60,100,175,175};
 int modeset;
 int pulses;
+//int test;
+int flag;
+int delta=15;
+int observationInterval=10000;
+const int vrp=100;
+const int pvarp=150;
+const int avi=65;
+const int pvab=10;
 
 const int sleepModeURI = 1000;
 const int sleepModeLRI = 2000;
@@ -53,6 +63,9 @@
 const int manualModeURI = 343;
 const int manualModeLRI = 2000;
 
+int lri=normalModeLRI;
+int uri=normalModeURI;
+
 typedef enum Modes {
     Test,
     Normal,
@@ -69,6 +82,8 @@
 Thread *heartreceive;
 Thread *heartsend;
 Thread *heartkeyboard;
+Thread *testmode;
+Thread *hmode;
 
 
 void resett();
@@ -82,6 +97,7 @@
 void HeartKeyBoardModeSwitch();
 void sound_lowAlarm();
 void sound_highAlarm();
+void Testmode();
 
 
 extern "C" void TIMER0_IRQHandler (void)
@@ -116,10 +132,14 @@
     k=0;
 }
 
-void sound_lowAlarm(){
-    pc.printf("lowalarm");}
-void sound_highAlarm(){
-    pc.printf("highalarm");}
+void sound_lowAlarm()
+{
+    pc.printf("lowalarm");
+}
+void sound_highAlarm()
+{
+    pc.printf("highalarm");
+}
 void VPacereceived()
 {
     pc.printf("VPace received");
@@ -129,14 +149,14 @@
     pulses++;
     Vreceived=true;
     Areceived=false;
-    if(mode == Test) {
-        //(*HeartTxPtr).signal_set(0x04);
+    if(mode == Test && flag==0) {
+        (*testmode).signal_set(0x04);
     }
     if(mode == Test && test) {
         if(testNumber == 2 || testNumber == 0 || testNumber == 3 || testNumber == 1) {
-            testResult = false;
+            test_result = false;
         } else if(testNumber == 6 || testNumber == 4) {
-            testResult = true;
+            test_result = true;
         }
     }
 }
@@ -150,9 +170,9 @@
     Areceived=true;
     if(mode == Test && test) {
         if(testNumber == 0) {
-            testResult = false;
+            test_result = false;
         } else if(testNumber == 4 || testNumber == 7 || testNumber == 3 || testNumber == 1) {
-            testResult = true;
+            test_result = true;
         }
     }
 }
@@ -203,11 +223,11 @@
         pc.printf("HS");
         while(t<minwait_V && mode!=Random && (mode==Test || mode==Manual));
         pc.printf("%u",t);
-        if(r==0) {
+        if(r==0 && mode!=Test) {
             VSignalsend();
             pc.printf("Vsignal");
             resett();
-        } else if(r==1) {
+        } else if(r==1&&mode!=Test) {
             ASignalsend();
             pc.printf("ASignal");
             resett();
@@ -217,40 +237,54 @@
 void HeartKeyBoardModeSwitch(void const* args)
 {
     while(1) {
-        if(pc.readable()) {
+       // if(pc.readable()) {
 
-            key_input = pc.getc();
+            //key_input = pc.getc();
 
             if(key_input=='r'||key_input=='R') {
                 mode=Random;
                 pc.printf("Random");
                 HeartSend("s");
+                flag_key=0;
             } else if(key_input=='t'||key_input=='T') {
                 mode=Test;
                 pc.printf("Test");
+                flag_key=0;
             } else if(key_input=='m'||key_input=='M') {
                 mode=Manual;
                 pc.printf("Manual");
+                flag_key=0;
             } else if(mode==Manual&&(key_input=='v'||key_input=='V')) {
                 mode=Manual;
                 VSignalsend();
                 pc.printf("Ventricular");
+                flag_key=0;
             } else if(mode==Manual&&(key_input=='a'||key_input=='A')) {
                 mode=Manual;
                 ASignalsend();
                 pc.printf("Atrial");
+                flag_key=0;
             } else if(key_input=='o'||key_input=='O') {
                 int a = 10 + rand() % 80;
                 mode=Observer;
                 pc.printf("Observer and %u",a);
                 heartInterval =a;
                 HeartSend("s");
-            } else {
-                pc.printf("Wrong key, sorry");
+                flag_key=0;
+
+            } else if(key_input=='t'||key_input=='A') {
+                mode=Manual;
+                (*testmode).signal_set(0x03);
+                pc.printf("Atrial");
+                flag_key=0;
             }
+            //else {
+//                pc.printf("Wrong key, sorry");
+//                fla
+//            }
         }
     }
-}
+
 
 void display(void const* args)
 {
@@ -289,8 +323,169 @@
 }
 
 
-void testmode(void const* args){
+void Testmode(void const* args)
+{
+    Thread::wait(0x03);
+    test = true;
+    switch(test) {
+        case 1:
+        flag=0;
+            Thread::signal_wait(0x04); 
+            flag=1;
+            t = 0;
+            Thread::wait(vrp);
+            ASignalsend();
+            test = true;
+            Thread::wait(lri-avi-vrp);
+            test_result=false;
+            if(test_result) {
+                pc.printf("Test %u passed",test);
+                test=2;
+            } else {
+                pc.printf("Test %u failed",test);
+                test=2;
+            }
+            break;
+            
+            case 2:
+            flag=0;
+            Thread::signal_wait(0x04); //Send A after PVARP
+            flag=1;
+            t = 0;
+            Thread::wait(pvarp);
+            ASignalsend();
+            
+            Thread::wait(uri-pvarp-delta);
+            
+            if(test_result) {
+                pc.printf("Test %u passed", &test_result);
+                test=3;
+            } else {
+                pc.printf("Test %u failed", &test_result);
+                test=3;
+            }
+            break;
+
+        case 3:
+            flag=0;
+            Thread::signal_wait(0x04);
+            flag=1;
+            t = 0;
+            Thread::wait(uri);
+            for(int i = 0; i<7; i++) {
+                ASignalsend();
+                Thread::wait(avi-delta);
+                VSignalsend();
+                Thread::wait(lri-avi-delta);
+            }
+            if(test_result) {
+                pc.printf("Test %u passed", &test_result);
+                test=4;
+            } else {
+                pc.printf("Test %u failed", &test_result);
+                test=4;
+            }
+            break;
+        case 4:
+            flag=0;
+            Thread::signal_wait(0x04); //Send A after PVARP
+            flag=1;
+            t = 0;
+            Thread::wait(pvarp);
+            ASignalsend();
+            
+            Thread::wait(uri-pvarp-delta);
+            
+            if(test_result) {
+                pc.printf("Test %u passed", &test_result);
+                test=5;
+            } else {
+                pc.printf("Test %u failed", &test_result);
+                test=5;
+            }
+            break;
+            case 5:
+            flag=0;
+            Thread::signal_wait(0x04); //Heart is dead
+            flag=1;
+            t = 0;
+            
+            Thread::wait(lri);
+            Thread::wait(lri);
+            test = false;
+            if(test_result) {
+                pc.printf("Test %u passed", &test_result);
+                test=6;
+            } else {
+                pc.printf("Test %u failed", &test_result);
+                test=6;
+            }
+            break;
+            
+            case 6:
+            flag=0;
+            Thread::signal_wait(0x04); //Heart beating very fast. ASig, VSig
+            flag=1;
+            t = 0;
+            for(int i = 0; i<50; i++) {
+                ASignalsend();
+                VSignalsend();
+            }
+            if(test_result) {
+                pc.printf("Test %u passed", &test_result);
+                test=7;
+            } else {
+                pc.printf("Test %u failed", &test_result);
+                test=7;
+            }            
+            break;
+            
+            case 7:
+            flag=0;
+            Thread::signal_wait(0x04); //Wait for VPace to be received
+            flag=1;
+            t= 0;
+            Thread::wait(uri);
+            ASignalsend();
+            Thread::wait(avi+delta);
+            if(test_result) {
+                pc.printf("Test %u passed", &test_result);
+                test=8;
+            } else {
+                pc.printf("Test %u failed", &test_result);
+                test=8;
+            }            
+        
+        break;
+            
+            case 8:
+            flag=0;
+            Thread::signal_wait(0x04); //Send an extra V to see if LRI starts then
+            flag=1;
+            t = 0;
+            Thread::wait(pvarp);
+            VSignalsend();
+            Thread::wait(lri-avi-delta);
+            if(test_result) {
+                pc.printf("Test %u passed", &test_result);
+                test=0;
+            } else {
+                pc.printf("Test %u failed", &test_result);
+                test=0;
+            }       
+            break;
+
     }
+}
+
+void Rx_interrupt()
+{
+    if(pc.readable())
+    {
+        key_input=LPC_UART0->RBR;                  //read uart buffer data and clear the interrupt flag
+        flag_key=1;    //initiate the serial thread to change the state of the timer
+    }
+}
 
 
 int main()
@@ -302,7 +497,10 @@
     Thread heartreceive(HeartReceive);
     Thread heartsend(HeartSend);
     Thread heartkeyboard(HeartKeyBoardModeSwitch);
+    hmode = &heartkeyboard;
     Thread disPlay(display);
+    Thread tmode(Testmode);
+    testmode = &tmode;
 
     while(1) {
     }