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_v7 by
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "TextLCD.h" 00004 #include <stdio.h> 00005 #include <stdlib.h> 00006 00007 #define RUN 0x1 00008 00009 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); 00010 Serial pc (USBTX, USBRX); 00011 00012 // ports 00013 DigitalIn VGet(p11); 00014 DigitalIn AGet(p12); 00015 DigitalOut VPace(p13); 00016 DigitalOut APace(p14); 00017 00018 // LEDs 00019 DigitalOut leds[] = {LED1, LED2, LED3, LED4}; 00020 // 1 = VP 00021 // 2 = AP 00022 // 3 = VS 00023 // 4 = AS 00024 00025 // global clocks 00026 Timer ta; // time since a event 00027 Timer tv; // time since v event 00028 00029 // mutexes 00030 Mutex t_mutex; // protect reading of ta, tv 00031 Mutex status_mutex; // protect reading of 00032 Mutex input_mutex; // protects reading input 00033 00034 // input stuff 00035 char input; 00036 00037 // heart rate global vars 00038 int HR = 0; 00039 int beats = 0; 00040 int sampleRate = 10000; // default 10 seconds 00041 int firstSample = 1; 00042 int Omode = 0; 00043 00044 // Normal Values 00045 const int N_PVARP = 325; // ms 00046 const int N_VRP = 300; // ms 00047 const int N_LRI = 857; // ms (= about 70ppm) 00048 const int N_AVI = 65; // ms 00049 const int N_UB = 100; // 100ppm 00050 const int N_LB = 40; // 40ppm 00051 00052 // Exercise Values 00053 const int E_PVARP = 175; // ms 00054 const int E_VRP = 150; // ms 00055 const int E_LRI = 428; // ms (= about 140ppm) 00056 const int E_AVI = 30; // ms 00057 const int E_UB = 175; // 175ppm 00058 const int E_LB = 100; // 100ppm 00059 00060 // Sleep Values 00061 const int S_PVARP = 500; // ms 00062 const int S_VRP = 475; // ms 00063 const int S_LRI = 1333; // ms (= about 45ppm) 00064 const int S_AVI = 100; // ms 00065 const int S_UB = 60; // 60ppm 00066 const int S_LB = 30; // 30ppm 00067 00068 // Heart Values - Normal Mode is default 00069 int PVARP = N_PVARP; 00070 int VRP = N_VRP; 00071 int LRI = N_LRI; 00072 int AVI = N_AVI; 00073 int UB = N_UB; 00074 int LB = N_LB; 00075 00076 // status flags 00077 int isVRP = 0; 00078 int isPVARP = 0; 00079 int waitingForV = 1; 00080 int inManual = 0; 00081 00082 // functions 00083 void A_func(void const *args); 00084 void V_func(void const *args); 00085 void manage_flags(void const *i); 00086 void flashLED(int i); 00087 void calcHR(void const *args); 00088 void disp(void const *args); 00089 void input_func(void const *args); 00090 void setVals(char c); 00091 void makeManual(); 00092 void blind(); 00093 void get_listener(void const *args); 00094 00095 // threads 00096 Thread * A_thread; 00097 Thread * V_thread; 00098 Thread * input_thread; 00099 Thread * disp_thread; 00100 Thread * listener; 00101 00102 // rtos timers 00103 RtosTimer * VRP_timer; 00104 RtosTimer * PVARP_timer; 00105 RtosTimer * HR_timer; 00106 00107 int main() { 00108 00109 // start global timer 00110 tv.start(); 00111 ta.start(); 00112 tv.stop(); 00113 00114 // init threads 00115 disp_thread = new Thread(disp); 00116 input_thread = new Thread(input_func); 00117 A_thread = new Thread(A_func); 00118 V_thread = new Thread(V_func); 00119 listener = new Thread(get_listener); 00120 00121 // init timers 00122 VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1); 00123 PVARP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)2); 00124 HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0); 00125 00126 // start display and heart rate sample 00127 HR_timer->start(sampleRate); 00128 disp_thread->signal_set(RUN); 00129 00130 // main thread 00131 while (1) { 00132 00133 } 00134 } 00135 00136 void A_func(void const *args) { 00137 while (1) { 00138 if (!inManual) { 00139 while (tv.read_ms() <= (LRI-AVI)) { 00140 t_mutex.unlock(); 00141 if (AGet==1 && !isPVARP && !waitingForV) { 00142 tv.reset(); 00143 tv.stop(); 00144 ta.start(); 00145 waitingForV = 1; 00146 flashLED(4); 00147 while (AGet == 1); 00148 } 00149 t_mutex.lock(); 00150 } 00151 APace = 1; 00152 Thread::wait(2); 00153 APace = 0; 00154 tv.reset(); 00155 tv.stop(); 00156 ta.start(); 00157 waitingForV = 1; 00158 flashLED(2); 00159 } 00160 } 00161 } 00162 00163 void V_func(void const *args) { 00164 while (1) { 00165 if (!inManual) { 00166 while (ta.read_ms() <= AVI) { 00167 t_mutex.unlock(); 00168 if (VGet==1 && !isVRP && waitingForV) { 00169 blind(); 00170 flashLED(3); 00171 while (VGet == 1); 00172 } 00173 t_mutex.lock(); 00174 } 00175 VPace = 1; 00176 Thread::wait(2); 00177 VPace = 0; 00178 blind(); 00179 flashLED(1); 00180 } 00181 } 00182 } 00183 00184 void input_func(void const *args) { 00185 while (1) { 00186 input_mutex.lock(); 00187 input=pc.getc(); 00188 if (input == 'n') setVals('n'); 00189 if (input == 's') setVals('s'); 00190 if (input == 'e') setVals('e'); 00191 if (input == 'm') makeManual(); 00192 if (input == 'o') { 00193 lcd.printf("Enter\n\n"); 00194 Omode = 1; 00195 input = pc.getc(); 00196 if (input == '1') sampleRate = 10000; 00197 if (input == '2') sampleRate = 20000; 00198 if (input == '3') sampleRate = 30000; 00199 if (input == '4') sampleRate = 60000; 00200 if (input == '5') sampleRate = 100000; 00201 beats = 0; 00202 HR = 0; 00203 Omode = 0; 00204 firstSample = 1; 00205 disp_thread->signal_set(RUN); 00206 } 00207 input_mutex.unlock(); 00208 } 00209 } 00210 00211 void calcHR(void const *args) { 00212 if (firstSample == 1) { 00213 HR = beats*(60000/sampleRate); 00214 firstSample = 0; 00215 } 00216 else { 00217 HR = (beats*60000/sampleRate+HR)/2; 00218 } 00219 disp_thread->signal_set(RUN); 00220 00221 if (HR>=UB || HR<=LB) { 00222 00223 } 00224 00225 00226 } 00227 00228 void disp(void const *args) { 00229 while (1) { 00230 Thread::signal_wait(RUN,osWaitForever); 00231 if (!Omode) { 00232 lcd.printf("HR = %d ppm\nCycle = %d s\n",HR,sampleRate/1000); 00233 } 00234 beats = 0; 00235 } 00236 } 00237 00238 void manage_flags(void const *i) { 00239 status_mutex.lock(); 00240 if ((int)i==1) isVRP = 0; 00241 if ((int)i==2) isPVARP = 0; 00242 status_mutex.unlock(); 00243 } 00244 00245 void flashLED(int i) { 00246 leds[i-1] = 1; 00247 wait(0.01); 00248 leds[i-1] = 0; 00249 } 00250 00251 void blind() { 00252 tv.start(); 00253 ta.reset(); 00254 ta.stop(); 00255 isVRP = 1; 00256 isPVARP = 1; 00257 waitingForV = 0; 00258 VRP_timer->start(VRP); 00259 PVARP_timer->start(PVARP); 00260 beats++; 00261 } 00262 00263 void makeManual() { 00264 inManual = 1; 00265 ta.reset(); 00266 ta.stop(); 00267 tv.reset(); 00268 tv.stop(); 00269 UB = 175; 00270 LB = 30; 00271 int done = 0; 00272 while (!done) { 00273 input = pc.getc(); 00274 if (input == 'v') { 00275 VPace = 1; 00276 Thread::wait(2); 00277 VPace = 0; 00278 flashLED(1); 00279 } 00280 if (input == 'a') { 00281 APace = 1; 00282 Thread::wait(2); 00283 APace = 0; 00284 flashLED(2); 00285 } 00286 if (input == 's') { 00287 setVals('s'); 00288 done = 1; 00289 } 00290 if (input == 'e') { 00291 setVals('s'); 00292 done = 1; 00293 } 00294 if (input == 'n') { 00295 setVals('s'); 00296 done = 1; 00297 } 00298 if (input == 'o') { 00299 lcd.printf("Enter\n\n"); 00300 Omode = 1; 00301 input = pc.getc(); 00302 if (input == '1') sampleRate = 10000; 00303 if (input == '2') sampleRate = 20000; 00304 if (input == '3') sampleRate = 30000; 00305 if (input == '4') sampleRate = 60000; 00306 if (input == '5') sampleRate = 100000; 00307 beats = 0; 00308 HR = 0; 00309 Omode = 0; 00310 firstSample = 1; 00311 disp_thread->signal_set(RUN); 00312 00313 } 00314 } 00315 tv.start(); 00316 waitingForV = 1; 00317 VRP_timer->stop(); 00318 PVARP_timer->stop(); 00319 inManual = 0; 00320 } 00321 00322 void get_listener(void const *args) { 00323 while (1) { 00324 if (inManual) { 00325 if (AGet == 1) { 00326 flashLED(4); 00327 while (AGet == 1); 00328 } 00329 if (VGet == 1) { 00330 flashLED(3); 00331 while (VGet == 1); 00332 } 00333 } 00334 } 00335 } 00336 00337 void setVals(char c) { 00338 if (c == 'n') { 00339 PVARP = N_PVARP; 00340 VRP = N_VRP; 00341 LRI = N_LRI; 00342 AVI = N_AVI; 00343 UB = N_UB; 00344 LB = N_LB; 00345 } 00346 if (c == 's') { 00347 PVARP = S_PVARP; 00348 VRP = S_VRP; 00349 LRI = S_LRI; 00350 AVI = S_AVI; 00351 UB = S_UB; 00352 LB = S_LB; 00353 } 00354 if (c == 'e') { 00355 PVARP = E_PVARP; 00356 VRP = E_VRP; 00357 LRI = E_LRI; 00358 AVI = E_AVI; 00359 UB = E_UB; 00360 LB = E_LB; 00361 } 00362 } 00363 00364 00365 00366 00367 00368 00369
Generated on Sun Jul 17 2022 02:58:39 by
1.7.2
