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