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