Pacemaker / Mbed 2 deprecated pacemaker_FINAL_vFINAL

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_FINAL_v2 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         if (!inManual) {
00074             while (!done) {
00075                 if (ta.read_ms() >= LRI-AVI) {
00076                     tv.reset();
00077                     V_thread->signal_set(RUN);
00078                     APace = 1;
00079                     Thread::wait(2);
00080                     APace = 0;
00081                     flashLED(2);
00082                     done = 1;
00083                 }
00084                 if (AGet == 1 && !isPVARP) {
00085                     tv.reset();
00086                     V_thread->signal_set(RUN);
00087                     flashLED(4);
00088                     done = 1;
00089                 }
00090             }
00091         }
00092     }
00093 }
00094 
00095 void V_func(void const *args) {
00096     while (1) {
00097         Thread::signal_wait(RUN, osWaitForever);
00098         int done = 0;
00099         if (!inManual) {
00100             while (!done) {
00101                 if (tv.read_ms() >= AVI) {
00102                     ta.reset();
00103                     A_thread->signal_set(RUN);
00104                     VPace = 1;
00105                     Thread::wait(2);
00106                     VPace = 0;
00107                     blind();
00108                     flashLED(1);
00109                     done = 1;
00110                 }
00111                 if (VGet == 1 && !isVRP) {
00112                     ta.reset();
00113                     A_thread->signal_set(RUN);
00114                     blind();
00115                     flashLED(3);
00116                     done = 1;
00117                 }
00118             }
00119         }
00120     }
00121 }
00122 
00123 void input_func(void const *args) {
00124     while (1) {
00125         input_mutex.lock();
00126         input=pc.getc();
00127         if (input == 'n') setVals('n');
00128         if (input == 's') setVals('s');
00129         if (input == 'e') setVals('e');
00130         if (input == 'm') makeManual();
00131         if (input == 'o') {
00132             lcd.printf("Enter\n\n");
00133             Omode = 1;
00134             input = pc.getc();
00135             if (input == '1') {
00136                 sampleRate = 10000;
00137                 updateSR();
00138             }
00139             if (input == '2') {
00140                 sampleRate = 20000;
00141                 updateSR();
00142             }
00143             if (input == '3') {
00144                 sampleRate = 30000;
00145                 updateSR();
00146             }
00147             if (input == '4') {
00148                 sampleRate = 60000;
00149                 updateSR();
00150             }
00151             if (input == '5') {
00152                 sampleRate = 100000;
00153                 updateSR();
00154             }
00155         }
00156         input_mutex.unlock();
00157     }
00158 }
00159 
00160 void calcHR(void const *args) {
00161     status_mutex.lock();
00162     if (firstSample == 1) {
00163         HR = beats*(60000/sampleRate);
00164         firstSample = 0;
00165     }
00166     else {
00167         HR = (beats*60000/sampleRate+HR)/2;
00168     }
00169     if (HR>=UB || HR<=LB) {
00170         speaker.period(1.0/500.0); // 500hz period
00171         speaker =0.5;
00172     }
00173     else {
00174         speaker=0.0;
00175     }
00176     status_mutex.unlock();
00177     disp_thread->signal_set(RUN);
00178 }
00179 
00180 void disp(void const *args) {
00181     while (1) {
00182         Thread::signal_wait(RUN,osWaitForever);
00183         status_mutex.lock();
00184         if (!Omode) {
00185             lcd.printf("HR = %d ppm\nCycle = %d s\n",HR,sampleRate/1000);
00186         }
00187         beats = 0;
00188         status_mutex.unlock();
00189     }
00190 }
00191 
00192 void manage_flags(void const *i) {
00193     status_mutex.lock();
00194     if ((int)i==1) isVRP = 0;
00195     if ((int)i==2) isPVARP = 0;
00196     status_mutex.unlock();
00197 }
00198 
00199 void flashLED(int i) {
00200     leds[i-1] = 1;
00201     wait(0.01);
00202     leds[i-1] = 0;
00203 }
00204 
00205 void blind() {
00206     status_mutex.lock();
00207     isVRP = 1;
00208     isPVARP = 1;
00209     VRP_timer->start(VRP);
00210     PVARP_timer->start(PVARP);
00211     beats++;
00212     status_mutex.unlock();
00213 }
00214 
00215 void makeManual() {
00216     man_mutex.lock();
00217     inManual = 1;
00218     man_mutex.unlock();
00219     UB = 175;
00220     LB = 30;
00221     int done = 0;
00222     while (!done) {
00223         input = pc.getc();
00224         if (input == 'v') {
00225             VPace = 1;
00226             Thread::wait(2);
00227             VPace = 0;
00228             flashLED(1);
00229         }
00230         if (input == 'a') {
00231             APace = 1;
00232             Thread::wait(2);
00233             APace = 0;
00234             flashLED(2);
00235         }
00236         if (input == 's') {
00237             setVals('s');
00238             done = 1;
00239         }
00240         if (input == 'e') {
00241             setVals('s');
00242             done = 1;
00243         }
00244         if (input == 'n') {
00245             setVals('s');
00246             done = 1;
00247         }
00248         if (input == 'o') {
00249             lcd.printf("Enter\n\n");
00250             Omode = 1;
00251             input = pc.getc();
00252             if (input == '1') {
00253                 sampleRate = 10000;
00254                 updateSR();
00255             }
00256             if (input == '2') {
00257                 sampleRate = 20000;
00258                 updateSR();
00259             }
00260             if (input == '3') {
00261                 sampleRate = 30000;
00262                 updateSR();
00263             }
00264             if (input == '4') {
00265                 sampleRate = 60000;
00266                 updateSR();
00267             }
00268             if (input == '5') {
00269                 sampleRate = 100000;
00270                 updateSR();
00271             }
00272         }
00273     }
00274     VRP_timer->stop();
00275     PVARP_timer->stop();
00276     man_mutex.lock();
00277     inManual = 0;
00278     man_mutex.unlock();
00279     tv.reset();
00280     V_thread->signal_set(RUN);
00281 }
00282 
00283 void get_listener(void const *args) {
00284     while (1) {
00285         if (inManual) {
00286             if (AGet == 1) {
00287                 flashLED(4);
00288                 while (AGet == 1);
00289             }
00290             if (VGet == 1) {
00291                 flashLED(3);
00292                 while (VGet == 1);
00293             }
00294         }
00295     }
00296 }
00297 
00298 void setVals(char c) {
00299     if (c == 'n') {
00300         PVARP = N_PVARP;
00301         VRP = N_VRP;
00302         LRI = N_LRI;
00303         AVI = N_AVI;
00304         UB = N_UB;
00305         LB = N_LB;
00306     }
00307     if (c == 's') {
00308         PVARP = S_PVARP;
00309         VRP = S_VRP;
00310         LRI = S_LRI;
00311         AVI = S_AVI;
00312         UB = S_UB;
00313         LB = S_LB;
00314     }
00315     if (c == 'e') {
00316         PVARP = E_PVARP;
00317         VRP = E_VRP;
00318         LRI = E_LRI;
00319         AVI = E_AVI;
00320         UB = E_UB;
00321         LB = E_LB;
00322     }
00323 }
00324 
00325 void updateSR() {
00326     status_mutex.lock();
00327     beats = 0;
00328     HR = 0;
00329     Omode = 0;
00330     firstSample = 1;
00331     HR_timer->stop();
00332     HR_timer->start(sampleRate);
00333     status_mutex.unlock();
00334     disp_thread->signal_set(RUN);
00335 }