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