Testing 1 blue pill

Dependencies:   mbed mbed-rtos TextLCD

main.cpp

Committer:
thomasmorris
Date:
2019-02-18
Revision:
24:728de4bf961e
Parent:
23:07a368f2cdb1
Child:
25:9751619fa030

File content as of revision 24:728de4bf961e:

#include "SETUP.hpp"


//Thread signal functions
void up_signal_rise(){up_thread.signal_set(1);}            //Sets the up thread to operate
void down_signal_rise(){down_thread.signal_set(1);}        //Sets the down thread to operate
void start_signal_rise(){start_stop_thread.signal_set(1);} //Sets the start thread to operate
void function_signal_rise(){function_thread.signal_set(1);}//Sets the function thread to operate
void select_signal_rise(){select_thread.signal_set(1);}    //Sets the select thread to operate
void up_signal_fall(){up_thread.signal_set(2);}            //Sets the up thread to operate
void down_signal_fall(){down_thread.signal_set(2);}        //Sets the down thread to operate
void start_signal_fall(){start_stop_thread.signal_set(2);} //Sets the start thread to operate
void function_signal_fall(){function_thread.signal_set(2);}//Sets the function thread to operate
void select_signal_fall(){select_thread.signal_set(2);}    //Sets the select thread to operate

void LCD_thread(){//Output data to the LCD for the interface
    while(1)
    {
        lcd.cls();//Clear the LCD screen
        if(INTERFACE.Get_Function() == 0)//Turn
        {
            lcd.printf("Mode: Turn\n");
            lcd.printf("Done:%dToDo: %d\n",INTERFACE.Get_Turns_Done(),INTERFACE.Get_Turns_To_Do());
        }
        else if(INTERFACE.Get_Function() == 1)//Anneal
        {
            lcd.printf("Mode: Anneal\n");
            lcd.printf("Loop:%d Wait:%d\n",INTERFACE.Get_Loop(),INTERFACE.Get_Wait_Time());
      
        }else if(INTERFACE.Get_Function() == 2)//Test
        {
            lcd.printf("Mode: Test\n");
            lcd.printf("Duty: %dTime: %d\n",INTERFACE.Get_Duty_Cycle(),INTERFACE.Get_Power_Time());
        }
        Thread::wait(250);//Refresh rate for Lcd
    } 
}
void LED_thread(){//Led thread to toggle the on board led to show the CPU is running
    while (1)
    {
        led2 = !led2;      //Toggle the LED
        Thread::wait(1000);//Wait 1 second
    }
}
//Interrupt functions
void up_thread_function()//Action if the up button is pressed increment
{
    while(1)
    {
        Thread::signal_wait(1);  //Wait to be signaled by the up button interrupt
        Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs
        Thread::signal_wait(2);  //Wait to be signaled by the up button interrupt
        up_thread.signal_set(0); //Set the thread signal low to wait for the interrupt before reaccuring
        pc.printf("Up Button Pressed\n");//Output data to the putty terminal
        INTERFACE.Up();     //Run the interface up routine
        Thread::wait(1000); //Button debounce to prevent multiple re-runs
    }
}
void down_thread_function() //Action if the down button is pressed decrement
{
    while(1)
    {
        Thread::signal_wait(1);  //Wait to be signaled by the up button interrupt
        Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs
        Thread::signal_wait(2);  //Wait to be signaled by the up button interrupt
        down_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
        pc.printf("Down Button Pressed\n");//Output data to the putty terminal
        INTERFACE.Down();//Run the interface down routine
         Thread::wait(1000);//Button debounce to prevent multiple re-runs
    }
}
void start_stop_thread_function() //Action if the Start/Stop button is pressed
{
    while(1)
    {
        Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs
        Thread::signal_wait(2);  //Wait to be signaled by the up button interrupt
        start_stop_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
        pc.printf("S/S Button Pressed\n");//Output data to the putty terminal
        INTERFACE.Start_Stop();//Run the interface start / stop routine
        Thread::wait(1000);//Button debounce to prevent multiple re-runs
    }
}
void Function_Selection_thread_function()//Action if the Function button is pressed
{
    while(1)
    {
        Thread::signal_wait(1);  //Wait to be signaled by the up button interrupt
        Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs
        Thread::signal_wait(2);  //Wait to be signaled by the up button interrupt
        function_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
        pc.printf("Function Button Pressed\n");//Output data to the putty terminal
        INTERFACE.Function();//Run the interface function routine
        Thread::wait(2000);//Button debounce to prevent multiple re-runs
    }
}
void Selection_thread_function()//Action if the Select button is pressed
{
    while(1)
    {
        Thread::signal_wait(1);  //Wait to be signaled by the up button interrupt
        Thread::wait(Debounce_Time); //Button debounce to prevent multiple re-runs
        Thread::signal_wait(2);  //Wait to be signaled by the up button interrupt
        select_thread.signal_set(0);//Set the thread signal low to wait for the interrupt before reaccuring
        pc.printf("Select Button Pressed\n");//Output data to the putty terminal
        INTERFACE.Select();//Run the interface selection routine
        Thread::wait(1000);//Button debounce to prevent multiple re-runs
    }
}
int main() 
{
    //Interrupt setters
    button_up.rise(&up_signal_rise);         //Sets up Up button
    button_down.rise(&down_signal_rise);     //Sets up Down Button
    button_start.rise(&start_signal_rise);  //Sets up Start/Stop Button
    button_funct.rise(&function_signal_rise);//Sets up Function Button
    button_select.rise(&select_signal_rise); //Sets up Select Button
    button_up.fall(&up_signal_fall);         //Sets up Up button
    button_down.fall(&down_signal_fall);     //Sets up Down Button
    button_start.fall(&start_signal_fall);   //Sets up Start/Stop Button
    button_funct.fall(&function_signal_fall);//Sets up Function Button
    button_select.fall(&select_signal_fall); //Sets up Select Button
    
    lcd.printf("Ready   Player\n");
    lcd.printf("     One      \n");
    Thread::wait(1000);
    pc.printf("Program start\n");//Outputs informtation to the putty terminal
    
    //Thread Starts
    lcd_thread.start(LCD_thread);//Output data to LCD
    led_thread.start(LED_thread);//Blinking led to show CPU running    
    up_thread.start(up_thread_function);//UP interface thread
    down_thread.start(down_thread_function);//Down interface thread
    start_stop_thread.start(start_stop_thread_function);//Start / stop interface thread
    function_thread.start(Function_Selection_thread_function);//Function interface thread
    select_thread.start(Selection_thread_function);//Start interface thread
    
    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
    INTERFACE.Interface_Init();
    while(1)// Main code in a main thread
    {
        INTERFACE.Interface_main();//Run main thread code
        Thread::wait(10);//Small delay to save CPU time  
    } 
}