![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Testing 1 blue pill
Dependencies: mbed mbed-rtos TextLCD
main.cpp@26:83550fc299aa, 2019-02-19 (annotated)
- Committer:
- thomasmorris
- Date:
- Tue Feb 19 14:40:01 2019 +0000
- Revision:
- 26:83550fc299aa
- Parent:
- 25:9751619fa030
- Child:
- 27:22d6fd88828e
Working code (needs stepper motor and mosfet output checking
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
thomasmorris | 12:d9c133b360b0 | 1 | #include "SETUP.hpp" |
thomasmorris | 26:83550fc299aa | 2 | void Polling_thread_function()// Polling thread for the buttons |
thomasmorris | 26:83550fc299aa | 3 | { |
thomasmorris | 26:83550fc299aa | 4 | while(1) |
thomasmorris | 26:83550fc299aa | 5 | { |
thomasmorris | 26:83550fc299aa | 6 | //printf("In polling\n");//Check the value |
thomasmorris | 26:83550fc299aa | 7 | if(button_up.read() == 0) |
thomasmorris | 26:83550fc299aa | 8 | { |
thomasmorris | 26:83550fc299aa | 9 | printf("Up button read\n"); |
thomasmorris | 26:83550fc299aa | 10 | Thread::wait(Debounce_Time); |
thomasmorris | 26:83550fc299aa | 11 | if(button_up.read() == 1) |
thomasmorris | 26:83550fc299aa | 12 | { |
thomasmorris | 26:83550fc299aa | 13 | printf("Up button released\n"); |
thomasmorris | 26:83550fc299aa | 14 | up_thread.signal_set(1); |
thomasmorris | 26:83550fc299aa | 15 | } |
thomasmorris | 26:83550fc299aa | 16 | } |
thomasmorris | 26:83550fc299aa | 17 | if(button_down.read() == 0) |
thomasmorris | 26:83550fc299aa | 18 | { |
thomasmorris | 26:83550fc299aa | 19 | printf("Down button read\n"); |
thomasmorris | 26:83550fc299aa | 20 | Thread::wait(Debounce_Time); |
thomasmorris | 26:83550fc299aa | 21 | if(button_down.read() == 1) |
thomasmorris | 26:83550fc299aa | 22 | { |
thomasmorris | 26:83550fc299aa | 23 | down_thread.signal_set(1); |
thomasmorris | 26:83550fc299aa | 24 | } |
thomasmorris | 26:83550fc299aa | 25 | } |
thomasmorris | 26:83550fc299aa | 26 | if(button_start.read() == 0) |
thomasmorris | 26:83550fc299aa | 27 | { |
thomasmorris | 26:83550fc299aa | 28 | printf("Start/Stop button read\n"); |
thomasmorris | 26:83550fc299aa | 29 | Thread::wait(Debounce_Time); |
thomasmorris | 26:83550fc299aa | 30 | if(button_start.read() == 1) |
thomasmorris | 26:83550fc299aa | 31 | { |
thomasmorris | 26:83550fc299aa | 32 | start_stop_thread.signal_set(1); |
thomasmorris | 26:83550fc299aa | 33 | } |
thomasmorris | 26:83550fc299aa | 34 | } |
thomasmorris | 26:83550fc299aa | 35 | if(button_function.read() == 0) |
thomasmorris | 26:83550fc299aa | 36 | { |
thomasmorris | 26:83550fc299aa | 37 | printf("Function button read\n"); |
thomasmorris | 26:83550fc299aa | 38 | Thread::wait(Debounce_Time); |
thomasmorris | 26:83550fc299aa | 39 | if(button_function.read() == 1) |
thomasmorris | 26:83550fc299aa | 40 | { |
thomasmorris | 26:83550fc299aa | 41 | function_thread.signal_set(1); |
thomasmorris | 26:83550fc299aa | 42 | } |
thomasmorris | 26:83550fc299aa | 43 | } |
thomasmorris | 26:83550fc299aa | 44 | if(button_select.read() == 0) |
thomasmorris | 26:83550fc299aa | 45 | { |
thomasmorris | 26:83550fc299aa | 46 | printf("Select button read\n"); |
thomasmorris | 26:83550fc299aa | 47 | Thread::wait(Debounce_Time); |
thomasmorris | 26:83550fc299aa | 48 | if(button_select.read() == 1) |
thomasmorris | 26:83550fc299aa | 49 | { |
thomasmorris | 26:83550fc299aa | 50 | select_thread.signal_set(1); |
thomasmorris | 26:83550fc299aa | 51 | } |
thomasmorris | 26:83550fc299aa | 52 | } |
thomasmorris | 26:83550fc299aa | 53 | Thread::wait(50); |
thomasmorris | 26:83550fc299aa | 54 | } |
thomasmorris | 26:83550fc299aa | 55 | } |
thomasmorris | 26:83550fc299aa | 56 | void LCD_thread()//Output data to the LCD for the interface |
thomasmorris | 26:83550fc299aa | 57 | { |
thomasmorris | 16:9f98ec0ededb | 58 | while(1) |
thomasmorris | 16:9f98ec0ededb | 59 | { |
thomasmorris | 17:68b3fdabe4c5 | 60 | lcd.cls();//Clear the LCD screen |
thomasmorris | 25:9751619fa030 | 61 | if(INTERFACE.Get_Function() == 0)//Anneal |
thomasmorris | 25:9751619fa030 | 62 | { |
thomasmorris | 25:9751619fa030 | 63 | lcd.printf("Mode: Anneal\n"); |
thomasmorris | 25:9751619fa030 | 64 | if(INTERFACE.Get_Select() == 0 || INTERFACE.Get_Select() == 1) |
thomasmorris | 25:9751619fa030 | 65 | { |
thomasmorris | 25:9751619fa030 | 66 | lcd.printf("Loop:%d On_t:%d\n",INTERFACE.Get_Loop(),INTERFACE.Get_On_Time()); |
thomasmorris | 25:9751619fa030 | 67 | } |
thomasmorris | 25:9751619fa030 | 68 | else if(INTERFACE.Get_Select() == 2) |
thomasmorris | 25:9751619fa030 | 69 | { |
thomasmorris | 25:9751619fa030 | 70 | lcd.printf("Loop:%d Offt:%d\n",INTERFACE.Get_Loop(),INTERFACE.Get_Off_Time()); |
thomasmorris | 25:9751619fa030 | 71 | } |
thomasmorris | 25:9751619fa030 | 72 | } |
thomasmorris | 25:9751619fa030 | 73 | else if(INTERFACE.Get_Function() == 1)//Test |
thomasmorris | 25:9751619fa030 | 74 | { |
thomasmorris | 25:9751619fa030 | 75 | lcd.printf("Mode: Test\n"); |
thomasmorris | 25:9751619fa030 | 76 | lcd.printf("Duty: %dTime: %d\n",INTERFACE.Get_Duty_Cycle(),INTERFACE.Get_Power_Time()); |
thomasmorris | 25:9751619fa030 | 77 | } |
thomasmorris | 25:9751619fa030 | 78 | else if(INTERFACE.Get_Function() == 2)//Coil |
thomasmorris | 16:9f98ec0ededb | 79 | { |
thomasmorris | 26:83550fc299aa | 80 | lcd.printf("Mode: Coil\n"); |
thomasmorris | 18:3523660f3930 | 81 | lcd.printf("Done:%dToDo: %d\n",INTERFACE.Get_Turns_Done(),INTERFACE.Get_Turns_To_Do()); |
thomasmorris | 16:9f98ec0ededb | 82 | } |
thomasmorris | 25:9751619fa030 | 83 | else if(INTERFACE.Get_Function() == 3)//Twist |
thomasmorris | 16:9f98ec0ededb | 84 | { |
thomasmorris | 26:83550fc299aa | 85 | lcd.printf("Mode: Twist\n"); |
thomasmorris | 25:9751619fa030 | 86 | lcd.printf("Done:%dToDo: %d\n",INTERFACE.Get_Turns_Done(),INTERFACE.Get_Turns_To_Do()); |
thomasmorris | 12:d9c133b360b0 | 87 | } |
thomasmorris | 17:68b3fdabe4c5 | 88 | Thread::wait(250);//Refresh rate for Lcd |
thomasmorris | 12:d9c133b360b0 | 89 | } |
thomasmorris | 12:d9c133b360b0 | 90 | } |
thomasmorris | 17:68b3fdabe4c5 | 91 | void LED_thread(){//Led thread to toggle the on board led to show the CPU is running |
thomasmorris | 16:9f98ec0ededb | 92 | while (1) |
thomasmorris | 16:9f98ec0ededb | 93 | { |
thomasmorris | 17:68b3fdabe4c5 | 94 | led2 = !led2; //Toggle the LED |
thomasmorris | 17:68b3fdabe4c5 | 95 | Thread::wait(1000);//Wait 1 second |
emilmont | 1:491820ee784d | 96 | } |
emilmont | 1:491820ee784d | 97 | } |
thomasmorris | 12:d9c133b360b0 | 98 | //Interrupt functions |
thomasmorris | 16:9f98ec0ededb | 99 | void up_thread_function()//Action if the up button is pressed increment |
thomasmorris | 16:9f98ec0ededb | 100 | { |
thomasmorris | 14:63998be3d43c | 101 | while(1) |
thomasmorris | 14:63998be3d43c | 102 | { |
thomasmorris | 25:9751619fa030 | 103 | /* |
thomasmorris | 17:68b3fdabe4c5 | 104 | Thread::signal_wait(1); //Wait to be signaled by the up button interrupt |
thomasmorris | 21:6d9f6a986647 | 105 | Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs |
thomasmorris | 21:6d9f6a986647 | 106 | Thread::signal_wait(2); //Wait to be signaled by the up button interrupt |
thomasmorris | 25:9751619fa030 | 107 | |
thomasmorris | 25:9751619fa030 | 108 | */ |
thomasmorris | 25:9751619fa030 | 109 | Thread::signal_wait(1); //Wait to be signaled by the up button interrupt |
thomasmorris | 26:83550fc299aa | 110 | pc.printf("Up Button Pressed in thread function\n");//Output data to the putty terminal |
thomasmorris | 26:83550fc299aa | 111 | Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs this debouce time might need to be longer |
thomasmorris | 17:68b3fdabe4c5 | 112 | up_thread.signal_set(0); //Set the thread signal low to wait for the interrupt before reaccuring |
thomasmorris | 17:68b3fdabe4c5 | 113 | pc.printf("Up Button Pressed\n");//Output data to the putty terminal |
thomasmorris | 17:68b3fdabe4c5 | 114 | INTERFACE.Up(); //Run the interface up routine |
thomasmorris | 14:63998be3d43c | 115 | } |
thomasmorris | 14:63998be3d43c | 116 | } |
thomasmorris | 16:9f98ec0ededb | 117 | void down_thread_function() //Action if the down button is pressed decrement |
thomasmorris | 16:9f98ec0ededb | 118 | { |
thomasmorris | 14:63998be3d43c | 119 | while(1) |
thomasmorris | 14:63998be3d43c | 120 | { |
thomasmorris | 26:83550fc299aa | 121 | /* |
thomasmorris | 26:83550fc299aa | 122 | Thread::signal_wait(1); //Wait to be signaled by the down button interrupt |
thomasmorris | 21:6d9f6a986647 | 123 | Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs |
thomasmorris | 26:83550fc299aa | 124 | Thread::signal_wait(2); //Wait to be signaled by the down button interrupt |
thomasmorris | 26:83550fc299aa | 125 | |
thomasmorris | 26:83550fc299aa | 126 | */ |
thomasmorris | 26:83550fc299aa | 127 | Thread::signal_wait(1); //Wait to be signaled by the down button interrupt |
thomasmorris | 26:83550fc299aa | 128 | Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer |
thomasmorris | 17:68b3fdabe4c5 | 129 | down_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring |
thomasmorris | 17:68b3fdabe4c5 | 130 | pc.printf("Down Button Pressed\n");//Output data to the putty terminal |
thomasmorris | 17:68b3fdabe4c5 | 131 | INTERFACE.Down();//Run the interface down routine |
thomasmorris | 12:d9c133b360b0 | 132 | } |
thomasmorris | 12:d9c133b360b0 | 133 | } |
thomasmorris | 16:9f98ec0ededb | 134 | void start_stop_thread_function() //Action if the Start/Stop button is pressed |
thomasmorris | 16:9f98ec0ededb | 135 | { |
thomasmorris | 14:63998be3d43c | 136 | while(1) |
thomasmorris | 14:63998be3d43c | 137 | { |
thomasmorris | 26:83550fc299aa | 138 | Thread::signal_wait(1); //Wait to be signaled by the start / stop button interrupt |
thomasmorris | 26:83550fc299aa | 139 | Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer |
thomasmorris | 17:68b3fdabe4c5 | 140 | start_stop_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring |
thomasmorris | 17:68b3fdabe4c5 | 141 | pc.printf("S/S Button Pressed\n");//Output data to the putty terminal |
thomasmorris | 17:68b3fdabe4c5 | 142 | INTERFACE.Start_Stop();//Run the interface start / stop routine |
thomasmorris | 26:83550fc299aa | 143 | //Thread::wait(1000);//Button debounce to prevent multiple re-runs |
thomasmorris | 14:63998be3d43c | 144 | } |
thomasmorris | 14:63998be3d43c | 145 | } |
thomasmorris | 16:9f98ec0ededb | 146 | void Function_Selection_thread_function()//Action if the Function button is pressed |
thomasmorris | 16:9f98ec0ededb | 147 | { |
thomasmorris | 14:63998be3d43c | 148 | while(1) |
thomasmorris | 14:63998be3d43c | 149 | { |
thomasmorris | 26:83550fc299aa | 150 | Thread::signal_wait(1); //Wait to be signaled by the function button interrupt |
thomasmorris | 26:83550fc299aa | 151 | Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer |
thomasmorris | 17:68b3fdabe4c5 | 152 | function_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring |
thomasmorris | 17:68b3fdabe4c5 | 153 | pc.printf("Function Button Pressed\n");//Output data to the putty terminal |
thomasmorris | 17:68b3fdabe4c5 | 154 | INTERFACE.Function();//Run the interface function routine |
thomasmorris | 26:83550fc299aa | 155 | //Thread::wait(2000);//Button debounce to prevent multiple re-runs |
emilmont | 1:491820ee784d | 156 | } |
emilmont | 1:491820ee784d | 157 | } |
thomasmorris | 16:9f98ec0ededb | 158 | void Selection_thread_function()//Action if the Select button is pressed |
thomasmorris | 16:9f98ec0ededb | 159 | { |
thomasmorris | 14:63998be3d43c | 160 | while(1) |
thomasmorris | 14:63998be3d43c | 161 | { |
thomasmorris | 26:83550fc299aa | 162 | Thread::signal_wait(1); //Wait to be signaled by the select button interrupt |
thomasmorris | 26:83550fc299aa | 163 | Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer |
thomasmorris | 17:68b3fdabe4c5 | 164 | select_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring |
thomasmorris | 17:68b3fdabe4c5 | 165 | pc.printf("Select Button Pressed\n");//Output data to the putty terminal |
thomasmorris | 17:68b3fdabe4c5 | 166 | INTERFACE.Select();//Run the interface selection routine |
thomasmorris | 26:83550fc299aa | 167 | //Thread::wait(1000);//Button debounce to prevent multiple re-runs |
thomasmorris | 14:63998be3d43c | 168 | } |
thomasmorris | 12:d9c133b360b0 | 169 | } |
thomasmorris | 12:d9c133b360b0 | 170 | int main() |
thomasmorris | 12:d9c133b360b0 | 171 | { |
thomasmorris | 12:d9c133b360b0 | 172 | //Interrupt setters |
thomasmorris | 26:83550fc299aa | 173 | |
thomasmorris | 17:68b3fdabe4c5 | 174 | |
thomasmorris | 17:68b3fdabe4c5 | 175 | lcd.printf("Ready Player\n"); |
thomasmorris | 17:68b3fdabe4c5 | 176 | lcd.printf(" One \n"); |
thomasmorris | 17:68b3fdabe4c5 | 177 | Thread::wait(1000); |
thomasmorris | 17:68b3fdabe4c5 | 178 | pc.printf("Program start\n");//Outputs informtation to the putty terminal |
thomasmorris | 14:63998be3d43c | 179 | |
thomasmorris | 14:63998be3d43c | 180 | //Thread Starts |
thomasmorris | 26:83550fc299aa | 181 | polling_thread.start(Polling_thread_function);//Start the button polling |
thomasmorris | 17:68b3fdabe4c5 | 182 | lcd_thread.start(LCD_thread);//Output data to LCD |
thomasmorris | 17:68b3fdabe4c5 | 183 | led_thread.start(LED_thread);//Blinking led to show CPU running |
thomasmorris | 17:68b3fdabe4c5 | 184 | up_thread.start(up_thread_function);//UP interface thread |
thomasmorris | 17:68b3fdabe4c5 | 185 | down_thread.start(down_thread_function);//Down interface thread |
thomasmorris | 17:68b3fdabe4c5 | 186 | start_stop_thread.start(start_stop_thread_function);//Start / stop interface thread |
thomasmorris | 17:68b3fdabe4c5 | 187 | function_thread.start(Function_Selection_thread_function);//Function interface thread |
thomasmorris | 17:68b3fdabe4c5 | 188 | select_thread.start(Selection_thread_function);//Start interface thread |
mwthewsey | 19:384642f39496 | 189 | |
thomasmorris | 23:07a368f2cdb1 | 190 | 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 |
thomasmorris | 24:728de4bf961e | 191 | INTERFACE.Interface_Init(); |
thomasmorris | 17:68b3fdabe4c5 | 192 | while(1)// Main code in a main thread |
thomasmorris | 12:d9c133b360b0 | 193 | { |
thomasmorris | 17:68b3fdabe4c5 | 194 | INTERFACE.Interface_main();//Run main thread code |
mwthewsey | 19:384642f39496 | 195 | Thread::wait(10);//Small delay to save CPU time |
thomasmorris | 16:9f98ec0ededb | 196 | } |
thomasmorris | 12:d9c133b360b0 | 197 | } |