fork the master
Dependencies: TextLCD mbed-rtos mbed
Fork of Pacemaker by
main.cpp@11:dd278ffcb31f, 2014-12-01 (annotated)
- Committer:
- mfrede
- Date:
- Mon Dec 01 21:08:41 2014 +0000
- Revision:
- 11:dd278ffcb31f
- Parent:
- 10:19a2ce5660ea
- Child:
- 12:53c2af2009d4
update to keyboard input
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfrede | 0:6d04b1860ecf | 1 | /* |
mfrede | 0:6d04b1860ecf | 2 | * Pacemaker MBED code |
mfrede | 0:6d04b1860ecf | 3 | * |
mfrede | 0:6d04b1860ecf | 4 | * CIS541 Embedded Systems for Life Critical Applications |
mfrede | 8:ce2565cfe709 | 5 | * Author: Jing Qiu |
mfrede | 8:ce2565cfe709 | 6 | * Michael Frederick |
mfrede | 0:6d04b1860ecf | 7 | * |
mfrede | 1:e37d0cad77e2 | 8 | */ |
Jing_Qiu | 3:34e9766539fe | 9 | |
mfrede | 1:e37d0cad77e2 | 10 | #include "mbed.h" |
mfrede | 1:e37d0cad77e2 | 11 | #include "LPC17xx.h" |
mfrede | 1:e37d0cad77e2 | 12 | #include "TextLCD.h" |
mfrede | 1:e37d0cad77e2 | 13 | #include "rtos.h" |
mfrede | 11:dd278ffcb31f | 14 | #include <ctype.h> |
mfrede | 1:e37d0cad77e2 | 15 | |
Jing_Qiu | 3:34e9766539fe | 16 | #define AVI_h 100 |
Jing_Qiu | 3:34e9766539fe | 17 | #define AVI_l 30 |
Jing_Qiu | 3:34e9766539fe | 18 | #define PVARP_h 500 |
Jing_Qiu | 3:34e9766539fe | 19 | #define PVARP_l 150 |
Jing_Qiu | 3:34e9766539fe | 20 | |
Jing_Qiu | 3:34e9766539fe | 21 | #define VRP_h 500 |
Jing_Qiu | 3:34e9766539fe | 22 | #define VRP_l 150 |
Jing_Qiu | 3:34e9766539fe | 23 | #define BM(x) (1<<(x)) |
Jing_Qiu | 3:34e9766539fe | 24 | #define PULSE_WIDTH_V 200 |
Jing_Qiu | 3:34e9766539fe | 25 | #define PULSE_WIDTH_A 100 |
Jing_Qiu | 3:34e9766539fe | 26 | |
Jing_Qiu | 5:376358077dc8 | 27 | |
Jing_Qiu | 3:34e9766539fe | 28 | int LRI_h = 1666; |
Jing_Qiu | 3:34e9766539fe | 29 | int LRI_l = 666; |
Jing_Qiu | 5:376358077dc8 | 30 | int observation_interval=10000; //ms |
mfrede | 8:ce2565cfe709 | 31 | bool setObservation = false; |
Jing_Qiu | 3:34e9766539fe | 32 | |
mfrede | 1:e37d0cad77e2 | 33 | Serial pc(USBTX, USBRX); |
mfrede | 1:e37d0cad77e2 | 34 | TextLCD myPanel(p15,p16,p17,p18,p19,p20,TextLCD::LCD16x2); |
mfrede | 1:e37d0cad77e2 | 35 | char mode = 'N'; |
Jing_Qiu | 3:34e9766539fe | 36 | |
mfrede | 1:e37d0cad77e2 | 37 | int a_clock; |
mfrede | 1:e37d0cad77e2 | 38 | int v_clock; |
mfrede | 6:6bc5e65ada4e | 39 | bool setobservation = false; |
mfrede | 1:e37d0cad77e2 | 40 | |
Jing_Qiu | 5:376358077dc8 | 41 | int beat; |
Jing_Qiu | 5:376358077dc8 | 42 | |
Jing_Qiu | 3:34e9766539fe | 43 | InterruptIn atrial_int(p17); |
Jing_Qiu | 3:34e9766539fe | 44 | InterruptIn vent_int(p18); |
Jing_Qiu | 3:34e9766539fe | 45 | |
Jing_Qiu | 3:34e9766539fe | 46 | const int a_pace = 5; //pin 21 a pace output |
Jing_Qiu | 3:34e9766539fe | 47 | const int v_pace = 3; //pin 23 v pace output |
Jing_Qiu | 3:34e9766539fe | 48 | |
Jing_Qiu | 3:34e9766539fe | 49 | bool aSensed = 0; // 0 means that we are expecting Apace or Asense next |
Jing_Qiu | 5:376358077dc8 | 50 | DigitalOut led1(LED1); //apace |
Jing_Qiu | 5:376358077dc8 | 51 | DigitalOut led2(LED2); //vpace |
Jing_Qiu | 5:376358077dc8 | 52 | DigitalOut led3(LED3); //asense |
Jing_Qiu | 5:376358077dc8 | 53 | DigitalOut led4(LED4); //vsense |
Jing_Qiu | 3:34e9766539fe | 54 | |
Jing_Qiu | 3:34e9766539fe | 55 | void initTimer(); |
Jing_Qiu | 3:34e9766539fe | 56 | void startTimer(); |
Jing_Qiu | 3:34e9766539fe | 57 | void initGPIO_outputs(); |
Jing_Qiu | 3:34e9766539fe | 58 | void setGPIO(const int pinName); |
Jing_Qiu | 3:34e9766539fe | 59 | void clearGPIO(const int pinName); |
Jing_Qiu | 3:34e9766539fe | 60 | |
Jing_Qiu | 3:34e9766539fe | 61 | void asense(); |
Jing_Qiu | 3:34e9766539fe | 62 | void vsense(); |
Jing_Qiu | 3:34e9766539fe | 63 | |
Jing_Qiu | 3:34e9766539fe | 64 | void apace(); |
Jing_Qiu | 3:34e9766539fe | 65 | void vpace(); |
Jing_Qiu | 3:34e9766539fe | 66 | |
Jing_Qiu | 3:34e9766539fe | 67 | |
Jing_Qiu | 3:34e9766539fe | 68 | void apace() |
Jing_Qiu | 3:34e9766539fe | 69 | { |
Jing_Qiu | 5:376358077dc8 | 70 | led1 = 1; |
Jing_Qiu | 3:34e9766539fe | 71 | setGPIO(a_pace); |
Jing_Qiu | 3:34e9766539fe | 72 | wait_us(PULSE_WIDTH_A); |
Jing_Qiu | 5:376358077dc8 | 73 | clearGPIO(a_pace); |
mfrede | 8:ce2565cfe709 | 74 | led1 = 0; |
Jing_Qiu | 5:376358077dc8 | 75 | |
Jing_Qiu | 3:34e9766539fe | 76 | } |
Jing_Qiu | 3:34e9766539fe | 77 | |
Jing_Qiu | 3:34e9766539fe | 78 | void vpace() |
Jing_Qiu | 3:34e9766539fe | 79 | { |
Jing_Qiu | 5:376358077dc8 | 80 | led2= 1; |
Jing_Qiu | 3:34e9766539fe | 81 | setGPIO(v_pace); |
Jing_Qiu | 3:34e9766539fe | 82 | wait_us(PULSE_WIDTH_V); |
Jing_Qiu | 3:34e9766539fe | 83 | clearGPIO(v_pace); |
Jing_Qiu | 5:376358077dc8 | 84 | led2 = 0; |
Jing_Qiu | 3:34e9766539fe | 85 | } |
Jing_Qiu | 3:34e9766539fe | 86 | |
Jing_Qiu | 3:34e9766539fe | 87 | void initGPIO_outputs() |
Jing_Qiu | 3:34e9766539fe | 88 | { |
Jing_Qiu | 3:34e9766539fe | 89 | LPC_SC->PCONP |= 1<<15; |
Jing_Qiu | 3:34e9766539fe | 90 | LPC_GPIO2->FIODIR |= 1<<5; |
Jing_Qiu | 3:34e9766539fe | 91 | LPC_GPIO2->FIODIR |= 1<<3; |
Jing_Qiu | 3:34e9766539fe | 92 | LPC_GPIO2->FIODIR &= ~(1<<1); |
Jing_Qiu | 3:34e9766539fe | 93 | LPC_GPIO2->FIODIR &= ~(1<<2); |
Jing_Qiu | 3:34e9766539fe | 94 | } |
Jing_Qiu | 3:34e9766539fe | 95 | |
Jing_Qiu | 3:34e9766539fe | 96 | void setGPIO(const int pinName) |
Jing_Qiu | 3:34e9766539fe | 97 | { |
Jing_Qiu | 3:34e9766539fe | 98 | LPC_GPIO2->FIOPIN |= (1<<pinName); |
Jing_Qiu | 3:34e9766539fe | 99 | } |
Jing_Qiu | 3:34e9766539fe | 100 | |
Jing_Qiu | 3:34e9766539fe | 101 | void clearGPIO(const int pinName) |
Jing_Qiu | 3:34e9766539fe | 102 | { |
Jing_Qiu | 3:34e9766539fe | 103 | LPC_GPIO2->FIOPIN &= ~(1<<pinName); |
Jing_Qiu | 3:34e9766539fe | 104 | } |
Jing_Qiu | 3:34e9766539fe | 105 | |
Jing_Qiu | 3:34e9766539fe | 106 | void initTimer() |
Jing_Qiu | 3:34e9766539fe | 107 | { |
Jing_Qiu | 3:34e9766539fe | 108 | // set up OS timer (timer0) |
Jing_Qiu | 3:34e9766539fe | 109 | LPC_SC->PCONP |= BM(1); //power up timer0 |
Jing_Qiu | 3:34e9766539fe | 110 | LPC_SC->PCLKSEL0 |= BM(2); // clock = CCLK (96 MHz) |
Jing_Qiu | 3:34e9766539fe | 111 | LPC_TIM0->PR = 48000; // set prescale to 48000 (2048 Hz timer) |
Jing_Qiu | 3:34e9766539fe | 112 | LPC_TIM0->MR0 = 1; // match0 compare value (32-bit) |
Jing_Qiu | 3:34e9766539fe | 113 | LPC_TIM0->MCR |= BM(0)|BM(1); // interrupt and reset on match0 compare |
Jing_Qiu | 3:34e9766539fe | 114 | NVIC_EnableIRQ(TIMER0_IRQn); // enable timer interrupt |
Jing_Qiu | 3:34e9766539fe | 115 | } |
Jing_Qiu | 3:34e9766539fe | 116 | |
Jing_Qiu | 3:34e9766539fe | 117 | void startTimer() |
Jing_Qiu | 3:34e9766539fe | 118 | { |
Jing_Qiu | 3:34e9766539fe | 119 | LPC_TIM0->TCR |= BM(1); // reset timer1 |
Jing_Qiu | 3:34e9766539fe | 120 | LPC_TIM0->TCR &= ~BM(1); // release reset |
Jing_Qiu | 3:34e9766539fe | 121 | LPC_TIM0->TCR |= BM(0); // start timer |
Jing_Qiu | 3:34e9766539fe | 122 | } |
Jing_Qiu | 3:34e9766539fe | 123 | |
Jing_Qiu | 3:34e9766539fe | 124 | void resetTimer() |
Jing_Qiu | 3:34e9766539fe | 125 | { |
Jing_Qiu | 3:34e9766539fe | 126 | LPC_TIM0->TCR |= BM(1); // reset timer0 |
Jing_Qiu | 3:34e9766539fe | 127 | LPC_TIM0->TCR &= ~BM(1); // release reset |
Jing_Qiu | 3:34e9766539fe | 128 | } |
Jing_Qiu | 3:34e9766539fe | 129 | |
Jing_Qiu | 3:34e9766539fe | 130 | |
Jing_Qiu | 3:34e9766539fe | 131 | |
mfrede | 1:e37d0cad77e2 | 132 | void display_thread_handler(void const *args) |
mfrede | 1:e37d0cad77e2 | 133 | { |
mfrede | 1:e37d0cad77e2 | 134 | while(1) |
mfrede | 1:e37d0cad77e2 | 135 | { |
Jing_Qiu | 5:376358077dc8 | 136 | wait_ms(observation_interval); |
Jing_Qiu | 5:376358077dc8 | 137 | myPanel.printf("BPM: %d\r\n", beat/observation_interval * 60); |
Jing_Qiu | 5:376358077dc8 | 138 | beat=0; |
mfrede | 1:e37d0cad77e2 | 139 | } |
mfrede | 1:e37d0cad77e2 | 140 | } |
mfrede | 1:e37d0cad77e2 | 141 | |
mfrede | 1:e37d0cad77e2 | 142 | |
mfrede | 1:e37d0cad77e2 | 143 | void asense() { |
Jing_Qiu | 3:34e9766539fe | 144 | if (v_clock >= VRP_l) { //Ignore vSense outside this time interval |
Jing_Qiu | 3:34e9766539fe | 145 | v_clock = 0; |
Jing_Qiu | 3:34e9766539fe | 146 | aSensed = 0; |
Jing_Qiu | 3:34e9766539fe | 147 | } |
Jing_Qiu | 5:376358077dc8 | 148 | led3 = 1; |
Jing_Qiu | 5:376358077dc8 | 149 | |
mfrede | 1:e37d0cad77e2 | 150 | } |
mfrede | 1:e37d0cad77e2 | 151 | |
mfrede | 1:e37d0cad77e2 | 152 | void vsense() { |
Jing_Qiu | 3:34e9766539fe | 153 | if ((v_clock >= PVARP_l) && aSensed == 0){ |
Jing_Qiu | 3:34e9766539fe | 154 | a_clock = 0; |
Jing_Qiu | 3:34e9766539fe | 155 | aSensed = 1; |
Jing_Qiu | 5:376358077dc8 | 156 | beat++; |
Jing_Qiu | 3:34e9766539fe | 157 | } |
Jing_Qiu | 5:376358077dc8 | 158 | led4 = 1; |
mfrede | 1:e37d0cad77e2 | 159 | } |
mfrede | 1:e37d0cad77e2 | 160 | |
mfrede | 1:e37d0cad77e2 | 161 | void button_handler(void const *args) |
mfrede | 1:e37d0cad77e2 | 162 | { |
mfrede | 8:ce2565cfe709 | 163 | int observation_temp=0; |
mfrede | 1:e37d0cad77e2 | 164 | while(1) |
mfrede | 1:e37d0cad77e2 | 165 | { |
mfrede | 1:e37d0cad77e2 | 166 | char buffer; |
mfrede | 1:e37d0cad77e2 | 167 | if(pc.readable()) { |
mfrede | 11:dd278ffcb31f | 168 | buffer = toupper(pc.getc()); |
mfrede | 1:e37d0cad77e2 | 169 | if (buffer == 'N') |
mfrede | 10:19a2ce5660ea | 170 | { |
mfrede | 1:e37d0cad77e2 | 171 | mode = buffer; |
mfrede | 10:19a2ce5660ea | 172 | LRI_h = 1500; |
mfrede | 10:19a2ce5660ea | 173 | LRI_l = 600; |
mfrede | 10:19a2ce5660ea | 174 | } |
mfrede | 1:e37d0cad77e2 | 175 | else if (buffer == 'S') |
mfrede | 10:19a2ce5660ea | 176 | { |
mfrede | 1:e37d0cad77e2 | 177 | mode = buffer; |
mfrede | 10:19a2ce5660ea | 178 | LRI_h = 2000; |
mfrede | 10:19a2ce5660ea | 179 | LRI_l = 1000; |
mfrede | 10:19a2ce5660ea | 180 | } |
mfrede | 1:e37d0cad77e2 | 181 | else if (buffer == 'E') |
mfrede | 10:19a2ce5660ea | 182 | { |
mfrede | 1:e37d0cad77e2 | 183 | mode = buffer; |
mfrede | 10:19a2ce5660ea | 184 | LRI_h = 600; |
mfrede | 10:19a2ce5660ea | 185 | LRI_l = 342; |
mfrede | 10:19a2ce5660ea | 186 | } |
mfrede | 1:e37d0cad77e2 | 187 | else if (buffer == 'M') |
mfrede | 10:19a2ce5660ea | 188 | { |
mfrede | 1:e37d0cad77e2 | 189 | mode = buffer; |
mfrede | 10:19a2ce5660ea | 190 | LRI_h = 2000; |
mfrede | 10:19a2ce5660ea | 191 | LRI_l = 342; |
mfrede | 10:19a2ce5660ea | 192 | } |
mfrede | 1:e37d0cad77e2 | 193 | else if (buffer == 'A' && mode == 'M') |
mfrede | 1:e37d0cad77e2 | 194 | apace(); |
mfrede | 1:e37d0cad77e2 | 195 | else if (buffer == 'V' && mode == 'M') |
mfrede | 1:e37d0cad77e2 | 196 | vpace(); |
mfrede | 8:ce2565cfe709 | 197 | else if (buffer == 'O') { |
mfrede | 8:ce2565cfe709 | 198 | setObservation = true; |
mfrede | 8:ce2565cfe709 | 199 | observation_temp = 0; |
mfrede | 8:ce2565cfe709 | 200 | } |
mfrede | 1:e37d0cad77e2 | 201 | else if (buffer == '\n') |
mfrede | 6:6bc5e65ada4e | 202 | { |
mfrede | 8:ce2565cfe709 | 203 | observation_interval = observation_temp*1000; |
mfrede | 8:ce2565cfe709 | 204 | setObservation = false; |
mfrede | 6:6bc5e65ada4e | 205 | } |
mfrede | 8:ce2565cfe709 | 206 | else if (setObservation) |
mfrede | 6:6bc5e65ada4e | 207 | { |
mfrede | 8:ce2565cfe709 | 208 | observation_temp *= 10; |
mfrede | 8:ce2565cfe709 | 209 | observation_temp += (int) buffer; |
mfrede | 1:e37d0cad77e2 | 210 | |
mfrede | 6:6bc5e65ada4e | 211 | } |
mfrede | 1:e37d0cad77e2 | 212 | } |
mfrede | 1:e37d0cad77e2 | 213 | } |
mfrede | 1:e37d0cad77e2 | 214 | } |
mfrede | 1:e37d0cad77e2 | 215 | |
Jing_Qiu | 3:34e9766539fe | 216 | void pacemaker_thread_handler(void const *args) |
Jing_Qiu | 3:34e9766539fe | 217 | { |
Jing_Qiu | 3:34e9766539fe | 218 | initGPIO_outputs(); |
Jing_Qiu | 3:34e9766539fe | 219 | |
Jing_Qiu | 3:34e9766539fe | 220 | /********************************************************** |
Jing_Qiu | 3:34e9766539fe | 221 | ************Initialize timer to interrupt every 1 ms********* |
Jing_Qiu | 3:34e9766539fe | 222 | ***********************************************************/ |
Jing_Qiu | 3:34e9766539fe | 223 | |
Jing_Qiu | 3:34e9766539fe | 224 | initTimer(); |
Jing_Qiu | 3:34e9766539fe | 225 | startTimer(); |
Jing_Qiu | 3:34e9766539fe | 226 | |
Jing_Qiu | 3:34e9766539fe | 227 | atrial_int.rise(&asense); |
Jing_Qiu | 3:34e9766539fe | 228 | vent_int.rise(&vsense); |
Jing_Qiu | 3:34e9766539fe | 229 | while(1){} |
Jing_Qiu | 3:34e9766539fe | 230 | |
Jing_Qiu | 3:34e9766539fe | 231 | } |
Jing_Qiu | 3:34e9766539fe | 232 | |
Jing_Qiu | 3:34e9766539fe | 233 | |
Jing_Qiu | 3:34e9766539fe | 234 | /********************************************************** |
Jing_Qiu | 3:34e9766539fe | 235 | ************ timer interrupt every 1 ms********* |
Jing_Qiu | 3:34e9766539fe | 236 | ***********************************************************/ |
Jing_Qiu | 3:34e9766539fe | 237 | |
Jing_Qiu | 3:34e9766539fe | 238 | extern "C" void TIMER0_IRQHandler (void) { |
Jing_Qiu | 3:34e9766539fe | 239 | if((LPC_TIM0->IR & 0x01) == 0x01) // if MR0 interrupt |
Jing_Qiu | 3:34e9766539fe | 240 | { |
Jing_Qiu | 3:34e9766539fe | 241 | LPC_TIM0->IR |= (1 << 0); // Clear MR0 interrupt flag |
Jing_Qiu | 3:34e9766539fe | 242 | if (v_clock >= (LRI_h-AVI_l) && aSensed == 0) { |
Jing_Qiu | 3:34e9766539fe | 243 | a_clock = 0; |
Jing_Qiu | 3:34e9766539fe | 244 | aSensed = 1; |
Jing_Qiu | 5:376358077dc8 | 245 | beat++; |
Jing_Qiu | 3:34e9766539fe | 246 | //printf("Apace %d\r\n",v_clk); |
Jing_Qiu | 3:34e9766539fe | 247 | apace(); |
Jing_Qiu | 3:34e9766539fe | 248 | /* |
Jing_Qiu | 3:34e9766539fe | 249 | setGPIO(a_pace); |
Jing_Qiu | 3:34e9766539fe | 250 | wait_us(PULSE_WIDTH_A); |
Jing_Qiu | 3:34e9766539fe | 251 | clearGPIO(a_pace); |
Jing_Qiu | 3:34e9766539fe | 252 | */ |
Jing_Qiu | 3:34e9766539fe | 253 | } |
Jing_Qiu | 3:34e9766539fe | 254 | if ((a_clock >= AVI_h) && aSensed == 1) { |
Jing_Qiu | 3:34e9766539fe | 255 | v_clock = 0; |
Jing_Qiu | 3:34e9766539fe | 256 | aSensed = 0; |
Jing_Qiu | 5:376358077dc8 | 257 | //led3 = 0; |
Jing_Qiu | 3:34e9766539fe | 258 | //printf("Vpace %d\r\n",a_clk); |
Jing_Qiu | 3:34e9766539fe | 259 | /* |
Jing_Qiu | 3:34e9766539fe | 260 | setGPIO(v_pace); |
Jing_Qiu | 3:34e9766539fe | 261 | wait_us(PULSE_WIDTH_V); |
Jing_Qiu | 3:34e9766539fe | 262 | clearGPIO(v_pace); |
Jing_Qiu | 3:34e9766539fe | 263 | */ |
Jing_Qiu | 3:34e9766539fe | 264 | vpace(); |
Jing_Qiu | 3:34e9766539fe | 265 | } |
Jing_Qiu | 3:34e9766539fe | 266 | v_clock++; |
Jing_Qiu | 3:34e9766539fe | 267 | a_clock++; |
Jing_Qiu | 5:376358077dc8 | 268 | |
Jing_Qiu | 5:376358077dc8 | 269 | if(v_clock>500) led3 = 0; |
Jing_Qiu | 5:376358077dc8 | 270 | if(a_clock>500) led4 = 0; |
Jing_Qiu | 3:34e9766539fe | 271 | } |
Jing_Qiu | 3:34e9766539fe | 272 | } |
mfrede | 1:e37d0cad77e2 | 273 | |
mfrede | 1:e37d0cad77e2 | 274 | |
Jing_Qiu | 3:34e9766539fe | 275 | int main (void) { |
Jing_Qiu | 3:34e9766539fe | 276 | //TODO set parameters |
Jing_Qiu | 5:376358077dc8 | 277 | |
Jing_Qiu | 3:34e9766539fe | 278 | |
Jing_Qiu | 3:34e9766539fe | 279 | Thread display(display_thread_handler); |
Jing_Qiu | 3:34e9766539fe | 280 | Thread keyboard(button_handler); |
Jing_Qiu | 3:34e9766539fe | 281 | Thread pacemaker(pacemaker_thread_handler); |
Jing_Qiu | 3:34e9766539fe | 282 | |
Jing_Qiu | 3:34e9766539fe | 283 | while(1){} |
Jing_Qiu | 3:34e9766539fe | 284 | |
Jing_Qiu | 3:34e9766539fe | 285 | |
mfrede | 1:e37d0cad77e2 | 286 | } |