yay!

Dependencies:   TextLCD mbed-rtos mbed

Fork of 541_Pacermaker by CIS541

Revision:
1:d1c452f164d4
Parent:
0:6c085ebcb2d5
Child:
2:3d47bb081502
--- a/main.cpp	Tue Dec 01 22:27:40 2015 +0000
+++ b/main.cpp	Wed Dec 02 00:10:55 2015 +0000
@@ -2,19 +2,22 @@
 #include "rtos.h"
 //#include "TextLCD.h"
 
-#define LRI 0
-#define AVI 1
-#define VRP 2
-#define PVARP 3
-#define URI 4
-#define PAVB 5
-#define VSP 6
-#define NR -1               //not running 
+#define NORMAL 1
+#define EXERCISE 2
+#define SLEEP 3
+#define MANUAL 4
 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
+#define LRI_const 1
+#define URI_const 2
+#define AVI_const 3
+#define VRP_const 4
+#define PVAB_const 5
+#define PVARP_const 6  
+
+DigitalOut vpaceLED(LED1);
+DigitalOut apaceLED(LED2);
+DigitalOut vsenseLED(LED3);
+DigitalOut asenseLED(LED4);
 DigitalOut buzzer(p9);
 DigitalOut aPace(p7);
 DigitalOut vPace(p8);
@@ -22,16 +25,46 @@
 InterruptIn VSignal(p6);
 Serial pc(USBTX, USBRX);
 
-bool expectingASignal, expectingVSignal, paceA, observationChange, digitOneReceived, modeChanged, canPaceV, paceVPending, ringAlarm, ringingAlarm, timerRunning, aSenseOccurred, digitTwoReceived;
-int timeOutValue[7];              //timeout array that holds values for LRI, VRP, PVARP, AVI, URI, PAVB, VSP; PVARP >VRP
-int heartRate, observationInterval, observationRate, timeOutStatus, waitCount, avgHeartRate, rateCoefficient, heartRateHeart, sec, avgHeartRateHeart;
+int32_t signal1 = 0x01;
+int32_t signal2 = 0x02;
+int32_t signal3 = 0x03;
+int32_t signal4 = 0x04;
+
+bool waitASignal, waitVSignal, paceA, observationChange, digitOneReceived, modeChanged, canPaceV, paceVPending, ringAlarm, ringingAlarm, timerRunning, aSenseOccurred, digitTwoReceived;
+int heartRate, observationInterval, observationRate, waitCount, avgHeartRate, rateCoefficient, heartRateHeart, sec, avgHeartRateHeart;
 int paceMakerMode=1;            //1 - Normal, 2 - Exercise, 3 - Sleep, 4 - Manual
-int uriTimeOutStatus=URI;
+int uriTimeOutStatus=4;
 char ch;
-char modeString[20];
-const int nLRI=1500, nAVI = 60, nPVARP = 150, nURI = 600, nVRP = 100, nVSP = 0, nPAVB = 20;
-const int sLRI=2000, sAVI = 60, sPVARP = 150, sURI = 1000, sVRP = 100, sVSP = 0, sPAVB = 20;
-const int eLRI=1000, eAVI = 60, ePVARP = 150, eURI = 400, eVRP = 100, eVSP = 0, ePAVB = 20;
+
+int timeConstraint;
+
+const int normalModeLRI= 1500;
+const int normalModeAVI = 65;
+const int normalModePVARP = 150;
+const int normalModeURI = 600;
+const int normalModeVRP = 100;
+const int normalModePVAB = 10;
+
+const int sleepModeLRI= 2000;
+const int sleepModeAVI = 65;
+const int sleepModePVARP = 150;
+const int sleepModeURI = 1000;
+const int sleepModeVRP = 100;
+const int sleepModePVAB = 10;
+
+const int exerciseModeLRI=1000;
+const int exerciseModeAVI = 65;
+const int exerciseModePVARP = 150;
+const int exerciseModeURI = 400;
+const int exerciseModeVRP = 100;
+const int exerciseModePVAB = 10;
+
+int LRI;
+int VRP;
+int PVARP;
+int AVI;
+int URI;
+int PVAB;
 
 Mutex displayMutex;
 Mutex observationChangeMutex;
@@ -50,24 +83,44 @@
 RtosTimer *KeyTimeOutTimer;
 RtosTimer *SecondsTimer;
 
-void setTimeOutValues(int tLRI, int tAVI, int tPVARP, int tURI, int tVRP, int tVSP, int tPAVB)
-{
-    timeOutValue[LRI]=tLRI;
-    timeOutValue[AVI]=tAVI;
-    timeOutValue[PVARP]=tPVARP;
-    timeOutValue[URI]=tURI;
-    timeOutValue[VRP]=tVRP;
-    timeOutValue[VSP]=tVSP;
-    timeOutValue[PAVB]=tPAVB;
+void println(const char *c) {
+    pc.printf(c);
+    pc.printf("\r\n");
+}
+
+void switchToNormal() {
+    LRI = normalModeLRI;
+    AVI = normalModeAVI;
+    PVARP = normalModePVARP;
+    URI = normalModeURI;
+    VRP = normalModeVRP;
+    PVAB = normalModePVAB;
+}
+
+void switchToExercise() {
+    LRI = exerciseModeLRI;
+    AVI = exerciseModeAVI;
+    PVARP = exerciseModePVARP;
+    URI = exerciseModeURI;
+    VRP = exerciseModeVRP;
+    PVAB = exerciseModePVAB;
+}
+
+void switchToSleep() {
+    LRI = sleepModeLRI;
+    AVI = sleepModeAVI;
+    PVARP = sleepModePVARP;
+    URI = sleepModeURI;
+    VRP = sleepModeVRP;
+    PVAB = sleepModePVAB;
 }
 
 void resetDisplay()
 {
     displayMutex.lock();
-    pc.printf("Pace Maker Display\r\n");
+    println("Pace Maker Display");
     pc.printf("Heart Rate :  %04d bpm\r\n", avgHeartRate);
     pc.printf("Observation Interval :  %02d seconds\r\n", observationInterval/1000);
-    pc.printf("Mode :  %s\r\n", modeString);
     pc.printf("Heart Beat Rate :  %04d  bpm\r\n", (heartRateHeart*(60/sec)));    
     displayMutex.unlock();
     ringingAlarm=false;
@@ -76,9 +129,9 @@
 void updateDisplay()
 {
     displayMutex.lock();
-    pc.printf("%04d", avgHeartRate);
-    pc.printf("%02d", observationInterval/1000);
-    pc.printf("%04d", (heartRateHeart*(60/sec))); 
+    pc.printf("%04d\r\n", avgHeartRate);
+    pc.printf("%02d\r\n", observationInterval/1000);
+    pc.printf("%04d\r\n", (heartRateHeart*(60/sec))); 
     displayMutex.unlock();
 }   
 
@@ -86,33 +139,25 @@
 {
     switch(paceMakerMode)
     {
-        case 1:             //Normal Mode
+        default:
+        case NORMAL:
         {
-            setTimeOutValues(nLRI, nAVI, nPVARP, nURI, nVRP, nVSP, nPAVB);
-            strcpy(modeString, "Normal");
-            break;
-        }
-        case 2:             //Exercise Mode
-        {
-            setTimeOutValues(eLRI, eAVI, ePVARP, eURI, eVRP, eVSP, ePAVB);
-            strcpy(modeString, "Exercise");
+            switchToNormal();
             break;
         }
-        case 3:             //Sleep Mode
+        case EXERCISE:
         {
-            setTimeOutValues(sLRI, sAVI, sPVARP, sURI, sVRP, sVSP, sPAVB);
-            strcpy(modeString, "Sleep");
+            switchToExercise();
             break;
         }
-        case 4:             //Manual Mode; do not chnage the time out values
+        case SLEEP:
         {
-            strcat(modeString, " + Manual");
+            switchToSleep();
             break;
         }
-        default:            //Normal mode is any error occurs
+        case MANUAL:
         {
-            setTimeOutValues(nLRI, nAVI, nPVARP, nURI, nVRP, nVSP, nPAVB);
-            strcpy(modeString, "Normal");
+            break;
         }
     }    
     modeChanged=false;
@@ -123,7 +168,7 @@
 {
     while(1)
     {
-        Thread::signal_wait(0x03);
+        Thread::signal_wait(signal3);
         if(modeChanged)
         {
             changeMode();
@@ -134,43 +179,38 @@
 
 void aSense()
 {
-    //ASignal received from heart  
-    if(expectingASignal)
+    if(waitASignal)
     {
-        led4=1;
+        asenseLED=1;
         wait(0.001);
-        pc.printf("ASense Received\r\n");
-        led4=0; 
+        println("ASense Received");
+        asenseLED=0; 
         if(modeChanged)
         {
-            (*ModeChangePTR).signal_set(0x03);
+            (*ModeChangePTR).signal_set(signal3);
         }
         aSenseOccurred=true;
-        (*PMSensePTR).signal_set(0x04);
+        (*PMSensePTR).signal_set(signal4);
     }    
 }
 
 void vSense()
 {
-    //VSignal received from heart
-    //reset timeout
-    //increment heart rate
     heartRateHeart++;
-    if(expectingVSignal)
+    if(waitVSignal)
     {
-        led3=1;
-        pc.printf("VSense Received\r\n");
+        vsenseLED=1;
+        println("VSense Received");
         wait(0.001);
-        led3=0;
+        vsenseLED=0;
         if(modeChanged)
         {
-            (*ModeChangePTR).signal_set(0x03);
+            (*ModeChangePTR).signal_set(signal3);
         }
         canPaceV=false;
         aSenseOccurred=false;
-        (*PMSensePTR).signal_set(0x04);
+        (*PMSensePTR).signal_set(signal4);
     }
-    
 }
 
 void seconds(const void *args)
@@ -189,61 +229,61 @@
     // check which time out has occurred
     // generate appropriate pace signal
     // reset timer to new value  
-    //led2=1; 
-    if(timeOutStatus==AVI)
+    //apaceLED=1; 
+    if(timeConstraint==AVI_const)
     {
         //generate VPace
         if(canPaceV)
         {
             paceA=false;
-            (*PacePTR).signal_set(0x01);
+            (*PacePTR).signal_set(signal1);
         }
         else
         {
             canPaceV=true;
         }
     }
-    else if(timeOutStatus==PAVB)
+    else if(timeConstraint==PVAB_const)
     {
         expectVMutex.lock();
-        expectingVSignal=true;
+        waitVSignal=true;
         expectVMutex.unlock();
         timeOutStatusMutex.lock();
-        timeOutStatus=AVI;
+        timeConstraint=AVI_const;
         timeOutStatusMutex.unlock();
         timerRunning=true;
-        TimeOutTimer->start(timeOutValue[AVI]-timeOutValue[PAVB]);
+        TimeOutTimer->start(AVI-PVAB);
     }
-    else if(timeOutStatus==VRP)
+    else if(timeConstraint==VRP_const)
     {
         //now we can sense a Ventrival event, but not an atrial event as PVARP is not over 
         //restart timer for PVARP
         expectVMutex.lock();        
-        expectingVSignal=true;         
+        waitVSignal=true;         
         expectVMutex.unlock();
         timeOutStatusMutex.lock();
-        timeOutStatus=PVARP;
+        timeConstraint=PVARP_const;
         timeOutStatusMutex.unlock();
         timerRunning=true;
-        TimeOutTimer->start(timeOutValue[PVARP]-timeOutValue[VRP]);
+        TimeOutTimer->start(PVARP-VRP);
     }
-    else if(timeOutStatus==PVARP)
+    else if(timeConstraint==PVARP_const)
     {
         //now we can sense Atrial events as well
         expectAMutex.lock();
-        expectingASignal=true;
+        waitASignal=true;
         expectAMutex.unlock();
         timeOutStatusMutex.lock();
-        timeOutStatus=LRI;
+        timeConstraint=LRI_const;
         timeOutStatusMutex.unlock();
         timerRunning=true;
-        TimeOutTimer->start(timeOutValue[LRI]-timeOutValue[PVARP]-timeOutValue[AVI]);
+        TimeOutTimer->start(LRI-PVARP-AVI);
     }
-    else if(timeOutStatus==LRI)
+    else if(timeConstraint==LRI_const)
     {
         //generate APace
         paceA=true;
-        (*PacePTR).signal_set(0x01);
+        (*PacePTR).signal_set(signal1);
     }
 }
 
@@ -256,7 +296,7 @@
         //pace V as a pace occurred during URI
         paceA=false;
         paceVPending=false;
-        (*PacePTR).signal_set(0x01);
+        (*PacePTR).signal_set(signal1);
     }
     else
     {
@@ -282,7 +322,7 @@
 {
     while(1)
     {
-        Thread::signal_wait(0x04);
+        Thread::signal_wait(signal4);
         if(timerRunning)
         {
             TimeOutTimer->stop();
@@ -291,16 +331,16 @@
         if(aSenseOccurred)
         {
             expectAMutex.lock();
-            expectingASignal=false;
+            waitASignal=false;
             expectAMutex.unlock();
             expectVMutex.lock();
-            expectingVSignal=true;
+            waitVSignal=true;
             expectVMutex.unlock();
             timerRunning=true;
             timeOutStatusMutex.lock();
-            timeOutStatus=AVI;
+            timeConstraint=AVI_const;
             timeOutStatusMutex.unlock();
-            TimeOutTimer->start(timeOutValue[AVI]);//500);            
+            TimeOutTimer->start(AVI);//500);            
         }
         else
         {
@@ -308,18 +348,18 @@
             heartRate++;
             heartRateMutex.unlock();
             expectVMutex.lock();         
-            expectingVSignal=false;         
+            waitVSignal=false;         
             expectVMutex.unlock();
             expectAMutex.lock();
-            expectingASignal=false;
+            waitASignal=false;
             expectAMutex.unlock();
             canPaceV=false;
-            URITimeOutTimer->start(timeOutValue[URI]);
+            URITimeOutTimer->start(URI);
             timerRunning=true;
             timeOutStatusMutex.lock();
-            timeOutStatus=VRP;
+            timeConstraint=VRP_const;
             timeOutStatusMutex.unlock();
-            TimeOutTimer->start(timeOutValue[VRP]);
+            TimeOutTimer->start(VRP);
         }
     }
 }
@@ -329,62 +369,62 @@
 {
     while(1)
     {
-        Thread::signal_wait(0x01);
+        Thread::signal_wait(signal1);
         if(paceA)
         {
-            pc.printf("APace Sent\r\n");
-            led2=1;
+            println("APace Sent");
+            apaceLED=1;
             aPace=1;
             Thread::wait(1);
             aPace=0;
-            led2=0;
+            apaceLED=0;
             if(modeChanged)
             {
                 changeMode();
             }
             // start AVI Timer
             expectAMutex.lock();
-            expectingASignal=false;
+            waitASignal=false;
             expectAMutex.unlock();
             expectVMutex.lock();         
-            expectingVSignal=false;         
+            waitVSignal=false;         
             expectVMutex.unlock();
             timeOutStatusMutex.lock();
-            timeOutStatus=PAVB;
+            timeConstraint=PVAB_const;
             timeOutStatusMutex.unlock();
             timerRunning=true;
-            TimeOutTimer->start(timeOutValue[PAVB]);
+            TimeOutTimer->start(PVAB);
             //generate the APace pulse
         }
         else
         {
-            pc.printf("VPace Sent\r\n");
-            led1=1;
+            println("VPace Sent");
+            vpaceLED=1;
             vPace=1;
             Thread::wait(1);
             vPace=0;
-            led1=0;
+            vpaceLED=0;
             if(modeChanged)
             {
                 changeMode();
             }
             // start VRP and URI timers
             expectVMutex.lock();         
-            expectingVSignal=false;         
+            waitVSignal=false;         
             expectVMutex.unlock();
             expectAMutex.lock();
-            expectingASignal=false;
+            waitASignal=false;
             expectAMutex.unlock();
             heartRateMutex.lock();
             heartRate++;
             heartRateMutex.unlock();
             canPaceV=false;
-            URITimeOutTimer->start(timeOutValue[URI]);
+            URITimeOutTimer->start(URI);
             timeOutStatusMutex.lock();
-            timeOutStatus=VRP;
+            timeConstraint=VRP_const;
             timeOutStatusMutex.unlock();
             timerRunning=true;
-            TimeOutTimer->start(timeOutValue[VRP]);
+            TimeOutTimer->start(VRP);
             //generate the VPace pulse
         }
     }
@@ -394,49 +434,49 @@
 {
     while(1)
     {
-        Thread::signal_wait(0x02);
-        if((((ch=='a')||(ch=='A')) && paceMakerMode==4) && !observationChange)
+        Thread::signal_wait(signal2);
+        if((((ch=='a')||(ch=='A')) && paceMakerMode==MANUAL) && !observationChange)
         {
             //fire A Pace
             paceA=true;
-            (*PacePTR).signal_set(0x01);
+            (*PacePTR).signal_set(signal1);
         }
-        else if((((ch=='v')||(ch=='V')) && paceMakerMode==4) && !observationChange)
+        else if((((ch=='v')||(ch=='V')) && paceMakerMode==MANUAL) && !observationChange)
         {
             //fire V Pace
             if(canPaceV)
             {
                 paceA=false;
                 paceVPending=false;
-                (*PacePTR).signal_set(0x01);
+                (*PacePTR).signal_set(signal1);
             }
             else
             {
                 paceVPending=true;
             }
         }
-        else if(((((ch=='n')||(ch=='N'))&& paceMakerMode!=1) && !observationChange) && !modeChanged)
+        else if(((((ch=='n')||(ch=='N'))&& paceMakerMode!=NORMAL) && !observationChange) && !modeChanged)
         {
-            paceMakerMode=1;                //change Mode to Normal
+            paceMakerMode=NORMAL;
             modeChanged=true;
         }
-        else if(((((ch=='e')||(ch=='E')) && paceMakerMode!=2) && !observationChange) && !modeChanged)
+        else if(((((ch=='e')||(ch=='E')) && paceMakerMode!=EXERCISE) && !observationChange) && !modeChanged)
         {
-            paceMakerMode=2;                //chnage mode to Exercise
+            paceMakerMode=EXERCISE;
             modeChanged=true;
         }
-        else if(((((ch=='s')||(ch=='S')) && paceMakerMode!=3) && !observationChange) && !modeChanged)
+        else if(((((ch=='s')||(ch=='S')) && paceMakerMode!=SLEEP) && !observationChange) && !modeChanged)
         {
-            paceMakerMode=3;            //change mode to Sleep
+            paceMakerMode=SLEEP;
             modeChanged=true;
         }
         else if(((((ch=='b')||(ch=='B')) ) && !observationChange) )
         {
             ringAlarm=!ringAlarm;
         }
-        else if(((((ch=='m')||(ch=='M')) && paceMakerMode!=4) && !observationChange) && !modeChanged)
+        else if(((((ch=='m')||(ch=='M')) && paceMakerMode!=MANUAL) && !observationChange) && !modeChanged)
         {
-            paceMakerMode=4;            //change mode to Sleep
+            paceMakerMode=MANUAL;
             modeChanged=true;
         }
         else if(((((ch=='o')||(ch=='O')) && !observationChange) && !modeChanged))
@@ -446,8 +486,6 @@
             digitTwoReceived=false;
             //spawn a timer for  3 seconds
             displayMutex.lock();
-            ////////pc.locate(30, 18);
-            pc.printf("Observation Interval change : -- seconds");
             displayMutex.unlock();
             KeyTimeOutTimer=new RtosTimer(keyTimeOut, osTimerOnce, (void *)0);
             KeyTimeOutTimer->start(3000);
@@ -462,8 +500,7 @@
                 observationChangeMutex.unlock();
                 digitOneReceived=true;
                 displayMutex.lock();
-                ////pc.locate(60, 18);
-                pc.printf("%02d", observationRate);
+                pc.printf("%02d\r\n", observationRate);
                 displayMutex.unlock();
             }
             else
@@ -475,8 +512,7 @@
                 observationChangeMutex.unlock();
                 observationChange=false;
                 displayMutex.lock();
-                ////pc.locate(60, 18);
-                pc.printf("%02d", observationRate);
+                pc.printf("%02d\r\n", observationRate);
                 displayMutex.unlock();
             }
         }                
@@ -488,7 +524,7 @@
     while(1)
     {
         Thread::wait(1000);
-        while(((heartRateHeart*(60/sec))>(60000/timeOutValue[URI])) && ringAlarm)
+        while(((heartRateHeart*(60/sec))>(60000/URI)) && ringAlarm)
         {
             buzzer=1;
             Thread::wait(5);
@@ -497,13 +533,12 @@
             if(!ringingAlarm)
             {
                 displayMutex.lock();
-                ////pc.locate(30, 16);
-                pc.printf("Alarm : HeartRate too high");
+                println("Alarm : HeartRate too high");
                 displayMutex.unlock();
                 ringingAlarm=true;
             }
         }
-        while(((heartRateHeart*(60/sec))<(60000/timeOutValue[LRI])) && ringAlarm)
+        while(((heartRateHeart*(60/sec))<(60000/LRI)) && ringAlarm)
         {
             buzzer=1;
             Thread::wait(10);
@@ -512,8 +547,7 @@
             if(!ringingAlarm)
             {
                 displayMutex.lock();
-                ////pc.locate(30, 16);
-                pc.printf("Alarm : HeartRate too Low");
+                println("Alarm : HeartRate too Low");
                 displayMutex.unlock();
                 ringingAlarm=true;
             }
@@ -543,11 +577,6 @@
                 resetDisplay();
                 observationInterval=observationRate*1000;           
                 rateCoefficient=(60000/observationInterval);
-            } 
-            else
-            {
-//                updateDisplay();
-                resetDisplay();
             }
             waitCount=0;   
         }        
@@ -555,21 +584,15 @@
 }
 
 int main() 
-{
-    pc.printf("Pace Maker Display\r\n");
-    pc.printf("Heart Rate :  --- bpm\r\n");
-    pc.printf("Observation Interval :  -- seconds\r\n");
-    pc.printf("Mode :  Normal\r\n");
-    pc.printf("Heart Beat Rate :  ----  bpm\r\n");   
-    
+{    
     //initialize variables
     expectAMutex.lock();
-    expectingASignal=true;
+    waitASignal=true;
     expectAMutex.unlock();
     expectVMutex.lock();         
-    expectingVSignal=true;         
+    waitVSignal=true;         
     expectVMutex.unlock();
-    setTimeOutValues(nLRI, nAVI, nPVARP, nURI, nVRP, nVSP, nPAVB);
+    switchToNormal();
     heartRate=0;
     avgHeartRate=0;
     paceMakerMode=1;
@@ -582,9 +605,8 @@
     digitOneReceived=false;
     digitTwoReceived=false;
     modeChanged=false;
-    canPaceV=true;                              //assume at begining that URI has lapsed and start with an atrial event
-    paceVPending=false;                                //assume that a VPace request has not happened already
-    strcpy(modeString, "Normal");
+    canPaceV=true;
+    paceVPending=false;
     buzzer=0;
     ringAlarm=true;
     heartRateHeart=0;
@@ -619,16 +641,14 @@
     Display.set_priority(osPriorityAboveNormal);
     
     timeOutStatusMutex.lock();
-    timeOutStatus=VRP;
+    timeConstraint=VRP_const;
     timeOutStatusMutex.unlock();
-    //test lines to start pacing without heart; heart starts immediately after an Atrial event
-    //timeOutStatus=AVI;
-    TimeOutTimer->start(timeOutValue[VRP]);
+    TimeOutTimer->start(VRP);
     while(1) {
         if(pc.readable())
         {
             ch=pc.getc();
-            (*SerialThreadPTR).signal_set(0x02);     //initiate the serial thread to change the state of the timer
+            (*SerialThreadPTR).signal_set(signal2);     //initiate the serial thread to change the state of the timer
         }
     }
 }
\ No newline at end of file