Testing 1 blue pill

Dependencies:   mbed mbed-rtos TextLCD

Interface/Interface.cpp

Committer:
thomasmorris
Date:
2019-02-18
Revision:
25:9751619fa030
Parent:
24:728de4bf961e
Child:
26:83550fc299aa

File content as of revision 25:9751619fa030:

#include "Interface.hpp"          //Include the header file, this acts like a series of forward declarations 
//Constructor
INTERFACE::INTERFACE(){}//Set all values to 0 

INTERFACE::~INTERFACE(){}   //Destructor
//Interrupt run functions
void INTERFACE::Interface_Init()//Set all values to 0 bar the default turns
{
    Led_Select_Left_mutex.lock();
    Led_Select_Left = 0;
    Led_Select_Left_mutex.unlock();
        
    Led_Select_Right_mutex.lock();
    Led_Select_Right= 0;
    Led_Select_Right_mutex.unlock();
    
    _Function_mutex.lock();
    _Function =2;
    _Function_mutex.unlock();
    
    _Twist_Go_mutex.lock();
    _Twist_Go =0;
    _Twist_Go_mutex.unlock();
    
    _Anneal_Go_mutex.lock();
    _Anneal_Go =0;
    _Anneal_Go_mutex.unlock();
    
    _Test_Go_mutex.lock();
    _Test_Go =0;
    _Test_Go_mutex.unlock();
    
    _Select_mutex.lock();
    _Select =0;
    _Select_mutex.unlock();
    
    _Turns_Done_mutex.lock();
    _Turns_Done =0;
    _Turns_Done_mutex.unlock();
    
    _Turns_Todo_mutex.lock();
    if(Type_of_Rig == 2)
    {
        _Turns_Todo = Default_Coiling_Turns;
    }
    else if(Type_of_Rig == 3)
    {
        _Turns_Todo = Default_Twisting_Turns;
    }
    _Turns_Todo_mutex.unlock();
    
    _Loop_mutex.lock();
    _Loop =6;
    _Loop_mutex.unlock();
    
    _On_Time_mutex.lock();
    _On_Time =6;
    _On_Time_mutex.unlock();
    
    _Off_Time_mutex.lock();
    _Off_Time =6;
    _Off_Time_mutex.unlock();
    
    _Duty_Cycle_mutex.lock();
    _Duty_Cycle =0;
    _Duty_Cycle_mutex.unlock();
    
    _Power_Time_mutex.lock();
    _Power_Time =0;
    _Power_Time_mutex.unlock();
    
     Led_Select_Left_mutex.lock();
     Led_Select_Left = 0;
     Led_Select_Left_mutex.unlock();
     
     Led_Select_Right_mutex.lock();
     Led_Select_Right= 0;
     Led_Select_Right_mutex.unlock();
     
     Led_Power_mutex.lock();
     Led_Power =0;
     Led_Power_mutex.unlock();
     
     _System_Running_mutex.lock();
     _System_Running =0;
     _System_Running_mutex.unlock();
     
     
    Led_Select_Left_mutex.lock();
    Led_Select_Left = 0;
    Led_Select_Left_mutex.unlock();
        
    Led_Select_Right_mutex.lock();
    Led_Select_Right= 1;
    Led_Select_Right_mutex.unlock();
}
void INTERFACE::Up()//Up Routine
{
    if (_Function == 2 || _Function == 3 )//Coiling or twist Function
    {
        _Turns_Todo_mutex.lock();
        while(button_up.read() == 1)
        {
            _Turns_Todo = _Turns_Todo +1;
            Thread::wait(100);    
        }
        _Turns_Todo_mutex.unlock();
    }
    else if (_Function == 0)//Annealing Function
    {
        if (_Select == 0)//Select = 0
        {   
            _Loop_mutex.lock();
            _Loop = _Loop + 1;  //Increases Repetitions in Annealing
            _Loop_mutex.unlock();
        }
        else if (_Select == 1)//Select = 1
        {
            _On_Time_mutex.lock();
            _On_Time = _On_Time + 1;  //Increases On time
            _On_Time_mutex.unlock();
        }
        else if (_Select == 2)//Select = 1
        {
            _Off_Time_mutex.lock();
            _Off_Time = _On_Time + 1;  //Increases Off time
            _Off_Time_mutex.unlock();
        }
    }
    else if (_Function == 1)//Test Function
    {
        if (_Select == 0)//Select = 0
        {
            if (_Duty_Cycle < 100)
            {
                _Duty_Cycle_mutex.lock();
                _Duty_Cycle = _Duty_Cycle + 10; //Increases Testing Duty Cycle
                _Duty_Cycle_mutex.unlock();
            }
        }
        else if (_Select == 1)//Select = 1
        {
            _Power_Time_mutex.lock();
            _Power_Time = _Power_Time + 1;    //Increases time on
            _Power_Time_mutex.unlock();
        }
    }
}
void INTERFACE::Down()//Down Routine
{
    if (_Function == 2 || _Function == 3)//Coiling or twist Function
    {
        _Turns_Todo_mutex.lock();
        while(button_up.read() == 1)
        {
            _Turns_Todo = _Turns_Todo -1;
            Thread::wait(100);    
        }
        _Turns_Todo_mutex.unlock();
    }
    else if (_Function == 0)//Anneal Function
    {
        if (_Select == 0)//Select = 0
        {

            if (_Loop > 0)
            {
                _Loop_mutex.lock();
                _Loop = _Loop - 1;
                _Loop_mutex.unlock();
            }
        }
        else if (_Select == 1)//Select = 1
        {
            _On_Time_mutex.lock();
            _On_Time = _On_Time - 1;  //Decrease On time
            _On_Time_mutex.unlock();
        }
        else if (_Select == 2)//Select = 1
        {
            _Off_Time_mutex.lock();
            _Off_Time = _On_Time - 1;  //Decrements Off time
            _Off_Time_mutex.unlock();
        }
    }
    else if (_Function == 1)//Test Function
    {  
        if (_Select == 0)//Select = 0
        {
            if (_Duty_Cycle > 0)
            {
                 _Duty_Cycle_mutex.lock();
                 _Duty_Cycle = _Duty_Cycle - 10;     //Decreases duty
                 _Duty_Cycle_mutex.unlock();
            }
        }
        else if (_Select == 1)//Select = 1
        {
            if (_Power_Time > 0)
            {
                _Power_Time_mutex.lock();
                _Power_Time = _Power_Time - 1;    //Decreases Time on
                _Power_Time_mutex.unlock();
            }
        }
        
    }
}
void INTERFACE::Start_Stop()//Start / Stop Routine
{
    
    if(_System_Running != 2)
    {
        _System_Running = !_System_Running;
    }
    //System not running cases (START)
    printf("Function is: %d, System is: %d\n",_Function,_System_Running);
    
    //System Running cases (Unpause)
    if ((_Function == 2 || _Function == 3) & _System_Running == 2)//Coil Twist selected to be unpaused
    {
        _Twist_Stop_mutex.lock();
        _Twist_Stop = false;
        _Twist_Stop_mutex.unlock();
        
        _Twist_Go_mutex.lock();
        _Twist_Go = true;//toggle used for coiling and twisting and Running the code
        _Twist_Go_mutex.unlock();
        
        STEPPER_MOTOR_1.Unpause_Code();
            
        if(_Function == 2)//Coiling
        {
            printf("Coiling Code unpaused\n");
        }
        if(_Function == 3)//Twisiting
        {
            printf("Twisting Code unpaused\n");
        }
        _System_Running = 0;

    }
    else if (_Function == 0 & _System_Running == 2) //Annealing selected to be unpaused
    {
        _Anneal_Stop_mutex.lock();
        _Anneal_Stop = false;
        _Anneal_Stop_mutex.unlock();
        printf("Annealing Code unpaused\n");
        
        _Anneal_Go_mutex.lock();
        _Anneal_Go = true;//toggle and Run the code
        _Anneal_Go_mutex.unlock();
        _System_Running = 0;
    }
    else if (_Function == 1 & _System_Running == 2) //Testing selected to be re run
    {
        _Test_Stop_mutex.lock();
        _Test_Stop = false;
        _Test_Stop_mutex.unlock();
        printf("Testing Code unpaused\n");
        _Test_Go_mutex.lock();
        _Test_Go = true;//toggle and run the code
        _Test_Go_mutex.unlock();
        _System_Running = 0;
        
    }
    //System Running cases (STOP)
    else if ((_Function == 2 || _Function == 3) & _System_Running == 1)//Coiling / Twisting selected to be stopped
    {
        _Twist_Stop_mutex.lock();
        _Twist_Stop = true;
        _Twist_Stop_mutex.unlock();
        
        if(_Function == 2)
        {
            printf("Coiling Code Paused\n");
        }
        if(_Function == 3)
        {
            printf("Twisting Code Paused\n");
        }
        _System_Running_mutex.lock();
        _System_Running = 2;
        _System_Running_mutex.unlock();
        STEPPER_MOTOR_1.Pause_Code();
    }
    else if (_Function == 0 & _System_Running == 1) //Annealing selected to be stopped
    {
        _Anneal_Stop_mutex.lock();
        _Anneal_Stop = true;
        _Anneal_Stop_mutex.unlock();
        printf("Annealing Code Paused\n");
        _System_Running_mutex.lock();
        _System_Running = 2;
        _System_Running_mutex.unlock();
    }
    else if (_Function == 1 & _System_Running == 1) //Testing selected to be stopped
    {
        _Test_Stop_mutex.lock();
        _Test_Stop = true;
        _Test_Stop_mutex.unlock();
        
        printf("Testing Code Paused\n");
        
        _Tendon_mutex.lock();
        Tendon_Power=0;
        _Tendon_mutex.unlock();
        
        _System_Running_mutex.lock();
        _System_Running = 2;
        _System_Running_mutex.unlock();
    }
    //Start Cases
    else if ((_Function == 2 || _Function == 3) & _System_Running == 0)//Twist selected
    {
        _Twist_Go_mutex.lock();
        _Twist_Go = true;//toggle used for coiling and twisting and Running the code
        _Twist_Go_mutex.unlock();
    }
    else if (_Function == 0 & _System_Running == 0) //Annealing selected 
    {
        _Anneal_Go_mutex.lock();
        _Anneal_Go = true;//toggle and Run the code
        _Anneal_Go_mutex.unlock();
    }
    else if (_Function == 1 & _System_Running == 0) //Testing selected 
    {
        _Test_Go_mutex.lock();
        _Test_Go = true;//toggle and run the code
        _Test_Go_mutex.unlock();
    }
}
void INTERFACE::Function()//Function Routine used to increment through the three functions
{
    /*
    Function 0 = Anneal
    Function 1 = Test
    Function 2 = Coiling
    Function 3 = Twisting
    */
    
    if(Type_of_Rig == 2)
    {
        if(_Function < 2)
        {
            _Function_mutex.lock();
            _Function = _Function + 1;
            _Function_mutex.unlock();
        }
        else if(_Function == 2)
        {
            _Function_mutex.lock();
            _Function = 0;
            _Function_mutex.unlock(); 
        }
    }
    else if(Type_of_Rig == 3)
    {
        if(_Function == 3)
        {
            _Function_mutex.lock();
            _Function = 0;
            _Function_mutex.unlock(); 
        }
        else if(_Function < 2)
        {
            _Function_mutex.lock();
            _Function = _Function + 1;
            _Function_mutex.unlock();
        }
        else if(_Function == 2)
        {
            _Function_mutex.lock();
            _Function = 3;   
            _Function_mutex.unlock();
        }
    }
}
void INTERFACE::Select()//Select Routine
{
    _Select_mutex.lock();
    if(_Select == 0 & _Function == !2)//All but anneal have 2 component menu's
    {
        _Select = 1;    
    }
    else if(_Select == 1 & _Function == !2)//Toggle the value
    {
        _Select = 0;    
    }
    else if(_Function == 1 & _Select < 2)
    {
        _Select = _Select + 1;        
    }
    else if(_Select == 2 & _Function == 1)//Set select to 0 if the function is anneal, as anneal has 3 components in the menu
    {
        _Select = 0;    
    }
    _Select_mutex.unlock();
    if(_Function == 0)
    {
        Led_Select_Left_mutex.lock();
        Led_Select_Right = 1;
        Led_Select_Left_mutex.unlock(); 
    }
    else if(_Function == 1 & _Select  == false)
    {
        Led_Select_Left_mutex.lock();
        Led_Select_Left = 1;
        Led_Select_Left_mutex.unlock();
        
        Led_Select_Right_mutex.lock();
        Led_Select_Right= 0;
        Led_Select_Right_mutex.unlock();
    }
    else if(_Function == 1 & _Select == true)
    {
        Led_Select_Left_mutex.lock();
        Led_Select_Left = 0;
        Led_Select_Left_mutex.unlock();
        
        Led_Select_Right_mutex.lock();
        Led_Select_Right= 1;
        Led_Select_Right_mutex.unlock();
    }
    else if(_Function == 2 & _Select  == false)
    {
        Led_Select_Left_mutex.lock();
        Led_Select_Left = 1;
        Led_Select_Left_mutex.unlock();
        
        Led_Select_Right_mutex.lock();
        Led_Select_Right= 0;
        Led_Select_Right_mutex.unlock();
    }
    else if(_Function == 2 & _Select == true)
    {
        Led_Select_Left_mutex.lock();
        Led_Select_Left = 0;
        Led_Select_Left_mutex.unlock();
        
        Led_Select_Right_mutex.lock();
        Led_Select_Right= 1;
        Led_Select_Right_mutex.unlock();
    }
}
void INTERFACE::Interface_main()//Main Routine to execute commands when the start button has been pressed
{
    if (_Function == 2 || _Function == 3)       //Turning Code
    {
        if (_Twist_Go == true)
        {
            Led_Power_mutex.lock();
            Led_Power = 1;
            Led_Power_mutex.unlock();
            
            _System_Running_mutex.lock();
            _System_Running = 1;
            _System_Running_mutex.unlock();
            
            STEPPER_MOTOR_1.Rotate_Steps(_Turns_Todo,_Function);//Rotates for the specified number of steps given
            
            _System_Running_mutex.lock();
            _System_Running = 0;
            _System_Running_mutex.unlock();
            
            
            _Twist_Go_mutex.lock();
            _Twist_Go = false;
            _Twist_Go_mutex.unlock();
            
            Led_Power_mutex.lock();
            Led_Power = 0;
            Led_Power_mutex.unlock();
        }
    }
    else if (_Function == 0)  //Annealing Code
    {
        if (_Anneal_Go == true)
        {
            Led_Power_mutex.lock();
            Led_Power = 1;
            Led_Power_mutex.unlock();
            
            _System_Running_mutex.lock();
            _System_Running = 1;
            _System_Running_mutex.unlock();
            
            for ( int counter = 0; counter < _Loop; counter++)  //Loop value, check if works
            {
                if(_System_Running == 1)
                {
                    _Tendon_mutex.lock();
                    Tendon_Power.period(0.01); // set PWM period to 10 ms don't use pwm have this just to declare
                    Tendon_Power=1; // set duty cycle to 100%
                    _On_Time_mutex.lock();
                    wait(_On_Time);//Variable
                    _On_Time_mutex.unlock();
                    Tendon_Power=0; // set duty cycle to 0%
                    _Off_Time_mutex.lock();
                    wait(_Off_Time);//Fixed off time
                    _Off_Time_mutex.unlock();
                    _Tendon_mutex.unlock();
                }
                while(_System_Running == 2)//Stops the system and just spins in this while loop until system running is not paused
                {
                    _Tendon_mutex.lock();
                    Tendon_Power =0;
                    _Tendon_mutex.unlock();
                }
            }
            
            _System_Running_mutex.lock();
            _System_Running = 0;
            _System_Running_mutex.unlock();
               
            _Anneal_Go_mutex.lock();
            _Anneal_Go = false;
            _Anneal_Go_mutex.unlock();
            
            Led_Power_mutex.lock();
            Led_Power = 0;
            Led_Power_mutex.unlock();
        }
    }
    else if (_Function == 1)  //Testing Code //This code is paused / unpaused in start / stop note this code is not paused it is re run.
    {
        if (_Test_Go == true)
        {
            Led_Power_mutex.lock();
            Led_Power = 1;
            Led_Power_mutex.unlock();
            
            _System_Running_mutex.lock();
            _System_Running = 1;
            _System_Running_mutex.unlock();
            
            _Tendon_mutex.lock();
            Tendon_Power.period(0.01); // set PWM period to 10 ms don't use pwm have this just to declare
            Tendon_Power= _Duty_Cycle / 100; // set duty cycle to variable input from buttons between 0-1 (on lcd this is a percentage) also increment by values of 10
            wait(_Power_Time);//Variable on time for power on
            Tendon_Power=0; // set duty cycle to 0% and power off
            _Tendon_mutex.unlock();
            
            _System_Running_mutex.lock();
            _System_Running = 0;
            _System_Running_mutex.unlock();
            
            _Test_Go_mutex.lock();
            _Test_Go = false;
            _Test_Go_mutex.unlock();
            
            Led_Power_mutex.lock();
            Led_Power = 0;
            Led_Power_mutex.unlock();
        }
    }
}
//Setter functions
void INTERFACE::Set_System_Running(int System_Running){_System_Running_mutex.lock(); _System_Running = System_Running; _System_Running_mutex.unlock();}
void INTERFACE::Set_Function(int Function){_Function_mutex.lock();_Function = Function;_Function_mutex.unlock();}
void INTERFACE::Set_Twist_Go(bool Twist_Go){_Twist_Go_mutex.lock();_Twist_Go = Twist_Go;_Twist_Go_mutex.unlock();}
void INTERFACE::Set_Anneal_Go(bool Anneal_Go){_Anneal_Go_mutex.lock();_Anneal_Go = Anneal_Go;_Anneal_Go_mutex.unlock();}
void INTERFACE::Set_Test_Go(bool Test_Go){_Test_Go_mutex.lock();_Test_Go = Test_Go;_Test_Go_mutex.unlock();}
void INTERFACE::Set_Twist_Stop(bool Twist_Stop){_Twist_Stop_mutex.lock();_Twist_Stop = Twist_Stop;_Twist_Stop_mutex.unlock();}
void INTERFACE::Set_Anneal_Stop(bool Anneal_Stop){_Anneal_Stop_mutex.lock();_Anneal_Stop = Anneal_Stop;_Anneal_Stop_mutex.unlock();}
void INTERFACE::Set_Test_Stop(bool Test_Stop){_Test_Stop_mutex.lock();_Test_Go = Test_Stop;_Test_Stop_mutex.unlock();}
void INTERFACE::Set_Select(int Select){_Select_mutex.lock();_Select = Select;_Select_mutex.unlock();}
void INTERFACE::Set_Turns_Done(int Turns_Done){_Turns_Done_mutex.lock();_Turns_Done = Turns_Done;_Turns_Done_mutex.unlock();}
void INTERFACE::Set_Turns_To_Do(int Turns_Todo){_Turns_Todo_mutex.lock();_Turns_Todo = Turns_Todo;_Turns_Todo_mutex.unlock();}
void INTERFACE::Set_Loop(int Loop){_Loop_mutex.lock();Loop = Loop;_Loop_mutex.unlock();}
void INTERFACE::Set_On_Time(int On_Time){_On_Time_mutex.lock();_On_Time = On_Time;_On_Time_mutex.unlock();}
void INTERFACE::Set_Off_Time(int Off_Time){_Off_Time_mutex.lock();_Off_Time = Off_Time;_Off_Time_mutex.unlock();}
void INTERFACE::Set_Duty_Cycle(int Duty_Cycle){_Duty_Cycle_mutex.lock();_Duty_Cycle = Duty_Cycle;_Duty_Cycle_mutex.unlock();}
void INTERFACE::Set_Power_Time(float Power_Time){_Power_Time_mutex.lock();_Power_Time = Power_Time;_Power_Time_mutex.unlock();} 
//Getter functions
int INTERFACE::Get_System_Running(){return _System_Running;}
int  INTERFACE::Get_Function(){return _Function;}
bool INTERFACE::Get_Twist_Go(){return _Twist_Go;}
bool INTERFACE::Get_Anneal_Go(){return _Anneal_Go;}
bool INTERFACE::Get_Test_Go(){return _Test_Go;}
bool INTERFACE::Get_Twist_Stop(){return _Twist_Stop;}
bool INTERFACE::Get_Anneal_Stop(){return _Anneal_Stop;}
bool INTERFACE::Get_Test_Stop(){return _Test_Stop;}
int INTERFACE::Get_Select(){return _Select;}
int INTERFACE::Get_Turns_Done(){_Turns_Done = STEPPER_MOTOR_1.Get_Turns(); return _Turns_Done;}
int INTERFACE::Get_Turns_To_Do(){return _Turns_Todo;}
int INTERFACE::Get_Loop(){return _Loop;}
int INTERFACE::Get_On_Time(){return _On_Time;}
int INTERFACE::Get_Off_Time(){return _Off_Time;}
int INTERFACE::Get_Duty_Cycle(){return _Duty_Cycle;}
int INTERFACE::Get_Power_Time(){return _Power_Time;}