the complete model of the heart model

Dependencies:   TextLCD mbed-rtos mbed

Fork of heart by William Archer

main.cpp

Committer:
shengtiz
Date:
2016-11-30
Revision:
2:b4551e56cd1c
Parent:
1:6ad7b5bf9c27
Child:
3:5941bb3c4fc2

File content as of revision 2:b4551e56cd1c:

#include "mbed.h"
#include "rtos.h"
#include "TextLCD.h"
#include <time.h>
#include <stdlib.h>

//use screen /dev/tty.. on mac in terminal to receive input

TextLCD lcd(p15,p16,p17,p18,p19,p20,TextLCD::LCD16x2);
Serial pc(USBTX, USBRX); //set up serial
DigitalOut natAPace(LED1); //leds for pacing
DigitalOut natVPace(LED2);
DigitalOut aPace(LED3);
DigitalOut vPace(LED4);
DigitalOut alarm(p5); //pin for alarm

DigitalOut a_signal(p6); //connected to pm
DigitalOut v_signal(p7); //connected to pm

unsigned int a_time = 0;
unsigned int v_time = 0;

int mode = 0; //0 = random, 1 = manual, 2 = test
int modeInManul = -1; //0 = manualmode apace, 1 = manualmode vpace
bool modeSwitch = false;
//constants

const int minwait_A = 100;
const int minwait_V = 200;

/*
const int LRI = 1000;
const int VRP = 400;
const int PVARP = 500;
const int URI = 1000;
const int AVI = 100;
*/

void switch_modes() {
    switch(mode) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            break;
        
    }    
}


void received_apace() {
    //TODO: DO 
    aPace = 1;
       
}

void received_vpace() {
    //TODO: DO
    vPace = 1;
        
}



void send_signal(int type) { //type=0 a_signal, type=1 v_signal
        
        switch(type) {
            case 0:
                a_signal = 1;
            case 1:
                v_signal = 1;
        }
        //TODO: Determine if time is right (is this supposed to be minWait?)
        wait(1); 
        //a_sense = 0;
        //v_sense = 0;
}

void modeSwitchDelay(){
    //srand(time(NULL));
    //int time = rand()%9700 + 300;
    Thread::wait(1000);   
}

void heart_keyboard() {
    while(true) { //thread is continuously running
            if(pc.readable()) {
                char c = pc.getc();
                
                switch(c) {
                    case 'r': //set to random mode
                        mode = 0;
                        modeSwitch = true;
                        break;
                    case 'm': //mset to manual mode
                        mode = 1;
                        modeSwitch = true;
                        break;
                    case 't': //set to test mode
                        mode = 2;
                        modeSwitch = true;
                        break;
                    case 'a': //asignal
                        if(mode == 1) { //only if in manual (heart_mode == 1)
                            modeInManul = 0;
                        }
                        break;
                    case 'v': //vsignal
                        if(mode == 1) { //only if in manual (heart_mode == 1)
                            modeInManul = 1;
                        }
                        break; 
                    default: //erroneous key get rid of it
                        break;
                }
            }
    }
}

void tick() {
    a_time++;
    v_time++;   
}
// Blink functions gets called when there is an Atrial/V entricular event.
void natApaceBlink(){
    natAPace = 1;
    Thread::wait(500);
    natAPace = 0;
    Thread::wait(500);
}

void natVpaceBlink(){
    natVPace = 1;
    Thread::wait(500);
    natVPace = 0;
    Thread::wait(500);
}

void ApaceBlink(){
    aPace = 1;
    Thread::wait(500);
    aPace = 0;
    Thread::wait(500);
}

void VpaceBlink(){
    vPace = 1;
    Thread::wait(500);
    vPace = 0;
    Thread::wait(500);
}

void performModeDelay(){
    if(modeSwitch == true){
        modeSwitchDelay();
        modeSwitch = false;
    }
}

void Random(){
    //TODO: heart behaviour in random mode
    while(1){
        if(mode == 0){
            performModeDelay();
            //natApaceBlink();
        }
    }
}

void Manual(){
    //TODO: heart behaviour in manual mode
    while(1){
        if(mode == 1){
            performModeDelay();
            //natVpaceBlink();
            if(modeInManul == 0){
                ApaceBlink();
                modeInManul = -1;
            }
            
            if(modeInManul == 1){
                VpaceBlink();
                modeInManul = -1;
            }
        }
    }   
}

void Test(){
    //TODO: heart behaviour in test mode
    while(1){
        if(mode == 2){
            performModeDelay();
            //ApaceBlink();
        }
    }
}

int main() {
    //TODO: Set up threads
    Thread keyboard(heart_keyboard);
    //Note: only one of the following threads will be on duty when heart runs
    //and it is done through checking the mode the heart is in.
    Thread random_(Random);
    Thread manual_(Manual);
    Thread test_(Test);
    while(1);
}