CIS441 Controller

Dependencies:   TextLCD mbed-rtos mbed

Fork of PacemakerController by Chad Nachiappan

Revision:
2:5e9c4d83d038
Parent:
1:979e9e785549
Child:
3:44d132582373
--- a/PacemakerController.cpp	Tue Nov 24 23:19:34 2015 +0000
+++ b/PacemakerController.cpp	Sat Nov 28 19:34:39 2015 +0000
@@ -1,34 +1,142 @@
 #include "mbed.h"
+#include "LPC17xx.h"
+#include "TextLCD.h"
+#include "rtos.h"
+#include "Thread.h"
+using namespace rtos;
 
+ 
 // This is for the pacemaker
-TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2);
+volatile unsigned short timer_count;
+Serial pc(USBTX, USBRX);
+TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7
+
 
 int h_clock;
 
-void A_Pace() {
-    
+
+// constants
+int MAX_PM_RT = 180;
+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;
+int obs_int = 10;
+mode curr_mode = NORMAL;
+
+
+
+// alarms 
+DigitalOut Apace(LED1);
+DigitalOut Vpace(LED2);
+DigitalOut Asense(LED3);
+DigitalOut Vsense(LED4);
+
+// hardware interrupt handler, adapted from code in piazza post by Dagaen
+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
+    timer_count++;                  //increment timer_count
+    }
+}
+ 
+// init the hardware interrupt (timer0), adapted same as above
+void timer0_init(void)
+{
+    LPC_SC->PCONP |=1<1;            //timer0 power on
+    LPC_SC-> PCLKSEL0 |= 1 << 2;    // set timer clock to CCLCK nondivided (1 clock cycle = 1 increment)
+    LPC_TIM0->MR0 = 1000000;          //100mhz clock cycle, 1 cycle = 10ns, 10ms = 10 000 000 ns = 1M cycles
+    LPC_TIM0->MCR = 3;              //interrupt and reset control
+                                    //3 = Interrupt & reset timer0 on match (111) sets all three bits
+    NVIC_EnableIRQ(TIMER0_IRQn);    //enable timer0 interrupt
 }
 
-void A_Pace() {
+
+void PM_ALARM(void const *argument){
+    
+    // min hr alarm
+    if( beats < MIN_PM_RT){
+        lcd.locate(0,1);
+        lcd.printf("!<"); 
+    }
+      
+    // max hr alarm
+    if(beats > MAX_PM_RT){
+        lcd.locate(0,1);
+        lcd.printf("!>"); 
+    }  
     
 }
 
-void timer0_init(void)
-{   
-    LPC_SC->PCONP |=1<1;            //timer0 power on
-    LPC_TIM0->MR0 = 239800;        //10 msec
-    LPC_TIM0->MCR = 3;              //interrupt and reset control
-                                    //3 = Interrupt & reset timer0 on match
-                                    //1 = Interrupt only, no reset of timer0
-    NVIC_EnableIRQ(TIMER0_IRQn);    //enable timer0 interrupt
-    LPC_TIM0->TCR = 1;              //enable Timer0
+// hw interrupt callback, deal with the keyboard input from PC
+void MODE_SWITCH() {
+    
+    // get the char, put it on the PC command line
+    char a = pc.getc();
+
+    // if the char is N, update bounds to normal mode
+    if(a == 'N'){
+        curr_mode = NORMAL;
+        upper_bound = 100;
+        lower_bound = 40;
+        pc.printf("MODE IS N\n");  
+    // if the char is S, set bounds to sleep
+    }else if (a == 'S'){
+        curr_mode = SLEEP;
+        upper_bound = 60;
+        lower_bound = 30;
+        pc.printf("MODE IS S\n");  
+    // if the char is E, set bounds to exercise
+    }else if (a == 'E'){
+        curr_mode = EXERCISE;
+        upper_bound = 175;
+        lower_bound = 100;
+        pc.printf("MODE IS E\n");  
+        beats = 2;
+    // if the char is M, set to manual
+    }else if (a == 'M'){
+        curr_mode = MANUAL;
+        upper_bound = 175;
+        lower_bound = 30;
+        beats = 300;
+        pc.printf("MODE IS MANUAL\n");  
+    // check for A if mode is manual
+    }else if (a == 'A'){
+        if(curr_mode == MANUAL){
+            pc.printf("MODE IS MANUAL GOT APACE\n");  
+        }
+    // check for V is mode is manual  
+    }else if (a == 'V'){
+        if(curr_mode == MANUAL){
+            pc.printf("MODE IS MANUAL GOT VPACE\n");   
+        }  
+    }else{
+        // do nothing for invalid char
+    }
+    
+    
+}
+
+
+
+void apace() {
+    
+}
+
+void vpace() {
+    
 }
 
 int main() {
-    while(1) {
-        myled = 1;
-        wait(0.2);
-        myled = 0;
-        wait(0.2);
-    }
+    // connect the serial device (PC keybd) to the interrupt
+    pc.attach(&MODE_SWITCH);
+    
+    //Thread t3(PM_ALARM);
 }