heart modes, send, receive

Dependencies:   Terminal TextLCD mbed-rtos mbed

Fork of Heartnew by CIS541

Committer:
sanjeet25
Date:
Tue Dec 01 22:57:12 2015 +0000
Revision:
3:b702a21b2990
Parent:
2:fc2f41386ee4
Child:
4:a64a0fea5266
finalheartwithouttest

Who changed what in which revision?

UserRevisionLine numberNew 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 3:b702a21b2990 23 volatile int t=0;
sanjeet25 3:b702a21b2990 24 volatile int k=0;
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 3:b702a21b2990 30 int testNumber;
sanjeet25 3:b702a21b2990 31 bool testResult;
sanjeet25 0:a307f0abfd4d 32
sanjeet25 0:a307f0abfd4d 33 int LRI=1500;
sanjeet25 0:a307f0abfd4d 34 int URI=600;
sanjeet25 2:fc2f41386ee4 35 int heartRate;
sanjeet25 0:a307f0abfd4d 36 volatile bool Areceived=false;
sanjeet25 0:a307f0abfd4d 37 volatile bool Vreceived=false;
sanjeet25 3:b702a21b2990 38 bool test=false;
sanjeet25 2:fc2f41386ee4 39 int low[4] = {30,40,100,30};
sanjeet25 2:fc2f41386ee4 40 int high[4] = {60,100,175,175};
sanjeet25 2:fc2f41386ee4 41 int modeset;
sanjeet25 3:b702a21b2990 42 int pulses;
sanjeet25 0:a307f0abfd4d 43
sanjeet25 0:a307f0abfd4d 44 const int sleepModeURI = 1000;
sanjeet25 0:a307f0abfd4d 45 const int sleepModeLRI = 2000;
sanjeet25 0:a307f0abfd4d 46
sanjeet25 0:a307f0abfd4d 47 const int normalModeURI = 600;
sanjeet25 0:a307f0abfd4d 48 const int normalModeLRI = 1500;
sanjeet25 0:a307f0abfd4d 49
sanjeet25 0:a307f0abfd4d 50 const int sportsModeURI = 343;
sanjeet25 0:a307f0abfd4d 51 const int sportsModeLRI = 600;
sanjeet25 0:a307f0abfd4d 52
sanjeet25 0:a307f0abfd4d 53 const int manualModeURI = 343;
sanjeet25 0:a307f0abfd4d 54 const int manualModeLRI = 2000;
sanjeet25 0:a307f0abfd4d 55
sanjeet25 0:a307f0abfd4d 56 typedef enum Modes {
sanjeet25 0:a307f0abfd4d 57 Test,
sanjeet25 0:a307f0abfd4d 58 Normal,
sanjeet25 0:a307f0abfd4d 59 Sleep,
sanjeet25 0:a307f0abfd4d 60 Random,
sanjeet25 0:a307f0abfd4d 61 Sports,
sanjeet25 0:a307f0abfd4d 62 Manual,
sanjeet25 2:fc2f41386ee4 63 Observer
sanjeet25 0:a307f0abfd4d 64 };
sanjeet25 0:a307f0abfd4d 65 Modes mode;
sanjeet25 0:a307f0abfd4d 66
sanjeet25 0:a307f0abfd4d 67 Mutex HeartMutex;
sanjeet25 0:a307f0abfd4d 68
sanjeet25 2:fc2f41386ee4 69 Thread *heartreceive;
sanjeet25 2:fc2f41386ee4 70 Thread *heartsend;
sanjeet25 2:fc2f41386ee4 71 Thread *heartkeyboard;
sanjeet25 2:fc2f41386ee4 72
sanjeet25 2:fc2f41386ee4 73
sanjeet25 3:b702a21b2990 74 void resett();
sanjeet25 3:b702a21b2990 75 void resetk();
sanjeet25 2:fc2f41386ee4 76 void VPacereceived();
sanjeet25 2:fc2f41386ee4 77 void APacereceived();
sanjeet25 0:a307f0abfd4d 78 void ASignalsend();
sanjeet25 0:a307f0abfd4d 79 void VSignalsend();
sanjeet25 0:a307f0abfd4d 80 void HeartReceive();
sanjeet25 0:a307f0abfd4d 81 void HeartSend();
sanjeet25 1:c340c31174a5 82 void HeartKeyBoardModeSwitch();
sanjeet25 3:b702a21b2990 83 void sound_lowAlarm();
sanjeet25 3:b702a21b2990 84 void sound_highAlarm();
sanjeet25 0:a307f0abfd4d 85
sanjeet25 0:a307f0abfd4d 86
sanjeet25 0:a307f0abfd4d 87 extern "C" void TIMER0_IRQHandler (void)
sanjeet25 0:a307f0abfd4d 88 {
sanjeet25 3:b702a21b2990 89 if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed
sanjeet25 0:a307f0abfd4d 90 LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag
sanjeet25 3:b702a21b2990 91 t++;
sanjeet25 3:b702a21b2990 92 k++;
sanjeet25 0:a307f0abfd4d 93 }
sanjeet25 0:a307f0abfd4d 94 }
sanjeet25 0:a307f0abfd4d 95
sanjeet25 3:b702a21b2990 96 //Initializing the timer 0
sanjeet25 3:b702a21b2990 97 void timer0_init(void)
sanjeet25 0:a307f0abfd4d 98 {
sanjeet25 0:a307f0abfd4d 99
sanjeet25 3:b702a21b2990 100 LPC_SC->PCONP |=1<1; //Timer0 Power On
sanjeet25 3:b702a21b2990 101 LPC_TIM0->MR0 = 23990; //1 msec
sanjeet25 3:b702a21b2990 102 LPC_TIM0->MCR = 3; //interrupt and reset control
sanjeet25 3:b702a21b2990 103 NVIC_EnableIRQ(TIMER1_IRQn); //Enable timer0 interrupt
sanjeet25 3:b702a21b2990 104 LPC_TIM0->TCR = 1; //enable Timer0
sanjeet25 3:b702a21b2990 105 t = 0;
sanjeet25 3:b702a21b2990 106 k = 0;
sanjeet25 3:b702a21b2990 107 }
sanjeet25 3:b702a21b2990 108
sanjeet25 3:b702a21b2990 109 void resett()
sanjeet25 3:b702a21b2990 110 {
sanjeet25 3:b702a21b2990 111 t=0;
sanjeet25 3:b702a21b2990 112 }
sanjeet25 3:b702a21b2990 113
sanjeet25 3:b702a21b2990 114 void resetk()
sanjeet25 3:b702a21b2990 115 {
sanjeet25 3:b702a21b2990 116 k=0;
sanjeet25 0:a307f0abfd4d 117 }
sanjeet25 0:a307f0abfd4d 118
sanjeet25 3:b702a21b2990 119 void sound_lowAlarm(){
sanjeet25 3:b702a21b2990 120 pc.printf("lowalarm");}
sanjeet25 3:b702a21b2990 121 void sound_highAlarm(){
sanjeet25 3:b702a21b2990 122 pc.printf("highalarm");}
sanjeet25 3:b702a21b2990 123 void VPacereceived()
sanjeet25 0:a307f0abfd4d 124 {
sanjeet25 3:b702a21b2990 125 pc.printf("VPace received");
sanjeet25 3:b702a21b2990 126 VPaceReceive=1;
sanjeet25 3:b702a21b2990 127 wait(0.002);
sanjeet25 3:b702a21b2990 128 APaceReceive=0;
sanjeet25 3:b702a21b2990 129 pulses++;
sanjeet25 0:a307f0abfd4d 130 Vreceived=true;
sanjeet25 3:b702a21b2990 131 Areceived=false;
sanjeet25 3:b702a21b2990 132 if(mode == Test) {
sanjeet25 3:b702a21b2990 133 //(*HeartTxPtr).signal_set(0x04);
sanjeet25 3:b702a21b2990 134 }
sanjeet25 3:b702a21b2990 135 if(mode == Test && test) {
sanjeet25 3:b702a21b2990 136 if(testNumber == 2 || testNumber == 0 || testNumber == 3 || testNumber == 1) {
sanjeet25 3:b702a21b2990 137 testResult = false;
sanjeet25 3:b702a21b2990 138 } else if(testNumber == 6 || testNumber == 4) {
sanjeet25 3:b702a21b2990 139 testResult = true;
sanjeet25 3:b702a21b2990 140 }
sanjeet25 3:b702a21b2990 141 }
sanjeet25 0:a307f0abfd4d 142 }
sanjeet25 0:a307f0abfd4d 143
sanjeet25 2:fc2f41386ee4 144 void APacereceived()
sanjeet25 3:b702a21b2990 145 {
sanjeet25 3:b702a21b2990 146 pc.printf("APace received");
sanjeet25 3:b702a21b2990 147 APaceReceive=1;
sanjeet25 3:b702a21b2990 148 wait(0.002);
sanjeet25 3:b702a21b2990 149 APaceReceive=0;
sanjeet25 0:a307f0abfd4d 150 Areceived=true;
sanjeet25 3:b702a21b2990 151 if(mode == Test && test) {
sanjeet25 3:b702a21b2990 152 if(testNumber == 0) {
sanjeet25 3:b702a21b2990 153 testResult = false;
sanjeet25 3:b702a21b2990 154 } else if(testNumber == 4 || testNumber == 7 || testNumber == 3 || testNumber == 1) {
sanjeet25 3:b702a21b2990 155 testResult = true;
sanjeet25 3:b702a21b2990 156 }
sanjeet25 3:b702a21b2990 157 }
sanjeet25 0:a307f0abfd4d 158 }
sanjeet25 0:a307f0abfd4d 159
sanjeet25 0:a307f0abfd4d 160 void ASignalsend()
sanjeet25 0:a307f0abfd4d 161 {
sanjeet25 0:a307f0abfd4d 162 ASignal=1;
sanjeet25 1:c340c31174a5 163 ASignal1=1;
sanjeet25 3:b702a21b2990 164 wait(0.02);
sanjeet25 3:b702a21b2990 165 Thread::wait(1);
sanjeet25 3:b702a21b2990 166 ASignal1=0;
sanjeet25 3:b702a21b2990 167 ASignal=0;
sanjeet25 0:a307f0abfd4d 168 }
sanjeet25 0:a307f0abfd4d 169
sanjeet25 0:a307f0abfd4d 170 void VSignalsend()
sanjeet25 0:a307f0abfd4d 171 {
sanjeet25 0:a307f0abfd4d 172 VSignal=1;
sanjeet25 1:c340c31174a5 173 VSignal1=1;
sanjeet25 3:b702a21b2990 174 wait(0.02);
sanjeet25 3:b702a21b2990 175 Thread::wait(1);
sanjeet25 3:b702a21b2990 176 VSignal1=0;
sanjeet25 3:b702a21b2990 177 VSignal==0;
sanjeet25 0:a307f0abfd4d 178 }
sanjeet25 2:fc2f41386ee4 179
sanjeet25 2:fc2f41386ee4 180 void updateHeart()
sanjeet25 2:fc2f41386ee4 181 {
sanjeet25 2:fc2f41386ee4 182 int temp = 60/heartInterval;
sanjeet25 2:fc2f41386ee4 183 heartRate = heartRate + temp;
sanjeet25 2:fc2f41386ee4 184 }
sanjeet25 0:a307f0abfd4d 185 void HeartReceive(void const* args)
sanjeet25 0:a307f0abfd4d 186 {
sanjeet25 0:a307f0abfd4d 187 while(1) {
sanjeet25 0:a307f0abfd4d 188 pc.printf("HR");
sanjeet25 1:c340c31174a5 189 while(Areceived==false||Vreceived==false);
sanjeet25 0:a307f0abfd4d 190 if(Vreceived==true) {
sanjeet25 3:b702a21b2990 191 resett();
sanjeet25 3:b702a21b2990 192
sanjeet25 0:a307f0abfd4d 193 } else if(Areceived==true) {
sanjeet25 3:b702a21b2990 194 resett();
sanjeet25 0:a307f0abfd4d 195 }
sanjeet25 0:a307f0abfd4d 196 }
sanjeet25 0:a307f0abfd4d 197 }
sanjeet25 0:a307f0abfd4d 198
sanjeet25 0:a307f0abfd4d 199 void HeartSend(void const* args)
sanjeet25 0:a307f0abfd4d 200 {
sanjeet25 1:c340c31174a5 201 while(!pc.readable()) {
sanjeet25 1:c340c31174a5 202 int r = rand() % 2;
sanjeet25 0:a307f0abfd4d 203 pc.printf("HS");
sanjeet25 3:b702a21b2990 204 while(t<minwait_V && mode!=Random && (mode==Test || mode==Manual));
sanjeet25 3:b702a21b2990 205 pc.printf("%u",t);
sanjeet25 3:b702a21b2990 206 if(r==0) {
sanjeet25 0:a307f0abfd4d 207 VSignalsend();
sanjeet25 1:c340c31174a5 208 pc.printf("Vsignal");
sanjeet25 3:b702a21b2990 209 resett();
sanjeet25 1:c340c31174a5 210 } else if(r==1) {
sanjeet25 0:a307f0abfd4d 211 ASignalsend();
sanjeet25 1:c340c31174a5 212 pc.printf("ASignal");
sanjeet25 3:b702a21b2990 213 resett();
sanjeet25 0:a307f0abfd4d 214 }
sanjeet25 0:a307f0abfd4d 215 }
sanjeet25 0:a307f0abfd4d 216 }
sanjeet25 0:a307f0abfd4d 217 void HeartKeyBoardModeSwitch(void const* args)
sanjeet25 0:a307f0abfd4d 218 {
sanjeet25 1:c340c31174a5 219 while(1) {
sanjeet25 1:c340c31174a5 220 if(pc.readable()) {
sanjeet25 1:c340c31174a5 221
sanjeet25 1:c340c31174a5 222 key_input = pc.getc();
sanjeet25 0:a307f0abfd4d 223
sanjeet25 1:c340c31174a5 224 if(key_input=='r'||key_input=='R') {
sanjeet25 1:c340c31174a5 225 mode=Random;
sanjeet25 1:c340c31174a5 226 pc.printf("Random");
sanjeet25 1:c340c31174a5 227 HeartSend("s");
sanjeet25 1:c340c31174a5 228 } else if(key_input=='t'||key_input=='T') {
sanjeet25 1:c340c31174a5 229 mode=Test;
sanjeet25 1:c340c31174a5 230 pc.printf("Test");
sanjeet25 1:c340c31174a5 231 } else if(key_input=='m'||key_input=='M') {
sanjeet25 1:c340c31174a5 232 mode=Manual;
sanjeet25 1:c340c31174a5 233 pc.printf("Manual");
sanjeet25 1:c340c31174a5 234 } else if(mode==Manual&&(key_input=='v'||key_input=='V')) {
sanjeet25 1:c340c31174a5 235 mode=Manual;
sanjeet25 3:b702a21b2990 236 VSignalsend();
sanjeet25 1:c340c31174a5 237 pc.printf("Ventricular");
sanjeet25 1:c340c31174a5 238 } else if(mode==Manual&&(key_input=='a'||key_input=='A')) {
sanjeet25 1:c340c31174a5 239 mode=Manual;
sanjeet25 3:b702a21b2990 240 ASignalsend();
sanjeet25 1:c340c31174a5 241 pc.printf("Atrial");
sanjeet25 2:fc2f41386ee4 242 } else if(key_input=='o'||key_input=='O') {
sanjeet25 2:fc2f41386ee4 243 int a = 10 + rand() % 80;
sanjeet25 2:fc2f41386ee4 244 mode=Observer;
sanjeet25 2:fc2f41386ee4 245 pc.printf("Observer and %u",a);
sanjeet25 2:fc2f41386ee4 246 heartInterval =a;
sanjeet25 2:fc2f41386ee4 247 HeartSend("s");
sanjeet25 3:b702a21b2990 248 } else {
sanjeet25 3:b702a21b2990 249 pc.printf("Wrong key, sorry");
sanjeet25 1:c340c31174a5 250 }
sanjeet25 0:a307f0abfd4d 251 }
sanjeet25 0:a307f0abfd4d 252 }
sanjeet25 0:a307f0abfd4d 253 }
sanjeet25 2:fc2f41386ee4 254
sanjeet25 3:b702a21b2990 255 void display(void const* args)
sanjeet25 3:b702a21b2990 256 {
sanjeet25 3:b702a21b2990 257 while(1) {
sanjeet25 3:b702a21b2990 258 Thread::wait(observationInterval/1000);
sanjeet25 3:b702a21b2990 259 // if(mode!=Test) {
sanjeet25 3:b702a21b2990 260 // if(mode!= changeobservationinterval) {
sanjeet25 3:b702a21b2990 261 // avgHeartRate=pulseCount*60000/(obsInterval);
sanjeet25 3:b702a21b2990 262 // pulseCount = 0;
sanjeet25 3:b702a21b2990 263 // updateDisplay();
sanjeet25 3:b702a21b2990 264 //
sanjeet25 3:b702a21b2990 265 // }
sanjeet25 3:b702a21b2990 266 // }
sanjeet25 3:b702a21b2990 267 if(!Test) {
sanjeet25 3:b702a21b2990 268 pc.printf("Avg Heart Rate: %02d bpm", heartRate);
sanjeet25 3:b702a21b2990 269 while(!Vreceived||!VSignal==0) {
sanjeet25 3:b702a21b2990 270 updateHeart();
sanjeet25 3:b702a21b2990 271 }
sanjeet25 3:b702a21b2990 272 while(k<=heartInterval*1000);
sanjeet25 3:b702a21b2990 273 if(heartRate>high[mode]) {
sanjeet25 3:b702a21b2990 274 sound_highAlarm();
sanjeet25 3:b702a21b2990 275 heartRate=0;
sanjeet25 3:b702a21b2990 276 resetk();
sanjeet25 3:b702a21b2990 277 } else if(heartRate<low[mode]) {
sanjeet25 3:b702a21b2990 278 sound_lowAlarm();
sanjeet25 3:b702a21b2990 279 heartRate=0;
sanjeet25 3:b702a21b2990 280 resetk();
sanjeet25 3:b702a21b2990 281 } else if(heartRate >= low[mode] && heartRate <= high[mode]) {
sanjeet25 3:b702a21b2990 282 heartRate=0;
sanjeet25 3:b702a21b2990 283 resetk();
sanjeet25 3:b702a21b2990 284 }
sanjeet25 3:b702a21b2990 285
sanjeet25 3:b702a21b2990 286
sanjeet25 3:b702a21b2990 287 }
sanjeet25 3:b702a21b2990 288 }
sanjeet25 3:b702a21b2990 289 }
sanjeet25 3:b702a21b2990 290
sanjeet25 2:fc2f41386ee4 291
sanjeet25 3:b702a21b2990 292 void testmode(void const* args){
sanjeet25 3:b702a21b2990 293 }
sanjeet25 3:b702a21b2990 294
sanjeet25 3:b702a21b2990 295
sanjeet25 3:b702a21b2990 296 int main()
sanjeet25 0:a307f0abfd4d 297 {
sanjeet25 1:c340c31174a5 298 pc.baud(9600);
sanjeet25 2:fc2f41386ee4 299 VPace.rise(&VPacereceived);
sanjeet25 2:fc2f41386ee4 300 APace.rise(&APacereceived);
sanjeet25 0:a307f0abfd4d 301 timer0_init();
sanjeet25 3:b702a21b2990 302 Thread heartreceive(HeartReceive);
sanjeet25 3:b702a21b2990 303 Thread heartsend(HeartSend);
sanjeet25 3:b702a21b2990 304 Thread heartkeyboard(HeartKeyBoardModeSwitch);
sanjeet25 3:b702a21b2990 305 Thread disPlay(display);
sanjeet25 0:a307f0abfd4d 306
sanjeet25 0:a307f0abfd4d 307 while(1) {
sanjeet25 0:a307f0abfd4d 308 }
sanjeet25 0:a307f0abfd4d 309 }