local timers

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_FINAL_v2 by Pacemaker

Committer:
jfields
Date:
Thu Dec 04 03:03:56 2014 +0000
Revision:
4:584d9e2e4fae
Parent:
3:c60266d9ce0b
Child:
5:8cd62e3c3c3a
single global timer

Who changed what in which revision?

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