Pacemaker / Mbed 2 deprecated pacemaker_FINAL_v1

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_SINGLETIMER_v1 by Pacemaker

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "constants.h"
00002 
00003 // global timer
00004 Timer tv;
00005 Timer ta;
00006 
00007 // mutexes
00008 Mutex t_mutex;          // protect reading of ta, tv
00009 Mutex status_mutex;     // protect reading of
00010 Mutex input_mutex;      // protects reading input
00011 Mutex man_mutex;        // protects manual
00012 Mutex state_mutex;      // protects waitingForV
00013 
00014 // functions
00015 void A_func(void const *args);
00016 void V_func(void const *args);
00017 void manage_flags(void const *i);
00018 void flashLED(int i);
00019 void calcHR(void const *args);
00020 void disp(void const *args);
00021 void input_func(void const *args);
00022 void setVals(char c);
00023 void makeManual();
00024 void blind();
00025 void get_listener(void const *args);
00026 void updateSR();
00027 
00028 // threads
00029 Thread * A_thread;
00030 Thread * V_thread;
00031 Thread * input_thread;
00032 Thread * disp_thread;
00033 Thread * listener;
00034 
00035 // rtos timers
00036 RtosTimer * VRP_timer;
00037 RtosTimer * PVARP_timer;
00038 RtosTimer * HR_timer;
00039 
00040 int main() {
00041     
00042     // init threads
00043     disp_thread = new Thread(disp);
00044     input_thread = new Thread(input_func);
00045     A_thread = new Thread(A_func);
00046     V_thread = new Thread(V_func);
00047     listener = new Thread(get_listener);
00048     
00049     // init timers
00050     VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1);
00051     PVARP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)2);
00052     HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0);
00053     
00054     // start display and heart rate sample
00055     HR_timer->start(sampleRate);
00056     disp_thread->signal_set(RUN);
00057     
00058     // start pacemaker
00059     ta.start();
00060     tv.start();
00061     V_thread->signal_set(RUN);
00062     
00063     // main thread
00064     while (1) {
00065         
00066     }
00067 }
00068 
00069 void A_func(void const *args) {
00070     while (1) {
00071         Thread::signal_wait(RUN, osWaitForever);
00072         int done = 0;
00073         ta.reset();
00074         while (!done) {
00075             if (AGet == 1 && !isPVARP) {
00076                 V_thread->signal_set(RUN);
00077                 flashLED(4);
00078                 done = 1;
00079             }
00080             if (ta.read_ms() >= LRI-AVI) {
00081                 V_thread->signal_set(RUN);
00082                 APace = 1;
00083                 Thread::wait(2);
00084                 APace = 0;
00085                 flashLED(2);
00086                 done = 1;
00087             }
00088         }
00089     }
00090 }
00091 
00092 void V_func(void const *args) {
00093     while (1) {
00094         Thread::signal_wait(RUN, osWaitForever);
00095         int done = 0;
00096         tv.reset();
00097         while (!done) {
00098             if (VGet == 1 && !isVRP) {
00099                 A_thread->signal_set(RUN);
00100                 blind();
00101                 flashLED(3);
00102                 done = 1;
00103             }
00104             if (tv.read_ms() >= AVI) {
00105                 A_thread->signal_set(RUN);
00106                 VPace = 1;
00107                 Thread::wait(2);
00108                 VPace = 0;
00109                 blind();
00110                 flashLED(1);
00111                 done = 1;
00112             }
00113         }
00114     }
00115 }
00116 
00117 void input_func(void const *args) {
00118     while (1) {
00119         input_mutex.lock();
00120         input=pc.getc();
00121         if (input == 'n') setVals('n');
00122         if (input == 's') setVals('s');
00123         if (input == 'e') setVals('e');
00124         if (input == 'm') makeManual();
00125         if (input == 'o') {
00126             lcd.printf("Enter\n\n");
00127             Omode = 1;
00128             input = pc.getc();
00129             if (input == '1') {
00130                 sampleRate = 10000;
00131                 updateSR();
00132             }
00133             if (input == '2') {
00134                 sampleRate = 20000;
00135                 updateSR();
00136             }
00137             if (input == '3') {
00138                 sampleRate = 30000;
00139                 updateSR();
00140             }
00141             if (input == '4') {
00142                 sampleRate = 60000;
00143                 updateSR();
00144             }
00145             if (input == '5') {
00146                 sampleRate = 100000;
00147                 updateSR();
00148             }
00149         }
00150         input_mutex.unlock();
00151     }
00152 }
00153 
00154 void calcHR(void const *args) {
00155     status_mutex.lock();
00156     if (firstSample == 1) {
00157         HR = beats*(60000/sampleRate);
00158         firstSample = 0;
00159     }
00160     else {
00161         HR = (beats*60000/sampleRate+HR)/2;
00162     }
00163     if (HR>=UB || HR<=LB) {
00164         speaker.period(1.0/500.0); // 500hz period
00165         speaker =0.5;
00166     }
00167     else {
00168         speaker=0.0;
00169     }
00170     status_mutex.unlock();
00171     disp_thread->signal_set(RUN);
00172 }
00173 
00174 void disp(void const *args) {
00175     while (1) {
00176         Thread::signal_wait(RUN,osWaitForever);
00177         status_mutex.lock();
00178         if (!Omode) {
00179             lcd.printf("HR = %d ppm\nCycle = %d s\n",HR,sampleRate/1000);
00180         }
00181         beats = 0;
00182         status_mutex.unlock();
00183     }
00184 }
00185 
00186 void manage_flags(void const *i) {
00187     status_mutex.lock();
00188     if ((int)i==1) isVRP = 0;
00189     if ((int)i==2) isPVARP = 0;
00190     status_mutex.unlock();
00191 }
00192 
00193 void flashLED(int i) {
00194     leds[i-1] = 1;
00195     wait(0.01);
00196     leds[i-1] = 0;
00197 }
00198 
00199 void blind() {
00200     status_mutex.lock();
00201     isVRP = 1;
00202     isPVARP = 1;
00203     VRP_timer->start(VRP);
00204     PVARP_timer->start(PVARP);
00205     beats++;
00206     status_mutex.unlock();
00207 }
00208 
00209 void makeManual() {
00210     man_mutex.lock();
00211     inManual = 1;
00212     man_mutex.unlock();
00213     UB = 175;
00214     LB = 30;
00215     int done = 0;
00216     while (!done) {
00217         input = pc.getc();
00218         if (input == 'v') {
00219             VPace = 1;
00220             Thread::wait(2);
00221             VPace = 0;
00222             flashLED(1);
00223         }
00224         if (input == 'a') {
00225             APace = 1;
00226             Thread::wait(2);
00227             APace = 0;
00228             flashLED(2);
00229         }
00230         if (input == 's') {
00231             setVals('s');
00232             done = 1;
00233         }
00234         if (input == 'e') {
00235             setVals('s');
00236             done = 1;
00237         }
00238         if (input == 'n') {
00239             setVals('s');
00240             done = 1;
00241         }
00242         if (input == 'o') {
00243             lcd.printf("Enter\n\n");
00244             Omode = 1;
00245             input = pc.getc();
00246             if (input == '1') {
00247                 sampleRate = 10000;
00248                 updateSR();
00249             }
00250             if (input == '2') {
00251                 sampleRate = 20000;
00252                 updateSR();
00253             }
00254             if (input == '3') {
00255                 sampleRate = 30000;
00256                 updateSR();
00257             }
00258             if (input == '4') {
00259                 sampleRate = 60000;
00260                 updateSR();
00261             }
00262             if (input == '5') {
00263                 sampleRate = 100000;
00264                 updateSR();
00265             }
00266         }
00267     }
00268     VRP_timer->stop();
00269     PVARP_timer->stop();
00270     man_mutex.lock();
00271     inManual = 0;
00272     man_mutex.unlock();
00273 }
00274 
00275 void get_listener(void const *args) {
00276     while (1) {
00277         if (inManual) {
00278             if (AGet == 1) {
00279                 flashLED(4);
00280                 while (AGet == 1);
00281             }
00282             if (VGet == 1) {
00283                 flashLED(3);
00284                 while (VGet == 1);
00285             }
00286         }
00287     }
00288 }
00289 
00290 void setVals(char c) {
00291     if (c == 'n') {
00292         PVARP = N_PVARP;
00293         VRP = N_VRP;
00294         LRI = N_LRI;
00295         AVI = N_AVI;
00296         UB = N_UB;
00297         LB = N_LB;
00298     }
00299     if (c == 's') {
00300         PVARP = S_PVARP;
00301         VRP = S_VRP;
00302         LRI = S_LRI;
00303         AVI = S_AVI;
00304         UB = S_UB;
00305         LB = S_LB;
00306     }
00307     if (c == 'e') {
00308         PVARP = E_PVARP;
00309         VRP = E_VRP;
00310         LRI = E_LRI;
00311         AVI = E_AVI;
00312         UB = E_UB;
00313         LB = E_LB;
00314     }
00315 }
00316 
00317 void updateSR() {
00318     status_mutex.lock();
00319     beats = 0;
00320     HR = 0;
00321     Omode = 0;
00322     firstSample = 1;
00323     HR_timer->stop();
00324     HR_timer->start(sampleRate);
00325     status_mutex.unlock();
00326     disp_thread->signal_set(RUN);
00327 }
00328 
00329 
00330 
00331 
00332