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