latest pacemaker
Dependencies: TextLCD mbed-rtos mbed
main.cpp@3:77efff091ef1, 2016-12-09 (annotated)
- Committer:
- terryfan
- Date:
- Fri Dec 09 06:50:30 2016 +0000
- Revision:
- 3:77efff091ef1
- Parent:
- 2:682a3ac9d7a3
- Child:
- 4:a9c37c60425c
INCOMPLETE
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 |
terryfan | 3:77efff091ef1 | 39 | double AVI = 500; // 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; |
terryfan | 0:dac187b013e0 | 178 | aClock.reset(); |
terryfan | 3:77efff091ef1 | 179 | // apace_timer->stop(); |
terryfan | 0:dac187b013e0 | 180 | Apace = 0; |
terryfan | 0:dac187b013e0 | 181 | osSignalSet(signalTid, 0x3); |
terryfan | 3:77efff091ef1 | 182 | |
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 | 1:e6f6471e2c00 | 189 | Vpace = 1; |
terryfan | 3:77efff091ef1 | 190 | |
terryfan | 0:dac187b013e0 | 191 | vClock.reset(); |
terryfan | 3:77efff091ef1 | 192 | // vpace_timer->start(LRI); |
terryfan | 3:77efff091ef1 | 193 | // apace_timer->start(LRI-AVI); |
terryfan | 0:dac187b013e0 | 194 | Vpace = 0; |
terryfan | 3:77efff091ef1 | 195 | |
terryfan | 3:77efff091ef1 | 196 | hr_mutex.lock(); |
terryfan | 0:dac187b013e0 | 197 | osSignalSet(signalTid, 0x4); |
terryfan | 0:dac187b013e0 | 198 | heart_beats++; |
terryfan | 1:e6f6471e2c00 | 199 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 200 | |
terryfan | 3:77efff091ef1 | 201 | toggleLed(4); |
terryfan | 0:dac187b013e0 | 202 | } |
terryfan | 3:77efff091ef1 | 203 | |
terryfan | 3:77efff091ef1 | 204 | |
terryfan | 3:77efff091ef1 | 205 | |
terryfan | 0:dac187b013e0 | 206 | void PaceSignal(void const *args) |
terryfan | 0:dac187b013e0 | 207 | { |
terryfan | 0:dac187b013e0 | 208 | int pFlag1 = 0; |
terryfan | 0:dac187b013e0 | 209 | int pFlag2 = 0; |
terryfan | 0:dac187b013e0 | 210 | vClock.start(); |
terryfan | 0:dac187b013e0 | 211 | aClock.start(); |
terryfan | 3:77efff091ef1 | 212 | arpClock.start(); |
terryfan | 3:77efff091ef1 | 213 | // if(!mm_flag) { |
terryfan | 3:77efff091ef1 | 214 | // vpace_timer->start(LRI); |
terryfan | 3:77efff091ef1 | 215 | // apace_timer->start(LRI-AVI); |
terryfan | 3:77efff091ef1 | 216 | // } |
terryfan | 0:dac187b013e0 | 217 | // vpace_timer2->start(AVI); |
terryfan | 0:dac187b013e0 | 218 | while(1) { |
terryfan | 0:dac187b013e0 | 219 | while (!pFlag1) { |
terryfan | 0:dac187b013e0 | 220 | osEvent ext_signal = osSignalWait(0, osWaitForever); |
terryfan | 0:dac187b013e0 | 221 | int evt = ext_signal.value.signals; |
terryfan | 3:77efff091ef1 | 222 | |
terryfan | 3:77efff091ef1 | 223 | //lcd.printf("%d",evt); // 4(Vpace), 3(Apace), 2(Vsignal), 1(Asignal) |
terryfan | 3:77efff091ef1 | 224 | |
terryfan | 3:77efff091ef1 | 225 | if (evt == 0x1 && vClock.read_ms() >= PVARP) { //aSense |
terryfan | 0:dac187b013e0 | 226 | osSignalSet(senseTid, 0x1); |
terryfan | 0:dac187b013e0 | 227 | aClock.reset(); |
terryfan | 3:77efff091ef1 | 228 | arpClock.reset(); |
terryfan | 0:dac187b013e0 | 229 | pFlag1 = 1; |
terryfan | 0:dac187b013e0 | 230 | } else if(evt == 0x2 && vClock.read_ms() >= VRP) { //vSignal |
terryfan | 0:dac187b013e0 | 231 | osSignalSet(senseTid, 0x2); |
terryfan | 0:dac187b013e0 | 232 | vClock.reset(); |
terryfan | 3:77efff091ef1 | 233 | // if(!mm_flag) { |
terryfan | 3:77efff091ef1 | 234 | // vpace_timer->start(LRI); |
terryfan | 3:77efff091ef1 | 235 | // apace_timer->start(LRI-AVI); |
terryfan | 3:77efff091ef1 | 236 | // } |
terryfan | 0:dac187b013e0 | 237 | } else if (evt == 0x3) { //aPace |
terryfan | 0:dac187b013e0 | 238 | pFlag1 = 1; |
terryfan | 0:dac187b013e0 | 239 | } |
terryfan | 0:dac187b013e0 | 240 | } |
terryfan | 0:dac187b013e0 | 241 | pFlag1 = 0; |
terryfan | 3:77efff091ef1 | 242 | lcd.cls(); |
terryfan | 3:77efff091ef1 | 243 | lcd.printf("PS1"); |
terryfan | 0:dac187b013e0 | 244 | while(!pFlag2) { |
terryfan | 3:77efff091ef1 | 245 | |
terryfan | 0:dac187b013e0 | 246 | osEvent ext_signal = osSignalWait(0, osWaitForever); |
terryfan | 0:dac187b013e0 | 247 | int evt = ext_signal.value.signals; |
terryfan | 3:77efff091ef1 | 248 | |
terryfan | 3:77efff091ef1 | 249 | //lcd.printf("%d",evt); |
terryfan | 3:77efff091ef1 | 250 | |
terryfan | 3:77efff091ef1 | 251 | if (evt == 0x1 && arpClock.read_ms() >= ARP) { //aSignal |
terryfan | 0:dac187b013e0 | 252 | osSignalSet(senseTid, 0x1); |
terryfan | 3:77efff091ef1 | 253 | arpClock.reset(); |
terryfan | 3:77efff091ef1 | 254 | // aClock.reset(); |
terryfan | 3:77efff091ef1 | 255 | } else if(evt == 0x2) { //vSense |
terryfan | 2:682a3ac9d7a3 | 256 | hr_mutex.lock(); |
terryfan | 3:77efff091ef1 | 257 | osSignalSet(senseTid, 0x2); |
terryfan | 2:682a3ac9d7a3 | 258 | heart_beats++; |
terryfan | 3:77efff091ef1 | 259 | vClock.reset(); |
terryfan | 2:682a3ac9d7a3 | 260 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 261 | // if(!mm_flag) { |
terryfan | 3:77efff091ef1 | 262 | // vpace_timer->start(LRI); |
terryfan | 3:77efff091ef1 | 263 | // apace_timer->start(LRI-AVI); |
terryfan | 3:77efff091ef1 | 264 | // } |
terryfan | 0:dac187b013e0 | 265 | pFlag2 = 1; |
terryfan | 0:dac187b013e0 | 266 | } else if (evt == 0x4) { //vPace |
terryfan | 0:dac187b013e0 | 267 | pFlag2 = 1; |
terryfan | 0:dac187b013e0 | 268 | } |
terryfan | 0:dac187b013e0 | 269 | } |
terryfan | 0:dac187b013e0 | 270 | pFlag2 = 0; |
terryfan | 3:77efff091ef1 | 271 | lcd.cls(); |
terryfan | 3:77efff091ef1 | 272 | lcd.printf("PS2"); |
terryfan | 0:dac187b013e0 | 273 | } |
terryfan | 0:dac187b013e0 | 274 | } |
terryfan | 3:77efff091ef1 | 275 | |
terryfan | 3:77efff091ef1 | 276 | |
terryfan | 0:dac187b013e0 | 277 | void PaceSense(void const *args) |
terryfan | 0:dac187b013e0 | 278 | { |
terryfan | 3:77efff091ef1 | 279 | int interval; |
terryfan | 0:dac187b013e0 | 280 | int pFlag1 = 0; |
terryfan | 0:dac187b013e0 | 281 | int pFlag2 = 0; |
terryfan | 0:dac187b013e0 | 282 | while(1) { |
terryfan | 0:dac187b013e0 | 283 | while (!pFlag1) { |
terryfan | 3:77efff091ef1 | 284 | osEvent ext_signal = osSignalWait(0, (LRI-AVI)); |
terryfan | 0:dac187b013e0 | 285 | int evt = ext_signal.value.signals; |
terryfan | 3:77efff091ef1 | 286 | |
terryfan | 3:77efff091ef1 | 287 | //lcd.printf("%d",evt); |
terryfan | 3:77efff091ef1 | 288 | if (evt == 0x3) { //aPace 0x0 |
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); |
terryfan | 3:77efff091ef1 | 293 | aClock.reset(); |
terryfan | 3:77efff091ef1 | 294 | arpClock.reset(); |
terryfan | 3:77efff091ef1 | 295 | |
terryfan | 3:77efff091ef1 | 296 | toggleLed(3); |
terryfan | 0:dac187b013e0 | 297 | pFlag1 = 1; |
terryfan | 0:dac187b013e0 | 298 | } |
terryfan | 3:77efff091ef1 | 299 | if (evt == 0x1) { //aSense |
terryfan | 3:77efff091ef1 | 300 | if(!mm_flag) { |
terryfan | 3:77efff091ef1 | 301 | // vpace_timer->stop(); |
terryfan | 3:77efff091ef1 | 302 | interval = (vClock.read_ms() + AVI >= URI) ? AVI : URI - vClock.read_ms(); |
terryfan | 3:77efff091ef1 | 303 | } |
terryfan | 3:77efff091ef1 | 304 | // if(mm_flag) |
terryfan | 3:77efff091ef1 | 305 | toggleLed(evt); |
terryfan | 3:77efff091ef1 | 306 | pFlag1 = 1; |
terryfan | 3:77efff091ef1 | 307 | } else if(evt == 0x2) { //vSense |
terryfan | 3:77efff091ef1 | 308 | toggleLed(evt); |
terryfan | 3:77efff091ef1 | 309 | } |
terryfan | 3:77efff091ef1 | 310 | // } else if (evt == 0x3) { //aPace |
terryfan | 3:77efff091ef1 | 311 | // aClock.reset(); |
terryfan | 3:77efff091ef1 | 312 | // arpClock.reset(); |
terryfan | 3:77efff091ef1 | 313 | // pFlag1 = 1; |
terryfan | 3:77efff091ef1 | 314 | // } |
terryfan | 0:dac187b013e0 | 315 | } |
terryfan | 0:dac187b013e0 | 316 | pFlag1 = 0; |
terryfan | 3:77efff091ef1 | 317 | lcd.cls(); |
terryfan | 3:77efff091ef1 | 318 | lcd.printf("PS3"); |
terryfan | 0:dac187b013e0 | 319 | while(!pFlag2) { |
terryfan | 3:77efff091ef1 | 320 | lcd.printf("%d", interval); |
terryfan | 3:77efff091ef1 | 321 | osEvent ext_signal = osSignalWait(0, interval); |
terryfan | 3:77efff091ef1 | 322 | |
terryfan | 0:dac187b013e0 | 323 | int evt = ext_signal.value.signals; |
terryfan | 3:77efff091ef1 | 324 | |
terryfan | 3:77efff091ef1 | 325 | //lcd.printf("%d",evt); // 4096, 256, 16, 1 |
terryfan | 3:77efff091ef1 | 326 | if (evt == 0x4) { //vPace 0x0 |
terryfan | 3:77efff091ef1 | 327 | hr_mutex.lock(); |
terryfan | 3:77efff091ef1 | 328 | Vpace = 1; |
terryfan | 3:77efff091ef1 | 329 | vClock.reset(); |
terryfan | 3:77efff091ef1 | 330 | Thread::wait(1); |
terryfan | 3:77efff091ef1 | 331 | Vpace = 0; |
terryfan | 3:77efff091ef1 | 332 | osSignalSet(signalTid, 0x4); |
terryfan | 3:77efff091ef1 | 333 | heart_beats++; |
terryfan | 3:77efff091ef1 | 334 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 335 | |
terryfan | 3:77efff091ef1 | 336 | toggleLed(4); |
terryfan | 0:dac187b013e0 | 337 | pFlag2 = 1; |
terryfan | 3:77efff091ef1 | 338 | } else if (evt == 0x1) { //aSense |
terryfan | 3:77efff091ef1 | 339 | toggleLed(evt); |
terryfan | 3:77efff091ef1 | 340 | } else if(evt == 0x2) { //vSense |
terryfan | 3:77efff091ef1 | 341 | toggleLed(evt); |
terryfan | 0:dac187b013e0 | 342 | pFlag2 = 1; |
terryfan | 0:dac187b013e0 | 343 | } |
terryfan | 0:dac187b013e0 | 344 | } |
terryfan | 0:dac187b013e0 | 345 | pFlag2 = 0; |
terryfan | 3:77efff091ef1 | 346 | lcd.cls(); |
terryfan | 3:77efff091ef1 | 347 | lcd.printf("PS4"); |
terryfan | 0:dac187b013e0 | 348 | } |
terryfan | 0:dac187b013e0 | 349 | } |
terryfan | 3:77efff091ef1 | 350 | |
terryfan | 0:dac187b013e0 | 351 | void normalmode(void const *args) |
terryfan | 0:dac187b013e0 | 352 | { |
terryfan | 0:dac187b013e0 | 353 | initialize_intervals(); |
terryfan | 0:dac187b013e0 | 354 | mode = 'n'; |
terryfan | 0:dac187b013e0 | 355 | lcd.printf("N"); |
terryfan | 0:dac187b013e0 | 356 | upperBound = 100; //beats per msecond |
terryfan | 0:dac187b013e0 | 357 | lowerBound = 40; //beats per msecond |
terryfan | 1:e6f6471e2c00 | 358 | //reset obs interval |
terryfan | 1:e6f6471e2c00 | 359 | hr_mutex.lock(); |
terryfan | 1:e6f6471e2c00 | 360 | heart_beats = 0; |
terryfan | 1:e6f6471e2c00 | 361 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 362 | |
terryfan | 3:77efff091ef1 | 363 | vClock.reset(); |
terryfan | 3:77efff091ef1 | 364 | aClock.reset(); |
terryfan | 3:77efff091ef1 | 365 | // vpace_timer->start(LRI); |
terryfan | 3:77efff091ef1 | 366 | // apace_timer->start(LRI-AVI); |
terryfan | 0:dac187b013e0 | 367 | } |
terryfan | 3:77efff091ef1 | 368 | |
terryfan | 0:dac187b013e0 | 369 | void exercisemode(void const *args) |
terryfan | 0:dac187b013e0 | 370 | { |
terryfan | 0:dac187b013e0 | 371 | initialize_intervals(); |
terryfan | 0:dac187b013e0 | 372 | mode = 'e'; |
terryfan | 0:dac187b013e0 | 373 | lcd.printf("E"); |
terryfan | 0:dac187b013e0 | 374 | upperBound = 175; //beats per msecond |
terryfan | 0:dac187b013e0 | 375 | lowerBound = 100; //beats per msecond |
terryfan | 0:dac187b013e0 | 376 | ratio = (175.00/100.00 + 100.00/40.00) / 2.00; |
terryfan | 0:dac187b013e0 | 377 | LRI /= ratio; |
terryfan | 0:dac187b013e0 | 378 | URI /= ratio; |
terryfan | 1:e6f6471e2c00 | 379 | //reset obs interval |
terryfan | 1:e6f6471e2c00 | 380 | hr_mutex.lock(); |
terryfan | 1:e6f6471e2c00 | 381 | heart_beats = 0; |
terryfan | 1:e6f6471e2c00 | 382 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 383 | |
terryfan | 3:77efff091ef1 | 384 | vClock.reset(); |
terryfan | 3:77efff091ef1 | 385 | aClock.reset(); |
terryfan | 3:77efff091ef1 | 386 | // vpace_timer->start(LRI); |
terryfan | 3:77efff091ef1 | 387 | // apace_timer->start(LRI-AVI); |
terryfan | 0:dac187b013e0 | 388 | } |
terryfan | 3:77efff091ef1 | 389 | |
terryfan | 0:dac187b013e0 | 390 | void sleepmode(void const *args) |
terryfan | 0:dac187b013e0 | 391 | { |
terryfan | 0:dac187b013e0 | 392 | initialize_intervals(); |
terryfan | 0:dac187b013e0 | 393 | mode = 's'; |
terryfan | 0:dac187b013e0 | 394 | lcd.printf("S"); |
terryfan | 0:dac187b013e0 | 395 | upperBound = 60; //beats per msecond |
terryfan | 0:dac187b013e0 | 396 | lowerBound = 30; //beats per msecond v-v 0.5s |
terryfan | 0:dac187b013e0 | 397 | ratio = (60.00/100.00 + 30.00/40.00) / 2.00; |
terryfan | 0:dac187b013e0 | 398 | LRI /= ratio; |
terryfan | 0:dac187b013e0 | 399 | URI /= ratio; |
terryfan | 1:e6f6471e2c00 | 400 | //reset obs interval |
terryfan | 1:e6f6471e2c00 | 401 | hr_mutex.lock(); |
terryfan | 1:e6f6471e2c00 | 402 | heart_beats = 0; |
terryfan | 1:e6f6471e2c00 | 403 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 404 | |
terryfan | 3:77efff091ef1 | 405 | vClock.reset(); |
terryfan | 3:77efff091ef1 | 406 | aClock.reset(); |
terryfan | 3:77efff091ef1 | 407 | // vpace_timer->start(LRI); |
terryfan | 3:77efff091ef1 | 408 | // apace_timer->start(LRI-AVI); |
terryfan | 0:dac187b013e0 | 409 | } |
terryfan | 3:77efff091ef1 | 410 | |
terryfan | 1:e6f6471e2c00 | 411 | void m_vpace() |
terryfan | 1:e6f6471e2c00 | 412 | { |
terryfan | 0:dac187b013e0 | 413 | vClock.reset(); |
terryfan | 0:dac187b013e0 | 414 | Vpace = 1; |
terryfan | 3:77efff091ef1 | 415 | Thread::wait(1); |
terryfan | 0:dac187b013e0 | 416 | Vpace = 0; |
terryfan | 0:dac187b013e0 | 417 | osSignalSet(signalTid, 0x4); |
terryfan | 1:e6f6471e2c00 | 418 | hr_mutex.lock(); |
terryfan | 0:dac187b013e0 | 419 | heart_beats++; |
terryfan | 1:e6f6471e2c00 | 420 | hr_mutex.unlock(); |
terryfan | 3:77efff091ef1 | 421 | |
terryfan | 3:77efff091ef1 | 422 | toggleLed(4); |
terryfan | 0:dac187b013e0 | 423 | } |
terryfan | 3:77efff091ef1 | 424 | |
terryfan | 1:e6f6471e2c00 | 425 | void m_apace() |
terryfan | 1:e6f6471e2c00 | 426 | { |
terryfan | 0:dac187b013e0 | 427 | aClock.reset(); |
terryfan | 0:dac187b013e0 | 428 | Apace = 1; |
terryfan | 3:77efff091ef1 | 429 | Thread::wait(1); |
terryfan | 0:dac187b013e0 | 430 | Apace = 0; |
terryfan | 0:dac187b013e0 | 431 | osSignalSet(signalTid, 0x3); |
terryfan | 3:77efff091ef1 | 432 | |
terryfan | 3:77efff091ef1 | 433 | toggleLed(3); |
terryfan | 0:dac187b013e0 | 434 | } |
terryfan | 3:77efff091ef1 | 435 | |
terryfan | 0:dac187b013e0 | 436 | void manualmode(void const *args) |
terryfan | 0:dac187b013e0 | 437 | { |
terryfan | 0:dac187b013e0 | 438 | upperBound = 175; //beats per msecond |
terryfan | 0:dac187b013e0 | 439 | lowerBound = 30; //beats per msecond |
terryfan | 0:dac187b013e0 | 440 | lcd.printf("M"); |
terryfan | 0:dac187b013e0 | 441 | mode = 'm'; |
terryfan | 1:e6f6471e2c00 | 442 | LRI = 2125; // max V-V (LRI) based on exercise mode |
terryfan | 1:e6f6471e2c00 | 443 | URI = 675; // min V-V (URI) based on sleep mode |
terryfan | 3:77efff091ef1 | 444 | |
terryfan | 0:dac187b013e0 | 445 | while(1) { |
terryfan | 0:dac187b013e0 | 446 | osEvent evt = signal_q.get(); |
terryfan | 0:dac187b013e0 | 447 | if(evt.status == osEventMessage) { |
terryfan | 0:dac187b013e0 | 448 | if((char)evt.value.p == 'v') { |
terryfan | 0:dac187b013e0 | 449 | m_vpace(); |
terryfan | 0:dac187b013e0 | 450 | } else if((char)evt.value.p == 'a') { |
terryfan | 0:dac187b013e0 | 451 | m_apace(); |
terryfan | 0:dac187b013e0 | 452 | } |
terryfan | 0:dac187b013e0 | 453 | } |
terryfan | 0:dac187b013e0 | 454 | } |
terryfan | 0:dac187b013e0 | 455 | } |
terryfan | 3:77efff091ef1 | 456 | |
terryfan | 1:e6f6471e2c00 | 457 | void obsinterval() |
terryfan | 1:e6f6471e2c00 | 458 | { |
terryfan | 1:e6f6471e2c00 | 459 | char newObsInt[8]; |
terryfan | 1:e6f6471e2c00 | 460 | int isChangingObsInt = 1; |
terryfan | 1:e6f6471e2c00 | 461 | int i = 0; |
terryfan | 1:e6f6471e2c00 | 462 | while(isChangingObsInt) { |
terryfan | 1:e6f6471e2c00 | 463 | osEvent evt = obsint_q.get(); |
terryfan | 1:e6f6471e2c00 | 464 | if(evt.status == osEventMessage) { |
terryfan | 1:e6f6471e2c00 | 465 | key = (char)evt.value.p; |
terryfan | 1:e6f6471e2c00 | 466 | if(key != '\r' && i < 7 ) { |
terryfan | 1:e6f6471e2c00 | 467 | newObsInt[i] = key; |
terryfan | 1:e6f6471e2c00 | 468 | i++; |
terryfan | 1:e6f6471e2c00 | 469 | } else if((key == '\r') && (i > 0)) { |
terryfan | 1:e6f6471e2c00 | 470 | heart_beats = 0; |
terryfan | 1:e6f6471e2c00 | 471 | int obsint; |
terryfan | 1:e6f6471e2c00 | 472 | newObsInt[i] = '\0'; |
terryfan | 1:e6f6471e2c00 | 473 | sscanf(newObsInt, "%d", &obsint); |
terryfan | 1:e6f6471e2c00 | 474 | observation_interval = (obsint > 0 ) ? obsint: 1; |
terryfan | 1:e6f6471e2c00 | 475 | isChangingObsInt = 0; |
terryfan | 1:e6f6471e2c00 | 476 | lcd.printf("%d", observation_interval); |
terryfan | 1:e6f6471e2c00 | 477 | } |
terryfan | 1:e6f6471e2c00 | 478 | } |
terryfan | 1:e6f6471e2c00 | 479 | } |
terryfan | 1:e6f6471e2c00 | 480 | } |
terryfan | 3:77efff091ef1 | 481 | |
terryfan | 0:dac187b013e0 | 482 | osThreadDef(PaceSignal, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 0:dac187b013e0 | 483 | osThreadDef(PaceSense, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 1:e6f6471e2c00 | 484 | osThreadDef(alarmThread, osPriorityBelowNormal, DEFAULT_STACK_SIZE); //priority BelowNormal |
terryfan | 3:77efff091ef1 | 485 | |
terryfan | 1:e6f6471e2c00 | 486 | osThreadDef(displayThread, osPriorityLow, DEFAULT_STACK_SIZE); //priority Low |
terryfan | 0:dac187b013e0 | 487 | osThreadDef(manualmode, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 3:77efff091ef1 | 488 | //osThreadDef(normalmode, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 3:77efff091ef1 | 489 | //osThreadDef(exercisemode, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 3:77efff091ef1 | 490 | //osThreadDef(sleepmode, osPriorityNormal, DEFAULT_STACK_SIZE); |
terryfan | 3:77efff091ef1 | 491 | |
terryfan | 3:77efff091ef1 | 492 | |
terryfan | 1:e6f6471e2c00 | 493 | //////////////////////////////////////////// |
terryfan | 1:e6f6471e2c00 | 494 | //////////////////////////////////////////// |
terryfan | 1:e6f6471e2c00 | 495 | //////////////////////////////////////////// |
terryfan | 3:77efff091ef1 | 496 | |
terryfan | 0:dac187b013e0 | 497 | int main() |
terryfan | 0:dac187b013e0 | 498 | { |
terryfan | 1:e6f6471e2c00 | 499 | alarmTid = osThreadCreate(osThread(alarmThread), NULL); |
terryfan | 0:dac187b013e0 | 500 | senseTid = osThreadCreate(osThread(PaceSense), NULL); |
terryfan | 0:dac187b013e0 | 501 | signalTid = osThreadCreate(osThread(PaceSignal), NULL); |
terryfan | 0:dac187b013e0 | 502 | displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 3:77efff091ef1 | 503 | // pacemodeTid = osThreadCreate(osThread(normalmode), NULL); |
terryfan | 3:77efff091ef1 | 504 | normalmode(NULL); |
terryfan | 1:e6f6471e2c00 | 505 | |
terryfan | 0:dac187b013e0 | 506 | vsignal.rise(&vsignal_irq); //rising edge of timer |
terryfan | 0:dac187b013e0 | 507 | asignal.rise(&asignal_irq); |
terryfan | 3:77efff091ef1 | 508 | |
terryfan | 3:77efff091ef1 | 509 | // Callback<void()> apaceTimerTask((void*)NULL, (void (*)(void*))&a_pace); |
terryfan | 3:77efff091ef1 | 510 | // Callback<void()> vpaceTimerTask((void*)NULL, (void (*)(void*))&v_pace); |
terryfan | 3:77efff091ef1 | 511 | // apace_timer = new RtosTimer(apaceTimerTask); |
terryfan | 3:77efff091ef1 | 512 | // vpace_timer = new RtosTimer(vpaceTimerTask); |
terryfan | 3:77efff091ef1 | 513 | |
terryfan | 0:dac187b013e0 | 514 | lcd.cls(); |
terryfan | 3:77efff091ef1 | 515 | |
terryfan | 0:dac187b013e0 | 516 | pc.attach(&Rx_interrupt, RawSerial::RxIrq); |
terryfan | 0:dac187b013e0 | 517 | while(true) { |
terryfan | 3:77efff091ef1 | 518 | // lcd.printf("test1"); |
terryfan | 0:dac187b013e0 | 519 | osEvent evt = mode_q.get(); |
terryfan | 0:dac187b013e0 | 520 | if(evt.status == osEventMessage) { |
terryfan | 0:dac187b013e0 | 521 | switch((char)evt.value.p) { |
terryfan | 0:dac187b013e0 | 522 | case('n'): |
terryfan | 0:dac187b013e0 | 523 | mm_flag = 0; |
terryfan | 3:77efff091ef1 | 524 | lcd.printf("paceN"); |
terryfan | 0:dac187b013e0 | 525 | osThreadTerminate (pacemodeTid); |
terryfan | 1:e6f6471e2c00 | 526 | osThreadTerminate (displayTid); |
terryfan | 3:77efff091ef1 | 527 | // pacemodeTid = osThreadCreate(osThread(normalmode), NULL); |
terryfan | 3:77efff091ef1 | 528 | normalmode(NULL); |
terryfan | 1:e6f6471e2c00 | 529 | displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 1:e6f6471e2c00 | 530 | lcd.printf("%d", observation_interval); |
terryfan | 0:dac187b013e0 | 531 | break; |
terryfan | 0:dac187b013e0 | 532 | case('s'): |
terryfan | 0:dac187b013e0 | 533 | mm_flag = 0; |
terryfan | 3:77efff091ef1 | 534 | lcd.printf("paceS"); |
terryfan | 0:dac187b013e0 | 535 | osThreadTerminate (pacemodeTid); |
terryfan | 1:e6f6471e2c00 | 536 | osThreadTerminate (displayTid); |
terryfan | 3:77efff091ef1 | 537 | // pacemodeTid = osThreadCreate(osThread(sleepmode), NULL); |
terryfan | 3:77efff091ef1 | 538 | sleepmode(NULL); |
terryfan | 1:e6f6471e2c00 | 539 | displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 0:dac187b013e0 | 540 | break; |
terryfan | 0:dac187b013e0 | 541 | case('e'): |
terryfan | 0:dac187b013e0 | 542 | mm_flag = 0; |
terryfan | 3:77efff091ef1 | 543 | lcd.printf("paceE"); |
terryfan | 0:dac187b013e0 | 544 | osThreadTerminate (pacemodeTid); |
terryfan | 1:e6f6471e2c00 | 545 | osThreadTerminate (displayTid); |
terryfan | 3:77efff091ef1 | 546 | // pacemodeTid = osThreadCreate(osThread(exercisemode), NULL); |
terryfan | 3:77efff091ef1 | 547 | exercisemode(NULL); |
terryfan | 1:e6f6471e2c00 | 548 | displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 1:e6f6471e2c00 | 549 | lcd.printf("%d", observation_interval); |
terryfan | 0:dac187b013e0 | 550 | break; |
terryfan | 0:dac187b013e0 | 551 | case('m'): |
terryfan | 0:dac187b013e0 | 552 | mm_flag = 1; |
terryfan | 0:dac187b013e0 | 553 | osThreadTerminate (pacemodeTid); |
terryfan | 1:e6f6471e2c00 | 554 | //osThreadTerminate (displayTid); |
terryfan | 3:77efff091ef1 | 555 | // apace_timer->stop(); |
terryfan | 3:77efff091ef1 | 556 | // vpace_timer->stop(); |
terryfan | 0:dac187b013e0 | 557 | pacemodeTid = osThreadCreate(osThread(manualmode), NULL); |
terryfan | 1:e6f6471e2c00 | 558 | // displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 0:dac187b013e0 | 559 | manual_mode = 1; |
terryfan | 0:dac187b013e0 | 560 | break; |
terryfan | 0:dac187b013e0 | 561 | case('o'): |
terryfan | 1:e6f6471e2c00 | 562 | lcd.printf("modeO"); |
terryfan | 3:77efff091ef1 | 563 | hr_mutex.lock(); |
terryfan | 3:77efff091ef1 | 564 | obsinterval(); |
terryfan | 3:77efff091ef1 | 565 | hr_mutex.unlock(); |
terryfan | 1:e6f6471e2c00 | 566 | osThreadTerminate (displayTid); |
terryfan | 1:e6f6471e2c00 | 567 | displayTid = osThreadCreate(osThread(displayThread), NULL); |
terryfan | 0:dac187b013e0 | 568 | break; |
terryfan | 0:dac187b013e0 | 569 | } |
terryfan | 0:dac187b013e0 | 570 | } |
terryfan | 0:dac187b013e0 | 571 | } |
terryfan | 3:77efff091ef1 | 572 | } |