single global timer

Dependencies:   TextLCD mbed-rtos mbed

Fork of pacemaker_v8 by Pacemaker

Committer:
jfields
Date:
Wed Dec 03 23:57:02 2014 +0000
Revision:
2:3773afd2256c
Parent:
1:ea01c3232c4a
Child:
3:c60266d9ce0b
simpler func

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 2:3773afd2256c 33 Mutex VP_helper;
jfields 1:ea01c3232c4a 34
jfields 1:ea01c3232c4a 35 // input stuff
jfields 1:ea01c3232c4a 36 char input;
jfields 1:ea01c3232c4a 37
jfields 0:3afa00a23ce2 38 // heart rate global vars
jfields 0:3afa00a23ce2 39 int HR = 0;
jfields 0:3afa00a23ce2 40 int beats = 0;
jfields 0:3afa00a23ce2 41 int sampleRate = 10000; // default 10 seconds
jfields 0:3afa00a23ce2 42 int firstSample = 1;
jfields 0:3afa00a23ce2 43
jfields 0:3afa00a23ce2 44 // Normal Values
jfields 0:3afa00a23ce2 45 const int N_PVARP = 325; // ms
jfields 0:3afa00a23ce2 46 const int N_VRP = 300; // ms
jfields 0:3afa00a23ce2 47 const int N_LRI = 857; // ms (= about 70ppm)
jfields 0:3afa00a23ce2 48 const int N_AVI = 65; // ms
jfields 0:3afa00a23ce2 49 const int N_UB = 100; // 100ppm
jfields 0:3afa00a23ce2 50 const int N_LB = 40; // 40ppm
jfields 0:3afa00a23ce2 51
jfields 1:ea01c3232c4a 52 // Exercise Values
jfields 1:ea01c3232c4a 53 const int E_PVARP = 175; // ms
jfields 1:ea01c3232c4a 54 const int E_VRP = 150; // ms
jfields 1:ea01c3232c4a 55 const int E_LRI = 428; // ms (= about 140ppm)
jfields 1:ea01c3232c4a 56 const int E_AVI = 30; // ms
jfields 1:ea01c3232c4a 57 const int E_UB = 175; // 175ppm
jfields 1:ea01c3232c4a 58 const int E_LB = 100; // 100ppm
jfields 1:ea01c3232c4a 59
jfields 1:ea01c3232c4a 60 // Sleep Values
jfields 1:ea01c3232c4a 61 const int S_PVARP = 500; // ms
jfields 1:ea01c3232c4a 62 const int S_VRP = 475; // ms
jfields 1:ea01c3232c4a 63 const int S_LRI = 1333; // ms (= about 45ppm)
jfields 1:ea01c3232c4a 64 const int S_AVI = 100; // ms
jfields 1:ea01c3232c4a 65 const int S_UB = 60; // 60ppm
jfields 1:ea01c3232c4a 66 const int S_LB = 30; // 30ppm
jfields 1:ea01c3232c4a 67
jfields 0:3afa00a23ce2 68 // Heart Values - Normal Mode is default
jfields 0:3afa00a23ce2 69 int PVARP = N_PVARP;
jfields 0:3afa00a23ce2 70 int VRP = N_VRP;
jfields 0:3afa00a23ce2 71 int LRI = N_LRI;
jfields 0:3afa00a23ce2 72 int AVI = N_AVI;
jfields 0:3afa00a23ce2 73 int UB = N_UB;
jfields 0:3afa00a23ce2 74 int LB = N_LB;
jfields 0:3afa00a23ce2 75
jfields 0:3afa00a23ce2 76 // status flags
jfields 0:3afa00a23ce2 77 int isVRP = 0;
jfields 0:3afa00a23ce2 78 int isPVARP = 0;
jfields 0:3afa00a23ce2 79 int waitingForV = 1;
jfields 0:3afa00a23ce2 80
jfields 0:3afa00a23ce2 81 // functions
jfields 2:3773afd2256c 82 void A_func(void const *args);
jfields 2:3773afd2256c 83 void V_func(void const *args);
jfields 0:3afa00a23ce2 84 void manage_flags(void const *i);
jfields 0:3afa00a23ce2 85 void flashLED(int i);
jfields 1:ea01c3232c4a 86 void calcHR(void const *args);
jfields 1:ea01c3232c4a 87 void disp(void const *args);
jfields 1:ea01c3232c4a 88 void input_func(void const *args);
jfields 1:ea01c3232c4a 89 void setVals(char c);
jfields 2:3773afd2256c 90 void makeManual();
jfields 2:3773afd2256c 91 void blind();
jfields 0:3afa00a23ce2 92
jfields 0:3afa00a23ce2 93 // threads
jfields 2:3773afd2256c 94 Thread * A_thread;
jfields 2:3773afd2256c 95 Thread * V_thread;
jfields 1:ea01c3232c4a 96 Thread * input_thread;
jfields 0:3afa00a23ce2 97 Thread * disp_thread;
jfields 2:3773afd2256c 98 Thread * send_Vpace_thread;
jfields 2:3773afd2256c 99 Thread * send_Apace_thread;
jfields 0:3afa00a23ce2 100
jfields 0:3afa00a23ce2 101 // rtos timers
jfields 0:3afa00a23ce2 102 RtosTimer * VRP_timer;
jfields 0:3afa00a23ce2 103 RtosTimer * PVARP_timer;
jfields 0:3afa00a23ce2 104 RtosTimer * HR_timer;
jfields 0:3afa00a23ce2 105
jfields 0:3afa00a23ce2 106 int main() {
jfields 0:3afa00a23ce2 107
jfields 0:3afa00a23ce2 108 // start global timer
jfields 0:3afa00a23ce2 109 tv.start();
jfields 0:3afa00a23ce2 110 ta.start();
jfields 0:3afa00a23ce2 111 tv.stop();
jfields 0:3afa00a23ce2 112
jfields 0:3afa00a23ce2 113 // init threads
jfields 1:ea01c3232c4a 114 disp_thread = new Thread(disp);
jfields 1:ea01c3232c4a 115 input_thread = new Thread(input_func);
jfields 2:3773afd2256c 116 A_thread = new Thread(A_func);
jfields 2:3773afd2256c 117 V_thread = new Thread(V_func);
jfields 0:3afa00a23ce2 118
jfields 0:3afa00a23ce2 119 // init timers
jfields 0:3afa00a23ce2 120 VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1);
jfields 0:3afa00a23ce2 121 PVARP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)2);
jfields 0:3afa00a23ce2 122 HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0);
jfields 0:3afa00a23ce2 123
jfields 0:3afa00a23ce2 124 // start display and heart rate sample
jfields 0:3afa00a23ce2 125 HR_timer->start(sampleRate);
jfields 0:3afa00a23ce2 126 disp_thread->signal_set(RUN);
jfields 0:3afa00a23ce2 127
jfields 0:3afa00a23ce2 128 // main thread
jfields 0:3afa00a23ce2 129 while (1) {
jfields 0:3afa00a23ce2 130
jfields 0:3afa00a23ce2 131 }
jfields 0:3afa00a23ce2 132 }
jfields 0:3afa00a23ce2 133
jfields 2:3773afd2256c 134 void A_func(void const *args) {
jfields 2:3773afd2256c 135 while (1) {
jfields 2:3773afd2256c 136 while (tv.read_ms() <= (LRI-AVI)) {
jfields 2:3773afd2256c 137 if (AGet==1 && !isPVARP && !waitingForV) {
jfields 2:3773afd2256c 138 tv.reset();
jfields 2:3773afd2256c 139 tv.stop();
jfields 2:3773afd2256c 140 ta.start();
jfields 2:3773afd2256c 141 waitingForV = 1;
jfields 2:3773afd2256c 142 flashLED(4);
jfields 2:3773afd2256c 143 while (AGet == 1);
jfields 2:3773afd2256c 144 }
jfields 2:3773afd2256c 145 }
jfields 2:3773afd2256c 146 APace = 1;
jfields 2:3773afd2256c 147 Thread::wait(2);
jfields 2:3773afd2256c 148 APace = 0;
jfields 2:3773afd2256c 149 tv.reset();
jfields 2:3773afd2256c 150 tv.stop();
jfields 2:3773afd2256c 151 ta.start();
jfields 2:3773afd2256c 152 waitingForV = 1;
jfields 2:3773afd2256c 153 flashLED(2);
jfields 2:3773afd2256c 154 }
jfields 2:3773afd2256c 155 }
jfields 2:3773afd2256c 156
jfields 2:3773afd2256c 157 void V_func(void const *args) {
jfields 2:3773afd2256c 158 while (1) {
jfields 2:3773afd2256c 159 while (ta.read_ms() <= AVI) {
jfields 2:3773afd2256c 160 if (VGet==1 && !isVRP && waitingForV) {
jfields 2:3773afd2256c 161 blind();
jfields 2:3773afd2256c 162 flashLED(3);
jfields 2:3773afd2256c 163 while (VGet == 1);
jfields 2:3773afd2256c 164 }
jfields 2:3773afd2256c 165 }
jfields 2:3773afd2256c 166 VPace = 1;
jfields 2:3773afd2256c 167 Thread::wait(2);
jfields 2:3773afd2256c 168 VPace = 0;
jfields 2:3773afd2256c 169 blind();
jfields 2:3773afd2256c 170 flashLED(1);
jfields 2:3773afd2256c 171 }
jfields 2:3773afd2256c 172 }
jfields 2:3773afd2256c 173
jfields 1:ea01c3232c4a 174 void input_func(void const *args) {
jfields 1:ea01c3232c4a 175 while (1) {
jfields 1:ea01c3232c4a 176 input_mutex.lock();
jfields 1:ea01c3232c4a 177 input=pc.getc();
jfields 1:ea01c3232c4a 178 if (input == 'n') setVals('n');
jfields 1:ea01c3232c4a 179 if (input == 's') setVals('s');
jfields 1:ea01c3232c4a 180 if (input == 'e') setVals('e');
jfields 2:3773afd2256c 181 if (input == 'm') makeManual();
jfields 1:ea01c3232c4a 182 input_mutex.unlock();
jfields 1:ea01c3232c4a 183 }
jfields 1:ea01c3232c4a 184 }
jfields 1:ea01c3232c4a 185
jfields 0:3afa00a23ce2 186 void calcHR(void const *args) {
jfields 0:3afa00a23ce2 187 if (firstSample == 1) {
jfields 0:3afa00a23ce2 188 HR = beats*(60000/sampleRate);
jfields 0:3afa00a23ce2 189 firstSample = 0;
jfields 0:3afa00a23ce2 190 }
jfields 0:3afa00a23ce2 191 else {
jfields 0:3afa00a23ce2 192 HR = (beats*60000/sampleRate+HR)/2;
jfields 0:3afa00a23ce2 193 }
jfields 0:3afa00a23ce2 194 disp_thread->signal_set(RUN);
jfields 0:3afa00a23ce2 195 }
jfields 0:3afa00a23ce2 196
jfields 0:3afa00a23ce2 197 void disp(void const *args) {
jfields 0:3afa00a23ce2 198 while (1) {
jfields 0:3afa00a23ce2 199 Thread::signal_wait(RUN,osWaitForever);
jfields 0:3afa00a23ce2 200 lcd.printf("HR = %d ppm\nCyle = %d s\n",HR,sampleRate/1000);
jfields 0:3afa00a23ce2 201 beats = 0;
jfields 0:3afa00a23ce2 202 }
jfields 0:3afa00a23ce2 203 }
jfields 0:3afa00a23ce2 204
jfields 0:3afa00a23ce2 205 void manage_flags(void const *i) {
jfields 1:ea01c3232c4a 206 status_mutex.lock();
jfields 0:3afa00a23ce2 207 if ((int)i==1) isVRP = 0;
jfields 0:3afa00a23ce2 208 if ((int)i==2) isPVARP = 0;
jfields 1:ea01c3232c4a 209 status_mutex.unlock();
jfields 0:3afa00a23ce2 210 }
jfields 0:3afa00a23ce2 211
jfields 0:3afa00a23ce2 212 void flashLED(int i) {
jfields 0:3afa00a23ce2 213 leds[i-1] = 1;
jfields 0:3afa00a23ce2 214 wait(0.01);
jfields 0:3afa00a23ce2 215 leds[i-1] = 0;
jfields 0:3afa00a23ce2 216 }
jfields 0:3afa00a23ce2 217
jfields 0:3afa00a23ce2 218 void blind() {
jfields 0:3afa00a23ce2 219 tv.start();
jfields 0:3afa00a23ce2 220 ta.reset();
jfields 0:3afa00a23ce2 221 ta.stop();
jfields 0:3afa00a23ce2 222 isVRP = 1;
jfields 0:3afa00a23ce2 223 isPVARP = 1;
jfields 1:ea01c3232c4a 224 waitingForV = 0;
jfields 0:3afa00a23ce2 225 VRP_timer->start(VRP);
jfields 0:3afa00a23ce2 226 PVARP_timer->start(PVARP);
jfields 0:3afa00a23ce2 227 beats++;
jfields 0:3afa00a23ce2 228 }
jfields 0:3afa00a23ce2 229
jfields 2:3773afd2256c 230 void makeManual() {
jfields 2:3773afd2256c 231 t_mutex.lock();
jfields 2:3773afd2256c 232 ta.reset();
jfields 0:3afa00a23ce2 233 tv.reset();
jfields 2:3773afd2256c 234 ta.stop();
jfields 0:3afa00a23ce2 235 tv.stop();
jfields 2:3773afd2256c 236 t_mutex.unlock();
jfields 2:3773afd2256c 237 while (1) {
jfields 2:3773afd2256c 238 input = pc.getc();
jfields 2:3773afd2256c 239 if (input == 'v') {
jfields 2:3773afd2256c 240 send_Vpace_thread->signal_set(RUN);
jfields 2:3773afd2256c 241 flashLED(1);
jfields 2:3773afd2256c 242 }
jfields 2:3773afd2256c 243 if (input == 'a') {
jfields 2:3773afd2256c 244 send_Apace_thread->signal_set(RUN);
jfields 2:3773afd2256c 245 flashLED(2);
jfields 2:3773afd2256c 246 }
jfields 2:3773afd2256c 247 t_mutex.lock();
jfields 2:3773afd2256c 248 ta.reset();
jfields 2:3773afd2256c 249 tv.reset();
jfields 2:3773afd2256c 250 ta.stop();
jfields 2:3773afd2256c 251 tv.stop();
jfields 2:3773afd2256c 252 t_mutex.unlock();
jfields 2:3773afd2256c 253 }
jfields 0:3afa00a23ce2 254 }
jfields 1:ea01c3232c4a 255
jfields 1:ea01c3232c4a 256 void setVals(char c) {
jfields 1:ea01c3232c4a 257 if (c == 'n') {
jfields 1:ea01c3232c4a 258 PVARP = N_PVARP;
jfields 1:ea01c3232c4a 259 VRP = N_VRP;
jfields 1:ea01c3232c4a 260 LRI = N_LRI;
jfields 1:ea01c3232c4a 261 AVI = N_AVI;
jfields 1:ea01c3232c4a 262 UB = N_UB;
jfields 1:ea01c3232c4a 263 LB = N_LB;
jfields 1:ea01c3232c4a 264 }
jfields 1:ea01c3232c4a 265 if (c == 's') {
jfields 1:ea01c3232c4a 266 PVARP = S_PVARP;
jfields 1:ea01c3232c4a 267 VRP = S_VRP;
jfields 1:ea01c3232c4a 268 LRI = S_LRI;
jfields 1:ea01c3232c4a 269 AVI = S_AVI;
jfields 1:ea01c3232c4a 270 UB = S_UB;
jfields 1:ea01c3232c4a 271 LB = S_LB;
jfields 1:ea01c3232c4a 272 }
jfields 1:ea01c3232c4a 273 if (c == 'e') {
jfields 1:ea01c3232c4a 274 PVARP = E_PVARP;
jfields 1:ea01c3232c4a 275 VRP = E_VRP;
jfields 1:ea01c3232c4a 276 LRI = E_LRI;
jfields 1:ea01c3232c4a 277 AVI = E_AVI;
jfields 1:ea01c3232c4a 278 UB = E_UB;
jfields 1:ea01c3232c4a 279 LB = E_LB;
jfields 1:ea01c3232c4a 280 }
jfields 1:ea01c3232c4a 281 }
jfields 2:3773afd2256c 282
jfields 2:3773afd2256c 283
jfields 2:3773afd2256c 284
jfields 2:3773afd2256c 285
jfields 2:3773afd2256c 286
jfields 2:3773afd2256c 287
jfields 2:3773afd2256c 288