heart modes, send, receive

Dependencies:   Terminal TextLCD mbed-rtos mbed

Fork of Heart by CIS541

main.cpp

Committer:
sanjeet25
Date:
2015-11-27
Revision:
0:a307f0abfd4d

File content as of revision 0:a307f0abfd4d:

#include "mbed.h"
#include "TextLCD.h"
#include "rtos.h"

TextLCD lcd(p15, p16, p17, p18, p19, p20);
Serial pc(USBTX, USBRX);

InterruptIn APace(LED1);
InterruptIn VPace(LED2);

DigitalOut ASignal(LED3);
DigitalOut VSignal(LED4);

const int minwait_A=50;
const int minwait_V=50;

volatile int time_count=0;
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();


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 = 239800;        //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
        time_count++;                   //increment time_count
    }
}

void timer1_init(void)
{
    LPC_SC->PCONP |=1<1;            //power on the timer
    LPC_TIM0->MR0 = 239800;        //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()
{
    LPC_TIM0->TCR = 0;
    lcd.locate(0,0);
    lcd.printf("%02d:%02d:%02d", m,s,ms);
}

void resetTimer1()
{
    LPC_TIM1->TCR = 0;
    lcd.locate(0,0);
    lcd.printf("%02d:%02d:%02d", m,s,ms);
}

void VCheck()
{
    Vreceived=true;
}

void ACheck()
{
    Areceived=true;
}

void ASignalsend()
{
    ASignal=1;
}

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

void HeartSend(void const* args)
{
    while(1) {
        pc.printf("HS");
        if(time_count>=minwait_V && mode==Random && mode!=Test && mode!=Manual) {
            VSignalsend();
            pc.printf("Vsignal bheja");
            resetTimer0();
        } else if(time_count>=minwait_A && mode==Random && mode!=Test && mode!=Manual) {
            ASignalsend();
            pc.printf("ASignal bheja");
            resetTimer0();

        }

    }
}

void HeartKeyBoardModeSwitch(void const* args)
{
    key_input = pc.getc();

    if(key_input=='r'||key_input=='R') {
        HeartMutex.lock();
        mode=Random;
        HeartMutex.unlock();
    } else if(key_input=='t'||key_input=='T') {
        HeartMutex.lock();
        mode=Test;
        HeartMutex.unlock();
    } else if(key_input=='m'||key_input=='M') {
        HeartMutex.lock();
        mode=Manual;
        HeartMutex.unlock();
        if(key_input=='v'||key_input=='V') {
            HeartMutex.lock();
            mode=VentricularMode;
            HeartMutex.unlock();
        } else if(key_input=='a'||key_input=='A') {
            HeartMutex.lock();
            mode=AtrialMode;
            HeartMutex.unlock();
        }
    }

}

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

    while(1) {
    }
}