Testing 1 blue pill

Dependencies:   mbed mbed-rtos TextLCD

Committer:
thomasmorris
Date:
Mon Mar 11 14:19:26 2019 +0000
Revision:
28:3193157ebb0c
Parent:
27:22d6fd88828e
Child:
29:f3b1387c81f1
Version 16 Working

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 28:3193157ebb0c 116 pc.printf("System running value is: %d\n",INTERFACE.Get_System_Running());
thomasmorris 28:3193157ebb0c 117 if(INTERFACE.Get_System_Running() == 0)
thomasmorris 28:3193157ebb0c 118 {
thomasmorris 28:3193157ebb0c 119 INTERFACE.Up(); //Run the interface up routine
thomasmorris 28:3193157ebb0c 120 }
thomasmorris 14:63998be3d43c 121 }
thomasmorris 14:63998be3d43c 122 }
thomasmorris 16:9f98ec0ededb 123 void down_thread_function() //Action if the down button is pressed decrement
thomasmorris 16:9f98ec0ededb 124 {
thomasmorris 14:63998be3d43c 125 while(1)
thomasmorris 14:63998be3d43c 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 28:3193157ebb0c 131 if(INTERFACE.Get_System_Running() == 0)
thomasmorris 28:3193157ebb0c 132 {
thomasmorris 28:3193157ebb0c 133 INTERFACE.Down();//Run the interface down routine
thomasmorris 28:3193157ebb0c 134 }
thomasmorris 12:d9c133b360b0 135 }
thomasmorris 12:d9c133b360b0 136 }
thomasmorris 16:9f98ec0ededb 137 void start_stop_thread_function() //Action if the Start/Stop button is pressed
thomasmorris 16:9f98ec0ededb 138 {
thomasmorris 14:63998be3d43c 139 while(1)
thomasmorris 14:63998be3d43c 140 {
thomasmorris 26:83550fc299aa 141 Thread::signal_wait(1); //Wait to be signaled by the start / stop button interrupt
thomasmorris 26:83550fc299aa 142 Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer
thomasmorris 17:68b3fdabe4c5 143 start_stop_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
thomasmorris 17:68b3fdabe4c5 144 pc.printf("S/S Button Pressed\n");//Output data to the putty terminal
thomasmorris 17:68b3fdabe4c5 145 INTERFACE.Start_Stop();//Run the interface start / stop routine
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 28:3193157ebb0c 156 if(INTERFACE.Get_System_Running() == 0)
thomasmorris 28:3193157ebb0c 157 {
thomasmorris 28:3193157ebb0c 158 INTERFACE.Function();//Run the interface function routine
thomasmorris 28:3193157ebb0c 159 }
emilmont 1:491820ee784d 160 }
emilmont 1:491820ee784d 161 }
thomasmorris 16:9f98ec0ededb 162 void Selection_thread_function()//Action if the Select button is pressed
thomasmorris 16:9f98ec0ededb 163 {
thomasmorris 14:63998be3d43c 164 while(1)
thomasmorris 14:63998be3d43c 165 {
thomasmorris 26:83550fc299aa 166 Thread::signal_wait(1); //Wait to be signaled by the select button interrupt
thomasmorris 26:83550fc299aa 167 Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runsn this debounce time might need to be longer
thomasmorris 17:68b3fdabe4c5 168 select_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
thomasmorris 17:68b3fdabe4c5 169 pc.printf("Select Button Pressed\n");//Output data to the putty terminal
thomasmorris 28:3193157ebb0c 170 if(INTERFACE.Get_System_Running() == 0)
thomasmorris 28:3193157ebb0c 171 {
thomasmorris 28:3193157ebb0c 172 INTERFACE.Select();//Run the interface selection routine
thomasmorris 28:3193157ebb0c 173 }
thomasmorris 14:63998be3d43c 174 }
thomasmorris 12:d9c133b360b0 175 }
thomasmorris 12:d9c133b360b0 176 int main()
thomasmorris 28:3193157ebb0c 177 {
thomasmorris 17:68b3fdabe4c5 178 lcd.printf("Ready Player\n");
thomasmorris 17:68b3fdabe4c5 179 lcd.printf(" One \n");
thomasmorris 17:68b3fdabe4c5 180 Thread::wait(1000);
thomasmorris 17:68b3fdabe4c5 181 pc.printf("Program start\n");//Outputs informtation to the putty terminal
thomasmorris 14:63998be3d43c 182
JDickson 27:22d6fd88828e 183 //Thread Initialisation
thomasmorris 26:83550fc299aa 184 polling_thread.start(Polling_thread_function);//Start the button polling
thomasmorris 17:68b3fdabe4c5 185 lcd_thread.start(LCD_thread);//Output data to LCD
thomasmorris 17:68b3fdabe4c5 186 led_thread.start(LED_thread);//Blinking led to show CPU running
thomasmorris 17:68b3fdabe4c5 187 up_thread.start(up_thread_function);//UP interface thread
thomasmorris 17:68b3fdabe4c5 188 down_thread.start(down_thread_function);//Down interface thread
thomasmorris 17:68b3fdabe4c5 189 start_stop_thread.start(start_stop_thread_function);//Start / stop interface thread
thomasmorris 17:68b3fdabe4c5 190 function_thread.start(Function_Selection_thread_function);//Function interface thread
thomasmorris 17:68b3fdabe4c5 191 select_thread.start(Selection_thread_function);//Start interface thread
mwthewsey 19:384642f39496 192
thomasmorris 23:07a368f2cdb1 193 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 194 INTERFACE.Interface_Init();
thomasmorris 17:68b3fdabe4c5 195 while(1)// Main code in a main thread
thomasmorris 12:d9c133b360b0 196 {
thomasmorris 17:68b3fdabe4c5 197 INTERFACE.Interface_main();//Run main thread code
mwthewsey 19:384642f39496 198 Thread::wait(10);//Small delay to save CPU time
thomasmorris 16:9f98ec0ededb 199 }
thomasmorris 12:d9c133b360b0 200 }