heart modes, send, receive

Dependencies:   Terminal TextLCD mbed-rtos mbed

Fork of Heart by CIS541

Revision:
3:b702a21b2990
Parent:
2:fc2f41386ee4
Child:
4:a64a0fea5266
--- a/heart.cpp	Mon Nov 30 21:00:10 2015 +0000
+++ b/heart.cpp	Tue Dec 01 22:57:12 2015 +0000
@@ -20,22 +20,26 @@
 const int minwait_A=50;
 const int minwait_V=50;
 
-volatile int time_count=0;
-volatile int time1_count;
+volatile int t=0;
+volatile int k=0;
 int m=0;
 int s=0;
 int ms=0;
 int heartInterval;
 char key_input;
+int testNumber;
+bool testResult;
 
 int LRI=1500;
 int URI=600;
 int heartRate;
 volatile bool Areceived=false;
 volatile bool Vreceived=false;
+bool test=false;
 int low[4] = {30,40,100,30};
 int high[4] = {60,100,175,175};
 int modeset;
+int pulses;
 
 const int sleepModeURI = 1000;
 const int sleepModeLRI = 2000;
@@ -67,8 +71,8 @@
 Thread *heartkeyboard;
 
 
-void resetTimer0();
-void resetTimer1();
+void resett();
+void resetk();
 void VPacereceived();
 void APacereceived();
 void ASignalsend();
@@ -76,96 +80,101 @@
 void HeartReceive();
 void HeartSend();
 void HeartKeyBoardModeSwitch();
+void sound_lowAlarm();
+void sound_highAlarm();
 
 
 extern "C" void TIMER0_IRQHandler (void)
 {
-    if((LPC_TIM0->IR & 0x01) == 0x01) { // if interrupt provided, continue to next line
+    if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed
         LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
-        time_count++;                   //increment time_count
-    }
-}
-
-void timer0_init(void)
-{
-    LPC_SC->PCONP |=1<1;            //power on the timer
-    LPC_TIM0->MR0 = 23980;        //10 msec period i.e the timer count will increment every 10ms
-    LPC_TIM0->MCR = 3;              //reset control
-    //3 = Interrupt & reset timer0 on match
-    //1 = Interrupt only, no reset
-    NVIC_EnableIRQ(TIMER0_IRQn);    //enable interrupt
-    LPC_TIM0->TCR = 1;              //enable the timer
-
-}
-
-extern "C" void TIMER1_IRQHandler (void)
-{
-    if((LPC_TIM0->IR & 0x01) == 0x01) { // if interrupt provided, continue to next line
-        LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
-        time1_count++;                   //increment time_count
+        t++;
+        k++;
     }
 }
 
-void timer1_init(void)
+//Initializing the timer 0
+void timer0_init(void)
 {
-    LPC_SC->PCONP |=1<1;            //power on the timer
-    LPC_TIM0->MR0 = 23980;        //10 msec period i.e the timer count will increment every 10ms
-    LPC_TIM0->MCR = 3;              //reset control
-    //3 = Interrupt & reset timer0 on match
-    //1 = Interrupt only, no reset
-    NVIC_EnableIRQ(TIMER0_IRQn);    //enable interrupt
-    LPC_TIM0->TCR = 1;              //enable the timer
 
+    LPC_SC->PCONP |=1<1;            //Timer0 Power On
+    LPC_TIM0->MR0 = 23990;         //1 msec
+    LPC_TIM0->MCR = 3;              //interrupt and reset control
+    NVIC_EnableIRQ(TIMER1_IRQn);    //Enable timer0 interrupt
+    LPC_TIM0->TCR = 1;              //enable Timer0
+    t = 0;
+    k = 0;
+}
+
+void resett()
+{
+    t=0;
+}
+
+void resetk()
+{
+    k=0;
 }
 
-void resetTimer0()
-{
-    m=0;
-    s=0;
-    ms=0;
-    time_count=0;
-    LPC_TIM0->TCR = 0;
-    lcd.locate(0,0);
-    lcd.printf("%02d:%02d:%02d", m,s,ms);
-}
-
-void resetTimer1()
+void sound_lowAlarm(){
+    pc.printf("lowalarm");}
+void sound_highAlarm(){
+    pc.printf("highalarm");}
+void VPacereceived()
 {
-    m=0;
-    s=0;
-    ms=0;
-    LPC_TIM1->TCR = 0;
-    lcd.locate(0,0);
-    time1_count=0;
-    
-}
-
-void VPacereceived()
-{   pc.printf("VPace aaya");
+    pc.printf("VPace received");
+    VPaceReceive=1;
+    wait(0.002);
+    APaceReceive=0;
+    pulses++;
     Vreceived=true;
-    VPaceReceive=1;
-    APaceReceive=0;
+    Areceived=false;
+    if(mode == Test) {
+        //(*HeartTxPtr).signal_set(0x04);
+    }
+    if(mode == Test && test) {
+        if(testNumber == 2 || testNumber == 0 || testNumber == 3 || testNumber == 1) {
+            testResult = false;
+        } else if(testNumber == 6 || testNumber == 4) {
+            testResult = true;
+        }
+    }
 }
 
 void APacereceived()
-{   pc.printf("APace aaya");
+{
+    pc.printf("APace received");
+    APaceReceive=1;
+    wait(0.002);
+    APaceReceive=0;
     Areceived=true;
-    VPaceReceive=0;
-    APaceReceive=1;
+    if(mode == Test && test) {
+        if(testNumber == 0) {
+            testResult = false;
+        } else if(testNumber == 4 || testNumber == 7 || testNumber == 3 || testNumber == 1) {
+            testResult = true;
+        }
+    }
 }
 
 void ASignalsend()
 {
     ASignal=1;
     ASignal1=1;
-    VSignal1=0;
+    wait(0.02);
+    Thread::wait(1);
+    ASignal1=0;
+    ASignal=0;
 }
 
 void VSignalsend()
 {
     VSignal=1;
     VSignal1=1;
-    ASignal1=0;
+    wait(0.02);
+    Thread::wait(1);
+    VSignal1=0;
+    VSignal==0;
 }
 
 void updateHeart()
@@ -179,10 +188,10 @@
         pc.printf("HR");
         while(Areceived==false||Vreceived==false);
         if(Vreceived==true) {
-            resetTimer0();
-            
+            resett();
+
         } else if(Areceived==true) {
-            resetTimer0();
+            resett();
         }
     }
 }
@@ -192,16 +201,16 @@
     while(!pc.readable()) {
         int r = rand() % 2;
         pc.printf("HS");
-        while(time_count<minwait_V && mode!=Random && (mode==Test || mode==Manual));
-        pc.printf("%u",time_count);
-        if(r==0) { 
+        while(t<minwait_V && mode!=Random && (mode==Test || mode==Manual));
+        pc.printf("%u",t);
+        if(r==0) {
             VSignalsend();
             pc.printf("Vsignal");
-            resetTimer0();
+            resett();
         } else if(r==1) {
             ASignalsend();
             pc.printf("ASignal");
-            resetTimer0();
+            resett();
         }
     }
 }
@@ -224,9 +233,11 @@
                 pc.printf("Manual");
             } else if(mode==Manual&&(key_input=='v'||key_input=='V')) {
                 mode=Manual;
+                VSignalsend();
                 pc.printf("Ventricular");
             } else if(mode==Manual&&(key_input=='a'||key_input=='A')) {
                 mode=Manual;
+                ASignalsend();
                 pc.printf("Atrial");
             } else if(key_input=='o'||key_input=='O') {
                 int a = 10 + rand() % 80;
@@ -234,22 +245,64 @@
                 pc.printf("Observer and %u",a);
                 heartInterval =a;
                 HeartSend("s");
+            } else {
+                pc.printf("Wrong key, sorry");
             }
         }
     }
 }
 
+void display(void const* args)
+{
+    while(1) {
+        Thread::wait(observationInterval/1000);
+//        if(mode!=Test) {
+//            if(mode!= changeobservationinterval) {
+//                avgHeartRate=pulseCount*60000/(obsInterval);
+//                pulseCount = 0;
+//                updateDisplay();
+//
+//            }
+//        }
+        if(!Test) {
+            pc.printf("Avg Heart Rate: %02d bpm", heartRate);
+            while(!Vreceived||!VSignal==0) {
+                updateHeart();
+            }
+            while(k<=heartInterval*1000);
+            if(heartRate>high[mode]) {
+                sound_highAlarm();
+                heartRate=0;
+                resetk();
+            } else if(heartRate<low[mode]) {
+                sound_lowAlarm();
+                heartRate=0;
+                resetk();
+            } else if(heartRate  >= low[mode]  && heartRate  <= high[mode]) {
+                heartRate=0;
+                resetk();
+            }
+
+
+        }
+    }
+}
+
 
-        int main()
+void testmode(void const* args){
+    }
+
+
+int main()
 {
     pc.baud(9600);
     VPace.rise(&VPacereceived);
     APace.rise(&APacereceived);
     timer0_init();
-    timer1_init();
-    heartreceive=new Thread(HeartReceive);
-    heartsend=new Thread(HeartSend);
-    heartkeyboard=new Thread(HeartKeyBoardModeSwitch);
+    Thread heartreceive(HeartReceive);
+    Thread heartsend(HeartSend);
+    Thread heartkeyboard(HeartKeyBoardModeSwitch);
+    Thread disPlay(display);
 
     while(1) {
     }