Part of the Pacemaker Project; this models the Pacemaker.

Dependencies:   mbed TextLCD mbed-rtos

Revision:
46:2d9cf50b4bc3
Parent:
45:ec9e76ccec6c
Child:
47:bac7fc1a44fe
--- a/PacemakerController.cpp	Wed Dec 02 07:23:57 2015 +0000
+++ b/PacemakerController.cpp	Wed Dec 02 07:50:33 2015 +0000
@@ -10,6 +10,10 @@
 Serial pc(USBTX, USBRX);
 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7
 
+Ticker rate_monitor;
+// counters
+volatile int beats = 0;
+volatile double bpm = 0;
 int keyboard_needs_numeric = 0; // boolean - is in middle of interval input?
 
 int h_clock;
@@ -28,9 +32,6 @@
 int MIN_PM_RT = 40;
 enum mode {NORMAL, SLEEP, EXERCISE, MANUAL};
 
-// counters
-int beats = 0;
-
 // state variables
 int upper_bound = 100;
 int lower_bound = 40;
@@ -115,19 +116,23 @@
     while (1) {
         // min hr alarm
         if( beats < MIN_PM_RT) {
-            lcd.cls();
             lcd.locate(0,1);
-            lcd.printf("!<\n");
+            lcd.printf("!<");
         }
 
         // max hr alarm
-        if(beats > MAX_PM_RT) {
-            lcd.cls();
+        else if(beats > MAX_PM_RT) {
+            lcd.locate(0,1);
+            lcd.printf("!>");
+        } else {
             lcd.locate(0,1);
-            lcd.printf("!>\n");
+            lcd.printf("  ");
+           
         }
+        
+        lcd.locate(0,0);
+        lcd.printf("BPM: %.1f   ", bpm);
     }
-
 }
 
 // hw interrupt callback, deal with the keyboard input from PC
@@ -175,6 +180,7 @@
             while (avi_clk.read_ms() < AVI) {
 
                 if (v_sense == 1) {
+                    beats++;
                     //sensed valid ventricular event
                     goInitialState = 1;
                     pulse_clk.reset();
@@ -187,7 +193,7 @@
                 sense_clk.reset();
                 
                 // PM_V! sets the LED high
-                pc.printf("in pmresponse 2");
+                beats++;
                 vpace();
             }
         } else if (pulse_clk.read_ms() < LRI - AVI) {
@@ -201,6 +207,7 @@
                 // At Atrial Event State
                 while (avi_clk.read() < AVI) {
                     if (v_sense == 1) {
+                        beats++;
                         pulse_clk.reset();
                         goInitialState = 1;
                         break;
@@ -208,6 +215,7 @@
                 }
                 if (!goInitialState) {
                     // Ventricular Event
+                    beats++;
                     sense_clk.reset();
                     pulse_clk.reset();
     
@@ -218,6 +226,15 @@
     }
 }
 
+/* Every observation interval, calculate beats per minute and display
+ *
+ */
+void update_display() {
+    bpm = beats / (double) obs_int * 60;
+    //reset count
+    beats = 0;
+}
+
 int main()
 {
     // https://developer.mbed.org/users/chadnach1/code/PacemakerController/
@@ -236,7 +253,10 @@
     Thread t3(PM_ALARM, (void *)"");
     sense_clk.start();
     pulse_clk.start();
-
+    
+    //update_display
+    rate_monitor.attach(&update_display, obs_int);
+    
     while(1) {
         if (pc.readable()) {
     
@@ -248,10 +268,12 @@
                     // update observation interval
                     obs_int = (a - '0' + 1) * 5;
                     keyboard_needs_numeric = 0;
+                    rate_monitor.attach(&update_display, obs_int);
+                    pc.printf("Set observation interval to %d seconds\n", obs_int);
                 } else {
                     pc.printf("Expected numeric key\n");
                 }
-            } else if(a == 'N') {
+            }  else if(a == 'N') {
                 // if the char is N, update bounds to normal mode
                 curr_mode = NORMAL;
                 upper_bound = 100;