udpated n,e,s modeswitch

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_v5 by Pacemaker

Committer:
xueliz
Date:
Wed Dec 03 23:28:06 2014 +0000
Revision:
2:4ce21a8fc0d5
Parent:
1:ea01c3232c4a
Child:
3:39870b37d0c4
-Shirley

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jfields 0:3afa00a23ce2 1 #include "mbed.h"
jfields 0:3afa00a23ce2 2 #include "rtos.h"
jfields 0:3afa00a23ce2 3 #include "TextLCD.h"
jfields 0:3afa00a23ce2 4 #include <stdio.h>
jfields 0:3afa00a23ce2 5 #include <stdlib.h>
jfields 0:3afa00a23ce2 6
jfields 0:3afa00a23ce2 7 #define RUN 0x1
jfields 0:3afa00a23ce2 8
jfields 0:3afa00a23ce2 9 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2);
jfields 0:3afa00a23ce2 10 Serial pc (USBTX, USBRX);
jfields 0:3afa00a23ce2 11
jfields 0:3afa00a23ce2 12 // ports
jfields 0:3afa00a23ce2 13 DigitalIn VGet(p11);
jfields 0:3afa00a23ce2 14 DigitalIn AGet(p12);
jfields 0:3afa00a23ce2 15 DigitalOut VPace(p13);
jfields 0:3afa00a23ce2 16 DigitalOut APace(p14);
jfields 0:3afa00a23ce2 17
jfields 0:3afa00a23ce2 18 // LEDs
jfields 0:3afa00a23ce2 19 DigitalOut leds[] = {LED1, LED2, LED3, LED4};
jfields 0:3afa00a23ce2 20 // 1 = VP
jfields 0:3afa00a23ce2 21 // 2 = AP
jfields 0:3afa00a23ce2 22 // 3 = VS
jfields 0:3afa00a23ce2 23 // 4 = AS
jfields 0:3afa00a23ce2 24
jfields 0:3afa00a23ce2 25 // global clocks
jfields 0:3afa00a23ce2 26 Timer ta; // time since a event
jfields 0:3afa00a23ce2 27 Timer tv; // time since v event
jfields 0:3afa00a23ce2 28
jfields 1:ea01c3232c4a 29 // mutexes
jfields 1:ea01c3232c4a 30 Mutex t_mutex; // protect reading of ta, tv
jfields 1:ea01c3232c4a 31 Mutex status_mutex; // protect reading of
jfields 1:ea01c3232c4a 32 Mutex input_mutex; // protects reading input
jfields 1:ea01c3232c4a 33
jfields 1:ea01c3232c4a 34 // input stuff
jfields 1:ea01c3232c4a 35 char input;
jfields 1:ea01c3232c4a 36
jfields 0:3afa00a23ce2 37 // heart rate global vars
jfields 0:3afa00a23ce2 38 int HR = 0;
jfields 0:3afa00a23ce2 39 int beats = 0;
jfields 0:3afa00a23ce2 40 int sampleRate = 10000; // default 10 seconds
jfields 0:3afa00a23ce2 41 int firstSample = 1;
jfields 0:3afa00a23ce2 42
jfields 0:3afa00a23ce2 43 // Normal Values
jfields 0:3afa00a23ce2 44 const int N_PVARP = 325; // ms
jfields 0:3afa00a23ce2 45 const int N_VRP = 300; // ms
jfields 0:3afa00a23ce2 46 const int N_LRI = 857; // ms (= about 70ppm)
jfields 0:3afa00a23ce2 47 const int N_AVI = 65; // ms
jfields 0:3afa00a23ce2 48 const int N_UB = 100; // 100ppm
jfields 0:3afa00a23ce2 49 const int N_LB = 40; // 40ppm
jfields 0:3afa00a23ce2 50
jfields 1:ea01c3232c4a 51 // Exercise Values
jfields 1:ea01c3232c4a 52 const int E_PVARP = 175; // ms
jfields 1:ea01c3232c4a 53 const int E_VRP = 150; // ms
jfields 1:ea01c3232c4a 54 const int E_LRI = 428; // ms (= about 140ppm)
jfields 1:ea01c3232c4a 55 const int E_AVI = 30; // ms
jfields 1:ea01c3232c4a 56 const int E_UB = 175; // 175ppm
jfields 1:ea01c3232c4a 57 const int E_LB = 100; // 100ppm
jfields 1:ea01c3232c4a 58
jfields 1:ea01c3232c4a 59 // Sleep Values
jfields 1:ea01c3232c4a 60 const int S_PVARP = 500; // ms
jfields 1:ea01c3232c4a 61 const int S_VRP = 475; // ms
jfields 1:ea01c3232c4a 62 const int S_LRI = 1333; // ms (= about 45ppm)
jfields 1:ea01c3232c4a 63 const int S_AVI = 100; // ms
jfields 1:ea01c3232c4a 64 const int S_UB = 60; // 60ppm
jfields 1:ea01c3232c4a 65 const int S_LB = 30; // 30ppm
jfields 1:ea01c3232c4a 66
jfields 0:3afa00a23ce2 67 // Heart Values - Normal Mode is default
jfields 0:3afa00a23ce2 68 int PVARP = N_PVARP;
jfields 0:3afa00a23ce2 69 int VRP = N_VRP;
jfields 0:3afa00a23ce2 70 int LRI = N_LRI;
jfields 0:3afa00a23ce2 71 int AVI = N_AVI;
jfields 0:3afa00a23ce2 72 int UB = N_UB;
jfields 0:3afa00a23ce2 73 int LB = N_LB;
jfields 0:3afa00a23ce2 74
jfields 0:3afa00a23ce2 75 // status flags
jfields 0:3afa00a23ce2 76 int isVRP = 0;
jfields 0:3afa00a23ce2 77 int isPVARP = 0;
jfields 0:3afa00a23ce2 78 int waitingForV = 1;
jfields 0:3afa00a23ce2 79
jfields 0:3afa00a23ce2 80 // functions
jfields 0:3afa00a23ce2 81 void VP_func(void const *args);
jfields 0:3afa00a23ce2 82 void AP_func(void const *args);
xueliz 2:4ce21a8fc0d5 83 //void VS_func(void const *args);
xueliz 2:4ce21a8fc0d5 84 //void AS_func(void const *args);
jfields 0:3afa00a23ce2 85 void manage_flags(void const *i);
jfields 0:3afa00a23ce2 86 void send_Apace();
jfields 0:3afa00a23ce2 87 void send_Vpace();
jfields 0:3afa00a23ce2 88 void listen_Aget(void const *args);
jfields 0:3afa00a23ce2 89 void listen_Vget(void const *args);
jfields 0:3afa00a23ce2 90 void flashLED(int i);
jfields 0:3afa00a23ce2 91 void blind();
jfields 0:3afa00a23ce2 92 void Aevent();
jfields 1:ea01c3232c4a 93 void calcHR(void const *args);
jfields 1:ea01c3232c4a 94 void disp(void const *args);
jfields 1:ea01c3232c4a 95 void input_func(void const *args);
jfields 1:ea01c3232c4a 96 void setVals(char c);
jfields 0:3afa00a23ce2 97
jfields 0:3afa00a23ce2 98 // threads
jfields 0:3afa00a23ce2 99 Thread * VS_thread;
jfields 0:3afa00a23ce2 100 Thread * AS_thread;
jfields 0:3afa00a23ce2 101 Thread * VP_thread;
jfields 0:3afa00a23ce2 102 Thread * AP_thread;
jfields 0:3afa00a23ce2 103 Thread * VG_thread;
jfields 0:3afa00a23ce2 104 Thread * AG_thread;
jfields 1:ea01c3232c4a 105 Thread * input_thread;
jfields 0:3afa00a23ce2 106 Thread * disp_thread;
jfields 0:3afa00a23ce2 107
jfields 0:3afa00a23ce2 108 // rtos timers
jfields 0:3afa00a23ce2 109 RtosTimer * VRP_timer;
jfields 0:3afa00a23ce2 110 RtosTimer * PVARP_timer;
jfields 0:3afa00a23ce2 111 RtosTimer * HR_timer;
jfields 0:3afa00a23ce2 112
jfields 0:3afa00a23ce2 113 int main() {
jfields 0:3afa00a23ce2 114
jfields 0:3afa00a23ce2 115 // start global timer
jfields 0:3afa00a23ce2 116 tv.start();
jfields 0:3afa00a23ce2 117 ta.start();
jfields 0:3afa00a23ce2 118 tv.stop();
jfields 0:3afa00a23ce2 119
jfields 0:3afa00a23ce2 120 // init threads
jfields 1:ea01c3232c4a 121 disp_thread = new Thread(disp);
xueliz 2:4ce21a8fc0d5 122 // VS_thread = new Thread(VS_func);
xueliz 2:4ce21a8fc0d5 123 // AS_thread = new Thread(AS_func);
jfields 0:3afa00a23ce2 124 VP_thread = new Thread(VP_func);
jfields 0:3afa00a23ce2 125 AP_thread = new Thread(AP_func);
jfields 0:3afa00a23ce2 126 VG_thread = new Thread(listen_Vget);
jfields 0:3afa00a23ce2 127 AG_thread = new Thread(listen_Aget);
jfields 1:ea01c3232c4a 128 input_thread = new Thread(input_func);
jfields 0:3afa00a23ce2 129
jfields 0:3afa00a23ce2 130 // init timers
jfields 0:3afa00a23ce2 131 VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1);
jfields 0:3afa00a23ce2 132 PVARP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)2);
jfields 0:3afa00a23ce2 133 HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0);
jfields 0:3afa00a23ce2 134
jfields 0:3afa00a23ce2 135 // start display and heart rate sample
jfields 0:3afa00a23ce2 136 HR_timer->start(sampleRate);
jfields 0:3afa00a23ce2 137 disp_thread->signal_set(RUN);
jfields 0:3afa00a23ce2 138
jfields 0:3afa00a23ce2 139 // main thread
jfields 0:3afa00a23ce2 140 while (1) {
jfields 0:3afa00a23ce2 141
jfields 0:3afa00a23ce2 142 }
jfields 0:3afa00a23ce2 143 }
jfields 0:3afa00a23ce2 144
jfields 1:ea01c3232c4a 145 void input_func(void const *args) {
jfields 1:ea01c3232c4a 146 while (1) {
jfields 1:ea01c3232c4a 147 input_mutex.lock();
jfields 1:ea01c3232c4a 148 input=pc.getc();
jfields 1:ea01c3232c4a 149 if (input == 'n') setVals('n');
jfields 1:ea01c3232c4a 150 if (input == 's') setVals('s');
jfields 1:ea01c3232c4a 151 if (input == 'e') setVals('e');
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 0:3afa00a23ce2 157 if (firstSample == 1) {
jfields 0:3afa00a23ce2 158 HR = beats*(60000/sampleRate);
jfields 0:3afa00a23ce2 159 firstSample = 0;
jfields 0:3afa00a23ce2 160 }
jfields 0:3afa00a23ce2 161 else {
jfields 0:3afa00a23ce2 162 HR = (beats*60000/sampleRate+HR)/2;
jfields 0:3afa00a23ce2 163 }
jfields 0:3afa00a23ce2 164 disp_thread->signal_set(RUN);
jfields 0:3afa00a23ce2 165 }
jfields 0:3afa00a23ce2 166
jfields 0:3afa00a23ce2 167 void disp(void const *args) {
jfields 0:3afa00a23ce2 168 while (1) {
jfields 0:3afa00a23ce2 169 Thread::signal_wait(RUN,osWaitForever);
jfields 0:3afa00a23ce2 170 lcd.printf("HR = %d ppm\nCyle = %d s\n",HR,sampleRate/1000);
jfields 0:3afa00a23ce2 171 beats = 0;
jfields 0:3afa00a23ce2 172 }
jfields 0:3afa00a23ce2 173 }
jfields 0:3afa00a23ce2 174
jfields 0:3afa00a23ce2 175 void manage_flags(void const *i) {
jfields 1:ea01c3232c4a 176 status_mutex.lock();
jfields 0:3afa00a23ce2 177 if ((int)i==1) isVRP = 0;
jfields 0:3afa00a23ce2 178 if ((int)i==2) isPVARP = 0;
jfields 1:ea01c3232c4a 179 status_mutex.unlock();
jfields 0:3afa00a23ce2 180 }
jfields 0:3afa00a23ce2 181
jfields 0:3afa00a23ce2 182 void AP_func(void const *args) {
jfields 0:3afa00a23ce2 183 while (1) {
jfields 1:ea01c3232c4a 184 t_mutex.lock();
jfields 0:3afa00a23ce2 185 if (tv.read_ms() >= (LRI-AVI)) {
jfields 0:3afa00a23ce2 186 Aevent();
jfields 0:3afa00a23ce2 187 send_Apace();
jfields 0:3afa00a23ce2 188 flashLED(2);
jfields 0:3afa00a23ce2 189 }
jfields 1:ea01c3232c4a 190 t_mutex.unlock();
jfields 0:3afa00a23ce2 191 }
jfields 0:3afa00a23ce2 192 }
jfields 0:3afa00a23ce2 193
jfields 0:3afa00a23ce2 194 void VP_func(void const *args) {
jfields 0:3afa00a23ce2 195 while (1) {
jfields 1:ea01c3232c4a 196 t_mutex.lock();
jfields 0:3afa00a23ce2 197 if (ta.read_ms() >= AVI) {
jfields 0:3afa00a23ce2 198 blind();
jfields 0:3afa00a23ce2 199 send_Vpace();
jfields 0:3afa00a23ce2 200 flashLED(1);
jfields 0:3afa00a23ce2 201 }
jfields 1:ea01c3232c4a 202 t_mutex.unlock();
jfields 0:3afa00a23ce2 203 }
jfields 0:3afa00a23ce2 204 }
xueliz 2:4ce21a8fc0d5 205 /*
jfields 0:3afa00a23ce2 206 void AS_func(void const *args) {
jfields 0:3afa00a23ce2 207 while (1) {
jfields 0:3afa00a23ce2 208 Thread::signal_wait(RUN,osWaitForever);
jfields 1:ea01c3232c4a 209 t_mutex.lock();
jfields 0:3afa00a23ce2 210 Aevent();
jfields 0:3afa00a23ce2 211 flashLED(4);
jfields 1:ea01c3232c4a 212 t_mutex.unlock();
jfields 0:3afa00a23ce2 213 }
xueliz 2:4ce21a8fc0d5 214 }*/
xueliz 2:4ce21a8fc0d5 215 /*
jfields 0:3afa00a23ce2 216 void VS_func(void const *args) {
jfields 0:3afa00a23ce2 217 while (1) {
jfields 0:3afa00a23ce2 218 Thread::signal_wait(RUN,osWaitForever);
jfields 1:ea01c3232c4a 219 t_mutex.lock();
jfields 0:3afa00a23ce2 220 blind();
jfields 1:ea01c3232c4a 221 flashLED(3);
jfields 1:ea01c3232c4a 222 t_mutex.unlock();
jfields 0:3afa00a23ce2 223 }
jfields 0:3afa00a23ce2 224 }
xueliz 2:4ce21a8fc0d5 225 */
jfields 0:3afa00a23ce2 226 void listen_Vget(void const *args) {
jfields 0:3afa00a23ce2 227 while (1) {
jfields 0:3afa00a23ce2 228 if (VGet==1) {
jfields 1:ea01c3232c4a 229 status_mutex.lock();
xueliz 2:4ce21a8fc0d5 230 if (!isVRP && waitingForV){
xueliz 2:4ce21a8fc0d5 231 t_mutex.lock();
xueliz 2:4ce21a8fc0d5 232 blind();
xueliz 2:4ce21a8fc0d5 233 flashLED(3);
xueliz 2:4ce21a8fc0d5 234 t_mutex.unlock();
xueliz 2:4ce21a8fc0d5 235 status_mutex.unlock();
xueliz 2:4ce21a8fc0d5 236 }
jfields 0:3afa00a23ce2 237 while(VGet == 1);
jfields 0:3afa00a23ce2 238 }
jfields 0:3afa00a23ce2 239 }
jfields 0:3afa00a23ce2 240 }
jfields 0:3afa00a23ce2 241
jfields 0:3afa00a23ce2 242 void listen_Aget(void const *args) {
jfields 0:3afa00a23ce2 243 while (1) {
jfields 0:3afa00a23ce2 244 if (AGet == 1) {
jfields 1:ea01c3232c4a 245 status_mutex.lock();
xueliz 2:4ce21a8fc0d5 246 if (!isPVARP && !waitingForV) {
xueliz 2:4ce21a8fc0d5 247 t_mutex.lock();
xueliz 2:4ce21a8fc0d5 248 ta.start();
xueliz 2:4ce21a8fc0d5 249 tv.reset();
xueliz 2:4ce21a8fc0d5 250 tv.stop();
xueliz 2:4ce21a8fc0d5 251
xueliz 2:4ce21a8fc0d5 252 waitingForV = 1;
xueliz 2:4ce21a8fc0d5 253
xueliz 2:4ce21a8fc0d5 254 t_mutex.unlock();
xueliz 2:4ce21a8fc0d5 255 flashLED(4);
xueliz 2:4ce21a8fc0d5 256 }
jfields 1:ea01c3232c4a 257 status_mutex.unlock();
jfields 0:3afa00a23ce2 258 while(AGet == 1);
jfields 0:3afa00a23ce2 259 }
jfields 0:3afa00a23ce2 260 }
jfields 0:3afa00a23ce2 261 }
jfields 0:3afa00a23ce2 262
jfields 0:3afa00a23ce2 263 void flashLED(int i) {
jfields 0:3afa00a23ce2 264 leds[i-1] = 1;
jfields 0:3afa00a23ce2 265 wait(0.01);
jfields 0:3afa00a23ce2 266 leds[i-1] = 0;
jfields 0:3afa00a23ce2 267 }
jfields 0:3afa00a23ce2 268
jfields 0:3afa00a23ce2 269 void blind() {
jfields 0:3afa00a23ce2 270 tv.start();
jfields 0:3afa00a23ce2 271 ta.reset();
jfields 0:3afa00a23ce2 272 ta.stop();
jfields 1:ea01c3232c4a 273 status_mutex.lock();
jfields 0:3afa00a23ce2 274 isVRP = 1;
jfields 0:3afa00a23ce2 275 isPVARP = 1;
jfields 1:ea01c3232c4a 276 waitingForV = 0;
jfields 1:ea01c3232c4a 277 status_mutex.unlock();
jfields 0:3afa00a23ce2 278 VRP_timer->start(VRP);
jfields 0:3afa00a23ce2 279 PVARP_timer->start(PVARP);
jfields 0:3afa00a23ce2 280 beats++;
jfields 1:ea01c3232c4a 281
jfields 0:3afa00a23ce2 282 }
jfields 0:3afa00a23ce2 283
jfields 0:3afa00a23ce2 284 void Aevent() {
jfields 0:3afa00a23ce2 285 ta.start();
jfields 0:3afa00a23ce2 286 tv.reset();
jfields 0:3afa00a23ce2 287 tv.stop();
jfields 1:ea01c3232c4a 288 status_mutex.lock();
jfields 0:3afa00a23ce2 289 waitingForV = 1;
jfields 1:ea01c3232c4a 290 status_mutex.unlock();
jfields 0:3afa00a23ce2 291 }
jfields 0:3afa00a23ce2 292
jfields 0:3afa00a23ce2 293 void send_Apace() {
jfields 0:3afa00a23ce2 294 APace = 1;
jfields 1:ea01c3232c4a 295 Thread::wait(2);
jfields 0:3afa00a23ce2 296 APace = 0;
jfields 0:3afa00a23ce2 297 }
jfields 0:3afa00a23ce2 298
jfields 0:3afa00a23ce2 299 void send_Vpace() {
jfields 0:3afa00a23ce2 300 VPace = 1;
jfields 1:ea01c3232c4a 301 Thread::wait(2);
jfields 0:3afa00a23ce2 302 VPace = 0;
jfields 0:3afa00a23ce2 303 }
jfields 1:ea01c3232c4a 304
jfields 1:ea01c3232c4a 305 void setVals(char c) {
jfields 1:ea01c3232c4a 306 if (c == 'n') {
jfields 1:ea01c3232c4a 307 PVARP = N_PVARP;
jfields 1:ea01c3232c4a 308 VRP = N_VRP;
jfields 1:ea01c3232c4a 309 LRI = N_LRI;
jfields 1:ea01c3232c4a 310 AVI = N_AVI;
jfields 1:ea01c3232c4a 311 UB = N_UB;
jfields 1:ea01c3232c4a 312 LB = N_LB;
jfields 1:ea01c3232c4a 313 }
jfields 1:ea01c3232c4a 314 if (c == 's') {
jfields 1:ea01c3232c4a 315 PVARP = S_PVARP;
jfields 1:ea01c3232c4a 316 VRP = S_VRP;
jfields 1:ea01c3232c4a 317 LRI = S_LRI;
jfields 1:ea01c3232c4a 318 AVI = S_AVI;
jfields 1:ea01c3232c4a 319 UB = S_UB;
jfields 1:ea01c3232c4a 320 LB = S_LB;
jfields 1:ea01c3232c4a 321 }
jfields 1:ea01c3232c4a 322 if (c == 'e') {
jfields 1:ea01c3232c4a 323 PVARP = E_PVARP;
jfields 1:ea01c3232c4a 324 VRP = E_VRP;
jfields 1:ea01c3232c4a 325 LRI = E_LRI;
jfields 1:ea01c3232c4a 326 AVI = E_AVI;
jfields 1:ea01c3232c4a 327 UB = E_UB;
jfields 1:ea01c3232c4a 328 LB = E_LB;
jfields 1:ea01c3232c4a 329 }
jfields 1:ea01c3232c4a 330 }