CIS541 / Mbed 2 deprecated pacemakerlatest

Dependencies:   TextLCD mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 #include "rtos.h"
00004 
00005 TextLCD lcd(p15, p16, p17, p18, p19, p20);
00006 Serial pc(USBTX, USBRX);
00007 
00008 int pacemakerRate=0;
00009 InterruptIn ASignal(p25);
00010 InterruptIn VSignal(p26);
00011 int AVI = 65;
00012 int ASense=0;
00013 int VSense=0;
00014 int modeset;
00015 bool aPace;
00016 DigitalOut APace(p23);
00017 DigitalOut VPace(p24);
00018 Mutex mutex;
00019 
00020 DigitalOut APace1(LED1);
00021 DigitalOut VPace1(LED2);
00022 
00023 DigitalOut ASense_LED(LED3);
00024 DigitalOut VSense_LED(LED4);
00025 
00026 InterruptIn ASig(p25);
00027 InterruptIn VSig(p26);
00028 
00029 int pacemakerInterval=10;
00030 const int minwait_A=50;
00031 const int minwait_V=50;
00032 int modeSwitchTimeInterval = 1500;
00033 volatile int time_count=0;
00034 int m=0;
00035 int s=0;
00036 int ms=0;
00037 int PVARP=150;
00038 
00039 char key_input;
00040 bool changePacemakerInterval;
00041 
00042 int LRI=1500;
00043 int URI=600;
00044 
00045 int low[4] = {30,40,100,30};
00046 int high[4] = {60,100,175,175};
00047 
00048 volatile bool asig=false;
00049 volatile bool vsig=false;
00050 
00051 const int sleepModeURI = 1000;
00052 const int sleepModeLRI = 2000;
00053 
00054 const int normalModeURI = 600;
00055 const int normalModeLRI = 1500;
00056 
00057 const int sportsModeURI = 343;
00058 const int sportsModeLRI = 600;
00059 
00060 const int manualModeURI = 343;
00061 const int manualModeLRI = 2000;
00062 
00063 int time1_count=0;
00064 
00065 int PVAB = 10;
00066 int VRP = 150;
00067 
00068 typedef enum Modes {
00069     Test,
00070     Normal,
00071     Sleep,
00072     Random,
00073     Sports,
00074     Manual,
00075     Observer
00076 };
00077 
00078 typedef enum pacemakersend {
00079     AVIEvent,
00080     VentricalEvent,
00081     ManualMode,
00082     PostVSense
00083 };
00084 
00085 typedef enum pacemakerventrical {
00086     AWait
00087 };
00088 
00089 pacemakerventrical pventrical;
00090 pacemakersend psend1;
00091 Modes mode;
00092 //mode=Normal;
00093 
00094 Mutex PacemakerMutex;
00095 
00096 Thread *pa;
00097 Thread *pv;
00098 Thread *psend;
00099 Thread *aorv;
00100 Thread *pmode;
00101 
00102 void resetTimer0();
00103 void resetTimer1();
00104 void AorVPacesend();
00105 void ASignalreceive();
00106 void VSignalreceive();
00107 void PacemakerAtrial();
00108 void PacemakerModes();
00109 void timer0_init();
00110 void timer1_init();
00111 void updatePacemaker();
00112 void PacemakerVentricularInterval();
00113 
00114 extern "C" void TIMER0_IRQHandler (void)
00115 {
00116     if((LPC_TIM0->IR & 0x01) == 0x01) { // if interrupt provided, continue to next line
00117         LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
00118         time_count++;                   //increment time_count
00119     }
00120 }
00121 
00122 void timer0_init(void)
00123 {
00124     LPC_SC->PCONP |=1<1;            //power on the timer
00125     LPC_TIM0->MR0 = 23980;        //10 msec period i.e the timer count will increment every 10ms
00126     LPC_TIM0->MCR = 3;              //reset control
00127     //3 = Interrupt & reset timer0 on match
00128     //1 = Interrupt only, no reset
00129     NVIC_EnableIRQ(TIMER0_IRQn);    //enable interrupt
00130     LPC_TIM0->TCR = 1;              //enable the timer
00131 
00132 }
00133 
00134 extern "C" void TIMER1_IRQHandler (void)
00135 {
00136     if((LPC_TIM1->IR & 0x01) == 0x01) { // if interrupt provided, continue to next line
00137         LPC_TIM1->IR |= 1 << 0;         // Clear MR0 interrupt flag
00138         time1_count++;                   //increment time_count
00139     }
00140 }
00141 
00142 void timer1_init(void)
00143 {
00144     LPC_SC->PCONP |=1<1;            //power on the timer
00145     LPC_TIM1->MR0 = 23980;        //10 msec period i.e the timer count will increment every 10ms
00146     LPC_TIM1->MCR = 3;              //reset control
00147     //3 = Interrupt & reset timer0 on match
00148     //1 = Interrupt only, no reset
00149     NVIC_EnableIRQ(TIMER1_IRQn);    //enable interrupt
00150     LPC_TIM1->TCR = 1;              //enable the timer
00151 
00152 }
00153 
00154 void resetTimer0()
00155 {
00156     time_count;
00157     LPC_TIM0->TCR = 0;
00158     lcd.locate(0,0);
00159     lcd.printf("%02d:%02d:%02d", m,s,ms);
00160 }
00161 
00162 void resetTimer1()
00163 {
00164     time1_count=0;
00165     LPC_TIM1->TCR = 0;
00166     lcd.locate(0,0);
00167     lcd.printf("%02d:%02d:%02d", m,s,ms);
00168 }
00169 
00170 void AorVPacesend(void const* args)
00171 {
00172     while(1) {
00173         Thread::signal_wait(0x01);
00174         if(aPace) {
00175             APace=1;
00176             VPace1=0;
00177             //APace1=1;
00178             wait(0.002);
00179             APace1=0;
00180         } else {
00181             // pc.printf("Sending VPace");
00182             VPace=1;
00183             //VPace1=1;
00184             wait(0.002);
00185             VPace1=0;
00186             //APace1=0;
00187         }
00188     }
00189 }
00190 
00191 void ASignalreceive()
00192 {
00193     //pc.printf("ASignalreceive");
00194     asig=true;
00195 
00196 }
00197 
00198 void VSignalreceive()
00199 {
00200     //pc.printf("VSignalreceive");
00201     vsig=true;
00202 }
00203 void ASensesend()
00204 {   mutex.unlock();
00205     ASense_LED = 1;
00206     //pc.printf("ASensesend");
00207     ASense=1;
00208     wait(0.002);
00209     ASense_LED=0;
00210     //ASense = 0;
00211     mutex.unlock();
00212 }
00213 
00214 void VSensesend()
00215 {   mutex.lock();
00216     ASense_LED = 1;
00217     //pc.printf("VSensesend");
00218     VSense=1;
00219     wait(0.002);
00220     ASense_LED=0;
00221     //VSense=0;
00222     mutex.unlock();
00223 }
00224 
00225 void PacemakerVentricalEvent(void const* args)
00226 {
00227     pc.printf("enteredpventricalevent");
00228     while(1) {
00229         switch(pventrical) {
00230             case AWait:
00231                 pc.printf("AWAIT ENTERED");
00232                 //while(vsig==false||ASense==0||VPace==0);
00233                 if(vsig==true) {
00234                     vsig=false;
00235                     pc.printf("vsig in");
00236                     VSensesend();
00237                     pventrical=AWait;
00238                     break;
00239                 } else if(ASense==1) {
00240                     resetTimer0();
00241                     ASense=0;
00242                 } else if(APace==1) {
00243                     resetTimer0();
00244                 }
00245                 while(time_count<PVAB);
00246                 //while(vsig==false||VPace==0);
00247                 if(vsig==true)
00248                     pc.printf("vsig in");
00249                 VSensesend();
00250                 resetTimer0();
00251                // while(time_count<VRP);
00252                 pc.printf("exiting pventrical event");
00253                 break;
00254 
00255         }
00256     }
00257 }
00258 
00259 void PacemakerSend(void const* args)
00260 {
00261     pc.printf("enteredpsend");
00262     while(1) {
00263         switch(psend1) {
00264             case AVIEvent:
00265                // while(time_count<AVI||VSense==0||mode!=Manual);
00266                 if(time_count>=AVI) {
00267                     (*aorv).signal_set(0x01);
00268                     //AorVPacesend("s");
00269                     psend1=VentricalEvent;
00270                 } else if(mode==Manual) {
00271                     while (mode==Manual);
00272                     psend1=AVIEvent;
00273                 } else if(VSense==1)
00274                     psend1=VentricalEvent;
00275                     VSense=0;
00276                 break;
00277             case VentricalEvent:
00278                 //while(ASense==0||time_count<LRI||VSense==0||mode!=Manual);
00279                 if(ASense==1) {
00280                     ASense=0;
00281                     if(mode==Manual) {
00282                         while(mode==Manual);
00283                         psend1=AVIEvent;
00284                     } else if(VSense==1) {
00285                         resetTimer0();
00286                         VSense=0;
00287                         psend1=PostVSense;
00288                     } else {
00289                         resetTimer0();
00290                         psend1=AVIEvent;
00291                     }
00292                 }
00293                 break;
00294             case PostVSense:
00295                 //while(mode!=Manual||ASense==0||time_count<LRI-AVI);
00296                 if(mode==Manual) {
00297                     while(mode==Manual);
00298                     psend1=AVIEvent;
00299                 } else if(ASense==1) {
00300                     if(mode==Manual) {
00301                         while(mode==Manual);
00302                     } else {
00303                         resetTimer0();
00304                         psend1=AVIEvent;
00305                     }
00306                 } else if(time_count>=LRI-AVI) {
00307                     while(mode!=Manual);
00308                     while(mode==Manual);
00309                     psend1=AVIEvent;
00310                 }
00311                 break;
00312         }
00313 
00314     }
00315 }
00316 
00317 void PacemakerAtrial(void const* args)
00318 {
00319     pc.printf("enteredpatrial");
00320     while(1) {
00321         //while(asig==false||APace==0);
00322         if(asig==true) {
00323             asig=false;
00324             pc.printf("asig in");
00325             ASensesend();
00326         }
00327         pc.printf("%u",time_count);
00328         resetTimer0();
00329         pc.printf("%u",time_count);
00330         //while(VSense==0||VPace==0);
00331         pc.printf("%u",time1_count);
00332         resetTimer1();
00333         pc.printf("%u",time1_count);
00334         pc.printf("waiting for pvarp");
00335         while(time_count<PVARP);
00336         while(time1_count<URI);
00337         pc.printf("exiting patrial event");
00338     }
00339 
00340 }
00341 void PacemakerModes(void const* args)
00342 {
00343     pc.printf("enteredpmodes");
00344     while(1) {
00345         //if(pc.readable()) {
00346             Thread::wait(0x02);
00347             mutex.lock();
00348             //key_input = pc.getc();
00349             if((key_input=='n'||key_input=='N')&&!changePacemakerInterval) {
00350                 mode = Normal;
00351                 pc.printf("Normal");
00352                 LRI=normalModeLRI;
00353                 URI=normalModeURI;
00354                 // (*Pace).signal_set(0x01);
00355                 //resetTimer0();
00356                 modeset=0;
00357             } else if((key_input=='s'||key_input=='S')&&!changePacemakerInterval) {
00358                 mode=Sleep;
00359                 pc.printf("Sleep");
00360                 modeset=1;
00361                 LRI=sleepModeLRI;
00362                 URI=sleepModeURI;
00363                 // (*Pace).signal_set(0x01);
00364                 //resetTimer0();
00365             } else if((key_input=='e'||key_input=='E')&&!changePacemakerInterval) {
00366                 mode=Sports;
00367                 pc.printf("Exercise");
00368                 modeset=2;
00369                 LRI=sportsModeLRI;
00370                 URI=sportsModeURI;
00371                 //  (*Pace).signal_set(0x01);
00372                 //resetTimer0();
00373             } else if((key_input=='m'||key_input=='M')&&!changePacemakerInterval) {
00374                 mode=Manual;
00375                 modeset=3;
00376                 LRI=manualModeLRI;
00377                 URI=manualModeURI;
00378                 aPace=true;
00379                 //(*Pace).signal_set(0x02);
00380                 pc.printf("Manual");
00381             } else if((key_input=='v'||key_input=='V')&& !changePacemakerInterval) {
00382                 if(mode==Manual) {
00383                     mode=Manual;
00384                     pc.printf("Ventricular");
00385                     // AorVPacesend();
00386                     aPace=false;
00387                     (*aorv).signal_set(0x01);
00388                 }
00389             } else if((key_input=='a'||key_input=='A')&&!changePacemakerInterval) {
00390                 if(mode==Manual) {
00391                     mode=Manual;
00392                     //AorVPacesend();
00393                     pc.printf("Atrial");
00394                     aPace=true;
00395                     (*aorv).signal_set(0x01);
00396                 }
00397             } else if((key_input=='o'||key_input=='O')&&!changePacemakerInterval) {
00398                 int a = 10 + rand() % 80;
00399                 mode=Observer;
00400                 pc.printf("Observer and %u",a);
00401                 pacemakerInterval =a;
00402             }
00403             mutex.unlock();
00404 
00405         //}
00406     }
00407 }
00408 
00409 void Rx_interrupt()
00410 {
00411     if(pc.readable())
00412     {
00413         key_input=LPC_UART0->RBR;                  //read uart buffer data and clear the interrupt flag
00414         (*pmode).signal_set(0x02);     //initiate the serial thread to change the state of the timer
00415     }
00416 }
00417 
00418 int main ()
00419 {
00420     pc.baud(9600);
00421     pc.attach(&Rx_interrupt, Serial::RxIrq);  
00422     VSignal.rise(&VSignalreceive);
00423     ASignal.rise(&ASignalreceive);
00424     timer0_init();
00425     timer1_init();
00426     Thread patrial(PacemakerAtrial);
00427     pa=&patrial;
00428     Thread pventrical(PacemakerVentricalEvent);
00429     pv=&pventrical;
00430     Thread pacesend(PacemakerSend);
00431     psend=&pacesend;
00432     Thread pmodes(PacemakerModes);
00433     pmode=&pmodes;
00434     Thread aorvpacesend(AorVPacesend);
00435     aorv=&aorvpacesend;
00436     //Thread pdisplay(PacemakerDisplay);
00437 
00438 
00439     while(1) {
00440     }
00441 }