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_v3 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}; // 1 = VP, 2 = AP, 3 = AS, 4 = VS 00020 00021 // create log file and debugging vars 00022 //LocalFileSystem local("local"); 00023 //FILE *fp = fopen("/local/out.txt", "w"); 00024 int between_a = 0; 00025 int between_v = 0; 00026 00027 // Heart Signals 00028 int AG = 0; 00029 int VG = 0; 00030 00031 // heart rate global vars 00032 int HR = 0; 00033 int beats = 0; 00034 int sampleRate = 10000; // default 10 seconds 00035 int firstSample = 1; 00036 00037 // Normal Values 00038 const int N_PVARP = 325; // ms 00039 const int N_VRP = 300; // ms 00040 const int N_LRI = 857; // ms (= about 70ppm) 00041 const int N_AVI = 65; // ms 00042 const int N_UB = 100; // 100ppm 00043 const int N_LB = 40; // 40ppm 00044 00045 // Heart Values - Normal Mode is default 00046 int PVARP = N_PVARP; 00047 int VRP = N_VRP; 00048 int LRI = N_LRI; 00049 int default_LRI = N_LRI; 00050 int AVI = N_AVI; 00051 int UB = N_UB; 00052 int LB = N_LB; 00053 00054 // time vars 00055 Timer global_t; 00056 int isVRP = 0; 00057 int isPVARP = 0; 00058 int waitingForV = 1; 00059 00060 // functions 00061 void VP_func(void const *args); 00062 void AP_func(void const *args); 00063 void VS_func(void const *args); 00064 void AS_func(void const *args); 00065 void PM_monitor_func(void const *args); 00066 void manage_signals(void const *i); 00067 void flashLED(int i); 00068 void event_out(char *s, int between_t); 00069 void blind(); 00070 //void rand_heart_func(void const *args); 00071 void calcHR(void const *args); 00072 void disp(void const *args); 00073 void send_Apace(); 00074 void send_Vpace(); 00075 00076 // threads 00077 Thread * VS_thread; 00078 Thread * AS_thread; 00079 Thread * PM_monitor_thread; 00080 Thread * disp_thread; 00081 //Thread * rand_heart_thread; // just for testing until mbed connection is made 00082 00083 // rtos timers 00084 RtosTimer * VP_timer; 00085 RtosTimer * AP_timer; 00086 RtosTimer * VRP_timer; 00087 RtosTimer * PVARP_timer; 00088 RtosTimer * HR_timer; 00089 //RtosTimer * exit_timer; // for log file 00090 00091 int main() { 00092 00093 // start global timer 00094 global_t.start(); 00095 00096 // init threads 00097 VS_thread = new Thread(VS_func); 00098 AS_thread = new Thread(AS_func); 00099 disp_thread = new Thread(disp); 00100 PM_monitor_thread = new Thread(PM_monitor_func); 00101 //rand_heart_thread = new Thread(rand_heart_func); // just for testing until mbed connection is made 00102 00103 // init timers 00104 VP_timer = new RtosTimer(VP_func, osTimerOnce, (void *)0); 00105 AP_timer = new RtosTimer(AP_func, osTimerOnce, (void *)0); 00106 HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0); 00107 VRP_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)1); 00108 PVARP_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)2); 00109 //exit_timer = new RtosTimer(manage_signals, osTimerOnce, (void *)99); 00110 00111 // init VP thread, HR timer, display timer 00112 VP_timer->start(AVI); 00113 HR_timer->start(sampleRate); 00114 disp_thread->signal_set(RUN); 00115 //exit_timer->start(60000); 00116 00117 // main thread 00118 while (1) { 00119 00120 } 00121 } 00122 00123 void calcHR(void const *args) { 00124 00125 // calc 00126 if (firstSample == 1) { 00127 HR = beats*(60000/sampleRate); 00128 firstSample = 0; 00129 } 00130 else { 00131 HR = (beats*60000/sampleRate+HR)/2; 00132 } 00133 00134 // display 00135 disp_thread->signal_set(RUN); 00136 } 00137 00138 void manage_signals(void const *i) { 00139 if ((int)i==1) isVRP = 0; 00140 if ((int)i==2) isPVARP = 0; 00141 00142 // for debuggging 00143 //if ((int)i==99) { 00144 //fclose(fp); 00145 //exit(1); 00146 //} 00147 } 00148 00149 void AP_func(void const *args) { 00150 00151 // start VP timer 00152 VP_timer->start(AVI); 00153 00154 // send Apace 00155 send_Apace(); 00156 00157 // update state 00158 waitingForV = 1; 00159 00160 // output 00161 event_out("AP",between_a); 00162 between_a = global_t.read_ms(); 00163 00164 // flash LED 00165 flashLED(2); 00166 } 00167 00168 void VP_func(void const *args) { 00169 00170 // start AP timer 00171 AP_timer->start(LRI-AVI); 00172 00173 // send Vpace 00174 send_Vpace(); 00175 00176 // update state 00177 waitingForV = 0; 00178 00179 // set VRP, PVARP, update beats 00180 blind(); 00181 00182 // output 00183 event_out("VP", between_v); 00184 between_v = global_t.read_ms(); 00185 00186 // flash LED 00187 flashLED(1); 00188 } 00189 00190 void AS_func(void const *args) { 00191 while (1) { 00192 00193 // wait for event 00194 Thread::signal_wait(RUN,osWaitForever); 00195 00196 // update state 00197 waitingForV = 1; 00198 00199 // stop AP timer and start VP timer 00200 AP_timer->stop(); 00201 VP_timer->start(AVI); 00202 00203 // output 00204 event_out("AS", between_a); 00205 between_a = global_t.read_ms(); 00206 00207 // flash LED 00208 flashLED(3); 00209 } 00210 } 00211 00212 void VS_func(void const *args) { 00213 while (1) { 00214 00215 // wait for event 00216 Thread::signal_wait(RUN,osWaitForever); 00217 00218 // update state 00219 waitingForV = 0; 00220 00221 // stop VP timer and start AP timer 00222 VP_timer->stop(); 00223 AP_timer->start(LRI-AVI); 00224 00225 // set VRP, PVARP 00226 blind(); 00227 00228 // output 00229 event_out("VS", between_v); 00230 between_v = global_t.read_ms(); 00231 00232 // flash LED 00233 flashLED(4); 00234 } 00235 } 00236 00237 /* 00238 void rand_heart_func(void const *args) { 00239 int interval; 00240 srand(time(NULL)); 00241 while (1) { 00242 interval = rand()%5000+10; 00243 //fprintf(fp,"interval = %d\n",interval); 00244 Thread::wait(interval); 00245 if (interval%2) AG = 1; 00246 else 00247 VG = 1; 00248 } 00249 } 00250 */ 00251 00252 void PM_monitor_func(void const *args) { 00253 while (1) { 00254 if (AGet == 1) { 00255 //fprintf(fp,"%f\tAget\t%d\n",global_t.read_ms(),isPVARP); 00256 //AG = 0; 00257 if (!isPVARP && !waitingForV) AS_thread->signal_set(RUN); 00258 wait(0.1); 00259 while(AGet == 1); 00260 } 00261 if (VGet == 1) { 00262 //fprintf(fp,"%f\tVget\t%d\n",global_t.read_ms(),isVRP); 00263 //VG = 0; 00264 if (!isVRP && waitingForV) VS_thread->signal_set(RUN); 00265 wait(0.1); 00266 while(VGet == 1); 00267 } 00268 } 00269 } 00270 00271 void flashLED(int i) { 00272 leds[i-1] = 1; 00273 wait(0.01); 00274 leds[i-1] = 0; 00275 } 00276 00277 void event_out(char *s, int between_t) { 00278 //lcd.printf("%d\t%s\t%d\n",global_t.read_ms(),s,global_t.read_ms()-between_t); 00279 //fprintf(fp, "%f\t%s\t%f\n",global_t.read_ms(),s,global_t.read_ms()-between_t); 00280 } 00281 00282 void blind() { 00283 beats++; 00284 isVRP = 1; 00285 isPVARP = 1; 00286 VRP_timer->start(VRP); 00287 PVARP_timer->start(PVARP); 00288 } 00289 00290 void disp(void const *args) { 00291 while (1) { 00292 Thread::signal_wait(RUN,osWaitForever); 00293 lcd.printf("HR = %d ppm\nCyle = %d s\n",HR,sampleRate/1000); 00294 beats = 0; 00295 } 00296 } 00297 00298 void send_Apace() { 00299 APace = 1; 00300 Thread::wait(50); 00301 APace = 0; 00302 } 00303 00304 void send_Vpace() { 00305 VPace = 1; 00306 Thread::wait(50); 00307 VPace = 0; 00308 }
Generated on Wed Jul 13 2022 11:37:44 by
1.7.2
