fork the master

Dependencies:   TextLCD mbed-rtos mbed

Fork of Pacemaker by pacemaker team

Revision:
3:34e9766539fe
Parent:
2:bb8dc0c2e25e
Child:
4:cce9946d9dcd
--- a/main.cpp	Sat Nov 29 19:29:10 2014 +0000
+++ b/main.cpp	Sat Nov 29 20:41:09 2014 +0000
@@ -5,19 +5,115 @@
  * 
  * 
  */
- 
+
 #include "mbed.h"
 #include "LPC17xx.h"
 #include "TextLCD.h"
 #include "rtos.h"
 
-int LRI = 500;
+#define AVI_h 100
+#define AVI_l 30
+#define PVARP_h 500
+#define PVARP_l 150
+
+#define VRP_h 500
+#define VRP_l 150
+#define BM(x) (1<<(x))
+#define PULSE_WIDTH_V 200
+#define PULSE_WIDTH_A 100
+
+int LRI_h = 1666;
+int LRI_l = 666;
+
 Serial pc(USBTX, USBRX);
 TextLCD myPanel(p15,p16,p17,p18,p19,p20,TextLCD::LCD16x2);
 char mode = 'N';
+
 int a_clock;
 int v_clock;
 
+InterruptIn atrial_int(p17);
+InterruptIn vent_int(p18);
+
+const int a_pace = 5; //pin 21  a pace output
+const int v_pace = 3; //pin 23  v pace output
+
+bool aSensed = 0; // 0 means that we are expecting Apace or Asense next
+DigitalOut myled(LED1);
+//DigitalOut testpin(p6);
+
+void initTimer();
+void startTimer();
+void initGPIO_outputs();
+void setGPIO(const int pinName);
+void clearGPIO(const int pinName);
+
+void asense();
+void vsense();
+
+void apace();
+void vpace();
+
+
+void apace()
+{
+    setGPIO(a_pace);
+    wait_us(PULSE_WIDTH_A);
+    clearGPIO(a_pace);    
+}
+
+void vpace()
+{
+    setGPIO(v_pace);
+    wait_us(PULSE_WIDTH_V);
+    clearGPIO(v_pace);  
+}
+
+void initGPIO_outputs()
+{
+    LPC_SC->PCONP |= 1<<15;
+    LPC_GPIO2->FIODIR |= 1<<5;
+    LPC_GPIO2->FIODIR |= 1<<3;
+    LPC_GPIO2->FIODIR &= ~(1<<1);
+    LPC_GPIO2->FIODIR &= ~(1<<2);
+}
+
+void setGPIO(const int pinName)
+{
+    LPC_GPIO2->FIOPIN |= (1<<pinName);
+}
+
+void clearGPIO(const int pinName)
+{
+    LPC_GPIO2->FIOPIN &= ~(1<<pinName);
+}
+
+void initTimer()
+{
+    // set up OS timer (timer0)
+    LPC_SC->PCONP |= BM(1); //power up timer0
+    LPC_SC->PCLKSEL0 |= BM(2); // clock = CCLK (96 MHz)
+    LPC_TIM0->PR = 48000; // set prescale to 48000 (2048 Hz timer) 
+    LPC_TIM0->MR0 = 1; // match0 compare value (32-bit)
+    LPC_TIM0->MCR |= BM(0)|BM(1); // interrupt and reset on match0 compare
+    NVIC_EnableIRQ(TIMER0_IRQn); // enable timer interrupt
+}
+
+void startTimer()
+{
+    LPC_TIM0->TCR |= BM(1); // reset timer1
+    LPC_TIM0->TCR &= ~BM(1); // release reset
+    LPC_TIM0->TCR |= BM(0); // start timer
+}
+
+void resetTimer()
+{
+    LPC_TIM0->TCR |= BM(1); // reset timer0
+    LPC_TIM0->TCR &= ~BM(1); // release reset
+}
+
+
+
 void display_thread_handler(void const *args)
 {    
     while(1)
@@ -25,22 +121,20 @@
 
     }
 }
-void apace(){
-    
-}
 
-void vpace() {
-    v_clock = 0;
-    
-    
-}
 
 void asense() {
-    
+    if (v_clock >= VRP_l) { //Ignore vSense outside this time interval
+        v_clock = 0;
+        aSensed = 0;
+     }
 }
 
 void vsense() {
-    
+        if ((v_clock >= PVARP_l) && aSensed == 0){ 
+        a_clock = 0;
+        aSensed = 1;
+    }
 }
 
 void button_handler(void const *args)
@@ -62,17 +156,79 @@
             apace();
         else if (buffer == 'V' && mode == 'M')
             vpace();
-        else if (buffer == 'O')
-            setobervation
-        else if (buffer == '\n')
+        //else if (buffer == 'O');
+            //setobervation
+        //else if (buffer == '\n');
         
-        else 
+        //else 
         
       }
     }
 }
 
-int main (void) {
+void pacemaker_thread_handler(void const *args)
+{
+    initGPIO_outputs();
+    
+    /**********************************************************
+    ************Initialize timer to interrupt every 1 ms*********
+    ***********************************************************/
+    
+    initTimer();
+    startTimer();
+    
+    atrial_int.rise(&asense);
+    vent_int.rise(&vsense);
+    while(1){}    
+    
+}
+
+ 
+/**********************************************************
+ ************ timer interrupt every 1 ms*********
+ ***********************************************************/
+    
+extern "C" void TIMER0_IRQHandler (void) {
+    if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt
+    {
+        LPC_TIM0->IR |= (1 << 0); // Clear MR0 interrupt flag
+       if (v_clock >= (LRI_h-AVI_l) && aSensed == 0) {
+            a_clock = 0;
+            aSensed = 1;  
+            //printf("Apace %d\r\n",v_clk);
+            apace();
+            /*
+            setGPIO(a_pace);
+            wait_us(PULSE_WIDTH_A);
+            clearGPIO(a_pace);
+            */
+        }
+        if ((a_clock >= AVI_h) && aSensed == 1) {
+            v_clock = 0;
+            aSensed = 0;
+            //printf("Vpace %d\r\n",a_clk);
+            /*
+            setGPIO(v_pace);
+            wait_us(PULSE_WIDTH_V);
+            clearGPIO(v_pace);
+            */         
+            vpace();
+        }
+        v_clock++;
+        a_clock++;
+    } 
+}
 
 
+int main (void) {
+    //TODO set parameters
+    myled = 0;
+
+    Thread display(display_thread_handler);
+    Thread keyboard(button_handler);
+    Thread pacemaker(pacemaker_thread_handler);
+
+    while(1){}
+    
+
 }
\ No newline at end of file