CIS441 Controller

Dependencies:   TextLCD mbed-rtos mbed

Fork of PacemakerController by Chad Nachiappan

Revision:
24:81cd9ef5c4f6
Parent:
20:dc272bfaa276
Child:
27:2c5aefcf3000
--- a/PacemakerController.cpp	Mon Nov 30 21:47:20 2015 +0000
+++ b/PacemakerController.cpp	Tue Dec 01 16:04:11 2015 +0000
@@ -47,8 +47,11 @@
 DigitalOut apace(p22);
 DigitalOut vpace(p21);
 
+bool vsense;
+bool asense;
+
 // hardware interrupt handler, adapted from code in piazza post by Dagaen
-    extern "C" void TIMER0_IRQHandler (void)
+extern "C" void TIMER0_IRQHandler (void)
 {
     if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed
         LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
@@ -149,11 +152,12 @@
     while(1) {
 
         if (timer_count >= VRP && vget == 1) {
-             // Valid_V state
-             
+            // Valid_V state
+
             timer_count = 0;
+            vsense();
             // do something with Vsense!
-            
+
         } else if (timer_count < VRP  && vget == 1) {
             // Invalid_V state
         }
@@ -162,7 +166,7 @@
             // Invalid_A state
         } else if (timer_count >= PVARP && aget == 1) {
             // Valid_A state
-            
+            asense();
             // do something with Asense!
         }
 
@@ -171,10 +175,10 @@
 
 void pm_response()
 {
-
     while(1) {
+        bool goInitalState = true;
         if (timer_count >= LRI-AVI) {
-
+            goInitialState = false;
             // PM_A! sets the LED high
             led_apace = 1;
 
@@ -183,26 +187,65 @@
 
             apace();
             // At Atrial Event State
-            while (avi_clk.read() < AVI);
+            while (avi_clk.read() < AVI) {
+                if (vsense == 1) {
+                    goInitialState = true;
+                    break;
+                }
+            }
+            if (!goInitialState) {
+                // Ventricular Event
+                timer_count = 0;
 
-            // Ventricular Event
-            timer_count = 0;
-
-            // PM_V! sets the LED high
-            led_vpace = 1;
+                // PM_V! sets the LED high
+                led_vpace = 1;
 
-            timer_count = 0;
-            vpace();
+                timer_count = 0;
+                vpace();
+            }
         } else if (timer_count < LRI - AVI) {
+            // if Asense, move on to atrial event
+            if (asense == 1) {
+                goInitialState = false;
+                // avi_clk = 0
+
+                avi_clk.reset();
 
-            // avi_clk = 0
-            avi_clk.reset();
+                // At Atrial Event State
+                while (avi_clk.read() < AVI) {
+                    if (vsense == 1) {
+                        goInitialState = true;
+                        break;
+                    }
+                }
 
-            // wait for Asense? naaah
+                // Ventricular Event
+                timer_count = 0;
+
+                // PM_V! sets the LED high
+                led_vpace = 1;
+
+                timer_count = 0;
+                vpace();
+            }
         }
     }
 }
 
+void asense()
+{
+    asense = 1;
+    Thread::wait(10);
+    asense = 0;
+}
+
+void vpace()
+{
+    vsense = 1;
+    Thread::wait(10);
+    vsense = 0;
+}
+
 void apace()
 {