local timers

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_FINAL_v2 by Pacemaker

Committer:
jfields
Date:
Thu Dec 04 03:47:04 2014 +0000
Revision:
6:704de3abb5e9
Parent:
5:8cd62e3c3c3a
Child:
7:e3f58c82a4d6
final v2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jfields 4:584d9e2e4fae 1 #include "constants.h"
jfields 0:3afa00a23ce2 2
jfields 5:8cd62e3c3c3a 3 // global timer
jfields 5:8cd62e3c3c3a 4 Timer tv;
jfields 5:8cd62e3c3c3a 5 Timer ta;
jfields 0:3afa00a23ce2 6
jfields 1:ea01c3232c4a 7 // mutexes
jfields 5:8cd62e3c3c3a 8 Mutex t_mutex; // protect reading of ta, tv
jfields 1:ea01c3232c4a 9 Mutex status_mutex; // protect reading of
jfields 1:ea01c3232c4a 10 Mutex input_mutex; // protects reading input
jfields 4:584d9e2e4fae 11 Mutex man_mutex; // protects manual
jfields 4:584d9e2e4fae 12 Mutex state_mutex; // protects waitingForV
jfields 0:3afa00a23ce2 13
jfields 0:3afa00a23ce2 14 // functions
jfields 2:3773afd2256c 15 void A_func(void const *args);
jfields 2:3773afd2256c 16 void V_func(void const *args);
jfields 0:3afa00a23ce2 17 void manage_flags(void const *i);
jfields 0:3afa00a23ce2 18 void flashLED(int i);
jfields 1:ea01c3232c4a 19 void calcHR(void const *args);
jfields 1:ea01c3232c4a 20 void disp(void const *args);
jfields 1:ea01c3232c4a 21 void input_func(void const *args);
jfields 1:ea01c3232c4a 22 void setVals(char c);
jfields 2:3773afd2256c 23 void makeManual();
jfields 2:3773afd2256c 24 void blind();
jfields 3:c60266d9ce0b 25 void get_listener(void const *args);
jfields 4:584d9e2e4fae 26 void updateSR();
jfields 0:3afa00a23ce2 27
jfields 0:3afa00a23ce2 28 // threads
jfields 2:3773afd2256c 29 Thread * A_thread;
jfields 2:3773afd2256c 30 Thread * V_thread;
jfields 1:ea01c3232c4a 31 Thread * input_thread;
jfields 0:3afa00a23ce2 32 Thread * disp_thread;
jfields 3:c60266d9ce0b 33 Thread * listener;
jfields 0:3afa00a23ce2 34
jfields 0:3afa00a23ce2 35 // rtos timers
jfields 0:3afa00a23ce2 36 RtosTimer * VRP_timer;
jfields 0:3afa00a23ce2 37 RtosTimer * PVARP_timer;
jfields 0:3afa00a23ce2 38 RtosTimer * HR_timer;
jfields 0:3afa00a23ce2 39
jfields 0:3afa00a23ce2 40 int main() {
jfields 0:3afa00a23ce2 41
jfields 0:3afa00a23ce2 42 // init threads
jfields 1:ea01c3232c4a 43 disp_thread = new Thread(disp);
jfields 1:ea01c3232c4a 44 input_thread = new Thread(input_func);
jfields 2:3773afd2256c 45 A_thread = new Thread(A_func);
jfields 2:3773afd2256c 46 V_thread = new Thread(V_func);
jfields 3:c60266d9ce0b 47 listener = new Thread(get_listener);
jfields 0:3afa00a23ce2 48
jfields 0:3afa00a23ce2 49 // init timers
jfields 0:3afa00a23ce2 50 VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1);
jfields 0:3afa00a23ce2 51 PVARP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)2);
jfields 0:3afa00a23ce2 52 HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0);
jfields 0:3afa00a23ce2 53
jfields 0:3afa00a23ce2 54 // start display and heart rate sample
jfields 0:3afa00a23ce2 55 HR_timer->start(sampleRate);
jfields 0:3afa00a23ce2 56 disp_thread->signal_set(RUN);
jfields 0:3afa00a23ce2 57
jfields 5:8cd62e3c3c3a 58 // start pacemaker
jfields 5:8cd62e3c3c3a 59 ta.start();
jfields 5:8cd62e3c3c3a 60 tv.start();
jfields 5:8cd62e3c3c3a 61 V_thread->signal_set(RUN);
jfields 5:8cd62e3c3c3a 62
jfields 0:3afa00a23ce2 63 // main thread
jfields 0:3afa00a23ce2 64 while (1) {
jfields 0:3afa00a23ce2 65
jfields 0:3afa00a23ce2 66 }
jfields 0:3afa00a23ce2 67 }
jfields 0:3afa00a23ce2 68
jfields 2:3773afd2256c 69 void A_func(void const *args) {
jfields 2:3773afd2256c 70 while (1) {
jfields 5:8cd62e3c3c3a 71 Thread::signal_wait(RUN, osWaitForever);
jfields 5:8cd62e3c3c3a 72 int done = 0;
jfields 5:8cd62e3c3c3a 73 while (!done) {
jfields 5:8cd62e3c3c3a 74 if (ta.read_ms() >= LRI-AVI) {
jfields 6:704de3abb5e9 75 tv.reset();
jfields 5:8cd62e3c3c3a 76 V_thread->signal_set(RUN);
jfields 5:8cd62e3c3c3a 77 APace = 1;
jfields 5:8cd62e3c3c3a 78 Thread::wait(2);
jfields 5:8cd62e3c3c3a 79 APace = 0;
jfields 5:8cd62e3c3c3a 80 flashLED(2);
jfields 5:8cd62e3c3c3a 81 done = 1;
jfields 5:8cd62e3c3c3a 82 }
jfields 6:704de3abb5e9 83 if (AGet == 1 && !isPVARP) {
jfields 6:704de3abb5e9 84 tv.reset();
jfields 6:704de3abb5e9 85 V_thread->signal_set(RUN);
jfields 6:704de3abb5e9 86 flashLED(4);
jfields 6:704de3abb5e9 87 done = 1;
jfields 6:704de3abb5e9 88 }
jfields 2:3773afd2256c 89 }
jfields 2:3773afd2256c 90 }
jfields 2:3773afd2256c 91 }
jfields 2:3773afd2256c 92
jfields 2:3773afd2256c 93 void V_func(void const *args) {
jfields 2:3773afd2256c 94 while (1) {
jfields 5:8cd62e3c3c3a 95 Thread::signal_wait(RUN, osWaitForever);
jfields 5:8cd62e3c3c3a 96 int done = 0;
jfields 5:8cd62e3c3c3a 97 while (!done) {
jfields 5:8cd62e3c3c3a 98 if (tv.read_ms() >= AVI) {
jfields 6:704de3abb5e9 99 ta.reset();
jfields 5:8cd62e3c3c3a 100 A_thread->signal_set(RUN);
jfields 5:8cd62e3c3c3a 101 VPace = 1;
jfields 5:8cd62e3c3c3a 102 Thread::wait(2);
jfields 5:8cd62e3c3c3a 103 VPace = 0;
jfields 5:8cd62e3c3c3a 104 blind();
jfields 5:8cd62e3c3c3a 105 flashLED(1);
jfields 5:8cd62e3c3c3a 106 done = 1;
jfields 5:8cd62e3c3c3a 107 }
jfields 6:704de3abb5e9 108 if (VGet == 1 && !isVRP) {
jfields 6:704de3abb5e9 109 ta.reset();
jfields 6:704de3abb5e9 110 A_thread->signal_set(RUN);
jfields 6:704de3abb5e9 111 blind();
jfields 6:704de3abb5e9 112 flashLED(3);
jfields 6:704de3abb5e9 113 done = 1;
jfields 6:704de3abb5e9 114 }
jfields 2:3773afd2256c 115 }
jfields 2:3773afd2256c 116 }
jfields 2:3773afd2256c 117 }
jfields 2:3773afd2256c 118
jfields 1:ea01c3232c4a 119 void input_func(void const *args) {
jfields 1:ea01c3232c4a 120 while (1) {
jfields 1:ea01c3232c4a 121 input_mutex.lock();
jfields 1:ea01c3232c4a 122 input=pc.getc();
jfields 1:ea01c3232c4a 123 if (input == 'n') setVals('n');
jfields 1:ea01c3232c4a 124 if (input == 's') setVals('s');
jfields 1:ea01c3232c4a 125 if (input == 'e') setVals('e');
jfields 2:3773afd2256c 126 if (input == 'm') makeManual();
jfields 3:c60266d9ce0b 127 if (input == 'o') {
jfields 3:c60266d9ce0b 128 lcd.printf("Enter\n\n");
jfields 3:c60266d9ce0b 129 Omode = 1;
jfields 3:c60266d9ce0b 130 input = pc.getc();
jfields 4:584d9e2e4fae 131 if (input == '1') {
jfields 4:584d9e2e4fae 132 sampleRate = 10000;
jfields 4:584d9e2e4fae 133 updateSR();
jfields 4:584d9e2e4fae 134 }
jfields 4:584d9e2e4fae 135 if (input == '2') {
jfields 4:584d9e2e4fae 136 sampleRate = 20000;
jfields 4:584d9e2e4fae 137 updateSR();
jfields 4:584d9e2e4fae 138 }
jfields 4:584d9e2e4fae 139 if (input == '3') {
jfields 4:584d9e2e4fae 140 sampleRate = 30000;
jfields 4:584d9e2e4fae 141 updateSR();
jfields 4:584d9e2e4fae 142 }
jfields 4:584d9e2e4fae 143 if (input == '4') {
jfields 4:584d9e2e4fae 144 sampleRate = 60000;
jfields 4:584d9e2e4fae 145 updateSR();
jfields 4:584d9e2e4fae 146 }
jfields 4:584d9e2e4fae 147 if (input == '5') {
jfields 4:584d9e2e4fae 148 sampleRate = 100000;
jfields 4:584d9e2e4fae 149 updateSR();
jfields 4:584d9e2e4fae 150 }
jfields 3:c60266d9ce0b 151 }
jfields 1:ea01c3232c4a 152 input_mutex.unlock();
jfields 1:ea01c3232c4a 153 }
jfields 1:ea01c3232c4a 154 }
jfields 1:ea01c3232c4a 155
jfields 0:3afa00a23ce2 156 void calcHR(void const *args) {
jfields 4:584d9e2e4fae 157 status_mutex.lock();
jfields 0:3afa00a23ce2 158 if (firstSample == 1) {
jfields 0:3afa00a23ce2 159 HR = beats*(60000/sampleRate);
jfields 0:3afa00a23ce2 160 firstSample = 0;
jfields 0:3afa00a23ce2 161 }
jfields 0:3afa00a23ce2 162 else {
jfields 0:3afa00a23ce2 163 HR = (beats*60000/sampleRate+HR)/2;
jfields 0:3afa00a23ce2 164 }
jfields 4:584d9e2e4fae 165 if (HR>=UB || HR<=LB) {
jfields 4:584d9e2e4fae 166 speaker.period(1.0/500.0); // 500hz period
jfields 4:584d9e2e4fae 167 speaker =0.5;
jfields 4:584d9e2e4fae 168 }
jfields 4:584d9e2e4fae 169 else {
jfields 4:584d9e2e4fae 170 speaker=0.0;
jfields 4:584d9e2e4fae 171 }
jfields 4:584d9e2e4fae 172 status_mutex.unlock();
jfields 0:3afa00a23ce2 173 disp_thread->signal_set(RUN);
jfields 0:3afa00a23ce2 174 }
jfields 0:3afa00a23ce2 175
jfields 0:3afa00a23ce2 176 void disp(void const *args) {
jfields 0:3afa00a23ce2 177 while (1) {
jfields 0:3afa00a23ce2 178 Thread::signal_wait(RUN,osWaitForever);
jfields 4:584d9e2e4fae 179 status_mutex.lock();
jfields 3:c60266d9ce0b 180 if (!Omode) {
jfields 3:c60266d9ce0b 181 lcd.printf("HR = %d ppm\nCycle = %d s\n",HR,sampleRate/1000);
jfields 3:c60266d9ce0b 182 }
jfields 0:3afa00a23ce2 183 beats = 0;
jfields 4:584d9e2e4fae 184 status_mutex.unlock();
jfields 0:3afa00a23ce2 185 }
jfields 0:3afa00a23ce2 186 }
jfields 0:3afa00a23ce2 187
jfields 0:3afa00a23ce2 188 void manage_flags(void const *i) {
jfields 1:ea01c3232c4a 189 status_mutex.lock();
jfields 0:3afa00a23ce2 190 if ((int)i==1) isVRP = 0;
jfields 0:3afa00a23ce2 191 if ((int)i==2) isPVARP = 0;
jfields 1:ea01c3232c4a 192 status_mutex.unlock();
jfields 0:3afa00a23ce2 193 }
jfields 0:3afa00a23ce2 194
jfields 0:3afa00a23ce2 195 void flashLED(int i) {
jfields 0:3afa00a23ce2 196 leds[i-1] = 1;
jfields 0:3afa00a23ce2 197 wait(0.01);
jfields 0:3afa00a23ce2 198 leds[i-1] = 0;
jfields 0:3afa00a23ce2 199 }
jfields 0:3afa00a23ce2 200
jfields 0:3afa00a23ce2 201 void blind() {
jfields 4:584d9e2e4fae 202 status_mutex.lock();
jfields 0:3afa00a23ce2 203 isVRP = 1;
jfields 0:3afa00a23ce2 204 isPVARP = 1;
jfields 0:3afa00a23ce2 205 VRP_timer->start(VRP);
jfields 0:3afa00a23ce2 206 PVARP_timer->start(PVARP);
jfields 0:3afa00a23ce2 207 beats++;
jfields 4:584d9e2e4fae 208 status_mutex.unlock();
jfields 0:3afa00a23ce2 209 }
jfields 0:3afa00a23ce2 210
jfields 2:3773afd2256c 211 void makeManual() {
jfields 4:584d9e2e4fae 212 man_mutex.lock();
jfields 3:c60266d9ce0b 213 inManual = 1;
jfields 4:584d9e2e4fae 214 man_mutex.unlock();
jfields 3:c60266d9ce0b 215 UB = 175;
jfields 3:c60266d9ce0b 216 LB = 30;
jfields 3:c60266d9ce0b 217 int done = 0;
jfields 3:c60266d9ce0b 218 while (!done) {
jfields 2:3773afd2256c 219 input = pc.getc();
jfields 2:3773afd2256c 220 if (input == 'v') {
jfields 3:c60266d9ce0b 221 VPace = 1;
jfields 3:c60266d9ce0b 222 Thread::wait(2);
jfields 3:c60266d9ce0b 223 VPace = 0;
jfields 2:3773afd2256c 224 flashLED(1);
jfields 2:3773afd2256c 225 }
jfields 2:3773afd2256c 226 if (input == 'a') {
jfields 3:c60266d9ce0b 227 APace = 1;
jfields 3:c60266d9ce0b 228 Thread::wait(2);
jfields 3:c60266d9ce0b 229 APace = 0;
jfields 2:3773afd2256c 230 flashLED(2);
jfields 2:3773afd2256c 231 }
jfields 3:c60266d9ce0b 232 if (input == 's') {
jfields 3:c60266d9ce0b 233 setVals('s');
jfields 3:c60266d9ce0b 234 done = 1;
jfields 3:c60266d9ce0b 235 }
jfields 3:c60266d9ce0b 236 if (input == 'e') {
jfields 3:c60266d9ce0b 237 setVals('s');
jfields 3:c60266d9ce0b 238 done = 1;
jfields 3:c60266d9ce0b 239 }
jfields 3:c60266d9ce0b 240 if (input == 'n') {
jfields 3:c60266d9ce0b 241 setVals('s');
jfields 3:c60266d9ce0b 242 done = 1;
jfields 3:c60266d9ce0b 243 }
jfields 3:c60266d9ce0b 244 if (input == 'o') {
jfields 3:c60266d9ce0b 245 lcd.printf("Enter\n\n");
jfields 3:c60266d9ce0b 246 Omode = 1;
jfields 3:c60266d9ce0b 247 input = pc.getc();
jfields 4:584d9e2e4fae 248 if (input == '1') {
jfields 4:584d9e2e4fae 249 sampleRate = 10000;
jfields 4:584d9e2e4fae 250 updateSR();
jfields 4:584d9e2e4fae 251 }
jfields 4:584d9e2e4fae 252 if (input == '2') {
jfields 4:584d9e2e4fae 253 sampleRate = 20000;
jfields 4:584d9e2e4fae 254 updateSR();
jfields 4:584d9e2e4fae 255 }
jfields 4:584d9e2e4fae 256 if (input == '3') {
jfields 4:584d9e2e4fae 257 sampleRate = 30000;
jfields 4:584d9e2e4fae 258 updateSR();
jfields 4:584d9e2e4fae 259 }
jfields 4:584d9e2e4fae 260 if (input == '4') {
jfields 4:584d9e2e4fae 261 sampleRate = 60000;
jfields 4:584d9e2e4fae 262 updateSR();
jfields 4:584d9e2e4fae 263 }
jfields 4:584d9e2e4fae 264 if (input == '5') {
jfields 4:584d9e2e4fae 265 sampleRate = 100000;
jfields 4:584d9e2e4fae 266 updateSR();
jfields 4:584d9e2e4fae 267 }
jfields 3:c60266d9ce0b 268 }
jfields 3:c60266d9ce0b 269 }
jfields 3:c60266d9ce0b 270 VRP_timer->stop();
jfields 3:c60266d9ce0b 271 PVARP_timer->stop();
jfields 4:584d9e2e4fae 272 man_mutex.lock();
jfields 3:c60266d9ce0b 273 inManual = 0;
jfields 4:584d9e2e4fae 274 man_mutex.unlock();
jfields 3:c60266d9ce0b 275 }
jfields 3:c60266d9ce0b 276
jfields 3:c60266d9ce0b 277 void get_listener(void const *args) {
jfields 3:c60266d9ce0b 278 while (1) {
jfields 3:c60266d9ce0b 279 if (inManual) {
jfields 3:c60266d9ce0b 280 if (AGet == 1) {
jfields 3:c60266d9ce0b 281 flashLED(4);
jfields 3:c60266d9ce0b 282 while (AGet == 1);
jfields 3:c60266d9ce0b 283 }
jfields 3:c60266d9ce0b 284 if (VGet == 1) {
jfields 3:c60266d9ce0b 285 flashLED(3);
jfields 3:c60266d9ce0b 286 while (VGet == 1);
jfields 3:c60266d9ce0b 287 }
jfields 3:c60266d9ce0b 288 }
jfields 2:3773afd2256c 289 }
jfields 0:3afa00a23ce2 290 }
jfields 1:ea01c3232c4a 291
jfields 1:ea01c3232c4a 292 void setVals(char c) {
jfields 1:ea01c3232c4a 293 if (c == 'n') {
jfields 1:ea01c3232c4a 294 PVARP = N_PVARP;
jfields 1:ea01c3232c4a 295 VRP = N_VRP;
jfields 1:ea01c3232c4a 296 LRI = N_LRI;
jfields 1:ea01c3232c4a 297 AVI = N_AVI;
jfields 1:ea01c3232c4a 298 UB = N_UB;
jfields 1:ea01c3232c4a 299 LB = N_LB;
jfields 1:ea01c3232c4a 300 }
jfields 1:ea01c3232c4a 301 if (c == 's') {
jfields 1:ea01c3232c4a 302 PVARP = S_PVARP;
jfields 1:ea01c3232c4a 303 VRP = S_VRP;
jfields 1:ea01c3232c4a 304 LRI = S_LRI;
jfields 1:ea01c3232c4a 305 AVI = S_AVI;
jfields 1:ea01c3232c4a 306 UB = S_UB;
jfields 1:ea01c3232c4a 307 LB = S_LB;
jfields 1:ea01c3232c4a 308 }
jfields 1:ea01c3232c4a 309 if (c == 'e') {
jfields 1:ea01c3232c4a 310 PVARP = E_PVARP;
jfields 1:ea01c3232c4a 311 VRP = E_VRP;
jfields 1:ea01c3232c4a 312 LRI = E_LRI;
jfields 1:ea01c3232c4a 313 AVI = E_AVI;
jfields 1:ea01c3232c4a 314 UB = E_UB;
jfields 1:ea01c3232c4a 315 LB = E_LB;
jfields 1:ea01c3232c4a 316 }
jfields 1:ea01c3232c4a 317 }
jfields 2:3773afd2256c 318
jfields 4:584d9e2e4fae 319 void updateSR() {
jfields 4:584d9e2e4fae 320 status_mutex.lock();
jfields 4:584d9e2e4fae 321 beats = 0;
jfields 4:584d9e2e4fae 322 HR = 0;
jfields 4:584d9e2e4fae 323 Omode = 0;
jfields 4:584d9e2e4fae 324 firstSample = 1;
jfields 4:584d9e2e4fae 325 HR_timer->stop();
jfields 4:584d9e2e4fae 326 HR_timer->start(sampleRate);
jfields 4:584d9e2e4fae 327 status_mutex.unlock();
jfields 4:584d9e2e4fae 328 disp_thread->signal_set(RUN);
jfields 4:584d9e2e4fae 329 }
jfields 2:3773afd2256c 330
jfields 2:3773afd2256c 331
jfields 2:3773afd2256c 332
jfields 2:3773afd2256c 333
jfields 2:3773afd2256c 334