local timers

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_FINAL_v2 by Pacemaker

Committer:
jfields
Date:
Thu Dec 04 04:04:31 2014 +0000
Revision:
7:e3f58c82a4d6
Parent:
6:704de3abb5e9
FINAL

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