heart modes, send, receive

Dependencies:   Terminal TextLCD mbed-rtos mbed

Fork of Heartnew by CIS541

heart.cpp

Committer:
sanjeet25
Date:
2015-11-29
Revision:
1:c340c31174a5
Parent:
main.cpp@ 0:a307f0abfd4d
Child:
2:fc2f41386ee4

File content as of revision 1:c340c31174a5:

#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 ASignal1(LED2);
DigitalOut VSignal1(LED3);

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;

char key_input;

int LRI=1500;
int URI=600;

volatile bool Areceived=false;
volatile bool Vreceived=false;

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,
    AtrialMode,
    VentricularMode
};
Modes mode;

Mutex HeartMutex;

void resetTimer0();
void resetTimer1();
void VCheck();
void ACheck();
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;
    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);
    time_count=0;
}

void VCheck()
{
    Vreceived=true;
}

void ACheck()
{
    Areceived=true;
}

void ASignalsend()
{
    ASignal=1;
    ASignal1=1;
    VSignal1=0;
}

void VSignalsend()
{
    VSignal=1;
    VSignal1=1;
    ASignal1=0;
}
void HeartReceive(void const* args)
{
    while(1) {
        pc.printf("HR");
        while(Areceived==false||Vreceived==false);
        if(Vreceived==true) {
            resetTimer0();
            pc.printf("VPace aaya");
        } else if(Areceived==true) {
            resetTimer0();
            pc.printf("APace aaya");
        }
    }
}

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) { //(time_count<minwait_V && mode!=Random && mode==Test && mode==Manual && 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");
                key_input = pc.getc();
            } 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");


            }
        }
    }
}
int main()
{
    pc.baud(9600);
    VPace.rise(&VCheck);
    APace.rise(&ACheck);
    timer0_init();
    timer1_init();
    Thread heartreceive(HeartReceive);
    Thread heartsend(HeartSend);
    Thread heartkeyboard(HeartKeyBoardModeSwitch);

    while(1) {
    }
}