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