Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed mbed-rtos TextLCD
main.cpp
00001 #include "SETUP.hpp" 00002 void Polling_thread_function()// Polling thread to look for button press 00003 { 00004 while(1) 00005 { 00006 if(button_up.read() == 0) //This checks the Up button 00007 { 00008 printf("Up button read\n"); 00009 Thread::wait(Debounce_Time); 00010 if(button_up.read() == 1) 00011 { 00012 printf("Up button released\n"); 00013 up_thread.signal_set(1); 00014 } 00015 } 00016 if(button_down.read() == 0) //This checks the Down button 00017 { 00018 printf("Down button read\n"); 00019 Thread::wait(Debounce_Time); 00020 if(button_down.read() == 1) 00021 { 00022 down_thread.signal_set(1); 00023 } 00024 } 00025 if(button_start.read() == 0) //This checks the Start/Stop button 00026 { 00027 printf("Start/Stop button read\n"); 00028 Thread::wait(Debounce_Time); 00029 if(button_start.read() == 1) 00030 { 00031 start_stop_thread.signal_set(1); 00032 } 00033 } 00034 if(button_function.read() == 0) //This checks the Function button 00035 { 00036 printf("Function button read\n"); 00037 Thread::wait(Debounce_Time); 00038 if(button_function.read() == 1) 00039 { 00040 function_thread.signal_set(1); 00041 } 00042 } 00043 if(button_select.read() == 0) //This checks the Select button 00044 { 00045 printf("Select button read\n"); 00046 Thread::wait(Debounce_Time); 00047 if(button_select.read() == 1) 00048 { 00049 select_thread.signal_set(1); 00050 } 00051 } 00052 Thread::wait(50); 00053 } 00054 } 00055 void LCD_thread() //Output data to the LCD for the interface 00056 /* 00057 This section of the code decides what data that should be sent to the LCD for each of the modes of the system. 00058 */ 00059 { 00060 while(1) 00061 { 00062 lcd.cls();//Clear the LCD screen 00063 if(INTERFACE.Get_Function() == 0)//Anneal Mode display 00064 { 00065 lcd.printf("Mode: Anneal\n"); //Top LCD line when the device is annealing 00066 if(INTERFACE.Get_Select() == 0 || INTERFACE.Get_Select() == 1) 00067 { 00068 lcd.printf("Loop:%d On_t:%d\n",INTERFACE.Get_Loop(),INTERFACE.Get_On_Time()); //When the On value is selected 00069 } 00070 else if(INTERFACE.Get_Select() == 2) 00071 { 00072 lcd.printf("Loop:%d Offt:%d\n",INTERFACE.Get_Loop(),INTERFACE.Get_Off_Time()); //When the Off value is selected 00073 } 00074 } 00075 else if(INTERFACE.Get_Function() == 1)//Testing mode display 00076 { 00077 lcd.printf("Mode: Test\n"); //Top LCD line when the device is Testing 00078 lcd.printf("Duty: %dTime: %d\n",INTERFACE.Get_Duty_Cycle(),INTERFACE.Get_Power_Time()); 00079 } 00080 else if(INTERFACE.Get_Function() == 2)//Coil 00081 { 00082 lcd.printf("Mode: Coil\n"); //Top LCD line when the device is Coiling 00083 lcd.printf("Done:%dToDo: %d\n",INTERFACE.Get_Turns_Done(),INTERFACE.Get_Turns_To_Do()); 00084 } 00085 else if(INTERFACE.Get_Function() == 3)//Twist 00086 { 00087 lcd.printf("Mode: Twist\n"); //Top LCD line when the device is Twisting 00088 lcd.printf("Done:%dToDo: %d\n",INTERFACE.Get_Turns_Done(),INTERFACE.Get_Turns_To_Do()); 00089 } 00090 Thread::wait(250);//Refresh rate for Lcd 00091 } 00092 } 00093 void LED_thread(){//Led thread to toggle the on board led to show the CPU is running 00094 while (1) 00095 { 00096 led2 = !led2; //Toggle the LED 00097 Thread::wait(1000);//Wait 1 second 00098 } 00099 } 00100 //Interrupt functions 00101 void up_thread_function()//Action if the up button is pressed increment 00102 { 00103 while(1) 00104 { 00105 /* 00106 Thread::signal_wait(1); //Wait to be signaled by the up button interrupt 00107 Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs 00108 Thread::signal_wait(2); //Wait to be signaled by the up button interrupt 00109 00110 */ 00111 Thread::signal_wait(1); //Wait to be signaled by the up button interrupt 00112 pc.printf("Up Button Pressed in thread function\n");//Output data to the putty terminal 00113 Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs this debouce time might need to be longer 00114 up_thread.signal_set(0); //Set the thread signal low to wait for the interrupt before reaccuring 00115 pc.printf("Up Button Pressed\n");//Output data to the putty terminal 00116 INTERFACE.Up(); //Run the interface up routine 00117 } 00118 } 00119 void down_thread_function() //Action if the down button is pressed decrement 00120 { 00121 while(1) 00122 { 00123 /* 00124 Thread::signal_wait(1); //Wait to be signaled by the down button interrupt 00125 Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs 00126 Thread::signal_wait(2); //Wait to be signaled by the down button interrupt 00127 00128 */ 00129 Thread::signal_wait(1); //Wait to be signaled by the down button interrupt 00130 Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer 00131 down_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring 00132 pc.printf("Down Button Pressed\n");//Output data to the putty terminal 00133 INTERFACE.Down();//Run the interface down routine 00134 } 00135 } 00136 void start_stop_thread_function() //Action if the Start/Stop button is pressed 00137 { 00138 while(1) 00139 { 00140 Thread::signal_wait(1); //Wait to be signaled by the start / stop button interrupt 00141 Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer 00142 start_stop_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring 00143 pc.printf("S/S Button Pressed\n");//Output data to the putty terminal 00144 INTERFACE.Start_Stop();//Run the interface start / stop routine 00145 //Thread::wait(1000);//Button debounce to prevent multiple re-runs 00146 } 00147 } 00148 void Function_Selection_thread_function()//Action if the Function button is pressed 00149 { 00150 while(1) 00151 { 00152 Thread::signal_wait(1); //Wait to be signaled by the function button interrupt 00153 Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer 00154 function_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring 00155 pc.printf("Function Button Pressed\n");//Output data to the putty terminal 00156 INTERFACE.Function();//Run the interface function routine 00157 //Thread::wait(2000);//Button debounce to prevent multiple re-runs 00158 } 00159 } 00160 void Selection_thread_function()//Action if the Select button is pressed 00161 { 00162 while(1) 00163 { 00164 Thread::signal_wait(1); //Wait to be signaled by the select button interrupt 00165 Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer 00166 select_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring 00167 pc.printf("Select Button Pressed\n");//Output data to the putty terminal 00168 INTERFACE.Select();//Run the interface selection routine 00169 //Thread::wait(1000);//Button debounce to prevent multiple re-runs 00170 } 00171 } 00172 int main() 00173 { 00174 //Interrupt setters 00175 00176 00177 lcd.printf("Ready Player\n"); 00178 lcd.printf(" One \n"); 00179 Thread::wait(1000); 00180 pc.printf("Program start\n");//Outputs informtation to the putty terminal 00181 00182 //Thread Initialisation 00183 polling_thread.start(Polling_thread_function);//Start the button polling 00184 lcd_thread.start(LCD_thread);//Output data to LCD 00185 led_thread.start(LED_thread);//Blinking led to show CPU running 00186 up_thread.start(up_thread_function);//UP interface thread 00187 down_thread.start(down_thread_function);//Down interface thread 00188 start_stop_thread.start(start_stop_thread_function);//Start / stop interface thread 00189 function_thread.start(Function_Selection_thread_function);//Function interface thread 00190 select_thread.start(Selection_thread_function);//Start interface thread 00191 00192 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 00193 INTERFACE.Interface_Init(); 00194 while(1)// Main code in a main thread 00195 { 00196 INTERFACE.Interface_main();//Run main thread code 00197 Thread::wait(10);//Small delay to save CPU time 00198 } 00199 }
Generated on Sat Jul 16 2022 17:32:46 by
1.7.2