Testing 1 blue pill

Dependencies:   mbed mbed-rtos TextLCD

Revision:
23:07a368f2cdb1
Parent:
21:6d9f6a986647
Child:
24:728de4bf961e
--- a/Interface/Interface.cpp	Sat Feb 16 12:43:26 2019 +0000
+++ b/Interface/Interface.cpp	Sat Feb 16 15:19:21 2019 +0000
@@ -1,43 +1,96 @@
-#include "Interface.hpp"          //Include the header file, this acts like a series of forward declarations
-PwmOut Tendon_Power(PE_8); 
+#include "Interface.hpp"          //Include the header file, this acts like a series of forward declarations 
 //Constructor
-INTERFACE::INTERFACE(int Default_Turns)//Set all values to 0
+INTERFACE::INTERFACE(int Default_Turns)//Set all values to 0 bar the turns to do which is passed through by the constructor being run
 {
+    _Turns_Todo_mutex.lock();
     _Turns_Todo = Default_Turns;
+    _Turns_Todo_mutex.unlock();
 }
 INTERFACE::~INTERFACE(){}   //Destructor
 //Interrupt run functions
 void INTERFACE::Interface_Init()//Set all values to 0 bar the default turns
 {
-    _No_Of_Rotations =20;
+    _Function_mutex.lock();
     _Function =0;
+    _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_Todo =20;
+    _Turns_Done_mutex.unlock();
+    
+    _Turns_Todo_mutex.lock();
+    _Turns_Todo =_Turns_Todo;
+    _Turns_Todo_mutex.unlock();
+    
+    _Loop_mutex.lock();
     _Loop =0;
+    _Loop_mutex.unlock();
+    
+    _Wait_Time_mutex.lock();
     _Wait_Time =0;
+    _Wait_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();
 }
 void INTERFACE::Up()//Up Routine
 {
     if (_Function == 0)//Turns Function
     {
-        _No_Of_Rotations = _No_Of_Rotations + 1;//Increases Turn number
-        _Turns_Todo = _No_Of_Rotations;
+        _Turns_Todo_mutex.lock();
+        _Turns_Todo = _Turns_Todo +1;//Increases Turn number
+        _Turns_Todo_mutex.unlock();
     }
     else if (_Function == 1)//Annealing Function
     {
         if (_Select == false)//Select = 0
-        {
+        {   
+            _Loop_mutex.lock();
             _Loop = _Loop + 1;  //Increases Repetitions in Annealing
+            _Loop_mutex.unlock();
         }
         else if (_Select == true)//Select = 1
         {
+            _Wait_Time_mutex.lock();
             _Wait_Time = _Wait_Time + 1;  //Increases Wait time
+            _Wait_Time_mutex.unlock();
         }
     }
     else if (_Function == 2)//Test Function
@@ -46,11 +99,15 @@
         {
             if (_Duty_Cycle < 100)
             {
+                _Duty_Cycle_mutex.lock();
                 _Duty_Cycle = _Duty_Cycle + 10; //Increases Testing Duty Cycle
+                _Duty_Cycle_mutex.unlock();
             }
             else if (_Select == true)//Select = 1
             {
+                _Power_Time_mutex.lock();
                 _Power_Time = _Power_Time + 1;    //Increases time on
+                _Power_Time_mutex.unlock();
             }
         }
     }
@@ -59,8 +116,9 @@
 {
     if (_Function == 0)//Turn Function
     { 
-        _No_Of_Rotations = _No_Of_Rotations - 1;
-        _Turns_Todo = _No_Of_Rotations;
+        _Turns_Todo_mutex.lock();
+        _Turns_Todo = _Turns_Todo -1;//Decrement Turn number
+        _Turns_Todo_mutex.unlock();
     }
     else if (_Function == 1)//Anneal Function
     {
@@ -69,13 +127,17 @@
 
             if (_Loop > 0)
             {
+                _Loop_mutex.lock();
                 _Loop = _Loop - 1;
+                _Loop_mutex.unlock();
             }
             else if (_Select == true)//Select = 1
             {
                 if (_Wait_Time > 0)
                 {
+                         _Wait_Time_mutex.lock();
                          _Wait_Time = _Wait_Time - 1;
+                         _Wait_Time_mutex.unlock();
                 }
             }
         }
@@ -86,13 +148,17 @@
         {
             if (_Duty_Cycle > 0)
             {
+                 _Duty_Cycle_mutex.lock();
                  _Duty_Cycle = _Duty_Cycle - 10;     //Decreases duty
+                 _Duty_Cycle_mutex.unlock();
             }
             else if (_Select == true)//Select = 1
             {
                 if (_Power_Time > 0)
                 {
-                _Power_Time = _Power_Time - 1;    //Decreases Time on
+                    _Power_Time_mutex.lock();
+                    _Power_Time = _Power_Time - 1;    //Decreases Time on
+                    _Power_Time_mutex.unlock();
                 }
             }
         }
@@ -100,17 +166,100 @@
 }
 void INTERFACE::Start_Stop()//Start / Stop Routine
 {
-    if (_Function == 0)//Twist selected
+    _System_Running = !_System_Running;
+    //System not running cases (START)
+    if (_Function == 0 & _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 == 1 & _System_Running == 0) //Annealing selected 
+    {
+        _Anneal_Go_mutex.lock();
+        _Anneal_Go = true;//toggle and Run the code
+        _Anneal_Go_mutex.unlock();
+    }
+    else if (_Function == 2 & _System_Running == 0) //Testing selected 
     {
-        _Twist_Go = !_Twist_Go;//toggle used for coiling and twisting and Running the code
+        _Test_Go_mutex.lock();
+        _Test_Go = true;//toggle and run the code
+        _Test_Go_mutex.unlock();
     }
-    else if (_Function == 1) //Annealing selected 
+    //System Running cases (STOP)
+    else if (_Function == 0 & _System_Running == 1)//Twist selected to be stopped
+    {
+        _Twist_Stop_mutex.lock();
+        _Twist_Stop = true;
+        _Twist_Stop_mutex.unlock();
+        printf("Twisting Code Paused\n");
+        _System_Running_mutex.lock();
+        _System_Running = 2;
+        _System_Running_mutex.unlock();
+        STEPPER_MOTOR_1.Pause_Code();
+    }
+    else if (_Function == 1 & _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 == 2 & _System_Running == 1) //Testing selected to be stopped
     {
-        _Anneal_Go = !_Anneal_Go;//toggle and Run the code
+        _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();
     }
-    else if (_Function == 2) //Testing selected 
+        //System Running cases (Unpause)
+    else if (_Function == 0 & _System_Running == 2)//Twist selected to be unpasued
+    {
+        _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();
+            
+        printf("Twisting Code unpaused\n");
+
+    }
+    else if (_Function == 1 & _System_Running == 2) //Annealing selected to be unpaused
     {
-        _Test_Go = !_Test_Go;//toggle and run the code
+        _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();
+    }
+    else if (_Function == 2 & _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();
+        
+        
     }
 }
 void INTERFACE::Function()//Function Routine used to increment through the three functions
@@ -121,25 +270,47 @@
     Function 2 = Test
     */
     if (_Function < 2){
+        _Function_mutex.lock();
         _Function = _Function + 1;
+        _Function_mutex.unlock();
     }
     else
     {
+        _Function_mutex.lock();
         _Function = 0;
+        _Function_mutex.unlock();
     }
 }
 void INTERFACE::Select()//Select Routine
 {
+    _Select_mutex.lock();
     _Select = !_Select;//Toggle select to choose between two options e.g. Duty cycle and loops as only two options are avalible
-    if(_Select  == false)
+    _Select_mutex.unlock();
+    if(_Function == 0)
     {
+        Led_Select_Left_mutex.lock();
+        Led_Select_Right = 1;
+        Led_Select_Left_mutex.unlock(); 
+    }
+    else if(_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
     {
+        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();
     }
     //Output to leds here dependant upon the select value e.g. 0 is left led and 1 is right led
 }
@@ -149,70 +320,137 @@
     {
         if (_Twist_Go == true)
         {
+            Led_Power_mutex.lock();
             Led_Power = 1;
-            //printf("20 Turns\n");
-            //STEPPER_MOTOR_1.Rotate_Steps(22);
+            Led_Power_mutex.unlock();
+            
+            _System_Running_mutex.lock();
+            _System_Running = 1;
+            _System_Running_mutex.unlock();
+            
             STEPPER_MOTOR_1.Rotate_Steps(_Turns_Todo);//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 == 1)  //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
             {
-                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%
-                wait(_Wait_Time);//Variable
-                Tendon_Power=0; // set duty cycle to 0%
-                wait(6);//Fixed off time
+                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%
+                    wait(_Wait_Time);//Variable
+                    Tendon_Power=0; // set duty cycle to 0%
+                    wait(6);//Fixed off time
+                    _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 == 2)  //Testing Code
+    else if (_Function == 2)  //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_No_Of_Rotations(int Rotations){_No_Of_Rotations = Rotations;}
-void INTERFACE::Set_Function(int Function){_Function = Function;}
-void INTERFACE::Set_Twist_Go(bool Twist_Go){_Twist_Go = Twist_Go;}
-void INTERFACE::Set_Anneal_Go(bool Anneal_Go){_Anneal_Go = Anneal_Go;}
-void INTERFACE::Set_Test_Go(bool Test_Go){_Test_Go = Test_Go;}
-void INTERFACE::Set_Select(bool Select){_Select = Select;}
-void INTERFACE::Set_Turns_Done(int Turns_Done){_Turns_Done = Turns_Done;}
-void INTERFACE::Set_Turns_To_Do(int Turns_Todo){_Turns_Todo = Turns_Todo;}
-void INTERFACE::Set_Loop(int Loop){Loop = Loop;}
-void INTERFACE::Set_Wait_Time(int Wait_Time){_Wait_Time = Wait_Time;}
-void INTERFACE::Set_Duty_Cycle(int Duty_Cycle){_Duty_Cycle = Duty_Cycle;}
-void INTERFACE::Set_Power_Time(float Power_Time){_Power_Time = Power_Time;} 
+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(bool 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_Wait_Time(int Wait_Time){_Wait_Time_mutex.lock();_Wait_Time = Wait_Time;_Wait_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_No_Of_Rotations(){return _No_Of_Rotations;}
-int INTERFACE::Get_Function(){return _Function;}
+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;}
 bool INTERFACE::Get_Select(){return _Select;}
 int INTERFACE::Get_Turns_Done(){_Turns_Done = STEPPER_MOTOR_1.Get_Turns(); return _Turns_Done;}
-//int INTERFACE::Get_Turns_Done(){return _Turns_Done;}
-
 int INTERFACE::Get_Turns_To_Do(){return _Turns_Todo;}
 int INTERFACE::Get_Loop(){return _Loop;}
 int INTERFACE::Get_Wait_Time(){return _Wait_Time;}
 int INTERFACE::Get_Duty_Cycle(){return _Duty_Cycle;}
-int INTERFACE::Get_Power_Time(){return _Power_Time;}
+int INTERFACE::Get_Power_Time(){return _Power_Time;}
\ No newline at end of file