mbed heart
Dependencies: TextLCD mbed-rtos mbed
PMTest.cpp@9:917f6d407d78, 2015-12-02 (annotated)
- Committer:
- lucastai
- Date:
- Wed Dec 02 21:34:10 2015 +0000
- Revision:
- 9:917f6d407d78
- Parent:
- 8:0f92ac2b9d0e
PM Test Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lucastai | 0:54ec65701097 | 1 | #include "mbed.h" |
lucastai | 0:54ec65701097 | 2 | #include "LPC17xx.h" |
lucastai | 0:54ec65701097 | 3 | #include "TextLCD.h" |
lucastai | 0:54ec65701097 | 4 | #include "rtos.h" |
lucastai | 0:54ec65701097 | 5 | #include "Thread.h" |
lucastai | 0:54ec65701097 | 6 | |
lucastai | 0:54ec65701097 | 7 | DigitalOut vsense(p23); |
lucastai | 0:54ec65701097 | 8 | DigitalOut asense(p24); |
lucastai | 0:54ec65701097 | 9 | DigitalIn apace(p22); |
lucastai | 0:54ec65701097 | 10 | DigitalIn vpace(p21); |
lucastai | 0:54ec65701097 | 11 | |
lucastai | 8:0f92ac2b9d0e | 12 | Ticker rate_monitor; |
lucastai | 8:0f92ac2b9d0e | 13 | |
lucastai | 8:0f92ac2b9d0e | 14 | |
lucastai | 8:0f92ac2b9d0e | 15 | TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD20x4); // rs, e, d4-d7 |
lucastai | 8:0f92ac2b9d0e | 16 | |
lucastai | 8:0f92ac2b9d0e | 17 | volatile int beats = 0; |
lucastai | 8:0f92ac2b9d0e | 18 | volatile double bpm = 0; |
lucastai | 8:0f92ac2b9d0e | 19 | |
lucastai | 4:6d3ddf74b49a | 20 | DigitalOut led_apace(LED1); |
lucastai | 4:6d3ddf74b49a | 21 | DigitalOut led_vpace(LED2); |
lucastai | 4:6d3ddf74b49a | 22 | DigitalOut led_asense(LED3); |
lucastai | 4:6d3ddf74b49a | 23 | DigitalOut led_vsense(LED4); |
lucastai | 4:6d3ddf74b49a | 24 | |
lucastai | 0:54ec65701097 | 25 | Serial serial_in(USBTX, USBRX); |
lucastai | 0:54ec65701097 | 26 | |
lucastai | 0:54ec65701097 | 27 | |
lucastai | 0:54ec65701097 | 28 | int test_time_count; |
lucastai | 0:54ec65701097 | 29 | int curr_test; |
lucastai | 5:8d9bcf25accb | 30 | int minwaitV = 200; |
lucastai | 5:8d9bcf25accb | 31 | int minwaitA = 400; |
lucastai | 0:54ec65701097 | 32 | |
lucastai | 8:0f92ac2b9d0e | 33 | int obs_int = 10; |
lucastai | 8:0f92ac2b9d0e | 34 | |
lucastai | 8:0f92ac2b9d0e | 35 | |
lucastai | 0:54ec65701097 | 36 | int needs_num_input = 0; |
lucastai | 0:54ec65701097 | 37 | |
chadnach1 | 3:189e64d01258 | 38 | enum mode {RANDOM,MANUAL,TEST}; |
lucastai | 0:54ec65701097 | 39 | mode test_curr_mode = RANDOM; |
lucastai | 0:54ec65701097 | 40 | Timer t; |
lucastai | 0:54ec65701097 | 41 | |
lucastai | 0:54ec65701097 | 42 | |
lucastai | 5:8d9bcf25accb | 43 | void test_LRI(){ |
lucastai | 5:8d9bcf25accb | 44 | Timer test_timer; |
lucastai | 5:8d9bcf25accb | 45 | |
lucastai | 5:8d9bcf25accb | 46 | test_timer.start(); |
lucastai | 5:8d9bcf25accb | 47 | // wait for next vpace |
lucastai | 6:315e6d6b1174 | 48 | while(vpace != 1){} |
lucastai | 5:8d9bcf25accb | 49 | serial_in.printf("LRI: The separation between Ventricle events was %i ", test_timer.read_ms()); |
lucastai | 5:8d9bcf25accb | 50 | |
lucastai | 5:8d9bcf25accb | 51 | } |
lucastai | 5:8d9bcf25accb | 52 | |
lucastai | 7:1c32e8f6bde8 | 53 | void test_PVARP(){ |
lucastai | 7:1c32e8f6bde8 | 54 | Timer test_timer; |
lucastai | 7:1c32e8f6bde8 | 55 | |
lucastai | 7:1c32e8f6bde8 | 56 | test_timer.start(); |
lucastai | 7:1c32e8f6bde8 | 57 | |
lucastai | 7:1c32e8f6bde8 | 58 | while(apace != 1){} |
lucastai | 7:1c32e8f6bde8 | 59 | serial_in.printf("PVARP: The separation between Ventricular and Atrial events was %i ", test_timer.read_ms()); |
lucastai | 7:1c32e8f6bde8 | 60 | |
lucastai | 7:1c32e8f6bde8 | 61 | |
lucastai | 7:1c32e8f6bde8 | 62 | } |
lucastai | 7:1c32e8f6bde8 | 63 | |
lucastai | 5:8d9bcf25accb | 64 | void test_VRP(){ |
lucastai | 5:8d9bcf25accb | 65 | Timer test_timer; |
lucastai | 5:8d9bcf25accb | 66 | |
lucastai | 5:8d9bcf25accb | 67 | test_timer.start(); |
lucastai | 5:8d9bcf25accb | 68 | |
lucastai | 9:917f6d407d78 | 69 | while(vpace != 1 && vsense != 1){} |
lucastai | 5:8d9bcf25accb | 70 | serial_in.printf("VRP: The separation between Ventricle events was %i ", test_timer.read_ms()); |
lucastai | 5:8d9bcf25accb | 71 | |
lucastai | 5:8d9bcf25accb | 72 | |
lucastai | 5:8d9bcf25accb | 73 | } |
lucastai | 5:8d9bcf25accb | 74 | |
lucastai | 7:1c32e8f6bde8 | 75 | |
lucastai | 5:8d9bcf25accb | 76 | void test_AVI(){ |
lucastai | 5:8d9bcf25accb | 77 | Timer test_timer; |
lucastai | 5:8d9bcf25accb | 78 | |
lucastai | 5:8d9bcf25accb | 79 | test_timer.start(); |
lucastai | 5:8d9bcf25accb | 80 | |
lucastai | 5:8d9bcf25accb | 81 | while(vpace != 1){} |
lucastai | 5:8d9bcf25accb | 82 | serial_in.printf("AVI: The separation between Atrial and Ventricle events was %i ", test_timer.read_ms()); |
lucastai | 5:8d9bcf25accb | 83 | |
lucastai | 5:8d9bcf25accb | 84 | |
lucastai | 5:8d9bcf25accb | 85 | } |
lucastai | 7:1c32e8f6bde8 | 86 | |
lucastai | 7:1c32e8f6bde8 | 87 | void test_AVI2(){ |
lucastai | 7:1c32e8f6bde8 | 88 | Timer test_timer; |
lucastai | 7:1c32e8f6bde8 | 89 | |
lucastai | 7:1c32e8f6bde8 | 90 | test_timer.start(); |
lucastai | 7:1c32e8f6bde8 | 91 | |
lucastai | 7:1c32e8f6bde8 | 92 | while(apace != 1){} |
lucastai | 7:1c32e8f6bde8 | 93 | serial_in.printf("LRI-AVI: The separation between Ventricle and Atrial events was %i ", test_timer.read_ms()); |
lucastai | 7:1c32e8f6bde8 | 94 | |
lucastai | 7:1c32e8f6bde8 | 95 | |
lucastai | 7:1c32e8f6bde8 | 96 | } |
lucastai | 5:8d9bcf25accb | 97 | |
lucastai | 0:54ec65701097 | 98 | |
lucastai | 0:54ec65701097 | 99 | |
lucastai | 0:54ec65701097 | 100 | |
chadnach1 | 3:189e64d01258 | 101 | void random_heart(void const *args) |
chadnach1 | 3:189e64d01258 | 102 | { |
lucastai | 0:54ec65701097 | 103 | t.reset(); |
lucastai | 0:54ec65701097 | 104 | t.start(); |
lucastai | 4:6d3ddf74b49a | 105 | while(1){ |
chadnach1 | 3:189e64d01258 | 106 | while(test_curr_mode == RANDOM) { |
lucastai | 0:54ec65701097 | 107 | int r = rand(); |
chadnach1 | 3:189e64d01258 | 108 | if(r%2 == 0) { |
lucastai | 9:917f6d407d78 | 109 | if(t.read_ms() > minwaitA) { |
lucastai | 4:6d3ddf74b49a | 110 | led_asense = 1; |
lucastai | 0:54ec65701097 | 111 | asense = 1; |
lucastai | 0:54ec65701097 | 112 | Thread::wait(10); |
lucastai | 0:54ec65701097 | 113 | asense = 0; |
lucastai | 4:6d3ddf74b49a | 114 | led_asense = 0; |
lucastai | 2:4761ca8ba841 | 115 | t.reset(); |
lucastai | 2:4761ca8ba841 | 116 | |
lucastai | 5:8d9bcf25accb | 117 | }else{ |
lucastai | 5:8d9bcf25accb | 118 | Thread::wait(100); |
lucastai | 0:54ec65701097 | 119 | } |
chadnach1 | 3:189e64d01258 | 120 | } else { |
lucastai | 9:917f6d407d78 | 121 | if(t.read_ms() > minwaitV) { |
lucastai | 4:6d3ddf74b49a | 122 | led_vsense = 1; |
lucastai | 0:54ec65701097 | 123 | vsense = 1; |
lucastai | 0:54ec65701097 | 124 | Thread::wait(10); |
lucastai | 0:54ec65701097 | 125 | vsense = 0; |
lucastai | 8:0f92ac2b9d0e | 126 | beats++; |
lucastai | 4:6d3ddf74b49a | 127 | led_vsense = 0; |
lucastai | 2:4761ca8ba841 | 128 | t.reset(); |
lucastai | 2:4761ca8ba841 | 129 | |
lucastai | 5:8d9bcf25accb | 130 | }else{ |
lucastai | 5:8d9bcf25accb | 131 | Thread::wait(100); |
lucastai | 0:54ec65701097 | 132 | } |
lucastai | 0:54ec65701097 | 133 | } |
lucastai | 5:8d9bcf25accb | 134 | |
lucastai | 5:8d9bcf25accb | 135 | Thread::wait(150); |
lucastai | 0:54ec65701097 | 136 | |
lucastai | 0:54ec65701097 | 137 | |
lucastai | 0:54ec65701097 | 138 | |
lucastai | 0:54ec65701097 | 139 | } |
lucastai | 4:6d3ddf74b49a | 140 | } |
lucastai | 0:54ec65701097 | 141 | } |
lucastai | 0:54ec65701097 | 142 | |
lucastai | 8:0f92ac2b9d0e | 143 | void pm_beat_update(void const *args){ |
lucastai | 8:0f92ac2b9d0e | 144 | while(1){ |
lucastai | 8:0f92ac2b9d0e | 145 | lcd.locate(0,0); |
lucastai | 8:0f92ac2b9d0e | 146 | lcd.printf("BPM: %.1f ", bpm); |
lucastai | 8:0f92ac2b9d0e | 147 | } |
lucastai | 8:0f92ac2b9d0e | 148 | } |
lucastai | 8:0f92ac2b9d0e | 149 | |
lucastai | 0:54ec65701097 | 150 | |
chadnach1 | 3:189e64d01258 | 151 | void kbd_intrp() |
chadnach1 | 3:189e64d01258 | 152 | { |
lucastai | 2:4761ca8ba841 | 153 | |
lucastai | 0:54ec65701097 | 154 | char a = serial_in.getc(); |
lucastai | 2:4761ca8ba841 | 155 | serial_in.printf("char was " + a); |
chadnach1 | 3:189e64d01258 | 156 | if(needs_num_input) { |
lucastai | 0:54ec65701097 | 157 | // DO O handling |
lucastai | 0:54ec65701097 | 158 | needs_num_input = 0; |
lucastai | 0:54ec65701097 | 159 | return; |
lucastai | 0:54ec65701097 | 160 | } |
lucastai | 0:54ec65701097 | 161 | |
chadnach1 | 3:189e64d01258 | 162 | if(a == 'R') { |
lucastai | 0:54ec65701097 | 163 | test_curr_mode = RANDOM; |
chadnach1 | 3:189e64d01258 | 164 | } else if(a == 'T') { |
lucastai | 0:54ec65701097 | 165 | test_curr_mode = TEST; |
chadnach1 | 3:189e64d01258 | 166 | } else if(a == 'M') { |
lucastai | 0:54ec65701097 | 167 | test_curr_mode = MANUAL; |
lucastai | 0:54ec65701097 | 168 | } |
chadnach1 | 3:189e64d01258 | 169 | |
chadnach1 | 3:189e64d01258 | 170 | if(a == 'A' && test_curr_mode == MANUAL) { |
lucastai | 0:54ec65701097 | 171 | asense = 1; |
lucastai | 0:54ec65701097 | 172 | Thread::wait(10); |
lucastai | 0:54ec65701097 | 173 | asense = 0; |
lucastai | 0:54ec65701097 | 174 | } |
chadnach1 | 3:189e64d01258 | 175 | |
chadnach1 | 3:189e64d01258 | 176 | if(a == 'V' && test_curr_mode == MANUAL) { |
lucastai | 0:54ec65701097 | 177 | vsense = 1; |
lucastai | 0:54ec65701097 | 178 | Thread::wait(10); |
lucastai | 0:54ec65701097 | 179 | vsense = 0; |
lucastai | 0:54ec65701097 | 180 | } |
chadnach1 | 3:189e64d01258 | 181 | |
lucastai | 0:54ec65701097 | 182 | } |
lucastai | 0:54ec65701097 | 183 | |
lucastai | 4:6d3ddf74b49a | 184 | void rec_apace(){ |
lucastai | 4:6d3ddf74b49a | 185 | led_apace = 1; |
lucastai | 4:6d3ddf74b49a | 186 | Thread::wait(10); |
lucastai | 4:6d3ddf74b49a | 187 | led_apace = 0; |
lucastai | 4:6d3ddf74b49a | 188 | } |
lucastai | 0:54ec65701097 | 189 | |
lucastai | 4:6d3ddf74b49a | 190 | void rec_vpace(){ |
lucastai | 4:6d3ddf74b49a | 191 | led_vpace = 1; |
lucastai | 4:6d3ddf74b49a | 192 | Thread::wait(10); |
lucastai | 4:6d3ddf74b49a | 193 | led_vpace = 0; |
lucastai | 4:6d3ddf74b49a | 194 | } |
lucastai | 4:6d3ddf74b49a | 195 | |
lucastai | 5:8d9bcf25accb | 196 | void heart_response1(void const *args){ |
lucastai | 4:6d3ddf74b49a | 197 | while(1){ |
lucastai | 4:6d3ddf74b49a | 198 | if(apace == 1){ |
lucastai | 4:6d3ddf74b49a | 199 | rec_apace(); |
lucastai | 4:6d3ddf74b49a | 200 | } |
lucastai | 5:8d9bcf25accb | 201 | |
lucastai | 5:8d9bcf25accb | 202 | } |
lucastai | 5:8d9bcf25accb | 203 | |
lucastai | 5:8d9bcf25accb | 204 | } |
lucastai | 5:8d9bcf25accb | 205 | |
lucastai | 5:8d9bcf25accb | 206 | void heart_response2(void const *args){ |
lucastai | 5:8d9bcf25accb | 207 | while(1){ |
lucastai | 4:6d3ddf74b49a | 208 | |
lucastai | 4:6d3ddf74b49a | 209 | if(vpace == 1){ |
lucastai | 4:6d3ddf74b49a | 210 | rec_vpace(); |
lucastai | 4:6d3ddf74b49a | 211 | } |
lucastai | 4:6d3ddf74b49a | 212 | } |
lucastai | 4:6d3ddf74b49a | 213 | |
lucastai | 4:6d3ddf74b49a | 214 | } |
lucastai | 0:54ec65701097 | 215 | |
lucastai | 0:54ec65701097 | 216 | // interrupt function |
chadnach1 | 3:189e64d01258 | 217 | void interrupt_and_run_test() |
chadnach1 | 3:189e64d01258 | 218 | { |
lucastai | 9:917f6d407d78 | 219 | test_curr_mode = TEST; |
lucastai | 9:917f6d407d78 | 220 | serial_in.printf("--BEGIN TEST--"); |
lucastai | 9:917f6d407d78 | 221 | while(vpace != 1){} |
lucastai | 9:917f6d407d78 | 222 | while(vpace == 1){} |
lucastai | 9:917f6d407d78 | 223 | test_LRI(); |
lucastai | 9:917f6d407d78 | 224 | while(vpace != 1){} |
lucastai | 9:917f6d407d78 | 225 | while(vpace == 1){} |
lucastai | 9:917f6d407d78 | 226 | test_PVARP(); |
lucastai | 9:917f6d407d78 | 227 | while(vpace != 1){} |
lucastai | 9:917f6d407d78 | 228 | while(vpace == 1){} |
lucastai | 9:917f6d407d78 | 229 | test_VRP(); |
lucastai | 9:917f6d407d78 | 230 | while(apace != 1){} |
lucastai | 9:917f6d407d78 | 231 | while(apace == 1){} |
lucastai | 9:917f6d407d78 | 232 | test_AVI(); |
lucastai | 9:917f6d407d78 | 233 | serial_in.printf("--END TEST--"); |
lucastai | 9:917f6d407d78 | 234 | test_curr_mode = RANDOM; |
chadnach1 | 3:189e64d01258 | 235 | } |
lucastai | 0:54ec65701097 | 236 | |
lucastai | 8:0f92ac2b9d0e | 237 | void update_display() { |
lucastai | 8:0f92ac2b9d0e | 238 | bpm = beats / (double) obs_int * 60; |
lucastai | 8:0f92ac2b9d0e | 239 | //reset count |
lucastai | 8:0f92ac2b9d0e | 240 | beats = 0; |
lucastai | 7:1c32e8f6bde8 | 241 | } |
lucastai | 7:1c32e8f6bde8 | 242 | |
chadnach1 | 3:189e64d01258 | 243 | int main(void) |
chadnach1 | 3:189e64d01258 | 244 | { |
lucastai | 5:8d9bcf25accb | 245 | |
lucastai | 5:8d9bcf25accb | 246 | |
lucastai | 8:0f92ac2b9d0e | 247 | |
chadnach1 | 3:189e64d01258 | 248 | |
lucastai | 2:4761ca8ba841 | 249 | Thread t3(random_heart, (void *)""); |
lucastai | 5:8d9bcf25accb | 250 | Thread t2(heart_response1, (void *)""); |
lucastai | 5:8d9bcf25accb | 251 | Thread t4(heart_response2, (void *)""); |
lucastai | 8:0f92ac2b9d0e | 252 | Thread t5(pm_beat_update, (void *)""); |
lucastai | 2:4761ca8ba841 | 253 | |
lucastai | 8:0f92ac2b9d0e | 254 | rate_monitor.attach(&update_display, obs_int); |
chadnach1 | 3:189e64d01258 | 255 | char a = 'Z'; |
chadnach1 | 3:189e64d01258 | 256 | |
lucastai | 8:0f92ac2b9d0e | 257 | |
chadnach1 | 3:189e64d01258 | 258 | while(1) { |
chadnach1 | 3:189e64d01258 | 259 | |
chadnach1 | 3:189e64d01258 | 260 | if(serial_in.readable()) { |
chadnach1 | 3:189e64d01258 | 261 | a = serial_in.getc(); |
lucastai | 5:8d9bcf25accb | 262 | serial_in.printf("GOT %c!!",a); |
lucastai | 8:0f92ac2b9d0e | 263 | |
lucastai | 8:0f92ac2b9d0e | 264 | if (needs_num_input) { |
lucastai | 8:0f92ac2b9d0e | 265 | if (a >= '0' && a <= '9') { |
lucastai | 8:0f92ac2b9d0e | 266 | // update observation interval |
lucastai | 8:0f92ac2b9d0e | 267 | obs_int = (a - '0' + 1) * 5; |
lucastai | 8:0f92ac2b9d0e | 268 | needs_num_input = 0; |
lucastai | 8:0f92ac2b9d0e | 269 | rate_monitor.attach(&update_display, obs_int); |
lucastai | 8:0f92ac2b9d0e | 270 | serial_in.printf("Set observation interval to %d seconds\n", obs_int); |
lucastai | 8:0f92ac2b9d0e | 271 | } else { |
lucastai | 8:0f92ac2b9d0e | 272 | serial_in.printf("Expected numeric key\n"); |
lucastai | 8:0f92ac2b9d0e | 273 | } |
chadnach1 | 3:189e64d01258 | 274 | } |
lucastai | 2:4761ca8ba841 | 275 | |
chadnach1 | 3:189e64d01258 | 276 | if(a == 'R') { |
lucastai | 5:8d9bcf25accb | 277 | serial_in.printf("GOT R!!"); |
chadnach1 | 3:189e64d01258 | 278 | test_curr_mode = RANDOM; |
chadnach1 | 3:189e64d01258 | 279 | } else if(a == 'T') { |
lucastai | 5:8d9bcf25accb | 280 | serial_in.printf("GOT T!!"); |
chadnach1 | 3:189e64d01258 | 281 | test_curr_mode = TEST; |
lucastai | 9:917f6d407d78 | 282 | interrupt_and_run_test(); |
lucastai | 5:8d9bcf25accb | 283 | |
chadnach1 | 3:189e64d01258 | 284 | } else if(a == 'M') { |
lucastai | 5:8d9bcf25accb | 285 | serial_in.printf("GOT M!!"); |
chadnach1 | 3:189e64d01258 | 286 | test_curr_mode = MANUAL; |
chadnach1 | 3:189e64d01258 | 287 | } |
lucastai | 8:0f92ac2b9d0e | 288 | else if(a == 'O') { |
lucastai | 8:0f92ac2b9d0e | 289 | serial_in.printf("GOT obs!!"); |
lucastai | 8:0f92ac2b9d0e | 290 | needs_num_input = 1; |
lucastai | 8:0f92ac2b9d0e | 291 | } |
chadnach1 | 3:189e64d01258 | 292 | |
chadnach1 | 3:189e64d01258 | 293 | if(a == 'A' && test_curr_mode == MANUAL) { |
chadnach1 | 3:189e64d01258 | 294 | asense = 1; |
lucastai | 9:917f6d407d78 | 295 | led_asense = 1; |
chadnach1 | 3:189e64d01258 | 296 | Thread::wait(10); |
lucastai | 9:917f6d407d78 | 297 | led_asense = 0; |
chadnach1 | 3:189e64d01258 | 298 | asense = 0; |
chadnach1 | 3:189e64d01258 | 299 | } |
chadnach1 | 3:189e64d01258 | 300 | |
chadnach1 | 3:189e64d01258 | 301 | if(a == 'V' && test_curr_mode == MANUAL) { |
chadnach1 | 3:189e64d01258 | 302 | vsense = 1; |
lucastai | 9:917f6d407d78 | 303 | led_vsense = 1; |
chadnach1 | 3:189e64d01258 | 304 | Thread::wait(10); |
lucastai | 9:917f6d407d78 | 305 | led_vsense = 0; |
chadnach1 | 3:189e64d01258 | 306 | vsense = 0; |
chadnach1 | 3:189e64d01258 | 307 | } |
lucastai | 5:8d9bcf25accb | 308 | |
lucastai | 5:8d9bcf25accb | 309 | while(serial_in.readable()){ |
lucastai | 5:8d9bcf25accb | 310 | serial_in.printf("GOT TO CONSUME!!",a); |
lucastai | 5:8d9bcf25accb | 311 | serial_in.getc(); |
lucastai | 5:8d9bcf25accb | 312 | } |
chadnach1 | 3:189e64d01258 | 313 | |
chadnach1 | 3:189e64d01258 | 314 | |
chadnach1 | 3:189e64d01258 | 315 | } |
lucastai | 0:54ec65701097 | 316 | } |
lucastai | 8:0f92ac2b9d0e | 317 | |
lucastai | 5:8d9bcf25accb | 318 | |
lucastai | 0:54ec65701097 | 319 | } |