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_v6 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 Mutex VP_helper; 00034 00035 // input stuff 00036 char input; 00037 00038 // heart rate global vars 00039 int HR = 0; 00040 int beats = 0; 00041 int sampleRate = 10000; // default 10 seconds 00042 int firstSample = 1; 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 00081 // functions 00082 void A_func(void const *args); 00083 void V_func(void const *args); 00084 void manage_flags(void const *i); 00085 void flashLED(int i); 00086 void calcHR(void const *args); 00087 void disp(void const *args); 00088 void input_func(void const *args); 00089 void setVals(char c); 00090 void makeManual(); 00091 void blind(); 00092 00093 // threads 00094 Thread * A_thread; 00095 Thread * V_thread; 00096 Thread * input_thread; 00097 Thread * disp_thread; 00098 Thread * send_Vpace_thread; 00099 Thread * send_Apace_thread; 00100 00101 // rtos timers 00102 RtosTimer * VRP_timer; 00103 RtosTimer * PVARP_timer; 00104 RtosTimer * HR_timer; 00105 00106 int main() { 00107 00108 // start global timer 00109 tv.start(); 00110 ta.start(); 00111 tv.stop(); 00112 00113 // init threads 00114 disp_thread = new Thread(disp); 00115 input_thread = new Thread(input_func); 00116 A_thread = new Thread(A_func); 00117 V_thread = new Thread(V_func); 00118 00119 // init timers 00120 VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1); 00121 PVARP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)2); 00122 HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0); 00123 00124 // start display and heart rate sample 00125 HR_timer->start(sampleRate); 00126 disp_thread->signal_set(RUN); 00127 00128 // main thread 00129 while (1) { 00130 00131 } 00132 } 00133 00134 void A_func(void const *args) { 00135 while (1) { 00136 while (tv.read_ms() <= (LRI-AVI)) { 00137 if (AGet==1 && !isPVARP && !waitingForV) { 00138 tv.reset(); 00139 tv.stop(); 00140 ta.start(); 00141 waitingForV = 1; 00142 flashLED(4); 00143 while (AGet == 1); 00144 } 00145 } 00146 APace = 1; 00147 Thread::wait(2); 00148 APace = 0; 00149 tv.reset(); 00150 tv.stop(); 00151 ta.start(); 00152 waitingForV = 1; 00153 flashLED(2); 00154 } 00155 } 00156 00157 void V_func(void const *args) { 00158 while (1) { 00159 while (ta.read_ms() <= AVI) { 00160 if (VGet==1 && !isVRP && waitingForV) { 00161 blind(); 00162 flashLED(3); 00163 while (VGet == 1); 00164 } 00165 } 00166 VPace = 1; 00167 Thread::wait(2); 00168 VPace = 0; 00169 blind(); 00170 flashLED(1); 00171 } 00172 } 00173 00174 void input_func(void const *args) { 00175 while (1) { 00176 input_mutex.lock(); 00177 input=pc.getc(); 00178 if (input == 'n') setVals('n'); 00179 if (input == 's') setVals('s'); 00180 if (input == 'e') setVals('e'); 00181 if (input == 'm') makeManual(); 00182 input_mutex.unlock(); 00183 } 00184 } 00185 00186 void calcHR(void const *args) { 00187 if (firstSample == 1) { 00188 HR = beats*(60000/sampleRate); 00189 firstSample = 0; 00190 } 00191 else { 00192 HR = (beats*60000/sampleRate+HR)/2; 00193 } 00194 disp_thread->signal_set(RUN); 00195 } 00196 00197 void disp(void const *args) { 00198 while (1) { 00199 Thread::signal_wait(RUN,osWaitForever); 00200 lcd.printf("HR = %d ppm\nCyle = %d s\n",HR,sampleRate/1000); 00201 beats = 0; 00202 } 00203 } 00204 00205 void manage_flags(void const *i) { 00206 status_mutex.lock(); 00207 if ((int)i==1) isVRP = 0; 00208 if ((int)i==2) isPVARP = 0; 00209 status_mutex.unlock(); 00210 } 00211 00212 void flashLED(int i) { 00213 leds[i-1] = 1; 00214 wait(0.01); 00215 leds[i-1] = 0; 00216 } 00217 00218 void blind() { 00219 tv.start(); 00220 ta.reset(); 00221 ta.stop(); 00222 isVRP = 1; 00223 isPVARP = 1; 00224 waitingForV = 0; 00225 VRP_timer->start(VRP); 00226 PVARP_timer->start(PVARP); 00227 beats++; 00228 } 00229 00230 void makeManual() { 00231 t_mutex.lock(); 00232 ta.reset(); 00233 tv.reset(); 00234 ta.stop(); 00235 tv.stop(); 00236 t_mutex.unlock(); 00237 while (1) { 00238 input = pc.getc(); 00239 if (input == 'v') { 00240 send_Vpace_thread->signal_set(RUN); 00241 flashLED(1); 00242 } 00243 if (input == 'a') { 00244 send_Apace_thread->signal_set(RUN); 00245 flashLED(2); 00246 } 00247 t_mutex.lock(); 00248 ta.reset(); 00249 tv.reset(); 00250 ta.stop(); 00251 tv.stop(); 00252 t_mutex.unlock(); 00253 } 00254 } 00255 00256 void setVals(char c) { 00257 if (c == 'n') { 00258 PVARP = N_PVARP; 00259 VRP = N_VRP; 00260 LRI = N_LRI; 00261 AVI = N_AVI; 00262 UB = N_UB; 00263 LB = N_LB; 00264 } 00265 if (c == 's') { 00266 PVARP = S_PVARP; 00267 VRP = S_VRP; 00268 LRI = S_LRI; 00269 AVI = S_AVI; 00270 UB = S_UB; 00271 LB = S_LB; 00272 } 00273 if (c == 'e') { 00274 PVARP = E_PVARP; 00275 VRP = E_VRP; 00276 LRI = E_LRI; 00277 AVI = E_AVI; 00278 UB = E_UB; 00279 LB = E_LB; 00280 } 00281 } 00282 00283 00284 00285 00286 00287 00288
Generated on Tue Jul 19 2022 09:50:01 by
1.7.2
