shengtian zhou / Mbed 2 deprecated heart

Dependencies:   TextLCD mbed-rtos mbed

Fork of heart by William Archer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "TextLCD.h"
00004 #include <time.h>
00005 #include <stdlib.h>
00006 
00007 //use screen /dev/tty.. on mac in terminal to receive input
00008 
00009 TextLCD lcd(p15,p16,p17,p18,p19,p20,TextLCD::LCD16x2);
00010 Serial pc(USBTX, USBRX); //set up serial
00011 DigitalOut natAPace(LED1); //leds for pacing
00012 DigitalOut natVPace(LED2);
00013 DigitalOut aPace(LED3);
00014 DigitalOut vPace(LED4);
00015 DigitalOut a_signal(p6); //connected to pm
00016 DigitalOut v_signal(p7); //connected to pm
00017 DigitalIn apace_signal(p8); //connected to pm
00018 DigitalIn vpace_signal(p9); //connected to pm
00019 
00020 Timer atimer;   //timer for atrial event
00021 Timer vtimer;   //timer for ventricular event
00022 
00023 unsigned int a_time = 0;
00024 unsigned int v_time = 0;
00025 
00026 int mode = 0; //0 = random, 1 = manual, 2 = test
00027 int modeInManul = -1; //0 = manualmode apace, 1 = manualmode vpace
00028 bool modeSwitch = false;
00029 
00030 //the following flags indicate pacing signals either from the heart or the pacemaker
00031 bool natural_apace = false;
00032 bool natural_vpace = false;
00033 bool apace = false;
00034 bool vpace = false;
00035 
00036 //constants
00037 const int minwait_A = 100; //in miliseconds
00038 const int minwait_V = 200; //in miliseconds
00039 /*
00040 //so far no use of the following in the heart model
00041 const int LRI = 1000;
00042 const int VRP = 400;
00043 const int PVARP = 500;
00044 const int URI = 1000;
00045 const int AVI = 100;
00046 */
00047 
00048 void switch_modes() {
00049     switch(mode) {
00050         case 0:
00051             break;
00052         case 1:
00053             break;
00054         case 2:
00055             break;
00056         
00057     }    
00058 }
00059 
00060 void send_signal(int type) { //type=0 a_signal, type=1 v_signal
00061         
00062         switch(type) {
00063             case 0:
00064                 a_signal = 1;
00065             case 1:
00066                 v_signal = 1;
00067         }
00068 }
00069 
00070 void modeSwitchDelay(){
00071     //srand(time(NULL));
00072     //int time = rand()%9700 + 300;
00073     Thread::wait(1000);   
00074 }
00075 
00076 void heart_keyboard() {
00077     while(true) { //thread is continuously running
00078             if(pc.readable()) {
00079                 char c = pc.getc();
00080                 
00081                 switch(c) {
00082                     case 'r': //set to random mode
00083                         mode = 0;
00084                         modeSwitch = true;
00085                         break;
00086                     case 'm': //mset to manual mode
00087                         mode = 1;
00088                         modeSwitch = true;
00089                         break;
00090                     case 't': //set to test mode
00091                         mode = 2;
00092                         modeSwitch = true;
00093                         break;
00094                     case 'a': //asignal
00095                         if(mode == 1) { //only if in manual (heart_mode == 1)
00096                             modeInManul = 0;
00097                         }
00098                         break;
00099                     case 'v': //vsignal
00100                         if(mode == 1) { //only if in manual (heart_mode == 1)
00101                             modeInManul = 1;
00102                         }
00103                         break; 
00104                     default: //erroneous key get rid of it
00105                         break;
00106                 }
00107             }
00108     }
00109 }
00110 
00111 void tick() {
00112     a_time++;
00113     v_time++;   
00114 }
00115 // Blink functions gets called when there is an Atrial/V entricular event.
00116 void natApaceBlink(){
00117     natAPace = 1;
00118     Thread::wait(50);
00119     natAPace = 0;
00120     Thread::wait(50);
00121 }
00122 
00123 void natVpaceBlink(){
00124     natVPace = 1;
00125     Thread::wait(50);
00126     natVPace = 0;
00127     Thread::wait(50);
00128 }
00129 
00130 void ApaceBlink(){
00131     aPace = 1;
00132     Thread::wait(50);
00133     aPace = 0;
00134     Thread::wait(50);
00135 }
00136 
00137 void VpaceBlink(){
00138     vPace = 1;
00139     Thread::wait(50);
00140     vPace = 0;
00141     Thread::wait(50);
00142 }
00143 
00144 void performModeDelay(){
00145     if(modeSwitch == true){
00146         modeSwitchDelay();
00147         modeSwitch = false;
00148     }
00149 }
00150 
00151 //generate the time the next atrial event will happen
00152 int Atrial_generate(){
00153     srand(time(NULL));
00154     return rand()%2000 + minwait_A;
00155 }
00156 
00157 //generate the time the next ventricular event will happen. 
00158 int Ventricular_generate(){
00159     srand(time(NULL));
00160     return rand()%2000 + minwait_V;
00161 }
00162 
00163 //The following two modules random_sensing and random_pacing are two components of the random mode.
00164 void random_sensing(){
00165     int aTime = 0; //the time that the next atrial event will happen, 
00166                    //if the time exceeds the expected limit, then it will be regarded as heart malfunction
00167                    //and the signal from the pacemaker will take effect.
00168     int vTime = 0; //the time that the next ventricular event will happen, 
00169                    //if the time exceeds the expected limit, then it will be regarded as heart malfunction
00170                    //and the signal from the pacemaker will take effect.
00171     while(1){
00172         if(mode == 0){
00173             performModeDelay();
00174             //use random functions to generate the sensing signals.
00175             //asense time generate
00176             //detect if there is an apace signal from the pacemaker, if there is, reset asense clock
00177             //otherwise natural apace
00178             //and go into vsense.
00179             aTime = Atrial_generate();
00180             atimer.start();
00181             while(1){
00182                 //the case when the hearth functions correctly
00183                  //this condition means the moment when vtimer passes aTime, the limit, and we still
00184                 // did not receive apace_signal, then, it means the heart is working correctly.
00185                 if(atimer.read_ms() >= aTime && !apace_signal){
00186                     atimer.stop();
00187                     atimer.reset();
00188                     send_signal(0);
00189                     natural_apace = true;
00190                     break;
00191                 }
00192                 
00193                 //the case when the heart malfunctions in the Atrial
00194                 if((atimer.read_ms () <= aTime) && apace_signal){
00195                     atimer.stop();
00196                     atimer.reset();
00197                     apace = true;
00198                     break;
00199                 }
00200             }
00201 
00202             //detect if there is a vpace signal from the pacemaker, if there is, reset the vsense clock.
00203             //otherwise natural vpace
00204             vTime = Ventricular_generate();
00205             vtimer.start();
00206             while(1){
00207                 //the case when the hearth functions correctly
00208                 //this condition means the moment when vtimer passes vTime, the limit, and we still
00209                 // did not receive vpace_signal, then, it means the heart is working correctly.
00210                 if(vtimer.read_ms() >= vTime && !vpace_signal){
00211                     vtimer.stop();
00212                     vtimer.reset();
00213                     send_signal(1);
00214                     natural_vpace = true;
00215                     break;
00216                 }
00217                 
00218                 //the case when the heart malfunctions in the Atrial
00219                 if((vtimer.read_ms () <= vTime) && vpace_signal){
00220                     vtimer.stop();
00221                     vtimer.reset();
00222                     vpace = true;
00223                     break;
00224                 }
00225             }
00226         }    
00227     }
00228 }
00229 
00230 void random_pacing(){
00231     while(1){
00232         if(mode == 0){
00233             performModeDelay();
00234             if(natural_apace){
00235                 natApaceBlink();
00236                 natural_apace = false;
00237             }
00238             
00239             if(natural_vpace){
00240                 natVpaceBlink();
00241                 natural_vpace = false;
00242             }
00243             
00244             if(apace){
00245                 ApaceBlink();
00246                 apace = false;
00247             }
00248             
00249             if(vpace){
00250                 VpaceBlink();
00251                 vpace = false;
00252             }
00253         }    
00254     }
00255 }
00256 
00257 
00258 //Heart Modes: Random, Manual, Test
00259 void Random(){
00260     //TODO: heart behaviour in random mode
00261     Thread s(random_sensing);
00262     Thread p(random_pacing);
00263     while(1);
00264 }
00265 
00266 void Manual(){
00267     //TODO: heart behaviour in manual mode
00268     while(1){
00269         if(mode == 1){
00270             performModeDelay();
00271             //natVpaceBlink();
00272             if(modeInManul == 0){
00273                 ApaceBlink();
00274                 modeInManul = -1;
00275             }
00276             
00277             if(modeInManul == 1){
00278                 VpaceBlink();
00279                 modeInManul = -1;
00280             }
00281         }
00282     }   
00283 }
00284 
00285 void Test(){
00286     //TODO: heart behaviour in test mode
00287     while(1){
00288         if(mode == 2){
00289             performModeDelay();
00290             //ApaceBlink();
00291         }
00292     }
00293 }
00294 
00295 int main() {
00296     //TODO: Set up threads
00297     Thread keyboard(heart_keyboard);
00298     //Note: only one of the following threads will be on duty when heart runs
00299     //and it is done through checking the mode the heart is in.
00300     Thread random_(Random);
00301     Thread manual_(Manual);
00302     Thread test_(Test);
00303     while(1);
00304 }