Testing 1 blue pill

Dependencies:   mbed mbed-rtos TextLCD

Committer:
JDickson
Date:
Fri Mar 01 21:57:59 2019 +0000
Revision:
27:22d6fd88828e
Parent:
26:83550fc299aa
Child:
28:3193157ebb0c
More comments

Who changed what in which revision?

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