heart modes, send, receive
Dependencies: Terminal TextLCD mbed-rtos mbed
Fork of Heart by
heart.cpp@2:fc2f41386ee4, 2015-11-30 (annotated)
- Committer:
- sanjeet25
- Date:
- Mon Nov 30 21:00:10 2015 +0000
- Revision:
- 2:fc2f41386ee4
- Parent:
- 1:c340c31174a5
- Child:
- 3:b702a21b2990
pmt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sanjeet25 | 0:a307f0abfd4d | 1 | #include "mbed.h" |
sanjeet25 | 0:a307f0abfd4d | 2 | #include "TextLCD.h" |
sanjeet25 | 0:a307f0abfd4d | 3 | #include "rtos.h" |
sanjeet25 | 0:a307f0abfd4d | 4 | |
sanjeet25 | 0:a307f0abfd4d | 5 | TextLCD lcd(p15, p16, p17, p18, p19, p20); |
sanjeet25 | 0:a307f0abfd4d | 6 | Serial pc(USBTX, USBRX); |
sanjeet25 | 0:a307f0abfd4d | 7 | |
sanjeet25 | 1:c340c31174a5 | 8 | InterruptIn APace(p23); |
sanjeet25 | 1:c340c31174a5 | 9 | InterruptIn VPace(p24); |
sanjeet25 | 0:a307f0abfd4d | 10 | |
sanjeet25 | 1:c340c31174a5 | 11 | DigitalOut ASignal(p25); |
sanjeet25 | 1:c340c31174a5 | 12 | DigitalOut VSignal(p26); |
sanjeet25 | 1:c340c31174a5 | 13 | |
sanjeet25 | 2:fc2f41386ee4 | 14 | DigitalOut APaceReceive(LED1); |
sanjeet25 | 2:fc2f41386ee4 | 15 | DigitalOut VPaceReceive(LED2); |
sanjeet25 | 2:fc2f41386ee4 | 16 | |
sanjeet25 | 2:fc2f41386ee4 | 17 | DigitalOut ASignal1(LED3); |
sanjeet25 | 2:fc2f41386ee4 | 18 | DigitalOut VSignal1(LED4); |
sanjeet25 | 0:a307f0abfd4d | 19 | |
sanjeet25 | 0:a307f0abfd4d | 20 | const int minwait_A=50; |
sanjeet25 | 0:a307f0abfd4d | 21 | const int minwait_V=50; |
sanjeet25 | 0:a307f0abfd4d | 22 | |
sanjeet25 | 0:a307f0abfd4d | 23 | volatile int time_count=0; |
sanjeet25 | 1:c340c31174a5 | 24 | volatile int time1_count; |
sanjeet25 | 0:a307f0abfd4d | 25 | int m=0; |
sanjeet25 | 0:a307f0abfd4d | 26 | int s=0; |
sanjeet25 | 0:a307f0abfd4d | 27 | int ms=0; |
sanjeet25 | 2:fc2f41386ee4 | 28 | int heartInterval; |
sanjeet25 | 0:a307f0abfd4d | 29 | char key_input; |
sanjeet25 | 0:a307f0abfd4d | 30 | |
sanjeet25 | 0:a307f0abfd4d | 31 | int LRI=1500; |
sanjeet25 | 0:a307f0abfd4d | 32 | int URI=600; |
sanjeet25 | 2:fc2f41386ee4 | 33 | int heartRate; |
sanjeet25 | 0:a307f0abfd4d | 34 | volatile bool Areceived=false; |
sanjeet25 | 0:a307f0abfd4d | 35 | volatile bool Vreceived=false; |
sanjeet25 | 2:fc2f41386ee4 | 36 | int low[4] = {30,40,100,30}; |
sanjeet25 | 2:fc2f41386ee4 | 37 | int high[4] = {60,100,175,175}; |
sanjeet25 | 2:fc2f41386ee4 | 38 | int modeset; |
sanjeet25 | 0:a307f0abfd4d | 39 | |
sanjeet25 | 0:a307f0abfd4d | 40 | const int sleepModeURI = 1000; |
sanjeet25 | 0:a307f0abfd4d | 41 | const int sleepModeLRI = 2000; |
sanjeet25 | 0:a307f0abfd4d | 42 | |
sanjeet25 | 0:a307f0abfd4d | 43 | const int normalModeURI = 600; |
sanjeet25 | 0:a307f0abfd4d | 44 | const int normalModeLRI = 1500; |
sanjeet25 | 0:a307f0abfd4d | 45 | |
sanjeet25 | 0:a307f0abfd4d | 46 | const int sportsModeURI = 343; |
sanjeet25 | 0:a307f0abfd4d | 47 | const int sportsModeLRI = 600; |
sanjeet25 | 0:a307f0abfd4d | 48 | |
sanjeet25 | 0:a307f0abfd4d | 49 | const int manualModeURI = 343; |
sanjeet25 | 0:a307f0abfd4d | 50 | const int manualModeLRI = 2000; |
sanjeet25 | 0:a307f0abfd4d | 51 | |
sanjeet25 | 0:a307f0abfd4d | 52 | typedef enum Modes { |
sanjeet25 | 0:a307f0abfd4d | 53 | Test, |
sanjeet25 | 0:a307f0abfd4d | 54 | Normal, |
sanjeet25 | 0:a307f0abfd4d | 55 | Sleep, |
sanjeet25 | 0:a307f0abfd4d | 56 | Random, |
sanjeet25 | 0:a307f0abfd4d | 57 | Sports, |
sanjeet25 | 0:a307f0abfd4d | 58 | Manual, |
sanjeet25 | 2:fc2f41386ee4 | 59 | Observer |
sanjeet25 | 0:a307f0abfd4d | 60 | }; |
sanjeet25 | 0:a307f0abfd4d | 61 | Modes mode; |
sanjeet25 | 0:a307f0abfd4d | 62 | |
sanjeet25 | 0:a307f0abfd4d | 63 | Mutex HeartMutex; |
sanjeet25 | 0:a307f0abfd4d | 64 | |
sanjeet25 | 2:fc2f41386ee4 | 65 | Thread *heartreceive; |
sanjeet25 | 2:fc2f41386ee4 | 66 | Thread *heartsend; |
sanjeet25 | 2:fc2f41386ee4 | 67 | Thread *heartkeyboard; |
sanjeet25 | 2:fc2f41386ee4 | 68 | |
sanjeet25 | 2:fc2f41386ee4 | 69 | |
sanjeet25 | 0:a307f0abfd4d | 70 | void resetTimer0(); |
sanjeet25 | 0:a307f0abfd4d | 71 | void resetTimer1(); |
sanjeet25 | 2:fc2f41386ee4 | 72 | void VPacereceived(); |
sanjeet25 | 2:fc2f41386ee4 | 73 | void APacereceived(); |
sanjeet25 | 0:a307f0abfd4d | 74 | void ASignalsend(); |
sanjeet25 | 0:a307f0abfd4d | 75 | void VSignalsend(); |
sanjeet25 | 0:a307f0abfd4d | 76 | void HeartReceive(); |
sanjeet25 | 0:a307f0abfd4d | 77 | void HeartSend(); |
sanjeet25 | 1:c340c31174a5 | 78 | void HeartKeyBoardModeSwitch(); |
sanjeet25 | 0:a307f0abfd4d | 79 | |
sanjeet25 | 0:a307f0abfd4d | 80 | |
sanjeet25 | 0:a307f0abfd4d | 81 | extern "C" void TIMER0_IRQHandler (void) |
sanjeet25 | 0:a307f0abfd4d | 82 | { |
sanjeet25 | 0:a307f0abfd4d | 83 | if((LPC_TIM0->IR & 0x01) == 0x01) { // if interrupt provided, continue to next line |
sanjeet25 | 0:a307f0abfd4d | 84 | LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag |
sanjeet25 | 0:a307f0abfd4d | 85 | time_count++; //increment time_count |
sanjeet25 | 0:a307f0abfd4d | 86 | } |
sanjeet25 | 0:a307f0abfd4d | 87 | } |
sanjeet25 | 0:a307f0abfd4d | 88 | |
sanjeet25 | 0:a307f0abfd4d | 89 | void timer0_init(void) |
sanjeet25 | 0:a307f0abfd4d | 90 | { |
sanjeet25 | 0:a307f0abfd4d | 91 | LPC_SC->PCONP |=1<1; //power on the timer |
sanjeet25 | 1:c340c31174a5 | 92 | LPC_TIM0->MR0 = 23980; //10 msec period i.e the timer count will increment every 10ms |
sanjeet25 | 0:a307f0abfd4d | 93 | LPC_TIM0->MCR = 3; //reset control |
sanjeet25 | 0:a307f0abfd4d | 94 | //3 = Interrupt & reset timer0 on match |
sanjeet25 | 0:a307f0abfd4d | 95 | //1 = Interrupt only, no reset |
sanjeet25 | 0:a307f0abfd4d | 96 | NVIC_EnableIRQ(TIMER0_IRQn); //enable interrupt |
sanjeet25 | 0:a307f0abfd4d | 97 | LPC_TIM0->TCR = 1; //enable the timer |
sanjeet25 | 0:a307f0abfd4d | 98 | |
sanjeet25 | 0:a307f0abfd4d | 99 | } |
sanjeet25 | 0:a307f0abfd4d | 100 | |
sanjeet25 | 0:a307f0abfd4d | 101 | extern "C" void TIMER1_IRQHandler (void) |
sanjeet25 | 0:a307f0abfd4d | 102 | { |
sanjeet25 | 0:a307f0abfd4d | 103 | if((LPC_TIM0->IR & 0x01) == 0x01) { // if interrupt provided, continue to next line |
sanjeet25 | 0:a307f0abfd4d | 104 | LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag |
sanjeet25 | 1:c340c31174a5 | 105 | time1_count++; //increment time_count |
sanjeet25 | 0:a307f0abfd4d | 106 | } |
sanjeet25 | 0:a307f0abfd4d | 107 | } |
sanjeet25 | 0:a307f0abfd4d | 108 | |
sanjeet25 | 0:a307f0abfd4d | 109 | void timer1_init(void) |
sanjeet25 | 0:a307f0abfd4d | 110 | { |
sanjeet25 | 0:a307f0abfd4d | 111 | LPC_SC->PCONP |=1<1; //power on the timer |
sanjeet25 | 1:c340c31174a5 | 112 | LPC_TIM0->MR0 = 23980; //10 msec period i.e the timer count will increment every 10ms |
sanjeet25 | 0:a307f0abfd4d | 113 | LPC_TIM0->MCR = 3; //reset control |
sanjeet25 | 0:a307f0abfd4d | 114 | //3 = Interrupt & reset timer0 on match |
sanjeet25 | 0:a307f0abfd4d | 115 | //1 = Interrupt only, no reset |
sanjeet25 | 0:a307f0abfd4d | 116 | NVIC_EnableIRQ(TIMER0_IRQn); //enable interrupt |
sanjeet25 | 0:a307f0abfd4d | 117 | LPC_TIM0->TCR = 1; //enable the timer |
sanjeet25 | 0:a307f0abfd4d | 118 | |
sanjeet25 | 0:a307f0abfd4d | 119 | } |
sanjeet25 | 0:a307f0abfd4d | 120 | |
sanjeet25 | 0:a307f0abfd4d | 121 | void resetTimer0() |
sanjeet25 | 0:a307f0abfd4d | 122 | { |
sanjeet25 | 1:c340c31174a5 | 123 | m=0; |
sanjeet25 | 1:c340c31174a5 | 124 | s=0; |
sanjeet25 | 1:c340c31174a5 | 125 | ms=0; |
sanjeet25 | 2:fc2f41386ee4 | 126 | time_count=0; |
sanjeet25 | 0:a307f0abfd4d | 127 | LPC_TIM0->TCR = 0; |
sanjeet25 | 0:a307f0abfd4d | 128 | lcd.locate(0,0); |
sanjeet25 | 0:a307f0abfd4d | 129 | lcd.printf("%02d:%02d:%02d", m,s,ms); |
sanjeet25 | 0:a307f0abfd4d | 130 | } |
sanjeet25 | 0:a307f0abfd4d | 131 | |
sanjeet25 | 0:a307f0abfd4d | 132 | void resetTimer1() |
sanjeet25 | 0:a307f0abfd4d | 133 | { |
sanjeet25 | 1:c340c31174a5 | 134 | m=0; |
sanjeet25 | 1:c340c31174a5 | 135 | s=0; |
sanjeet25 | 1:c340c31174a5 | 136 | ms=0; |
sanjeet25 | 0:a307f0abfd4d | 137 | LPC_TIM1->TCR = 0; |
sanjeet25 | 0:a307f0abfd4d | 138 | lcd.locate(0,0); |
sanjeet25 | 2:fc2f41386ee4 | 139 | time1_count=0; |
sanjeet25 | 2:fc2f41386ee4 | 140 | |
sanjeet25 | 0:a307f0abfd4d | 141 | } |
sanjeet25 | 0:a307f0abfd4d | 142 | |
sanjeet25 | 2:fc2f41386ee4 | 143 | void VPacereceived() |
sanjeet25 | 2:fc2f41386ee4 | 144 | { pc.printf("VPace aaya"); |
sanjeet25 | 0:a307f0abfd4d | 145 | Vreceived=true; |
sanjeet25 | 2:fc2f41386ee4 | 146 | VPaceReceive=1; |
sanjeet25 | 2:fc2f41386ee4 | 147 | APaceReceive=0; |
sanjeet25 | 0:a307f0abfd4d | 148 | } |
sanjeet25 | 0:a307f0abfd4d | 149 | |
sanjeet25 | 2:fc2f41386ee4 | 150 | void APacereceived() |
sanjeet25 | 2:fc2f41386ee4 | 151 | { pc.printf("APace aaya"); |
sanjeet25 | 0:a307f0abfd4d | 152 | Areceived=true; |
sanjeet25 | 2:fc2f41386ee4 | 153 | VPaceReceive=0; |
sanjeet25 | 2:fc2f41386ee4 | 154 | APaceReceive=1; |
sanjeet25 | 0:a307f0abfd4d | 155 | } |
sanjeet25 | 0:a307f0abfd4d | 156 | |
sanjeet25 | 0:a307f0abfd4d | 157 | void ASignalsend() |
sanjeet25 | 0:a307f0abfd4d | 158 | { |
sanjeet25 | 0:a307f0abfd4d | 159 | ASignal=1; |
sanjeet25 | 1:c340c31174a5 | 160 | ASignal1=1; |
sanjeet25 | 1:c340c31174a5 | 161 | VSignal1=0; |
sanjeet25 | 0:a307f0abfd4d | 162 | } |
sanjeet25 | 0:a307f0abfd4d | 163 | |
sanjeet25 | 0:a307f0abfd4d | 164 | void VSignalsend() |
sanjeet25 | 0:a307f0abfd4d | 165 | { |
sanjeet25 | 0:a307f0abfd4d | 166 | VSignal=1; |
sanjeet25 | 1:c340c31174a5 | 167 | VSignal1=1; |
sanjeet25 | 1:c340c31174a5 | 168 | ASignal1=0; |
sanjeet25 | 0:a307f0abfd4d | 169 | } |
sanjeet25 | 2:fc2f41386ee4 | 170 | |
sanjeet25 | 2:fc2f41386ee4 | 171 | void updateHeart() |
sanjeet25 | 2:fc2f41386ee4 | 172 | { |
sanjeet25 | 2:fc2f41386ee4 | 173 | int temp = 60/heartInterval; |
sanjeet25 | 2:fc2f41386ee4 | 174 | heartRate = heartRate + temp; |
sanjeet25 | 2:fc2f41386ee4 | 175 | } |
sanjeet25 | 0:a307f0abfd4d | 176 | void HeartReceive(void const* args) |
sanjeet25 | 0:a307f0abfd4d | 177 | { |
sanjeet25 | 0:a307f0abfd4d | 178 | while(1) { |
sanjeet25 | 0:a307f0abfd4d | 179 | pc.printf("HR"); |
sanjeet25 | 1:c340c31174a5 | 180 | while(Areceived==false||Vreceived==false); |
sanjeet25 | 0:a307f0abfd4d | 181 | if(Vreceived==true) { |
sanjeet25 | 0:a307f0abfd4d | 182 | resetTimer0(); |
sanjeet25 | 2:fc2f41386ee4 | 183 | |
sanjeet25 | 0:a307f0abfd4d | 184 | } else if(Areceived==true) { |
sanjeet25 | 0:a307f0abfd4d | 185 | resetTimer0(); |
sanjeet25 | 0:a307f0abfd4d | 186 | } |
sanjeet25 | 0:a307f0abfd4d | 187 | } |
sanjeet25 | 0:a307f0abfd4d | 188 | } |
sanjeet25 | 0:a307f0abfd4d | 189 | |
sanjeet25 | 0:a307f0abfd4d | 190 | void HeartSend(void const* args) |
sanjeet25 | 0:a307f0abfd4d | 191 | { |
sanjeet25 | 1:c340c31174a5 | 192 | while(!pc.readable()) { |
sanjeet25 | 1:c340c31174a5 | 193 | int r = rand() % 2; |
sanjeet25 | 0:a307f0abfd4d | 194 | pc.printf("HS"); |
sanjeet25 | 1:c340c31174a5 | 195 | while(time_count<minwait_V && mode!=Random && (mode==Test || mode==Manual)); |
sanjeet25 | 1:c340c31174a5 | 196 | pc.printf("%u",time_count); |
sanjeet25 | 2:fc2f41386ee4 | 197 | if(r==0) { |
sanjeet25 | 0:a307f0abfd4d | 198 | VSignalsend(); |
sanjeet25 | 1:c340c31174a5 | 199 | pc.printf("Vsignal"); |
sanjeet25 | 0:a307f0abfd4d | 200 | resetTimer0(); |
sanjeet25 | 1:c340c31174a5 | 201 | } else if(r==1) { |
sanjeet25 | 0:a307f0abfd4d | 202 | ASignalsend(); |
sanjeet25 | 1:c340c31174a5 | 203 | pc.printf("ASignal"); |
sanjeet25 | 0:a307f0abfd4d | 204 | resetTimer0(); |
sanjeet25 | 0:a307f0abfd4d | 205 | } |
sanjeet25 | 0:a307f0abfd4d | 206 | } |
sanjeet25 | 0:a307f0abfd4d | 207 | } |
sanjeet25 | 0:a307f0abfd4d | 208 | void HeartKeyBoardModeSwitch(void const* args) |
sanjeet25 | 0:a307f0abfd4d | 209 | { |
sanjeet25 | 1:c340c31174a5 | 210 | while(1) { |
sanjeet25 | 1:c340c31174a5 | 211 | if(pc.readable()) { |
sanjeet25 | 1:c340c31174a5 | 212 | |
sanjeet25 | 1:c340c31174a5 | 213 | key_input = pc.getc(); |
sanjeet25 | 0:a307f0abfd4d | 214 | |
sanjeet25 | 1:c340c31174a5 | 215 | if(key_input=='r'||key_input=='R') { |
sanjeet25 | 1:c340c31174a5 | 216 | mode=Random; |
sanjeet25 | 1:c340c31174a5 | 217 | pc.printf("Random"); |
sanjeet25 | 1:c340c31174a5 | 218 | HeartSend("s"); |
sanjeet25 | 1:c340c31174a5 | 219 | } else if(key_input=='t'||key_input=='T') { |
sanjeet25 | 1:c340c31174a5 | 220 | mode=Test; |
sanjeet25 | 1:c340c31174a5 | 221 | pc.printf("Test"); |
sanjeet25 | 1:c340c31174a5 | 222 | } else if(key_input=='m'||key_input=='M') { |
sanjeet25 | 1:c340c31174a5 | 223 | mode=Manual; |
sanjeet25 | 1:c340c31174a5 | 224 | pc.printf("Manual"); |
sanjeet25 | 1:c340c31174a5 | 225 | } else if(mode==Manual&&(key_input=='v'||key_input=='V')) { |
sanjeet25 | 1:c340c31174a5 | 226 | mode=Manual; |
sanjeet25 | 1:c340c31174a5 | 227 | pc.printf("Ventricular"); |
sanjeet25 | 1:c340c31174a5 | 228 | } else if(mode==Manual&&(key_input=='a'||key_input=='A')) { |
sanjeet25 | 1:c340c31174a5 | 229 | mode=Manual; |
sanjeet25 | 1:c340c31174a5 | 230 | pc.printf("Atrial"); |
sanjeet25 | 2:fc2f41386ee4 | 231 | } else if(key_input=='o'||key_input=='O') { |
sanjeet25 | 2:fc2f41386ee4 | 232 | int a = 10 + rand() % 80; |
sanjeet25 | 2:fc2f41386ee4 | 233 | mode=Observer; |
sanjeet25 | 2:fc2f41386ee4 | 234 | pc.printf("Observer and %u",a); |
sanjeet25 | 2:fc2f41386ee4 | 235 | heartInterval =a; |
sanjeet25 | 2:fc2f41386ee4 | 236 | HeartSend("s"); |
sanjeet25 | 1:c340c31174a5 | 237 | } |
sanjeet25 | 0:a307f0abfd4d | 238 | } |
sanjeet25 | 0:a307f0abfd4d | 239 | } |
sanjeet25 | 0:a307f0abfd4d | 240 | } |
sanjeet25 | 2:fc2f41386ee4 | 241 | |
sanjeet25 | 2:fc2f41386ee4 | 242 | |
sanjeet25 | 2:fc2f41386ee4 | 243 | int main() |
sanjeet25 | 0:a307f0abfd4d | 244 | { |
sanjeet25 | 1:c340c31174a5 | 245 | pc.baud(9600); |
sanjeet25 | 2:fc2f41386ee4 | 246 | VPace.rise(&VPacereceived); |
sanjeet25 | 2:fc2f41386ee4 | 247 | APace.rise(&APacereceived); |
sanjeet25 | 0:a307f0abfd4d | 248 | timer0_init(); |
sanjeet25 | 0:a307f0abfd4d | 249 | timer1_init(); |
sanjeet25 | 2:fc2f41386ee4 | 250 | heartreceive=new Thread(HeartReceive); |
sanjeet25 | 2:fc2f41386ee4 | 251 | heartsend=new Thread(HeartSend); |
sanjeet25 | 2:fc2f41386ee4 | 252 | heartkeyboard=new Thread(HeartKeyBoardModeSwitch); |
sanjeet25 | 0:a307f0abfd4d | 253 | |
sanjeet25 | 0:a307f0abfd4d | 254 | while(1) { |
sanjeet25 | 0:a307f0abfd4d | 255 | } |
sanjeet25 | 0:a307f0abfd4d | 256 | } |