heart modes, send, receive
Dependencies: Terminal TextLCD mbed-rtos mbed
Fork of Pacemaker by
heart.cpp
- Committer:
- sanjeet25
- Date:
- 2015-11-30
- Revision:
- 2:fc2f41386ee4
- Parent:
- 1:c340c31174a5
- Child:
- 3:b702a21b2990
File content as of revision 2:fc2f41386ee4:
#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 time_count=0; volatile int time1_count; int m=0; int s=0; int ms=0; int heartInterval; char key_input; int LRI=1500; int URI=600; int heartRate; volatile bool Areceived=false; volatile bool Vreceived=false; int low[4] = {30,40,100,30}; int high[4] = {60,100,175,175}; int modeset; 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; typedef enum Modes { Test, Normal, Sleep, Random, Sports, Manual, Observer }; Modes mode; Mutex HeartMutex; Thread *heartreceive; Thread *heartsend; Thread *heartkeyboard; void resetTimer0(); void resetTimer1(); void VPacereceived(); void APacereceived(); void ASignalsend(); void VSignalsend(); void HeartReceive(); void HeartSend(); void HeartKeyBoardModeSwitch(); extern "C" void TIMER0_IRQHandler (void) { if((LPC_TIM0->IR & 0x01) == 0x01) { // if interrupt provided, continue to next line LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag time_count++; //increment time_count } } void timer0_init(void) { LPC_SC->PCONP |=1<1; //power on the timer LPC_TIM0->MR0 = 23980; //10 msec period i.e the timer count will increment every 10ms LPC_TIM0->MCR = 3; //reset control //3 = Interrupt & reset timer0 on match //1 = Interrupt only, no reset NVIC_EnableIRQ(TIMER0_IRQn); //enable interrupt LPC_TIM0->TCR = 1; //enable the timer } extern "C" void TIMER1_IRQHandler (void) { if((LPC_TIM0->IR & 0x01) == 0x01) { // if interrupt provided, continue to next line LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag time1_count++; //increment time_count } } void timer1_init(void) { LPC_SC->PCONP |=1<1; //power on the timer LPC_TIM0->MR0 = 23980; //10 msec period i.e the timer count will increment every 10ms LPC_TIM0->MCR = 3; //reset control //3 = Interrupt & reset timer0 on match //1 = Interrupt only, no reset NVIC_EnableIRQ(TIMER0_IRQn); //enable interrupt LPC_TIM0->TCR = 1; //enable the timer } void resetTimer0() { m=0; s=0; ms=0; time_count=0; LPC_TIM0->TCR = 0; lcd.locate(0,0); lcd.printf("%02d:%02d:%02d", m,s,ms); } void resetTimer1() { m=0; s=0; ms=0; LPC_TIM1->TCR = 0; lcd.locate(0,0); time1_count=0; } void VPacereceived() { pc.printf("VPace aaya"); Vreceived=true; VPaceReceive=1; APaceReceive=0; } void APacereceived() { pc.printf("APace aaya"); Areceived=true; VPaceReceive=0; APaceReceive=1; } void ASignalsend() { ASignal=1; ASignal1=1; VSignal1=0; } void VSignalsend() { VSignal=1; VSignal1=1; ASignal1=0; } void updateHeart() { int temp = 60/heartInterval; heartRate = heartRate + temp; } void HeartReceive(void const* args) { while(1) { pc.printf("HR"); while(Areceived==false||Vreceived==false); if(Vreceived==true) { resetTimer0(); } else if(Areceived==true) { resetTimer0(); } } } void HeartSend(void const* args) { while(!pc.readable()) { int r = rand() % 2; pc.printf("HS"); while(time_count<minwait_V && mode!=Random && (mode==Test || mode==Manual)); pc.printf("%u",time_count); if(r==0) { VSignalsend(); pc.printf("Vsignal"); resetTimer0(); } else if(r==1) { ASignalsend(); pc.printf("ASignal"); resetTimer0(); } } } void HeartKeyBoardModeSwitch(void const* args) { while(1) { if(pc.readable()) { key_input = pc.getc(); if(key_input=='r'||key_input=='R') { mode=Random; pc.printf("Random"); HeartSend("s"); } else if(key_input=='t'||key_input=='T') { mode=Test; pc.printf("Test"); } else if(key_input=='m'||key_input=='M') { mode=Manual; pc.printf("Manual"); } else if(mode==Manual&&(key_input=='v'||key_input=='V')) { mode=Manual; pc.printf("Ventricular"); } else if(mode==Manual&&(key_input=='a'||key_input=='A')) { mode=Manual; pc.printf("Atrial"); } else if(key_input=='o'||key_input=='O') { int a = 10 + rand() % 80; mode=Observer; pc.printf("Observer and %u",a); heartInterval =a; HeartSend("s"); } } } } int main() { pc.baud(9600); VPace.rise(&VPacereceived); APace.rise(&APacereceived); timer0_init(); timer1_init(); heartreceive=new Thread(HeartReceive); heartsend=new Thread(HeartSend); heartkeyboard=new Thread(HeartKeyBoardModeSwitch); while(1) { } }