CIS541 / Mbed 2 deprecated CIS541PM

Dependencies:   mbed mbed-rtos TextLCD

main.cpp

Committer:
shibulal
Date:
2015-11-30
Revision:
2:b178e27d9f22
Parent:
1:e3a5388e46ab
Child:
3:ac2e5aceb324

File content as of revision 2:b178e27d9f22:

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

InterruptIn A(p8);
InterruptIn V(p9);
DigitalOut Apace(p1);
DigitalOut Vpace(p2);
Serial pc(USBTX, USBRX);


void PacemakerKeyboardInput(void const *args);
void PacemakerModes(void const *args);
void PacemakerReceive(void const *args);
void PacemakerSend(void const *args);

int modeSwitchTimeInterval = 5000;
int flag=0;
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;
Timer k;
Timer p;
Timer t_loc
const int PVARP = 150; 
const int VRP = PVARP; 
const int AVI = 65;
char mode;
char manualPace;
char AorV;
char AorVsense;
Thread Pmki(PacemakerKeyboardInput,(void *) 0); 
Thread Pmm(PacemakerModes,(void *) 0);
Thread Pmr (PacemakerReceive, (void *) 0);
Thread Pmr (PacemakerSend, (void *) 0);

void PacemakerKeyboardInput(void const *args){
    k.start();
    char input;
    while(true){
        input=pc.getc();
        if (k.read()>=modeSwitchTimeInterval && flag==0){
            if (input=='F'){
                mode='F';
                Pmm.signal_set(0x1);
                Thread::signal_wait(0x1);
                }
            else if (input=='S'){
                mode='S';
                Pmm.signal_set(0x1);
                Thread::signal_wait(0x1);
                }
            else if (input=='N'){
                mode='N';
                Pmm.signal_set(0x1);
                Thread::signal_wait(0x1);
                }
            else if (input=='O'){
                mode='O';
                Pmm.signal_set(0x1);
                Thread::signal_wait(0x1);
                }
            else if (input=='M'){
                mode='M';
                Pmm.signal_set(0x1);
                Thread::signal_wait(0x1);
                }
            else if (input=='A'){
                if (mode=='M')
                    manualPace='A';
                    Pmm.signal_set(0x1);
                    Thread::signal_wait(0x1);
                    }
                }
            else if (input=='V'){
                if (mode=='M'){

                    manualPace='V';
                    Pmm.signal_set(0x1);
                    Thread::signal_wait(0x1);
                    }
                }
            }
        }
    
    
    
void PacemakerModes(void const *args){
    while(1){
        Thread::signal_wait(0x1);
        if (mode=='F'){
            LRI=sportsModeLRI;
            URI=sportsModeURI;
            Pmki.signal_set(0x1);
            }
        else if (mode=='S'){
            LRI=sleepModeLRI;
            URI=sleepModeURI;
            Pmki.signal_set(0x1);
            }
        else if (mode=='N'){
            LRI=normalModeLRI;
            URI=normalModeURI;
            Pmki.signal_set(0x1);
            }
        else if (mode=='O'){
            //not sure what to do here
            Pmki.signal_set(0x1);
            }
        else if (mode=='M'){
            LRI=manualModeLRI;
            URI=manualModeURI;
            Pmki.signal_set(0x1);
            while(1){
                Thread::signal_wait(0x1);
                if (mode=='M'){
                    if (manualPace=='A'){
                        Pms.signal_set(0x01);
                        AorVsense='Am'
                        manualPace=0;
                        }
                    else if (manualPace=='V'){
                        Pms.signal_set(0x01);
                        AorVsense='Vm'
                        manualPace=0;
                        }
                    }
                else {
                    Pmm.signal_set(0x1);
                    Pmki.signal_set(0x1);
                    break;
                    }
                Pmki.signal_set(0x1);
                }
            }
        }
    } 
    
void PacemakerSend(void const *args){
    while(1){
        Thread::signal_wait(0x1);
        if (AorVsense=='V'){
            p.reset();
            }
        else if (AorVsense=='A'||p.read()>=LRI-AVI){
            p.reset();
            flag=1;
            while(1){
                Thread::signal_wait(0x1);
                if (AorVsense=='A'){
                    while(1){     
                        if (p<=LRI){
                        flag=0;
                        break;     
                    }
                }
                else if (AorVsense=='V'){
                   p.reset();
                   flag=0;
                   while(1){
                        if (p<=LRI-AVI){
                            break;
                    }
                }
                else if (AorVsense=='Vm'){
                   flag=0;
                    p.reset();
                  Vpace=1;
                 wait(0.1)
                    Vpace(0.1)
                while(1){
                if (p<=LRI-AVI){
                    break;
                    }
                }
                   }
                else if(p.read()==LRI) {
                   Vpace=1;
                 wait(0.1)
                    Vpace=0;
                    p.reset();
                    flag=0;
                    while(1){
                if (p<=LRI-AVI){
                    break;
                    }
                   }
                   }
            }
       else  if (AorVsense=='Vm'){
            p.reset();
            Vpace=1;
            wait(0.1)
            Vpace(0.1)
            while(1){
                if (p<=LRI-AVI){
                    break;
                    }
                }
            }
        else if (AorVsense=='Am'){
            p.reset();
            Apace=1;
            wait(0.1)
            Apace(0.1)
            flag=1;
            while(1){
                Thread::signal_wait(0x1);
                if (AorVsense=='A'){
                    while(1){     
                        if (p<=LRI){
                        flag=0;
                        break;     
                    }
                }
                else if (AorVsense=='V'){
                   p.reset();
                   flag=0;
                   while(1){
                        if (p<=LRI-AVI){
                            break;
                    }
                }
                else if (AorVsense=='Vm'){
                   flag=0;
                    p.reset();
                  Vpace=1;
                 wait(0.1)
                    Vpace(0.1)
                while(1){
                if (p<=LRI-AVI){
                    break;
                    }
                }
                   }
                else if(p.read()==LRI) {
                   Vpace=1;
                 wait(0.1)
                    Vpace=0;
                    p.reset();
                    flag=0;
                    while(1){
                if (p<=LRI-AVI){
                    break;
                    }
                   }
                   }
                   
                    }
            }

            }
        }
    }
void PacemakerReceive(void const *args){
    while(1){
        Thread::signal_wait(0x1);
        if (AorV=='A' && p.read()<PVARP){
            
        }
        else if (AorV=='V' && p.read()<VRP){
            }
        else if (AorV=='A' && p.read()>=PVARP){
            Pms.signal_set(0x01);
            AorVsense='A'
            t_loc.reset
            while(1){
                Thread::signal_wait(0x1);
                if(AorV=='V'){
                    break;
                    }
                }
            while(1){
                if (t_loc>AVI && p>URI){
                    Pms.signal_set(0x01);
                    AorVsense='V';
                    p=0;
                    t_loc=0;
                    break;
                    }
            }
            }
            else if (AorV=='V' && p.read()>=VRP){
                while(1){
                    if ((t_loc>=AVI && p<=URI) || (t_loc<=AVI && p<=URI) ||(t_loc<=AVI && p>=URI)){
                        break;
                    }
                }
                while(1){
                Thread::signal_wait(0x1);
                if(AorV=='V'){
                    break;
                    }
                }
            while(1){
                if (t_loc>AVI && p>URI){
                    Pms.signal_set(0x01);
                    AorVsense='V';
                    p=0;
                    t_loc=0;
                    break;
                    }
            }
                
            }
            
        
            
    
    }

int main(){
    A.mode(PullDown);              
    A.rise(Asig);               
    V.mode(PullDown);              
    V.rise(Vsig);               
    
    while(1);
}


void Asig() {
    Pmr.signal_set(0x1);  
    AorV='A'; 
                             
}
void Vsig() {
       Pmr.signal_set(0x1);
       AorV='V';
                             
}