Thomas Morris / Mbed 2 deprecated Stacy

Dependencies:   mbed mbed-rtos TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "SETUP.hpp"
00002 void Interface_Serial_thread_function()
00003 {
00004     while(1)
00005     {
00006         INTERFACE.Interface_Serial();
00007     }
00008 }
00009 
00010 void Polling_thread_function()// Polling thread to look for button press
00011 {
00012     while(1)
00013     {
00014         if(button_up.read() == 0)               //This checks the Up button
00015         {
00016             printf("Up button read\n");
00017             Thread::wait(Up_Down_Debounce_Time);
00018             if(INTERFACE.Get_Function() == 0 || INTERFACE.Get_Function() == 1 || (INTERFACE.Get_Function() == 2 || INTERFACE.Get_Function() == 3) & INTERFACE.Get_Select() == 1 )
00019             {
00020                 if(button_up.read() == 1)
00021                 {
00022                         printf("Up button released\n");
00023                         up_thread.signal_set(1);   
00024                 }
00025             }
00026             else if((INTERFACE.Get_Function() == 2) & INTERFACE.Get_Select() == 0)
00027             {
00028                 if(button_up.read() == 1)
00029                 {
00030                     printf("Up button released\n");
00031                     up_thread.signal_set(1);   
00032                 }
00033                 Thread::wait(1000);
00034                 
00035                 if(button_up.read() == 0)
00036                 {
00037                     printf("Up function coiling or twisting select = 0\n");
00038                     while(button_up.read() == 0)
00039                     {   
00040                         if(INTERFACE.Get_Turns_To_Do() >0)
00041                         {
00042                             INTERFACE.Set_Turns_To_Do(INTERFACE.Get_Turns_To_Do()+1);
00043                         }
00044                         Thread::wait(50);
00045                     }
00046                 }
00047             }
00048         }
00049         if(button_down.read() == 0)               //This checks the Down button
00050         {
00051             printf("Down button read\n");
00052             Thread::wait(Up_Down_Debounce_Time);
00053             if(INTERFACE.Get_Function() == 0 || INTERFACE.Get_Function() == 1 || (INTERFACE.Get_Function() == 2 || INTERFACE.Get_Function() == 3) & INTERFACE.Get_Select() == 1 )
00054             {
00055                 if(button_down.read() == 1)
00056                 {
00057                         printf("Down button released\n");
00058                         down_thread.signal_set(1);   
00059                 }
00060             }
00061             else if((INTERFACE.Get_Function() == 2) & INTERFACE.Get_Select() == 0)
00062             {
00063                 if(button_down.read() == 1)
00064                 {
00065                     printf("Down button released\n");
00066                     down_thread.signal_set(1);   
00067                 }
00068                 Thread::wait(1000);
00069                 
00070                 if(button_down.read() == 0)
00071                 {
00072                     printf("down function coiling or twisting select = 0\n");
00073                     while(button_down.read() == 0)
00074                     {   
00075                         if(INTERFACE.Get_Turns_To_Do() >0)
00076                         {
00077                             INTERFACE.Set_Turns_To_Do(INTERFACE.Get_Turns_To_Do()-1);
00078                         }
00079                         Thread::wait(50);
00080                     }
00081                 }
00082             }
00083         } 
00084         if(button_start.read() == 0)               //This checks the Start/Stop button
00085         {
00086             printf("Start/Stop button read\n");
00087             Thread::wait(Debounce_Time);
00088             if(button_start.read() == 1)
00089             {
00090                 start_stop_thread.signal_set(1);
00091             }   
00092         }
00093         if(button_function.read() == 0)               //This checks the Function button
00094         {
00095             printf("Function button read\n");
00096             Thread::wait(Debounce_Time);
00097             if(button_function.read() == 1)
00098             {
00099                 function_thread.signal_set(1);
00100             }   
00101         }
00102         if(button_select.read() == 0)               //This checks the Select button
00103         {
00104             printf("Select button read\n");
00105             Thread::wait(Debounce_Time);
00106             if(button_select.read() == 1)
00107             {
00108                 select_thread.signal_set(1);
00109             }   
00110         }
00111         Thread::wait(50);
00112     }   
00113 }
00114 void LCD_thread()   //Output data to the LCD for the interface
00115         /*
00116             This section of the code decides what data that should be sent to the LCD for each of the modes of the system.
00117         */
00118 {
00119     while(1)
00120     {
00121         lcd.cls();//Clear the LCD screen
00122         if(INTERFACE.Get_Function() == 0)//Anneal Mode display
00123         {
00124             lcd.printf("Mode: Anneal\n");      //Top LCD line when the device is annealing
00125             if(INTERFACE.Get_Select() == 0 || INTERFACE.Get_Select() == 1)
00126             {
00127                 lcd.printf("L:%d,%d Ont:%1.1f\n",INTERFACE.Get_Loops_done(),INTERFACE.Get_Loop(),INTERFACE.Get_On_Time());   //When the On value is selected
00128                 //lcd.printf("Loop:%d On_t:%d\n",INTERFACE.Get_Loop(),INTERFACE.Get_On_Time());   //When the On value is selected
00129             }
00130             else if(INTERFACE.Get_Select() == 2)
00131             {
00132                lcd.printf("L:%d,%d Offt:%d\n",INTERFACE.Get_Loops_done(),INTERFACE.Get_Loop(),INTERFACE.Get_Off_Time());   //When the Off value is selected
00133                //lcd.printf("Loop:%d Offt:%d\n",INTERFACE.Get_Loop(),INTERFACE.Get_Off_Time());   //When the Off value is selected
00134             }     
00135         }
00136         else if(INTERFACE.Get_Function() == 1)//Testing mode display
00137         {
00138             lcd.printf("Mode: Test\n");      //Top LCD line when the device is Testing
00139             lcd.printf("Duty: %dTime: %d\n",INTERFACE.Get_Duty_Cycle(),INTERFACE.Get_Power_Time());
00140         }
00141         else if(INTERFACE.Get_Function() == 2)//Coil
00142         {
00143             if(INTERFACE.Get_Select() == 0)
00144             {
00145                 lcd.printf("Mode: Coil\n");      //Top LCD line when the device is Coiling
00146                 lcd.printf("Done:%dToDo: %d\n",INTERFACE.Get_Turns_Done(),INTERFACE.Get_Turns_To_Do());
00147             }
00148             else if(INTERFACE.Get_Select() == 1)
00149             {
00150                 lcd.printf("Mode: Coil\n");      //Top LCD line when the device is Coiling
00151                 if(INTERFACE.Get_Direction() == 0)//Clockwise
00152                 {
00153                     lcd.printf("Dir: Clock\n");
00154                 }
00155                 else if (INTERFACE.Get_Direction() == 1)//AntiClockwise
00156                 {
00157                     lcd.printf("Dir: AntiClock\n");
00158                 }
00159             }
00160         }
00161         else if(INTERFACE.Get_Function() == 3)//Run Once
00162         {
00163             lcd.printf("Mode: RunOnce\n");      //Top LCD line when the device is annealing
00164             lcd.printf("Loops: 1 Ont:%1.1f\n",INTERFACE.Get_Run_Once_On_Time());   //When the On value is selected
00165         }
00166         Thread::wait(100);//Refresh rate for Lcd
00167     } 
00168 }
00169 void LED_thread(){//Led thread to toggle the on board led to show the CPU is running
00170     while (1)
00171     {
00172         led2 = !led2;      //Toggle the LED
00173         Thread::wait(1000);//Wait 1 second
00174     }
00175 }
00176 //Interrupt functions
00177 void up_thread_function()//Action if the up button is pressed increment
00178 {
00179     while(1)
00180     {
00181 
00182         Thread::signal_wait(1);  //Wait to be signaled by the up button interrupt
00183         pc.printf("Up Button Pressed in thread function\n");//Output data to the putty terminal
00184         Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs this debouce time might need to be longer
00185         up_thread.signal_set(0); //Set the thread signal low to wait for the interrupt before reaccuring
00186         pc.printf("Up Button Pressed\n");//Output data to the putty terminal
00187         
00188         if(INTERFACE.Get_System_Running() == 0)
00189         {
00190                 INTERFACE.Up();     //Run the interface up routine 
00191         }
00192     }
00193 }
00194 void down_thread_function() //Action if the down button is pressed decrement
00195 {
00196     while(1)
00197     {
00198         Thread::signal_wait(1);  //Wait to be signaled by the down button interrupt
00199         Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer
00200         down_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
00201         pc.printf("Down Button Pressed\n");//Output data to the putty terminal
00202         if(INTERFACE.Get_System_Running() == 0)
00203         {
00204                 INTERFACE.Down();//Run the interface down routine
00205         }
00206     }
00207 }
00208 void start_stop_thread_function() //Action if the Start/Stop button is pressed
00209 {
00210     while(1)
00211     {
00212         Thread::signal_wait(1);  //Wait to be signaled by the start / stop button interrupt
00213         Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer
00214         start_stop_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
00215         pc.printf("S/S Button Pressed\n");//Output data to the putty terminal
00216         INTERFACE.Start_Stop();//Run the interface start / stop routine
00217     }
00218 }
00219 void Function_Selection_thread_function()//Action if the Function button is pressed
00220 {
00221     while(1)
00222     {
00223         Thread::signal_wait(1);  //Wait to be signaled by the function button interrupt
00224         Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer
00225         function_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
00226         pc.printf("Function Button Pressed\n");//Output data to the putty terminal
00227         if(INTERFACE.Get_System_Running() == 0)
00228         {
00229             INTERFACE.Function();//Run the interface function routine
00230         }
00231     }
00232 }
00233 void Selection_thread_function()//Action if the Select button is pressed
00234 {
00235     while(1)
00236     {
00237         Thread::signal_wait(1);  //Wait to be signaled by the select button interrupt
00238         Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer
00239         select_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
00240         pc.printf("Select Button Pressed\n");//Output data to the putty terminal
00241         if(INTERFACE.Get_System_Running() == 0)
00242         {
00243             INTERFACE.Select();//Run the interface selection routine
00244         }
00245     }
00246 }
00247 int main() 
00248 {   
00249     lcd.printf("Ready   Player\n");
00250     lcd.printf("     One      \n");
00251     Thread::wait(1000);
00252     pc.printf("Program start\n");//Outputs informtation to the putty terminal
00253     
00254     //Thread Initialisation
00255     polling_thread.start(Polling_thread_function);//Start the button polling
00256     lcd_thread.start(LCD_thread);//Output data to LCD
00257     led_thread.start(LED_thread);//Blinking led to show CPU running    
00258     up_thread.start(up_thread_function);//UP interface thread
00259     down_thread.start(down_thread_function);//Down interface thread
00260     start_stop_thread.start(start_stop_thread_function);//Start / stop interface thread
00261     function_thread.start(Function_Selection_thread_function);//Function interface thread
00262     select_thread.start(Selection_thread_function);//Start interface thread
00263     Interface_Serial_thread.start(Interface_Serial_thread_function);
00264     
00265     osThreadSetPriority(osThreadGetId(), osPriorityHigh);//This is done to make sure the code for the stepper motor is the highest priority to ensure correct rotations of the stepper motor
00266     INTERFACE.Interface_Init(55);//Pass in turns 
00267     while(1)// Main code in a main thread
00268     {
00269         INTERFACE.Interface_main();//Run main thread code
00270         Thread::wait(10);//Small delay to save CPU time  
00271     } 
00272 }