simpler func

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_v7 by Pacemaker

Committer:
jfields
Date:
Wed Dec 03 22:38:41 2014 +0000
Revision:
1:ea01c3232c4a
Parent:
0:3afa00a23ce2
Child:
2:3773afd2256c
mode switching n,e,s

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);
jfields 0:3afa00a23ce2 83 void VS_func(void const *args);
jfields 0:3afa00a23ce2 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);
jfields 0:3afa00a23ce2 122 VS_thread = new Thread(VS_func);
jfields 0:3afa00a23ce2 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 }
jfields 0:3afa00a23ce2 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 }
jfields 0:3afa00a23ce2 214 }
jfields 0:3afa00a23ce2 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 }
jfields 0:3afa00a23ce2 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();
jfields 0:3afa00a23ce2 230 if (!isVRP && waitingForV) VS_thread->signal_set(RUN);
jfields 1:ea01c3232c4a 231 status_mutex.unlock();
jfields 0:3afa00a23ce2 232 while(VGet == 1);
jfields 0:3afa00a23ce2 233 }
jfields 0:3afa00a23ce2 234 }
jfields 0:3afa00a23ce2 235 }
jfields 0:3afa00a23ce2 236
jfields 0:3afa00a23ce2 237 void listen_Aget(void const *args) {
jfields 0:3afa00a23ce2 238 while (1) {
jfields 0:3afa00a23ce2 239 if (AGet == 1) {
jfields 1:ea01c3232c4a 240 status_mutex.lock();
jfields 0:3afa00a23ce2 241 if (!isPVARP && !waitingForV) AS_thread->signal_set(RUN);
jfields 1:ea01c3232c4a 242 status_mutex.unlock();
jfields 0:3afa00a23ce2 243 while(AGet == 1);
jfields 0:3afa00a23ce2 244 }
jfields 0:3afa00a23ce2 245 }
jfields 0:3afa00a23ce2 246 }
jfields 0:3afa00a23ce2 247
jfields 0:3afa00a23ce2 248 void flashLED(int i) {
jfields 0:3afa00a23ce2 249 leds[i-1] = 1;
jfields 0:3afa00a23ce2 250 wait(0.01);
jfields 0:3afa00a23ce2 251 leds[i-1] = 0;
jfields 0:3afa00a23ce2 252 }
jfields 0:3afa00a23ce2 253
jfields 0:3afa00a23ce2 254 void blind() {
jfields 0:3afa00a23ce2 255 tv.start();
jfields 0:3afa00a23ce2 256 ta.reset();
jfields 0:3afa00a23ce2 257 ta.stop();
jfields 1:ea01c3232c4a 258 status_mutex.lock();
jfields 0:3afa00a23ce2 259 isVRP = 1;
jfields 0:3afa00a23ce2 260 isPVARP = 1;
jfields 1:ea01c3232c4a 261 waitingForV = 0;
jfields 1:ea01c3232c4a 262 status_mutex.unlock();
jfields 0:3afa00a23ce2 263 VRP_timer->start(VRP);
jfields 0:3afa00a23ce2 264 PVARP_timer->start(PVARP);
jfields 0:3afa00a23ce2 265 beats++;
jfields 1:ea01c3232c4a 266
jfields 0:3afa00a23ce2 267 }
jfields 0:3afa00a23ce2 268
jfields 0:3afa00a23ce2 269 void Aevent() {
jfields 0:3afa00a23ce2 270 ta.start();
jfields 0:3afa00a23ce2 271 tv.reset();
jfields 0:3afa00a23ce2 272 tv.stop();
jfields 1:ea01c3232c4a 273 status_mutex.lock();
jfields 0:3afa00a23ce2 274 waitingForV = 1;
jfields 1:ea01c3232c4a 275 status_mutex.unlock();
jfields 0:3afa00a23ce2 276 }
jfields 0:3afa00a23ce2 277
jfields 0:3afa00a23ce2 278 void send_Apace() {
jfields 0:3afa00a23ce2 279 APace = 1;
jfields 1:ea01c3232c4a 280 Thread::wait(2);
jfields 0:3afa00a23ce2 281 APace = 0;
jfields 0:3afa00a23ce2 282 }
jfields 0:3afa00a23ce2 283
jfields 0:3afa00a23ce2 284 void send_Vpace() {
jfields 0:3afa00a23ce2 285 VPace = 1;
jfields 1:ea01c3232c4a 286 Thread::wait(2);
jfields 0:3afa00a23ce2 287 VPace = 0;
jfields 0:3afa00a23ce2 288 }
jfields 1:ea01c3232c4a 289
jfields 1:ea01c3232c4a 290 void setVals(char c) {
jfields 1:ea01c3232c4a 291 if (c == 'n') {
jfields 1:ea01c3232c4a 292 PVARP = N_PVARP;
jfields 1:ea01c3232c4a 293 VRP = N_VRP;
jfields 1:ea01c3232c4a 294 LRI = N_LRI;
jfields 1:ea01c3232c4a 295 AVI = N_AVI;
jfields 1:ea01c3232c4a 296 UB = N_UB;
jfields 1:ea01c3232c4a 297 LB = N_LB;
jfields 1:ea01c3232c4a 298 }
jfields 1:ea01c3232c4a 299 if (c == 's') {
jfields 1:ea01c3232c4a 300 PVARP = S_PVARP;
jfields 1:ea01c3232c4a 301 VRP = S_VRP;
jfields 1:ea01c3232c4a 302 LRI = S_LRI;
jfields 1:ea01c3232c4a 303 AVI = S_AVI;
jfields 1:ea01c3232c4a 304 UB = S_UB;
jfields 1:ea01c3232c4a 305 LB = S_LB;
jfields 1:ea01c3232c4a 306 }
jfields 1:ea01c3232c4a 307 if (c == 'e') {
jfields 1:ea01c3232c4a 308 PVARP = E_PVARP;
jfields 1:ea01c3232c4a 309 VRP = E_VRP;
jfields 1:ea01c3232c4a 310 LRI = E_LRI;
jfields 1:ea01c3232c4a 311 AVI = E_AVI;
jfields 1:ea01c3232c4a 312 UB = E_UB;
jfields 1:ea01c3232c4a 313 LB = E_LB;
jfields 1:ea01c3232c4a 314 }
jfields 1:ea01c3232c4a 315 }