heart modes, send, receive
Dependencies: Terminal TextLCD mbed-rtos mbed
Fork of Heart by
heart.cpp
- Committer:
- sanjeet25
- Date:
- 2015-12-02
- Revision:
- 6:cc4fcc38b9f0
- Parent:
- 5:8e3b9032ddfc
- Child:
- 7:46aeded22784
File content as of revision 6:cc4fcc38b9f0:
#include "mbed.h" #include "TextLCD.h" #include "rtos.h" TextLCD lcd(p15, p16, p17, p18, p19, p20); Serial pc(USBTX, USBRX); InterruptIn APace(p23); InterruptIn VPace(p24); DigitalOut ASignal(p25); DigitalOut VSignal(p26); DigitalOut APaceReceive(LED1); DigitalOut VPaceReceive(LED2); DigitalOut ASignal1(LED3); DigitalOut VSignal1(LED4); const int minwait_A=50; const int minwait_V=50; volatile int t=0; volatile int k=0; int m=0; int s=0; int ms=0; int heartInterval; char key_input; int testNumber; bool test_result; const int prop_delay=15; int flag_key=0; Mutex heartmut, keymutex; int LRI=1500; int URI=600; int heartRate; volatile bool Areceived=false; volatile bool Vreceived=false; bool test=false; int low[4] = {30,40,100,30}; int high[4] = {60,100,175,175}; int modeset; int pulses; //int test; int flag; int delta=15; int observationInterval=10000; const int vrp=100; const int pvarp=150; const int avi=65; const int pvab=10; const int sleepModeURI = 1000; const int sleepModeLRI = 2000; const int normalModeURI = 600; const int normalModeLRI = 1500; const int sportsModeURI = 343; const int sportsModeLRI = 600; const int manualModeURI = 343; const int manualModeLRI = 2000; int lri=normalModeLRI; int uri=normalModeURI; int flagr=0; typedef enum Modes { Test, Normal, Sleep, Random, Sports, Manual, Observer }; Modes mode; Mutex HeartMutex; Thread *heartreceive; Thread *heartsend; Thread *heartkeyboard; Thread *testmode; Thread *hmode; void resett(); void resetk(); void VPacereceived(); void APacereceived(); void ASignalsend(); void VSignalsend(); void HeartReceive(); void HeartSend(); void HeartKeyBoardModeSwitch(); void sound_lowAlarm(); void sound_highAlarm(); void Testmode(); extern "C" void TIMER0_IRQHandler (void) { if((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt, proceed LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag t++; k++; } } //Initializing the timer 0 void timer0_init(void) { LPC_SC->PCONP |=1<1; //Timer0 Power On LPC_TIM0->MR0 = 23990; //1 msec LPC_TIM0->MCR = 3; //interrupt and reset control NVIC_EnableIRQ(TIMER1_IRQn); //Enable timer0 interrupt LPC_TIM0->TCR = 1; //enable Timer0 t = 0; k = 0; } void resett() { t=0; } void resetk() { k=0; } void sound_lowAlarm() { pc.printf("lowalarm"); } void sound_highAlarm() { pc.printf("highalarm"); } void VPacereceived() { pc.printf("VPace received"); VPaceReceive=1; wait(0.002); APaceReceive=0; //pulses++; Vreceived=true; Areceived=false; if(mode == Test && flag==0) { (*testmode).signal_set(0x04); } if(mode == Test && test) { if(testNumber == 2 || testNumber == 0 || testNumber == 3 || testNumber == 1) { test_result = false; } else if(testNumber == 6 || testNumber == 4) { test_result = true; } } } void APacereceived() { pc.printf("APace received"); APaceReceive=1; wait(0.002); APaceReceive=0; Areceived=true; if(mode == Test && test) { if(testNumber == 0) { test_result = false; } else if(testNumber == 4 || testNumber == 7 || testNumber == 3 || testNumber == 1) { test_result = true; } } } void ASignalsend() { ASignal=1; ASignal1=1; wait(0.02); Thread::wait(1); ASignal1=0; ASignal=0; } void VSignalsend() { VSignal=1; VSignal1=1; wait(0.02); Thread::wait(1); VSignal1=0; VSignal==0; } void updateHeart() { int temp = 60/heartInterval; heartmut.lock(); heartRate = heartRate + temp; heartmut.lock(); } void HeartReceive(void const* args) { while(1) { pc.printf("HR"); while(Areceived==false||Vreceived==false); if(Vreceived==true) { resett(); } else if(Areceived==true) { resett(); } } } void HeartSend(void const* args) { while(1) { if(flagr==1){ int r = rand() % 2; pc.printf("HS"); while(t<minwait_V && mode!=Random && (mode==Test || mode==Manual)); pc.printf("%u",t); if(r==0 && mode!=Test) { VSignalsend(); pc.printf("Vsignal"); resett(); } else if(r==1&&mode!=Test) { ASignalsend(); pc.printf("ASignal"); resett(); }} } //keymutex.unlock(); // } } void HeartKeyBoardModeSwitch(void const* args) { while(1) { keymutex.lock(); if((key_input=='r'||key_input=='R')&&flag_key==1) { mode=Random; pc.printf("Random"); flagr=1; HeartSend("s"); flag_key=0; } else if((key_input=='m'||key_input=='M')&&flag_key) { mode=Manual; pc.printf("Manual"); flag_key=0; } else if(mode==Manual&&(key_input=='v'||key_input=='V')&&flag_key==1) { mode=Manual; VSignalsend(); pc.printf("Ventricular"); flag_key=0; } else if(mode==Manual&&flag_key==1&&(key_input=='a'||key_input=='A')) { mode=Manual; ASignalsend(); pc.printf("Atrial"); flag_key=0; } else if((key_input=='o'||key_input=='O')&&flag_key==1) { int a = 10 + rand() % 80; mode=Observer; pc.printf("Observer and %u",a); heartInterval =a; HeartSend("s"); flag_key=0; } else if(flag_key==1&&(key_input=='t'||key_input=='T')) { mode=Test; (*testmode).signal_set(0x03); pc.printf("Test"); flag_key=0; } keymutex.unlock(); } } void display(void const* args) { while(1) { Thread::wait(observationInterval/1000); // if(mode!=Test) { // if(mode!= changeobservationinterval) { // avgHeartRate=pulseCount*60000/(obsInterval); // pulseCount = 0; // updateDisplay(); // // } // } if(!Test) { heartmut.lock(); pc.printf("Avg Heart Rate: %02d bpm", heartRate); heartmut.unlock(); while(!Vreceived||!VSignal==0) { updateHeart(); } while(k<=heartInterval*1000); if(heartRate>high[mode]) { sound_highAlarm(); heartmut.lock(); heartRate=0; heartmut.unlock(); resetk(); } else if(heartRate<low[mode]) { sound_lowAlarm(); heartmut.lock(); heartRate=0; heartmut.unlock(); resetk(); } else if(heartRate >= low[mode] && heartRate <= high[mode]) { heartmut.lock(); heartRate=0; heartmut.unlock(); resetk(); } } } } void Testmode(void const* args) { Thread::wait(0x03); test = true; switch(test) { case 1: flag=0; Thread::signal_wait(0x04); flag=1; t = 0; Thread::wait(vrp); ASignalsend(); test = true; Thread::wait(lri-avi-vrp); test_result=false; if(test_result) { pc.printf("Test %u passed",test); test=2; } else { pc.printf("Test %u failed",test); test=2; } break; case 2: flag=0; Thread::signal_wait(0x04); //Send A after PVARP flag=1; t = 0; Thread::wait(pvarp); ASignalsend(); Thread::wait(uri-pvarp-delta); if(test_result) { pc.printf("Test %u passed", &test_result); test=3; } else { pc.printf("Test %u failed", &test_result); test=3; } break; case 3: flag=0; Thread::signal_wait(0x04); flag=1; t = 0; Thread::wait(uri); for(int i = 0; i<7; i++) { ASignalsend(); Thread::wait(avi-delta); VSignalsend(); Thread::wait(lri-avi-delta); } if(test_result) { pc.printf("Test %u passed", &test_result); test=4; } else { pc.printf("Test %u failed", &test_result); test=4; } break; case 4: flag=0; Thread::signal_wait(0x04); //Send A after PVARP flag=1; t = 0; Thread::wait(pvarp); ASignalsend(); Thread::wait(uri-pvarp-delta); if(test_result) { pc.printf("Test %u passed", &test_result); test=5; } else { pc.printf("Test %u failed", &test_result); test=5; } break; case 5: flag=0; Thread::signal_wait(0x04); //Heart is dead flag=1; t = 0; Thread::wait(lri); Thread::wait(lri); test = false; if(test_result) { pc.printf("Test %u passed", &test_result); test=6; } else { pc.printf("Test %u failed", &test_result); test=6; } break; case 6: flag=0; Thread::signal_wait(0x04); //Heart beating very fast. ASig, VSig flag=1; t = 0; for(int i = 0; i<50; i++) { ASignalsend(); VSignalsend(); } if(test_result) { pc.printf("Test %u passed", &test_result); test=7; } else { pc.printf("Test %u failed", &test_result); test=7; } break; case 7: flag=0; Thread::signal_wait(0x04); //Wait for VPace to be received flag=1; t= 0; Thread::wait(uri); ASignalsend(); Thread::wait(avi+delta); if(test_result) { pc.printf("Test %u passed", &test_result); test=8; } else { pc.printf("Test %u failed", &test_result); test=8; } break; case 8: flag=0; Thread::signal_wait(0x04); //Send an extra V to see if LRI starts then flag=1; t = 0; Thread::wait(pvarp); VSignalsend(); Thread::wait(lri-avi-delta); if(test_result) { pc.printf("Test %u passed", &test_result); test=0; } else { pc.printf("Test %u failed", &test_result); test=0; } break; } } int main() { pc.baud(9600); VPace.rise(&VPacereceived); APace.rise(&APacereceived); timer0_init(); Thread heartreceive(HeartReceive); Thread heartsend(HeartSend); Thread heartkeyboard(HeartKeyBoardModeSwitch); hmode = &heartkeyboard; Thread disPlay(display); Thread tmode(Testmode); testmode = &tmode; while(1) { if(pc.readable()) { key_input=pc.getc(); //read uart buffer data and clear the interrupt flag //initiate the serial thread to change the state of the timer pc.printf("setting"); flag_key=1; if(key_input=='o'||key_input=='O'||key_input=='m'||key_input=='M'||key_input=='t'||key_input=='T') flagr=0; pc.printf("set"); } } }