
here it is
Dependencies: TextLCD mbed-rtos mbed
Fork of 541-pacemaker by
main.cpp@4:f887d8169f83, 2016-12-09 (annotated)
- Committer:
- ems316
- Date:
- Fri Dec 09 11:05:21 2016 +0000
- Revision:
- 4:f887d8169f83
- Parent:
- 3:77efff091ef1
hahahahah
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
terryfan | 0:dac187b013e0 | 1 | #include "mbed.h" |
terryfan | 0:dac187b013e0 | 2 | #include "rtos.h" |
terryfan | 0:dac187b013e0 | 3 | #include "TextLCD.h" |
terryfan | 0:dac187b013e0 | 4 | #include <stdio.h> |
terryfan | 3:77efff091ef1 | 5 | |
terryfan | 0:dac187b013e0 | 6 | InterruptIn vsignal(p7); |
terryfan | 0:dac187b013e0 | 7 | InterruptIn asignal(p8); |
terryfan | 0:dac187b013e0 | 8 | DigitalOut Vpace(p5); |
terryfan | 0:dac187b013e0 | 9 | DigitalOut Apace(p6); |
terryfan | 3:77efff091ef1 | 10 | |
terryfan | 0:dac187b013e0 | 11 | DigitalOut asense_led(LED1); |
terryfan | 0:dac187b013e0 | 12 | DigitalOut vsense_led(LED2); |
terryfan | 0:dac187b013e0 | 13 | DigitalOut apace_led(LED3); |
terryfan | 0:dac187b013e0 | 14 | DigitalOut vpace_led(LED4); |
terryfan | 3:77efff091ef1 | 15 | |
terryfan | 0:dac187b013e0 | 16 | Thread *pacemodeThread; |
terryfan | 3:77efff091ef1 | 17 | |
terryfan | 0:dac187b013e0 | 18 | osThreadId signalTid; |
terryfan | 0:dac187b013e0 | 19 | osThreadId senseTid; |
terryfan | 0:dac187b013e0 | 20 | osThreadId displayTid; |
terryfan | 0:dac187b013e0 | 21 | osThreadId pacemodeTid; |
terryfan | 1:e6f6471e2c00 | 22 | osThreadId alarmTid; |
terryfan | 0:dac187b013e0 | 23 | |
terryfan | 0:dac187b013e0 | 24 | TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); |
terryfan | 0:dac187b013e0 | 25 | RawSerial pc(USBTX, USBRX); |
terryfan | 3:77efff091ef1 | 26 | |
terryfan | 0:dac187b013e0 | 27 | Timer vClock; |
terryfan | 0:dac187b013e0 | 28 | Timer aClock; //PaceSignal model |
terryfan | 3:77efff091ef1 | 29 | Timer arpClock; |
terryfan | 3:77efff091ef1 | 30 | |
terryfan | 3:77efff091ef1 | 31 | //RtosTimer *apace_timer; |
terryfan | 3:77efff091ef1 | 32 | //RtosTimer *vpace_timer; |
terryfan | 0:dac187b013e0 | 33 | //RtosTimer *vpace_timer2; |
terryfan | 3:77efff091ef1 | 34 | |
terryfan | 0:dac187b013e0 | 35 | double LRI = 1000; |
terryfan | 0:dac187b013e0 | 36 | double URI = 700; |
terryfan | 0:dac187b013e0 | 37 | double VRP = 200; // V noise interval |
terryfan | 0:dac187b013e0 | 38 | double ARP = 50; // A noise interval |
ems316 | 4:f887d8169f83 | 39 | double AVI = 150; // A-V max interval |
terryfan | 0:dac187b013e0 | 40 | double PVARP = 300; // V-A max interval |
terryfan | 0:dac187b013e0 | 41 | double ratio; |
terryfan | 0:dac187b013e0 | 42 | int wait_period = 10; // 3a requirement |
terryfan | 3:77efff091ef1 | 43 | |
terryfan | 0:dac187b013e0 | 44 | int observation_interval = 10000; // In miliseconds |
terryfan | 0:dac187b013e0 | 45 | int upperBound; //for mode changes |
terryfan | 0:dac187b013e0 | 46 | int lowerBound; //for mode changes |
terryfan | 0:dac187b013e0 | 47 | int heart_beats = 0; // Heart-Beats (sensed or paced) since the last observation interval |
terryfan | 0:dac187b013e0 | 48 | char mode = 'n'; |
terryfan | 0:dac187b013e0 | 49 | char key = 'n'; |
terryfan | 0:dac187b013e0 | 50 | char newObsInt[8]; |
terryfan | 0:dac187b013e0 | 51 | int manual_mode = 0; |
terryfan | 1:e6f6471e2c00 | 52 | Mutex hr_mutex; //hr_mutex.lock()/unlock() |
terryfan | 3:77efff091ef1 | 53 | |
terryfan | 0:dac187b013e0 | 54 | Queue<char,256> mode_q; |
terryfan | 0:dac187b013e0 | 55 | Queue<char,256> signal_q; |
terryfan | 1:e6f6471e2c00 | 56 | Queue<char,256> obsint_q; |
terryfan | 3:77efff091ef1 | 57 | |
terryfan | 0:dac187b013e0 | 58 | volatile char c; |
terryfan | 0:dac187b013e0 | 59 | volatile int mm = 0; |
terryfan | 3:77efff091ef1 | 60 | volatile int om = 0; |
terryfan | 0:dac187b013e0 | 61 | int mm_flag = 0; |
terryfan | 3:77efff091ef1 | 62 | |
terryfan | 0:dac187b013e0 | 63 | void initialize_intervals() |
terryfan | 0:dac187b013e0 | 64 | { |
terryfan | 0:dac187b013e0 | 65 | LRI = 1000; |
terryfan | 0:dac187b013e0 | 66 | URI = 700; |
terryfan | 1:e6f6471e2c00 | 67 | // VRP = 200; |
terryfan | 1:e6f6471e2c00 | 68 | // ARP = 50; |
terryfan | 1:e6f6471e2c00 | 69 | // AVI = 150; |
terryfan | 1:e6f6471e2c00 | 70 | // PVARP = 300; |
terryfan | 0:dac187b013e0 | 71 | } |
terryfan | 3:77efff091ef1 | 72 | |
terryfan | 0:dac187b013e0 | 73 | void Rx_interrupt() |
terryfan | 0:dac187b013e0 | 74 | { |
terryfan | 0:dac187b013e0 | 75 | while(pc.readable()) { |
terryfan | 0:dac187b013e0 | 76 | c = pc.getc(); |
terryfan | 1:e6f6471e2c00 | 77 | if(c == 'm' && om != 1) { |
terryfan | 0:dac187b013e0 | 78 | mode_q.put((char*)c); |
terryfan | 0:dac187b013e0 | 79 | mm = 1; |
terryfan | 1:e6f6471e2c00 | 80 | } else if(c == 'n' || c == 'e' || c == 's' && om != 1) { |
terryfan | 0:dac187b013e0 | 81 | mode_q.put((char*)c); |
terryfan | 0:dac187b013e0 | 82 | mm = 0; |
terryfan | 0:dac187b013e0 | 83 | } else if((c == 'a' || c == 'v') && mm) { |
terryfan | 0:dac187b013e0 | 84 | signal_q.put((char*)c); |
terryfan | 1:e6f6471e2c00 | 85 | } else if(c == 'o' && om != 1) { |
terryfan | 1:e6f6471e2c00 | 86 | mode_q.put((char*)c); |
terryfan | 1:e6f6471e2c00 | 87 | om = 1; |
terryfan | 1:e6f6471e2c00 | 88 | } else if (c == '\r' && om) { |
terryfan | 1:e6f6471e2c00 | 89 | obsint_q.put((char*)c); |
terryfan | 1:e6f6471e2c00 | 90 | om = 0; |
terryfan | 1:e6f6471e2c00 | 91 | } else if ((int)c > 47 && (int)c < 58 && om) { |
terryfan | 1:e6f6471e2c00 | 92 | obsint_q.put((char*)c); |
terryfan | 0:dac187b013e0 | 93 | } |
terryfan | 0:dac187b013e0 | 94 | } |
terryfan | 0:dac187b013e0 | 95 | } |
terryfan | 0:dac187b013e0 | 96 | |
terryfan | 3:77efff091ef1 | 97 | // Function to toggle the LEDs 1,2,3,4 |
terryfan | 3:77efff091ef1 | 98 | void toggleLed(int led) |
terryfan | 3:77efff091ef1 | 99 | { |
terryfan | 3:77efff091ef1 | 100 | switch (led) { |
terryfan | 3:77efff091ef1 | 101 | case (1): |
terryfan | 3:77efff091ef1 | 102 | asense_led = 1; |
terryfan | 3:77efff091ef1 | 103 | Thread::wait(wait_period); |
terryfan | 3:77efff091ef1 | 104 | asense_led = 0; |
terryfan | 3:77efff091ef1 | 105 | break; |
terryfan | 3:77efff091ef1 | 106 | case (2): |
terryfan | 3:77efff091ef1 | 107 | vsense_led = 1; |
terryfan | 3:77efff091ef1 | 108 | Thread::wait(wait_period); |
terryfan | 3:77efff091ef1 | 109 | vsense_led = 0; |
terryfan | 3:77efff091ef1 | 110 | break; |
terryfan | 3:77efff091ef1 | 111 | case (3): |
terryfan | 3:77efff091ef1 | 112 | apace_led = 1; |
terryfan | 3:77efff091ef1 | 113 | Thread::wait(wait_period); |
terryfan | 3:77efff091ef1 | 114 | apace_led = 0; |
terryfan | 3:77efff091ef1 | 115 | break; |
terryfan | 3:77efff091ef1 | 116 | case (4): |
terryfan | 3:77efff091ef1 | 117 | vpace_led = 1; |
terryfan | 3:77efff091ef1 | 118 | Thread::wait(wait_period); |
terryfan | 3:77efff091ef1 | 119 | vpace_led = 0; |
terryfan | 3:77efff091ef1 | 120 | break; |
terryfan | 3:77efff091ef1 | 121 | } |
terryfan | 3:77efff091ef1 | 122 | } |
terryfan | 3:77efff091ef1 | 123 | |
terryfan | 3:77efff091ef1 | 124 | void alarmThread(void const *args) |
terryfan | 0:dac187b013e0 | 125 | { |
terryfan | 2:682a3ac9d7a3 | 126 | while (1) { |
terryfan | 2:682a3ac9d7a3 | 127 | osEvent ext_signal = osSignalWait(0, osWaitForever); |
terryfan | 2:682a3ac9d7a3 | 128 | int evt = ext_signal.value.signals; |
terryfan | 3:77efff091ef1 | 129 | |
terryfan | 3:77efff091ef1 | 130 | if (evt == 0xb) { |
terryfan | 3:77efff091ef1 | 131 | lcd.printf("%s", "\nALARM HIGH"); |
terryfan | 3:77efff091ef1 | 132 | } else if (evt == 0xc) { |
terryfan | 1:e6f6471e2c00 | 133 | lcd.printf("%s", "\nALARM LOW"); |
terryfan | 1:e6f6471e2c00 | 134 | } |
terryfan | 1:e6f6471e2c00 | 135 | } |
terryfan | 1:e6f6471e2c00 | 136 | } |
terryfan | 3:77efff091ef1 | 137 | |
terryfan | 0:dac187b013e0 | 138 | void displayThread(void const *args) |
terryfan | 0:dac187b013e0 | 139 | { |
terryfan | 0:dac187b013e0 | 140 | while (1) { |
terryfan | 0:dac187b013e0 | 141 | Thread::wait(observation_interval); |
terryfan | 0:dac187b013e0 | 142 | lcd.cls(); |
terryfan | 3:77efff091ef1 | 143 | |
terryfan | 1:e6f6471e2c00 | 144 | hr_mutex.lock(); |
terryfan | 0:dac187b013e0 | 145 | int hr = (heart_beats*60) / (observation_interval / 1000); |
terryfan | 1:e6f6471e2c00 | 146 | heart_beats = 0; |
terryfan | 1:e6f6471e2c00 | 147 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 148 | |
terryfan | 0:dac187b013e0 | 149 | lcd.printf("%s%d%s","HR: ", hr, " bpm"); |
terryfan | 3:77efff091ef1 | 150 | |
terryfan | 3:77efff091ef1 | 151 | if (hr > upperBound) { |
terryfan | 1:e6f6471e2c00 | 152 | osSignalSet(alarmTid, 0xb); |
terryfan | 3:77efff091ef1 | 153 | } else if (hr < lowerBound) { |
terryfan | 1:e6f6471e2c00 | 154 | osSignalSet(alarmTid, 0xc); |
terryfan | 1:e6f6471e2c00 | 155 | } |
terryfan | 0:dac187b013e0 | 156 | } |
terryfan | 0:dac187b013e0 | 157 | } |
terryfan | 3:77efff091ef1 | 158 | |
terryfan | 3:77efff091ef1 | 159 | |
terryfan | 3:77efff091ef1 | 160 | |
terryfan | 0:dac187b013e0 | 161 | // Incoming signal from the heart |
terryfan | 0:dac187b013e0 | 162 | void asignal_irq() |
terryfan | 0:dac187b013e0 | 163 | { |
terryfan | 0:dac187b013e0 | 164 | osSignalSet(signalTid, 0x1); |
terryfan | 0:dac187b013e0 | 165 | } |
terryfan | 3:77efff091ef1 | 166 | |
terryfan | 0:dac187b013e0 | 167 | // Incoming signal from the heart |
terryfan | 0:dac187b013e0 | 168 | void vsignal_irq() |
terryfan | 0:dac187b013e0 | 169 | { |
terryfan | 0:dac187b013e0 | 170 | osSignalSet(signalTid, 0x2); |
terryfan | 0:dac187b013e0 | 171 | } |
terryfan | 3:77efff091ef1 | 172 | |
terryfan | 3:77efff091ef1 | 173 | |
terryfan | 0:dac187b013e0 | 174 | // Timer-driven function to pace the Atrial |
terryfan | 0:dac187b013e0 | 175 | void a_pace(void const*) |
terryfan | 0:dac187b013e0 | 176 | { |
terryfan | 1:e6f6471e2c00 | 177 | Apace = 1; |
ems316 | 4:f887d8169f83 | 178 | Thread::wait(1); |
terryfan | 0:dac187b013e0 | 179 | aClock.reset(); |
ems316 | 4:f887d8169f83 | 180 | arpClock.reset(); |
terryfan | 0:dac187b013e0 | 181 | Apace = 0; |
terryfan | 0:dac187b013e0 | 182 | osSignalSet(signalTid, 0x3); |
terryfan | 3:77efff091ef1 | 183 | toggleLed(3); |
terryfan | 0:dac187b013e0 | 184 | } |
terryfan | 3:77efff091ef1 | 185 | |
terryfan | 0:dac187b013e0 | 186 | // Timer-driven function to pace the ventrical |
terryfan | 0:dac187b013e0 | 187 | void v_pace(void const*) |
terryfan | 0:dac187b013e0 | 188 | { |
terryfan | 3:77efff091ef1 | 189 | hr_mutex.lock(); |
terryfan | 0:dac187b013e0 | 190 | osSignalSet(signalTid, 0x4); |
terryfan | 0:dac187b013e0 | 191 | heart_beats++; |
ems316 | 4:f887d8169f83 | 192 | vClock.reset(); |
ems316 | 4:f887d8169f83 | 193 | aClock.reset(); |
ems316 | 4:f887d8169f83 | 194 | arpClock.reset(); |
terryfan | 1:e6f6471e2c00 | 195 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 196 | |
terryfan | 3:77efff091ef1 | 197 | toggleLed(4); |
terryfan | 0:dac187b013e0 | 198 | } |
terryfan | 3:77efff091ef1 | 199 | |
terryfan | 3:77efff091ef1 | 200 | |
terryfan | 3:77efff091ef1 | 201 | |
terryfan | 0:dac187b013e0 | 202 | void PaceSignal(void const *args) |
terryfan | 0:dac187b013e0 | 203 | { |
terryfan | 0:dac187b013e0 | 204 | int pFlag1 = 0; |
terryfan | 0:dac187b013e0 | 205 | int pFlag2 = 0; |
terryfan | 0:dac187b013e0 | 206 | vClock.start(); |
terryfan | 0:dac187b013e0 | 207 | aClock.start(); |
terryfan | 3:77efff091ef1 | 208 | arpClock.start(); |
ems316 | 4:f887d8169f83 | 209 | |
terryfan | 0:dac187b013e0 | 210 | while(1) { |
terryfan | 0:dac187b013e0 | 211 | while (!pFlag1) { |
terryfan | 0:dac187b013e0 | 212 | osEvent ext_signal = osSignalWait(0, osWaitForever); |
terryfan | 0:dac187b013e0 | 213 | int evt = ext_signal.value.signals; |
terryfan | 3:77efff091ef1 | 214 | |
terryfan | 3:77efff091ef1 | 215 | if (evt == 0x1 && vClock.read_ms() >= PVARP) { //aSense |
terryfan | 0:dac187b013e0 | 216 | osSignalSet(senseTid, 0x1); |
terryfan | 0:dac187b013e0 | 217 | aClock.reset(); |
terryfan | 3:77efff091ef1 | 218 | arpClock.reset(); |
terryfan | 0:dac187b013e0 | 219 | pFlag1 = 1; |
ems316 | 4:f887d8169f83 | 220 | } else if(evt == 0x2 && vClock.read_ms() >= VRP) { //vSense |
ems316 | 4:f887d8169f83 | 221 | pc.printf("%d VSense\n", vClock.read_ms()); |
ems316 | 4:f887d8169f83 | 222 | hr_mutex.lock(); |
terryfan | 0:dac187b013e0 | 223 | osSignalSet(senseTid, 0x2); |
ems316 | 4:f887d8169f83 | 224 | heart_beats++; |
terryfan | 0:dac187b013e0 | 225 | vClock.reset(); |
ems316 | 4:f887d8169f83 | 226 | aClock.reset(); |
ems316 | 4:f887d8169f83 | 227 | arpClock.reset(); |
ems316 | 4:f887d8169f83 | 228 | hr_mutex.unlock(); |
ems316 | 4:f887d8169f83 | 229 | pFlag1 = 1; |
ems316 | 4:f887d8169f83 | 230 | |
terryfan | 0:dac187b013e0 | 231 | } else if (evt == 0x3) { //aPace |
terryfan | 0:dac187b013e0 | 232 | pFlag1 = 1; |
terryfan | 0:dac187b013e0 | 233 | } |
terryfan | 0:dac187b013e0 | 234 | } |
terryfan | 0:dac187b013e0 | 235 | pFlag1 = 0; |
terryfan | 0:dac187b013e0 | 236 | while(!pFlag2) { |
terryfan | 3:77efff091ef1 | 237 | |
terryfan | 0:dac187b013e0 | 238 | osEvent ext_signal = osSignalWait(0, osWaitForever); |
terryfan | 0:dac187b013e0 | 239 | int evt = ext_signal.value.signals; |
terryfan | 3:77efff091ef1 | 240 | |
ems316 | 4:f887d8169f83 | 241 | if (evt == 0x1 && arpClock.read_ms() >= ARP) { //aSense |
terryfan | 0:dac187b013e0 | 242 | osSignalSet(senseTid, 0x1); |
terryfan | 3:77efff091ef1 | 243 | arpClock.reset(); |
ems316 | 4:f887d8169f83 | 244 | |
terryfan | 3:77efff091ef1 | 245 | } else if(evt == 0x2) { //vSense |
terryfan | 2:682a3ac9d7a3 | 246 | hr_mutex.lock(); |
terryfan | 3:77efff091ef1 | 247 | osSignalSet(senseTid, 0x2); |
terryfan | 2:682a3ac9d7a3 | 248 | heart_beats++; |
terryfan | 3:77efff091ef1 | 249 | vClock.reset(); |
ems316 | 4:f887d8169f83 | 250 | aClock.reset(); |
ems316 | 4:f887d8169f83 | 251 | arpClock.reset(); |
terryfan | 2:682a3ac9d7a3 | 252 | hr_mutex.unlock(); |
terryfan | 0:dac187b013e0 | 253 | pFlag2 = 1; |
terryfan | 0:dac187b013e0 | 254 | } else if (evt == 0x4) { //vPace |
ems316 | 4:f887d8169f83 | 255 | pc.printf("%d VPace\n", vClock.read_ms()); |
terryfan | 0:dac187b013e0 | 256 | pFlag2 = 1; |
terryfan | 0:dac187b013e0 | 257 | } |
terryfan | 0:dac187b013e0 | 258 | } |
terryfan | 0:dac187b013e0 | 259 | pFlag2 = 0; |
terryfan | 0:dac187b013e0 | 260 | } |
terryfan | 0:dac187b013e0 | 261 | } |
terryfan | 3:77efff091ef1 | 262 | |
terryfan | 3:77efff091ef1 | 263 | |
terryfan | 0:dac187b013e0 | 264 | void PaceSense(void const *args) |
terryfan | 0:dac187b013e0 | 265 | { |
terryfan | 3:77efff091ef1 | 266 | int interval; |
terryfan | 0:dac187b013e0 | 267 | int pFlag1 = 0; |
terryfan | 0:dac187b013e0 | 268 | int pFlag2 = 0; |
ems316 | 4:f887d8169f83 | 269 | int time_sub = 0; |
ems316 | 4:f887d8169f83 | 270 | int evt = 0; |
terryfan | 0:dac187b013e0 | 271 | while(1) { |
terryfan | 0:dac187b013e0 | 272 | while (!pFlag1) { |
ems316 | 4:f887d8169f83 | 273 | |
ems316 | 4:f887d8169f83 | 274 | time_sub = LRI-AVI - vClock.read_ms(); |
terryfan | 3:77efff091ef1 | 275 | |
ems316 | 4:f887d8169f83 | 276 | if (time_sub > 0 && !mm_flag) { |
ems316 | 4:f887d8169f83 | 277 | osEvent ext_signal = osSignalWait(0, time_sub); |
ems316 | 4:f887d8169f83 | 278 | evt = ext_signal.value.signals; |
ems316 | 4:f887d8169f83 | 279 | } else if(mm_flag) { |
ems316 | 4:f887d8169f83 | 280 | osEvent ext_signal = osSignalWait(0, osWaitForever); |
ems316 | 4:f887d8169f83 | 281 | evt = ext_signal.value.signals; |
ems316 | 4:f887d8169f83 | 282 | } else { |
ems316 | 4:f887d8169f83 | 283 | evt = 0x0; |
ems316 | 4:f887d8169f83 | 284 | } |
ems316 | 4:f887d8169f83 | 285 | |
ems316 | 4:f887d8169f83 | 286 | if (evt == 0x0) { //aPace 0x0 |
ems316 | 4:f887d8169f83 | 287 | aClock.reset(); |
ems316 | 4:f887d8169f83 | 288 | arpClock.reset(); |
terryfan | 3:77efff091ef1 | 289 | Apace = 1; |
terryfan | 3:77efff091ef1 | 290 | Thread::wait(1); |
terryfan | 3:77efff091ef1 | 291 | Apace = 0; |
terryfan | 3:77efff091ef1 | 292 | osSignalSet(signalTid, 0x3); |
ems316 | 4:f887d8169f83 | 293 | |
terryfan | 3:77efff091ef1 | 294 | toggleLed(3); |
ems316 | 4:f887d8169f83 | 295 | interval = AVI; |
terryfan | 0:dac187b013e0 | 296 | pFlag1 = 1; |
ems316 | 4:f887d8169f83 | 297 | } else if (evt == 0x1) { //aSense |
terryfan | 3:77efff091ef1 | 298 | if(!mm_flag) { |
ems316 | 4:f887d8169f83 | 299 | interval = (vClock.read_ms() + AVI >= URI) ? AVI : URI; |
ems316 | 4:f887d8169f83 | 300 | time_sub = interval; |
terryfan | 3:77efff091ef1 | 301 | } |
ems316 | 4:f887d8169f83 | 302 | toggleLed(1); |
terryfan | 3:77efff091ef1 | 303 | pFlag1 = 1; |
terryfan | 3:77efff091ef1 | 304 | } else if(evt == 0x2) { //vSense |
ems316 | 4:f887d8169f83 | 305 | toggleLed(2); |
ems316 | 4:f887d8169f83 | 306 | } else if(evt == 0x3) { //apace |
ems316 | 4:f887d8169f83 | 307 | pFlag1 = 1; |
terryfan | 3:77efff091ef1 | 308 | } |
terryfan | 0:dac187b013e0 | 309 | } |
terryfan | 0:dac187b013e0 | 310 | pFlag1 = 0; |
ems316 | 4:f887d8169f83 | 311 | |
terryfan | 0:dac187b013e0 | 312 | while(!pFlag2) { |
ems316 | 4:f887d8169f83 | 313 | time_sub = (interval == AVI) ? AVI - aClock.read_ms() : URI - vClock.read_ms(); |
ems316 | 4:f887d8169f83 | 314 | |
ems316 | 4:f887d8169f83 | 315 | if (time_sub > 0 && !mm_flag) { |
ems316 | 4:f887d8169f83 | 316 | osEvent ext_signal = osSignalWait(0, time_sub); |
ems316 | 4:f887d8169f83 | 317 | evt = ext_signal.value.signals; |
ems316 | 4:f887d8169f83 | 318 | } else if(mm_flag) { |
ems316 | 4:f887d8169f83 | 319 | osEvent ext_signal = osSignalWait(0, osWaitForever); |
ems316 | 4:f887d8169f83 | 320 | evt = ext_signal.value.signals; |
ems316 | 4:f887d8169f83 | 321 | } else { |
ems316 | 4:f887d8169f83 | 322 | evt = 0x0; |
ems316 | 4:f887d8169f83 | 323 | } |
ems316 | 4:f887d8169f83 | 324 | |
ems316 | 4:f887d8169f83 | 325 | if (evt == 0x0) { //vPace 0x0 |
ems316 | 4:f887d8169f83 | 326 | |
terryfan | 3:77efff091ef1 | 327 | hr_mutex.lock(); |
ems316 | 4:f887d8169f83 | 328 | heart_beats++; |
ems316 | 4:f887d8169f83 | 329 | vClock.reset(); |
ems316 | 4:f887d8169f83 | 330 | aClock.reset(); |
ems316 | 4:f887d8169f83 | 331 | arpClock.reset(); |
terryfan | 3:77efff091ef1 | 332 | Vpace = 1; |
terryfan | 3:77efff091ef1 | 333 | Thread::wait(1); |
terryfan | 3:77efff091ef1 | 334 | Vpace = 0; |
terryfan | 3:77efff091ef1 | 335 | osSignalSet(signalTid, 0x4); |
terryfan | 3:77efff091ef1 | 336 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 337 | |
terryfan | 3:77efff091ef1 | 338 | toggleLed(4); |
terryfan | 0:dac187b013e0 | 339 | pFlag2 = 1; |
ems316 | 4:f887d8169f83 | 340 | |
terryfan | 3:77efff091ef1 | 341 | } else if (evt == 0x1) { //aSense |
ems316 | 4:f887d8169f83 | 342 | toggleLed(1); |
terryfan | 3:77efff091ef1 | 343 | } else if(evt == 0x2) { //vSense |
ems316 | 4:f887d8169f83 | 344 | toggleLed(2); |
ems316 | 4:f887d8169f83 | 345 | pFlag2 = 1; |
ems316 | 4:f887d8169f83 | 346 | } else if (evt == 0x4) { //vpace |
terryfan | 0:dac187b013e0 | 347 | pFlag2 = 1; |
terryfan | 0:dac187b013e0 | 348 | } |
terryfan | 0:dac187b013e0 | 349 | } |
terryfan | 0:dac187b013e0 | 350 | pFlag2 = 0; |
terryfan | 0:dac187b013e0 | 351 | } |
terryfan | 0:dac187b013e0 | 352 | } |
terryfan | 3:77efff091ef1 | 353 | |
terryfan | 0:dac187b013e0 | 354 | void normalmode(void const *args) |
terryfan | 0:dac187b013e0 | 355 | { |
terryfan | 0:dac187b013e0 | 356 | initialize_intervals(); |
terryfan | 0:dac187b013e0 | 357 | mode = 'n'; |
terryfan | 0:dac187b013e0 | 358 | lcd.printf("N"); |
terryfan | 0:dac187b013e0 | 359 | upperBound = 100; //beats per msecond |
terryfan | 0:dac187b013e0 | 360 | lowerBound = 40; //beats per msecond |
terryfan | 1:e6f6471e2c00 | 361 | //reset obs interval |
terryfan | 1:e6f6471e2c00 | 362 | hr_mutex.lock(); |
terryfan | 1:e6f6471e2c00 | 363 | heart_beats = 0; |
terryfan | 1:e6f6471e2c00 | 364 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 365 | |
terryfan | 3:77efff091ef1 | 366 | vClock.reset(); |
terryfan | 3:77efff091ef1 | 367 | aClock.reset(); |
terryfan | 3:77efff091ef1 | 368 | // vpace_timer->start(LRI); |
terryfan | 3:77efff091ef1 | 369 | // apace_timer->start(LRI-AVI); |
terryfan | 0:dac187b013e0 | 370 | } |
terryfan | 3:77efff091ef1 | 371 | |
terryfan | 0:dac187b013e0 | 372 | void exercisemode(void const *args) |
terryfan | 0:dac187b013e0 | 373 | { |
terryfan | 0:dac187b013e0 | 374 | initialize_intervals(); |
terryfan | 0:dac187b013e0 | 375 | mode = 'e'; |
terryfan | 0:dac187b013e0 | 376 | lcd.printf("E"); |
terryfan | 0:dac187b013e0 | 377 | upperBound = 175; //beats per msecond |
terryfan | 0:dac187b013e0 | 378 | lowerBound = 100; //beats per msecond |
terryfan | 0:dac187b013e0 | 379 | ratio = (175.00/100.00 + 100.00/40.00) / 2.00; |
terryfan | 0:dac187b013e0 | 380 | LRI /= ratio; |
terryfan | 0:dac187b013e0 | 381 | URI /= ratio; |
terryfan | 1:e6f6471e2c00 | 382 | //reset obs interval |
terryfan | 1:e6f6471e2c00 | 383 | hr_mutex.lock(); |
terryfan | 1:e6f6471e2c00 | 384 | heart_beats = 0; |
terryfan | 1:e6f6471e2c00 | 385 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 386 | |
terryfan | 3:77efff091ef1 | 387 | vClock.reset(); |
terryfan | 3:77efff091ef1 | 388 | aClock.reset(); |
terryfan | 3:77efff091ef1 | 389 | // vpace_timer->start(LRI); |
terryfan | 3:77efff091ef1 | 390 | // apace_timer->start(LRI-AVI); |
terryfan | 0:dac187b013e0 | 391 | } |
terryfan | 3:77efff091ef1 | 392 | |
terryfan | 0:dac187b013e0 | 393 | void sleepmode(void const *args) |
terryfan | 0:dac187b013e0 | 394 | { |
terryfan | 0:dac187b013e0 | 395 | initialize_intervals(); |
terryfan | 0:dac187b013e0 | 396 | mode = 's'; |
terryfan | 0:dac187b013e0 | 397 | lcd.printf("S"); |
terryfan | 0:dac187b013e0 | 398 | upperBound = 60; //beats per msecond |
terryfan | 0:dac187b013e0 | 399 | lowerBound = 30; //beats per msecond v-v 0.5s |
terryfan | 0:dac187b013e0 | 400 | ratio = (60.00/100.00 + 30.00/40.00) / 2.00; |
terryfan | 0:dac187b013e0 | 401 | LRI /= ratio; |
terryfan | 0:dac187b013e0 | 402 | URI /= ratio; |
terryfan | 1:e6f6471e2c00 | 403 | //reset obs interval |
terryfan | 1:e6f6471e2c00 | 404 | hr_mutex.lock(); |
terryfan | 1:e6f6471e2c00 | 405 | heart_beats = 0; |
terryfan | 1:e6f6471e2c00 | 406 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 407 | |
terryfan | 3:77efff091ef1 | 408 | vClock.reset(); |
terryfan | 3:77efff091ef1 | 409 | aClock.reset(); |
terryfan | 3:77efff091ef1 | 410 | // vpace_timer->start(LRI); |
terryfan | 3:77efff091ef1 | 411 | // apace_timer->start(LRI-AVI); |
terryfan | 0:dac187b013e0 | 412 | } |
terryfan | 3:77efff091ef1 | 413 | |
terryfan | 1:e6f6471e2c00 | 414 | void m_vpace() |
terryfan | 1:e6f6471e2c00 | 415 | { |
terryfan | 0:dac187b013e0 | 416 | vClock.reset(); |
ems316 | 4:f887d8169f83 | 417 | aClock.reset(); |
ems316 | 4:f887d8169f83 | 418 | arpClock.reset(); |
terryfan | 0:dac187b013e0 | 419 | Vpace = 1; |
terryfan | 3:77efff091ef1 | 420 | Thread::wait(1); |
terryfan | 0:dac187b013e0 | 421 | Vpace = 0; |
terryfan | 0:dac187b013e0 | 422 | osSignalSet(signalTid, 0x4); |
ems316 | 4:f887d8169f83 | 423 | osSignalSet(senseTid, 0x4); |
terryfan | 1:e6f6471e2c00 | 424 | hr_mutex.lock(); |
terryfan | 0:dac187b013e0 | 425 | heart_beats++; |
terryfan | 1:e6f6471e2c00 | 426 | hr_mutex.unlock(); |
ems316 | 4:f887d8169f83 | 427 | |
terryfan | 3:77efff091ef1 | 428 | toggleLed(4); |
terryfan | 0:dac187b013e0 | 429 | } |
terryfan | 3:77efff091ef1 | 430 | |
terryfan | 1:e6f6471e2c00 | 431 | void m_apace() |
terryfan | 1:e6f6471e2c00 | 432 | { |
terryfan | 0:dac187b013e0 | 433 | aClock.reset(); |
ems316 | 4:f887d8169f83 | 434 | arpClock.reset(); |
terryfan | 0:dac187b013e0 | 435 | Apace = 1; |
terryfan | 3:77efff091ef1 | 436 | Thread::wait(1); |
terryfan | 0:dac187b013e0 | 437 | Apace = 0; |
ems316 | 4:f887d8169f83 | 438 | osSignalSet(senseTid, 0x3); |
terryfan | 0:dac187b013e0 | 439 | osSignalSet(signalTid, 0x3); |
terryfan | 3:77efff091ef1 | 440 | toggleLed(3); |
terryfan | 0:dac187b013e0 | 441 | } |
terryfan | 3:77efff091ef1 | 442 | |
terryfan | 0:dac187b013e0 | 443 | void manualmode(void const *args) |
terryfan | 0:dac187b013e0 | 444 | { |
terryfan | 0:dac187b013e0 | 445 | upperBound = 175; //beats per msecond |
terryfan | 0:dac187b013e0 | 446 | lowerBound = 30; //beats per msecond |
terryfan | 0:dac187b013e0 | 447 | lcd.printf("M"); |
terryfan | 0:dac187b013e0 | 448 | mode = 'm'; |
terryfan | 1:e6f6471e2c00 | 449 | LRI = 2125; // max V-V (LRI) based on exercise mode |
terryfan | 1:e6f6471e2c00 | 450 | URI = 675; // min V-V (URI) based on sleep mode |
terryfan | 3:77efff091ef1 | 451 | |
terryfan | 0:dac187b013e0 | 452 | while(1) { |
terryfan | 0:dac187b013e0 | 453 | osEvent evt = signal_q.get(); |
terryfan | 0:dac187b013e0 | 454 | if(evt.status == osEventMessage) { |
terryfan | 0:dac187b013e0 | 455 | if((char)evt.value.p == 'v') { |
terryfan | 0:dac187b013e0 | 456 | m_vpace(); |
terryfan | 0:dac187b013e0 | 457 | } else if((char)evt.value.p == 'a') { |
terryfan | 0:dac187b013e0 | 458 | m_apace(); |
terryfan | 0:dac187b013e0 | 459 | } |
terryfan | 0:dac187b013e0 | 460 | } |
terryfan | 0:dac187b013e0 | 461 | } |
terryfan | 0:dac187b013e0 | 462 | } |
terryfan | 3:77efff091ef1 | 463 | |
terryfan | 1:e6f6471e2c00 | 464 | void obsinterval() |
terryfan | 1:e6f6471e2c00 | 465 | { |
terryfan | 1:e6f6471e2c00 | 466 | char newObsInt[8]; |
terryfan | 1:e6f6471e2c00 | 467 | int isChangingObsInt = 1; |
terryfan | 1:e6f6471e2c00 | 468 | int i = 0; |
terryfan | 1:e6f6471e2c00 | 469 | while(isChangingObsInt) { |
terryfan | 1:e6f6471e2c00 | 470 | osEvent evt = obsint_q.get(); |
terryfan | 1:e6f6471e2c00 | 471 | if(evt.status == osEventMessage) { |
terryfan | 1:e6f6471e2c00 | 472 | key = (char)evt.value.p; |
terryfan | 1:e6f6471e2c00 | 473 | if(key != '\r' && i < 7 ) { |
terryfan | 1:e6f6471e2c00 | 474 | newObsInt[i] = key; |
terryfan | 1:e6f6471e2c00 | 475 | i++; |
terryfan | 1:e6f6471e2c00 | 476 | } else if((key == '\r') && (i > 0)) { |
terryfan | 1:e6f6471e2c00 | 477 | heart_beats = 0; |
terryfan | 1:e6f6471e2c00 | 478 | int obsint; |
terryfan | 1:e6f6471e2c00 | 479 | newObsInt[i] = '\0'; |
terryfan | 1:e6f6471e2c00 | 480 | sscanf(newObsInt, "%d", &obsint); |
terryfan | 1:e6f6471e2c00 | 481 | observation_interval = (obsint > 0 ) ? obsint: 1; |
terryfan | 1:e6f6471e2c00 | 482 | isChangingObsInt = 0; |
terryfan | 1:e6f6471e2c00 | 483 | lcd.printf("%d", observation_interval); |
terryfan | 1:e6f6471e2c00 | 484 | } |
terryfan | 1:e6f6471e2c00 | 485 | } |
terryfan | 1:e6f6471e2c00 | 486 | } |
terryfan | 1:e6f6471e2c00 | 487 | } |
terryfan | 3:77efff091ef1 | 488 | |
terryfan | 0:dac187b013e0 | 489 | osThreadDef(PaceSignal, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 0:dac187b013e0 | 490 | osThreadDef(PaceSense, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 1:e6f6471e2c00 | 491 | osThreadDef(alarmThread, osPriorityBelowNormal, DEFAULT_STACK_SIZE); //priority BelowNormal |
terryfan | 3:77efff091ef1 | 492 | |
terryfan | 1:e6f6471e2c00 | 493 | osThreadDef(displayThread, osPriorityLow, DEFAULT_STACK_SIZE); //priority Low |
terryfan | 0:dac187b013e0 | 494 | osThreadDef(manualmode, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 3:77efff091ef1 | 495 | //osThreadDef(normalmode, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 3:77efff091ef1 | 496 | //osThreadDef(exercisemode, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 3:77efff091ef1 | 497 | //osThreadDef(sleepmode, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 3:77efff091ef1 | 498 | |
terryfan | 3:77efff091ef1 | 499 | |
terryfan | 1:e6f6471e2c00 | 500 | //////////////////////////////////////////// |
terryfan | 1:e6f6471e2c00 | 501 | //////////////////////////////////////////// |
terryfan | 1:e6f6471e2c00 | 502 | //////////////////////////////////////////// |
terryfan | 3:77efff091ef1 | 503 | |
terryfan | 0:dac187b013e0 | 504 | int main() |
terryfan | 0:dac187b013e0 | 505 | { |
terryfan | 1:e6f6471e2c00 | 506 | alarmTid = osThreadCreate(osThread(alarmThread), NULL); |
terryfan | 0:dac187b013e0 | 507 | senseTid = osThreadCreate(osThread(PaceSense), NULL); |
terryfan | 0:dac187b013e0 | 508 | signalTid = osThreadCreate(osThread(PaceSignal), NULL); |
terryfan | 0:dac187b013e0 | 509 | displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 3:77efff091ef1 | 510 | // pacemodeTid = osThreadCreate(osThread(normalmode), NULL); |
terryfan | 3:77efff091ef1 | 511 | normalmode(NULL); |
ems316 | 4:f887d8169f83 | 512 | |
terryfan | 0:dac187b013e0 | 513 | vsignal.rise(&vsignal_irq); //rising edge of timer |
terryfan | 0:dac187b013e0 | 514 | asignal.rise(&asignal_irq); |
terryfan | 3:77efff091ef1 | 515 | |
terryfan | 3:77efff091ef1 | 516 | // Callback<void()> apaceTimerTask((void*)NULL, (void (*)(void*))&a_pace); |
terryfan | 3:77efff091ef1 | 517 | // Callback<void()> vpaceTimerTask((void*)NULL, (void (*)(void*))&v_pace); |
terryfan | 3:77efff091ef1 | 518 | // apace_timer = new RtosTimer(apaceTimerTask); |
terryfan | 3:77efff091ef1 | 519 | // vpace_timer = new RtosTimer(vpaceTimerTask); |
terryfan | 3:77efff091ef1 | 520 | |
terryfan | 0:dac187b013e0 | 521 | lcd.cls(); |
terryfan | 3:77efff091ef1 | 522 | |
terryfan | 0:dac187b013e0 | 523 | pc.attach(&Rx_interrupt, RawSerial::RxIrq); |
terryfan | 0:dac187b013e0 | 524 | while(true) { |
terryfan | 3:77efff091ef1 | 525 | // lcd.printf("test1"); |
terryfan | 0:dac187b013e0 | 526 | osEvent evt = mode_q.get(); |
terryfan | 0:dac187b013e0 | 527 | if(evt.status == osEventMessage) { |
terryfan | 0:dac187b013e0 | 528 | switch((char)evt.value.p) { |
terryfan | 0:dac187b013e0 | 529 | case('n'): |
terryfan | 0:dac187b013e0 | 530 | mm_flag = 0; |
ems316 | 4:f887d8169f83 | 531 | osSignalSet(senseTid, 0x5); |
terryfan | 3:77efff091ef1 | 532 | lcd.printf("paceN"); |
terryfan | 0:dac187b013e0 | 533 | osThreadTerminate (pacemodeTid); |
terryfan | 1:e6f6471e2c00 | 534 | osThreadTerminate (displayTid); |
terryfan | 3:77efff091ef1 | 535 | // pacemodeTid = osThreadCreate(osThread(normalmode), NULL); |
terryfan | 3:77efff091ef1 | 536 | normalmode(NULL); |
terryfan | 1:e6f6471e2c00 | 537 | displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 1:e6f6471e2c00 | 538 | lcd.printf("%d", observation_interval); |
terryfan | 0:dac187b013e0 | 539 | break; |
terryfan | 0:dac187b013e0 | 540 | case('s'): |
terryfan | 0:dac187b013e0 | 541 | mm_flag = 0; |
ems316 | 4:f887d8169f83 | 542 | osSignalSet(senseTid, 0x5); |
terryfan | 3:77efff091ef1 | 543 | lcd.printf("paceS"); |
terryfan | 0:dac187b013e0 | 544 | osThreadTerminate (pacemodeTid); |
terryfan | 1:e6f6471e2c00 | 545 | osThreadTerminate (displayTid); |
terryfan | 3:77efff091ef1 | 546 | // pacemodeTid = osThreadCreate(osThread(sleepmode), NULL); |
terryfan | 3:77efff091ef1 | 547 | sleepmode(NULL); |
terryfan | 1:e6f6471e2c00 | 548 | displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 0:dac187b013e0 | 549 | break; |
terryfan | 0:dac187b013e0 | 550 | case('e'): |
terryfan | 0:dac187b013e0 | 551 | mm_flag = 0; |
ems316 | 4:f887d8169f83 | 552 | osSignalSet(senseTid, 0x5); |
terryfan | 3:77efff091ef1 | 553 | lcd.printf("paceE"); |
terryfan | 0:dac187b013e0 | 554 | osThreadTerminate (pacemodeTid); |
terryfan | 1:e6f6471e2c00 | 555 | osThreadTerminate (displayTid); |
terryfan | 3:77efff091ef1 | 556 | // pacemodeTid = osThreadCreate(osThread(exercisemode), NULL); |
terryfan | 3:77efff091ef1 | 557 | exercisemode(NULL); |
terryfan | 1:e6f6471e2c00 | 558 | displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 1:e6f6471e2c00 | 559 | lcd.printf("%d", observation_interval); |
terryfan | 0:dac187b013e0 | 560 | break; |
terryfan | 0:dac187b013e0 | 561 | case('m'): |
terryfan | 0:dac187b013e0 | 562 | mm_flag = 1; |
terryfan | 0:dac187b013e0 | 563 | osThreadTerminate (pacemodeTid); |
terryfan | 1:e6f6471e2c00 | 564 | //osThreadTerminate (displayTid); |
terryfan | 3:77efff091ef1 | 565 | // apace_timer->stop(); |
terryfan | 3:77efff091ef1 | 566 | // vpace_timer->stop(); |
terryfan | 0:dac187b013e0 | 567 | pacemodeTid = osThreadCreate(osThread(manualmode), NULL); |
terryfan | 1:e6f6471e2c00 | 568 | // displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 0:dac187b013e0 | 569 | manual_mode = 1; |
terryfan | 0:dac187b013e0 | 570 | break; |
terryfan | 0:dac187b013e0 | 571 | case('o'): |
terryfan | 1:e6f6471e2c00 | 572 | lcd.printf("modeO"); |
terryfan | 3:77efff091ef1 | 573 | hr_mutex.lock(); |
terryfan | 3:77efff091ef1 | 574 | obsinterval(); |
terryfan | 3:77efff091ef1 | 575 | hr_mutex.unlock(); |
terryfan | 1:e6f6471e2c00 | 576 | osThreadTerminate (displayTid); |
terryfan | 1:e6f6471e2c00 | 577 | displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 0:dac187b013e0 | 578 | break; |
terryfan | 0:dac187b013e0 | 579 | } |
terryfan | 0:dac187b013e0 | 580 | } |
terryfan | 0:dac187b013e0 | 581 | } |
terryfan | 3:77efff091ef1 | 582 | } |