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