heart modes, send, receive

Dependencies:   Terminal TextLCD mbed-rtos mbed

Fork of Pacemaker by CIS541

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers heart.cpp Source File

heart.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 InterruptIn APace(p23);
00009 InterruptIn VPace(p24);
00010 
00011 DigitalOut ASignal(p25);
00012 DigitalOut VSignal(p26);
00013 
00014 DigitalOut APaceReceive(LED1);
00015 DigitalOut VPaceReceive(LED2);
00016 
00017 DigitalOut ASignal1(LED3);
00018 DigitalOut VSignal1(LED4);
00019 
00020 const int minwait_A=50;
00021 const int minwait_V=50;
00022 
00023 volatile int t=0;
00024 volatile int k=0;
00025 int m=0;
00026 int s=0;
00027 int ms=0;
00028 int heartInterval;
00029 char key_input;
00030 int testNumber;
00031 bool testResult;
00032 
00033 int LRI=1500;
00034 int URI=600;
00035 int heartRate;
00036 volatile bool Areceived=false;
00037 volatile bool Vreceived=false;
00038 bool test=false;
00039 int low[4] = {30,40,100,30};
00040 int high[4] = {60,100,175,175};
00041 int modeset;
00042 int pulses;
00043 
00044 const int sleepModeURI = 1000;
00045 const int sleepModeLRI = 2000;
00046 
00047 const int normalModeURI = 600;
00048 const int normalModeLRI = 1500;
00049 
00050 const int sportsModeURI = 343;
00051 const int sportsModeLRI = 600;
00052 
00053 const int manualModeURI = 343;
00054 const int manualModeLRI = 2000;
00055 
00056 typedef enum Modes {
00057     Test,
00058     Normal,
00059     Sleep,
00060     Random,
00061     Sports,
00062     Manual,
00063     Observer
00064 };
00065 Modes mode;
00066 
00067 Mutex HeartMutex;
00068 
00069 Thread *heartreceive;
00070 Thread *heartsend;
00071 Thread *heartkeyboard;
00072 
00073 
00074 void resett();
00075 void resetk();
00076 void VPacereceived();
00077 void APacereceived();
00078 void ASignalsend();
00079 void VSignalsend();
00080 void HeartReceive();
00081 void HeartSend();
00082 void HeartKeyBoardModeSwitch();
00083 void sound_lowAlarm();
00084 void sound_highAlarm();
00085 
00086 
00087 extern "C" void TIMER0_IRQHandler (void)
00088 {
00089     if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed
00090         LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
00091         t++;
00092         k++;
00093     }
00094 }
00095 
00096 //Initializing the timer 0
00097 void timer0_init(void)
00098 {
00099 
00100     LPC_SC->PCONP |=1<1;            //Timer0 Power On
00101     LPC_TIM0->MR0 = 23990;         //1 msec
00102     LPC_TIM0->MCR = 3;              //interrupt and reset control
00103     NVIC_EnableIRQ(TIMER1_IRQn);    //Enable timer0 interrupt
00104     LPC_TIM0->TCR = 1;              //enable Timer0
00105     t = 0;
00106     k = 0;
00107 }
00108 
00109 void resett()
00110 {
00111     t=0;
00112 }
00113 
00114 void resetk()
00115 {
00116     k=0;
00117 }
00118 
00119 void sound_lowAlarm(){
00120     pc.printf("lowalarm");}
00121 void sound_highAlarm(){
00122     pc.printf("highalarm");}
00123 void VPacereceived()
00124 {
00125     pc.printf("VPace received");
00126     VPaceReceive=1;
00127     wait(0.002);
00128     APaceReceive=0;
00129     pulses++;
00130     Vreceived=true;
00131     Areceived=false;
00132     if(mode == Test) {
00133         //(*HeartTxPtr).signal_set(0x04);
00134     }
00135     if(mode == Test && test) {
00136         if(testNumber == 2 || testNumber == 0 || testNumber == 3 || testNumber == 1) {
00137             testResult = false;
00138         } else if(testNumber == 6 || testNumber == 4) {
00139             testResult = true;
00140         }
00141     }
00142 }
00143 
00144 void APacereceived()
00145 {
00146     pc.printf("APace received");
00147     APaceReceive=1;
00148     wait(0.002);
00149     APaceReceive=0;
00150     Areceived=true;
00151     if(mode == Test && test) {
00152         if(testNumber == 0) {
00153             testResult = false;
00154         } else if(testNumber == 4 || testNumber == 7 || testNumber == 3 || testNumber == 1) {
00155             testResult = true;
00156         }
00157     }
00158 }
00159 
00160 void ASignalsend()
00161 {
00162     ASignal=1;
00163     ASignal1=1;
00164     wait(0.02);
00165     Thread::wait(1);
00166     ASignal1=0;
00167     ASignal=0;
00168 }
00169 
00170 void VSignalsend()
00171 {
00172     VSignal=1;
00173     VSignal1=1;
00174     wait(0.02);
00175     Thread::wait(1);
00176     VSignal1=0;
00177     VSignal==0;
00178 }
00179 
00180 void updateHeart()
00181 {
00182     int temp = 60/heartInterval;
00183     heartRate = heartRate + temp;
00184 }
00185 void HeartReceive(void const* args)
00186 {
00187     while(1) {
00188         pc.printf("HR");
00189         while(Areceived==false||Vreceived==false);
00190         if(Vreceived==true) {
00191             resett();
00192 
00193         } else if(Areceived==true) {
00194             resett();
00195         }
00196     }
00197 }
00198 
00199 void HeartSend(void const* args)
00200 {
00201     while(!pc.readable()) {
00202         int r = rand() % 2;
00203         pc.printf("HS");
00204         while(t<minwait_V && mode!=Random && (mode==Test || mode==Manual));
00205         pc.printf("%u",t);
00206         if(r==0) {
00207             VSignalsend();
00208             pc.printf("Vsignal");
00209             resett();
00210         } else if(r==1) {
00211             ASignalsend();
00212             pc.printf("ASignal");
00213             resett();
00214         }
00215     }
00216 }
00217 void HeartKeyBoardModeSwitch(void const* args)
00218 {
00219     while(1) {
00220         if(pc.readable()) {
00221 
00222             key_input = pc.getc();
00223 
00224             if(key_input=='r'||key_input=='R') {
00225                 mode=Random;
00226                 pc.printf("Random");
00227                 HeartSend("s");
00228             } else if(key_input=='t'||key_input=='T') {
00229                 mode=Test;
00230                 pc.printf("Test");
00231             } else if(key_input=='m'||key_input=='M') {
00232                 mode=Manual;
00233                 pc.printf("Manual");
00234             } else if(mode==Manual&&(key_input=='v'||key_input=='V')) {
00235                 mode=Manual;
00236                 VSignalsend();
00237                 pc.printf("Ventricular");
00238             } else if(mode==Manual&&(key_input=='a'||key_input=='A')) {
00239                 mode=Manual;
00240                 ASignalsend();
00241                 pc.printf("Atrial");
00242             } else if(key_input=='o'||key_input=='O') {
00243                 int a = 10 + rand() % 80;
00244                 mode=Observer;
00245                 pc.printf("Observer and %u",a);
00246                 heartInterval =a;
00247                 HeartSend("s");
00248             } else {
00249                 pc.printf("Wrong key, sorry");
00250             }
00251         }
00252     }
00253 }
00254 
00255 void display(void const* args)
00256 {
00257     while(1) {
00258         Thread::wait(observationInterval/1000);
00259 //        if(mode!=Test) {
00260 //            if(mode!= changeobservationinterval) {
00261 //                avgHeartRate=pulseCount*60000/(obsInterval);
00262 //                pulseCount = 0;
00263 //                updateDisplay();
00264 //
00265 //            }
00266 //        }
00267         if(!Test) {
00268             pc.printf("Avg Heart Rate: %02d bpm", heartRate);
00269             while(!Vreceived||!VSignal==0) {
00270                 updateHeart();
00271             }
00272             while(k<=heartInterval*1000);
00273             if(heartRate>high[mode]) {
00274                 sound_highAlarm();
00275                 heartRate=0;
00276                 resetk();
00277             } else if(heartRate<low[mode]) {
00278                 sound_lowAlarm();
00279                 heartRate=0;
00280                 resetk();
00281             } else if(heartRate  >= low[mode]  && heartRate  <= high[mode]) {
00282                 heartRate=0;
00283                 resetk();
00284             }
00285 
00286 
00287         }
00288     }
00289 }
00290 
00291 
00292 void testmode(void const* args){
00293     }
00294 
00295 
00296 int main()
00297 {
00298     pc.baud(9600);
00299     VPace.rise(&VPacereceived);
00300     APace.rise(&APacereceived);
00301     timer0_init();
00302     Thread heartreceive(HeartReceive);
00303     Thread heartsend(HeartSend);
00304     Thread heartkeyboard(HeartKeyBoardModeSwitch);
00305     Thread disPlay(display);
00306 
00307     while(1) {
00308     }
00309 }