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