udpated
Dependencies: TextLCD mbed-rtos mbed
main.cpp@0:3afa00a23ce2, 2014-12-03 (annotated)
- Committer:
- jfields
- Date:
- Wed Dec 03 21:20:04 2014 +0000
- Revision:
- 0:3afa00a23ce2
threads for vp, ap
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:3afa00a23ce2 | 29 | // heart rate global vars |
jfields | 0:3afa00a23ce2 | 30 | int HR = 0; |
jfields | 0:3afa00a23ce2 | 31 | int beats = 0; |
jfields | 0:3afa00a23ce2 | 32 | int sampleRate = 10000; // default 10 seconds |
jfields | 0:3afa00a23ce2 | 33 | int firstSample = 1; |
jfields | 0:3afa00a23ce2 | 34 | |
jfields | 0:3afa00a23ce2 | 35 | // Normal Values |
jfields | 0:3afa00a23ce2 | 36 | const int N_PVARP = 325; // ms |
jfields | 0:3afa00a23ce2 | 37 | const int N_VRP = 300; // ms |
jfields | 0:3afa00a23ce2 | 38 | const int N_LRI = 857; // ms (= about 70ppm) |
jfields | 0:3afa00a23ce2 | 39 | const int N_AVI = 65; // ms |
jfields | 0:3afa00a23ce2 | 40 | const int N_UB = 100; // 100ppm |
jfields | 0:3afa00a23ce2 | 41 | const int N_LB = 40; // 40ppm |
jfields | 0:3afa00a23ce2 | 42 | |
jfields | 0:3afa00a23ce2 | 43 | // Heart Values - Normal Mode is default |
jfields | 0:3afa00a23ce2 | 44 | int PVARP = N_PVARP; |
jfields | 0:3afa00a23ce2 | 45 | int VRP = N_VRP; |
jfields | 0:3afa00a23ce2 | 46 | int LRI = N_LRI; |
jfields | 0:3afa00a23ce2 | 47 | int AVI = N_AVI; |
jfields | 0:3afa00a23ce2 | 48 | int UB = N_UB; |
jfields | 0:3afa00a23ce2 | 49 | int LB = N_LB; |
jfields | 0:3afa00a23ce2 | 50 | |
jfields | 0:3afa00a23ce2 | 51 | // status flags |
jfields | 0:3afa00a23ce2 | 52 | int isVRP = 0; |
jfields | 0:3afa00a23ce2 | 53 | int isPVARP = 0; |
jfields | 0:3afa00a23ce2 | 54 | int waitingForV = 1; |
jfields | 0:3afa00a23ce2 | 55 | |
jfields | 0:3afa00a23ce2 | 56 | // functions |
jfields | 0:3afa00a23ce2 | 57 | void VP_func(void const *args); |
jfields | 0:3afa00a23ce2 | 58 | void AP_func(void const *args); |
jfields | 0:3afa00a23ce2 | 59 | void VS_func(void const *args); |
jfields | 0:3afa00a23ce2 | 60 | void AS_func(void const *args); |
jfields | 0:3afa00a23ce2 | 61 | void manage_flags(void const *i); |
jfields | 0:3afa00a23ce2 | 62 | void calcHR(void const *args); |
jfields | 0:3afa00a23ce2 | 63 | void disp(void const *args); |
jfields | 0:3afa00a23ce2 | 64 | void send_Apace(); |
jfields | 0:3afa00a23ce2 | 65 | void send_Vpace(); |
jfields | 0:3afa00a23ce2 | 66 | void listen_Aget(void const *args); |
jfields | 0:3afa00a23ce2 | 67 | void listen_Vget(void const *args); |
jfields | 0:3afa00a23ce2 | 68 | void flashLED(int i); |
jfields | 0:3afa00a23ce2 | 69 | void blind(); |
jfields | 0:3afa00a23ce2 | 70 | void Aevent(); |
jfields | 0:3afa00a23ce2 | 71 | |
jfields | 0:3afa00a23ce2 | 72 | // threads |
jfields | 0:3afa00a23ce2 | 73 | Thread * VS_thread; |
jfields | 0:3afa00a23ce2 | 74 | Thread * AS_thread; |
jfields | 0:3afa00a23ce2 | 75 | Thread * VP_thread; |
jfields | 0:3afa00a23ce2 | 76 | Thread * AP_thread; |
jfields | 0:3afa00a23ce2 | 77 | Thread * VG_thread; |
jfields | 0:3afa00a23ce2 | 78 | Thread * AG_thread; |
jfields | 0:3afa00a23ce2 | 79 | Thread * disp_thread; |
jfields | 0:3afa00a23ce2 | 80 | |
jfields | 0:3afa00a23ce2 | 81 | // rtos timers |
jfields | 0:3afa00a23ce2 | 82 | RtosTimer * VRP_timer; |
jfields | 0:3afa00a23ce2 | 83 | RtosTimer * PVARP_timer; |
jfields | 0:3afa00a23ce2 | 84 | RtosTimer * HR_timer; |
jfields | 0:3afa00a23ce2 | 85 | |
jfields | 0:3afa00a23ce2 | 86 | int main() { |
jfields | 0:3afa00a23ce2 | 87 | |
jfields | 0:3afa00a23ce2 | 88 | // start global timer |
jfields | 0:3afa00a23ce2 | 89 | tv.start(); |
jfields | 0:3afa00a23ce2 | 90 | ta.start(); |
jfields | 0:3afa00a23ce2 | 91 | tv.stop(); |
jfields | 0:3afa00a23ce2 | 92 | |
jfields | 0:3afa00a23ce2 | 93 | // init threads |
jfields | 0:3afa00a23ce2 | 94 | VS_thread = new Thread(VS_func); |
jfields | 0:3afa00a23ce2 | 95 | AS_thread = new Thread(AS_func); |
jfields | 0:3afa00a23ce2 | 96 | VP_thread = new Thread(VP_func); |
jfields | 0:3afa00a23ce2 | 97 | AP_thread = new Thread(AP_func); |
jfields | 0:3afa00a23ce2 | 98 | VG_thread = new Thread(listen_Vget); |
jfields | 0:3afa00a23ce2 | 99 | AG_thread = new Thread(listen_Aget); |
jfields | 0:3afa00a23ce2 | 100 | |
jfields | 0:3afa00a23ce2 | 101 | // init timers |
jfields | 0:3afa00a23ce2 | 102 | VRP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)1); |
jfields | 0:3afa00a23ce2 | 103 | PVARP_timer = new RtosTimer(manage_flags, osTimerOnce, (void *)2); |
jfields | 0:3afa00a23ce2 | 104 | HR_timer = new RtosTimer(calcHR, osTimerPeriodic, (void *)0); |
jfields | 0:3afa00a23ce2 | 105 | |
jfields | 0:3afa00a23ce2 | 106 | // start display and heart rate sample |
jfields | 0:3afa00a23ce2 | 107 | HR_timer->start(sampleRate); |
jfields | 0:3afa00a23ce2 | 108 | disp_thread->signal_set(RUN); |
jfields | 0:3afa00a23ce2 | 109 | |
jfields | 0:3afa00a23ce2 | 110 | // main thread |
jfields | 0:3afa00a23ce2 | 111 | while (1) { |
jfields | 0:3afa00a23ce2 | 112 | |
jfields | 0:3afa00a23ce2 | 113 | } |
jfields | 0:3afa00a23ce2 | 114 | } |
jfields | 0:3afa00a23ce2 | 115 | |
jfields | 0:3afa00a23ce2 | 116 | void calcHR(void const *args) { |
jfields | 0:3afa00a23ce2 | 117 | if (firstSample == 1) { |
jfields | 0:3afa00a23ce2 | 118 | HR = beats*(60000/sampleRate); |
jfields | 0:3afa00a23ce2 | 119 | firstSample = 0; |
jfields | 0:3afa00a23ce2 | 120 | } |
jfields | 0:3afa00a23ce2 | 121 | else { |
jfields | 0:3afa00a23ce2 | 122 | HR = (beats*60000/sampleRate+HR)/2; |
jfields | 0:3afa00a23ce2 | 123 | } |
jfields | 0:3afa00a23ce2 | 124 | disp_thread->signal_set(RUN); |
jfields | 0:3afa00a23ce2 | 125 | } |
jfields | 0:3afa00a23ce2 | 126 | |
jfields | 0:3afa00a23ce2 | 127 | void disp(void const *args) { |
jfields | 0:3afa00a23ce2 | 128 | while (1) { |
jfields | 0:3afa00a23ce2 | 129 | Thread::signal_wait(RUN,osWaitForever); |
jfields | 0:3afa00a23ce2 | 130 | lcd.printf("HR = %d ppm\nCyle = %d s\n",HR,sampleRate/1000); |
jfields | 0:3afa00a23ce2 | 131 | beats = 0; |
jfields | 0:3afa00a23ce2 | 132 | } |
jfields | 0:3afa00a23ce2 | 133 | } |
jfields | 0:3afa00a23ce2 | 134 | |
jfields | 0:3afa00a23ce2 | 135 | void manage_flags(void const *i) { |
jfields | 0:3afa00a23ce2 | 136 | if ((int)i==1) isVRP = 0; |
jfields | 0:3afa00a23ce2 | 137 | if ((int)i==2) isPVARP = 0; |
jfields | 0:3afa00a23ce2 | 138 | } |
jfields | 0:3afa00a23ce2 | 139 | |
jfields | 0:3afa00a23ce2 | 140 | void AP_func(void const *args) { |
jfields | 0:3afa00a23ce2 | 141 | while (1) { |
jfields | 0:3afa00a23ce2 | 142 | if (tv.read_ms() >= (LRI-AVI)) { |
jfields | 0:3afa00a23ce2 | 143 | Aevent(); |
jfields | 0:3afa00a23ce2 | 144 | send_Apace(); |
jfields | 0:3afa00a23ce2 | 145 | flashLED(2); |
jfields | 0:3afa00a23ce2 | 146 | } |
jfields | 0:3afa00a23ce2 | 147 | } |
jfields | 0:3afa00a23ce2 | 148 | } |
jfields | 0:3afa00a23ce2 | 149 | |
jfields | 0:3afa00a23ce2 | 150 | void VP_func(void const *args) { |
jfields | 0:3afa00a23ce2 | 151 | while (1) { |
jfields | 0:3afa00a23ce2 | 152 | if (ta.read_ms() >= AVI) { |
jfields | 0:3afa00a23ce2 | 153 | blind(); |
jfields | 0:3afa00a23ce2 | 154 | send_Vpace(); |
jfields | 0:3afa00a23ce2 | 155 | flashLED(1); |
jfields | 0:3afa00a23ce2 | 156 | } |
jfields | 0:3afa00a23ce2 | 157 | } |
jfields | 0:3afa00a23ce2 | 158 | } |
jfields | 0:3afa00a23ce2 | 159 | |
jfields | 0:3afa00a23ce2 | 160 | void AS_func(void const *args) { |
jfields | 0:3afa00a23ce2 | 161 | while (1) { |
jfields | 0:3afa00a23ce2 | 162 | Thread::signal_wait(RUN,osWaitForever); |
jfields | 0:3afa00a23ce2 | 163 | Aevent(); |
jfields | 0:3afa00a23ce2 | 164 | flashLED(4); |
jfields | 0:3afa00a23ce2 | 165 | } |
jfields | 0:3afa00a23ce2 | 166 | } |
jfields | 0:3afa00a23ce2 | 167 | |
jfields | 0:3afa00a23ce2 | 168 | void VS_func(void const *args) { |
jfields | 0:3afa00a23ce2 | 169 | while (1) { |
jfields | 0:3afa00a23ce2 | 170 | Thread::signal_wait(RUN,osWaitForever); |
jfields | 0:3afa00a23ce2 | 171 | blind(); |
jfields | 0:3afa00a23ce2 | 172 | flashLED(4); |
jfields | 0:3afa00a23ce2 | 173 | } |
jfields | 0:3afa00a23ce2 | 174 | } |
jfields | 0:3afa00a23ce2 | 175 | |
jfields | 0:3afa00a23ce2 | 176 | void listen_Vget(void const *args) { |
jfields | 0:3afa00a23ce2 | 177 | while (1) { |
jfields | 0:3afa00a23ce2 | 178 | if (VGet==1) { |
jfields | 0:3afa00a23ce2 | 179 | if (!isVRP && waitingForV) VS_thread->signal_set(RUN); |
jfields | 0:3afa00a23ce2 | 180 | while(VGet == 1); |
jfields | 0:3afa00a23ce2 | 181 | } |
jfields | 0:3afa00a23ce2 | 182 | } |
jfields | 0:3afa00a23ce2 | 183 | } |
jfields | 0:3afa00a23ce2 | 184 | |
jfields | 0:3afa00a23ce2 | 185 | void listen_Aget(void const *args) { |
jfields | 0:3afa00a23ce2 | 186 | while (1) { |
jfields | 0:3afa00a23ce2 | 187 | if (AGet == 1) { |
jfields | 0:3afa00a23ce2 | 188 | if (!isPVARP && !waitingForV) AS_thread->signal_set(RUN); |
jfields | 0:3afa00a23ce2 | 189 | while(AGet == 1); |
jfields | 0:3afa00a23ce2 | 190 | } |
jfields | 0:3afa00a23ce2 | 191 | } |
jfields | 0:3afa00a23ce2 | 192 | } |
jfields | 0:3afa00a23ce2 | 193 | |
jfields | 0:3afa00a23ce2 | 194 | void flashLED(int i) { |
jfields | 0:3afa00a23ce2 | 195 | leds[i-1] = 1; |
jfields | 0:3afa00a23ce2 | 196 | wait(0.01); |
jfields | 0:3afa00a23ce2 | 197 | leds[i-1] = 0; |
jfields | 0:3afa00a23ce2 | 198 | } |
jfields | 0:3afa00a23ce2 | 199 | |
jfields | 0:3afa00a23ce2 | 200 | void blind() { |
jfields | 0:3afa00a23ce2 | 201 | tv.start(); |
jfields | 0:3afa00a23ce2 | 202 | ta.reset(); |
jfields | 0:3afa00a23ce2 | 203 | ta.stop(); |
jfields | 0:3afa00a23ce2 | 204 | isVRP = 1; |
jfields | 0:3afa00a23ce2 | 205 | isPVARP = 1; |
jfields | 0:3afa00a23ce2 | 206 | VRP_timer->start(VRP); |
jfields | 0:3afa00a23ce2 | 207 | PVARP_timer->start(PVARP); |
jfields | 0:3afa00a23ce2 | 208 | beats++; |
jfields | 0:3afa00a23ce2 | 209 | waitingForV = 0; |
jfields | 0:3afa00a23ce2 | 210 | } |
jfields | 0:3afa00a23ce2 | 211 | |
jfields | 0:3afa00a23ce2 | 212 | void Aevent() { |
jfields | 0:3afa00a23ce2 | 213 | ta.start(); |
jfields | 0:3afa00a23ce2 | 214 | tv.reset(); |
jfields | 0:3afa00a23ce2 | 215 | tv.stop(); |
jfields | 0:3afa00a23ce2 | 216 | waitingForV = 1; |
jfields | 0:3afa00a23ce2 | 217 | } |
jfields | 0:3afa00a23ce2 | 218 | |
jfields | 0:3afa00a23ce2 | 219 | void send_Apace() { |
jfields | 0:3afa00a23ce2 | 220 | APace = 1; |
jfields | 0:3afa00a23ce2 | 221 | Thread::wait(50); |
jfields | 0:3afa00a23ce2 | 222 | APace = 0; |
jfields | 0:3afa00a23ce2 | 223 | } |
jfields | 0:3afa00a23ce2 | 224 | |
jfields | 0:3afa00a23ce2 | 225 | void send_Vpace() { |
jfields | 0:3afa00a23ce2 | 226 | VPace = 1; |
jfields | 0:3afa00a23ce2 | 227 | Thread::wait(50); |
jfields | 0:3afa00a23ce2 | 228 | VPace = 0; |
jfields | 0:3afa00a23ce2 | 229 | } |