Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

main.cpp

Committer:
chills
Date:
2018-01-07
Revision:
34:c0b8705f183d
Parent:
33:3b5096f0126a
Child:
35:26b0a9b55d82

File content as of revision 34:c0b8705f183d:

/*
ELEC 351 Group T
Team Members : Christopher Hills, Thomas Morris
Current Verision 15
Overiew: Working Tasks 1,2,4,5,6,7,8,10,11,12,13

Last Revision: Added Mail Box to serial
Todo:
make a mailbox within SD card, LCD, network so they can be used from within
Fix the network so it uses the mail box data.
*/

#include "SETUP.hpp"

Mutex Time_Lock_Main;

void Network()                                                                  //Interrupt service routine for handling the timeout
{

}
void LCD_Write_Year()
{
        Time_Lock_Main.lock();
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Years = 1900 + Time_Pointer->tm_year;
        Time_Lock_Main.unlock();
        stringstream ss;
        ss << Years;
        string Year_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Year");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Year_String);   
}

void LCD_Write_Month()
{
        Time_Lock_Main.lock();
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Months = 1 + Time_Pointer->tm_mon;
        Time_Lock_Main.unlock();
        stringstream ss;
        ss << Months;
        string Month_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Month");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Month_String);   
}
void LCD_Write_Day()
{
        Time_Lock_Main.lock();
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Days = Time_Pointer->tm_mday;
        Time_Lock_Main.unlock();
        stringstream ss;
        ss << Days;
        string Day_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Day");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Day_String);   
}


void LCD_Write_Hour()
{
        Time_Lock_Main.lock();
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Hours = Time_Pointer->tm_hour;
        Time_Lock_Main.unlock();
        stringstream ss;
        ss << Hours;
        string Hour_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Hour");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Hour_String);   
}
void LCD_Write_Minute()
{
        Time_Lock_Main.lock();
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Minutes = Time_Pointer->tm_min;
        Time_Lock_Main.unlock();
        stringstream ss;
        ss << Minutes;
        string Minute_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Minute");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Minute_String);   
}
void LCD_Write_Seconds()
{
        Time_Lock_Main.lock();
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Seconds = Time_Pointer->tm_sec;
        Time_Lock_Main.unlock();
        stringstream ss;
        ss << Seconds;
        string Second_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Second");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Second_String);   
}

void LCD_Output()
{      
    while(1)
    {
        Thread::wait(10);//Dont Delete
        if(mode == 0)//Default mode
        { 
            if(Log_Value == 1){cout << "In mode 0 " << endl;}
            
            Thread::wait(3000);
    
            sprintf (LCD_buffer, "%1.1f %1.1f %1.1f",Data_Active.get_temperature(),Data_Active.get_pressure(),Data_Active.get_light());//Used for converting to a sting  
             
            LCD.DDRAM_Address(0x00);
            LCD.Write_String("Temp Pres  li");
            LCD.DDRAM_Address(0x40);
            LCD.Write_String(LCD_buffer);

            Thread::wait(3000);    
            
            Time_Lock_Main.lock();
            time_t Time = Data_Active.get_time();
            tm* Time_Pointer = localtime(&Time);
            LCD.Display_Clear();
            sprintf (LCD_buffer, "%d:%d    %d,%d",Time_Pointer->tm_hour,Time_Pointer->tm_min,(Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));//Used for converting to a sting  
            Time_Lock_Main.unlock(); 
            
            LCD.DDRAM_Address(0x00);
            LCD.Write_String("Current Time:");
            LCD.DDRAM_Address(0x40);
            LCD.Write_String(LCD_buffer);
            
            if(SW1.read() & SW2.read() == 1)
            {
                mode = 1;   
            }
        }
        else if(mode == 1)//Choose either date setting or time setting
        {
            if(Log_Value == 1){cout << "In Mode 1" << endl;}
            LCD.Display_Clear();
            while(1)
            {
                LCD.DDRAM_Address(0x00);
                LCD.Write_String("Date        Time");
                LCD.DDRAM_Address(0x40);
                LCD.Write_String("<              >");
                Thread::wait(1000);
                if(SW1.read() == 1)
                {
                    mode = 2;
                    break;   
                }
                if(SW2.read() == 1)
                {
                    mode = 5;
                    break;
                }
            }
        } 
        else if(mode == 2)//Set the Year
        {
            if(Log_Value == 1){cout << "In Mode 2" << endl;}
            LCD.Display_Clear();
            while(1)
            {          
                LCD_Write_Year();
                Thread::wait(1000);
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 3;
                    break;   
                }
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Year();
                }
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Year();   
                }
                LCD_Write_Year();
            }
        } 
        else if(mode == 3)//Set the Month
        {
            if(Log_Value == 1){cout << "In Mode 3" << endl;}
            LCD.Display_Clear();
            while(1)
            {
                LCD_Write_Month();
                Thread::wait(1000);
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 4;
                    break;   
                }                
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Month();
                }
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Month();   
                }                

            }
        }
        else if(mode == 4)//Set the Day
        {
            if(Log_Value == 1){cout << "In Mode 4" << endl;}
            LCD.Display_Clear();
            while(1)
            {      
                LCD_Write_Day();
                Thread::wait(1000);
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 0;   
                    break;
                }
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Day();
                }
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Day();   
                }                
            }
        } 
        else if(mode == 5)//Set the Hour
        {
            if(Log_Value == 1){cout << "In Mode 5" << endl;}
            LCD.Display_Clear();
            while(1)
            {
                LCD_Write_Hour();
                Thread::wait(1000);
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 6;  
                    break; 
                }                
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Hour();
                }
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Hour();   
                }
            }
        } 
        else if(mode == 6)//Set the Minute
        {
            if(Log_Value == 1){cout << "In Mode 6" << endl;}
            LCD.Display_Clear();
            while(1)
            {
                LCD_Write_Minute();
                Thread::wait(1000);
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 7;  
                    break; 
                }                
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Minute();
                }
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Minute();   
                }                
            }
        }  
        else if(mode == 7)//Set the Seconds
        {
            if(Log_Value == 1){cout << "In Mode 7" << endl;}
            LCD.Display_Clear();
            while(1)
            {
                LCD_Write_Seconds();
                Thread::wait(1000);
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 0;  
                    break; 
                }                
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Second();
                }
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Second();   
                }                
            }
        }
        else
        {
            mode == 0;
        }
    }
}
void Serial_Commands()//Used for getting input from the user to determine the opperations to perform
{
    string Serial_Input;
    int A,B,C;
    while(1) {
        Serial_Input = "Blank";
        cout << "Please type in a command" << endl;
        
        cin >> Serial_Input >> A >> B >> C;

        if (Serial_Input == "Test")
        {
            cout << "Test Confirmed" << endl;
            cout << A << " " << B << " " << C << endl;
            cout << "This is the time " << get_current_time() << endl;
        } 
        else if(Serial_Input == "READALL")
        {
            cout << "Read All Confirmed " << Sample_Rate << endl;
            Console_Output_Timer.attach(&Console_Output_ISR,Sample_Rate);
        } 
        else if(Serial_Input == "DELETEALL")
        {   
        Sampling_Timer.detach();
        Console_Output_Timer.detach();
        for (int x = 0; x < mailsize; x++)
        {

        }
        Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
        cout << "Sampling Restarted" << endl;
        } 
        else if(Serial_Input == "READ") 
        {
            for (int x = 0; x < A; x++)
            {
                Serial_Comms_Stealth_Mode();
                if(Log_Value == 1){cout << "Read " << x << " samples" << endl;}
            }
        } 
        else if(Serial_Input == "DELETE")
        {
            Sampling_Timer.detach();
            Console_Output_Timer.detach();
            for (int x = 0; x < A; x++){
                if(Log_Value == 1){cout << "Deleted " << x << " samples" << endl;}
            }
            Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
            cout << "Sampling Restarted" << endl;
        } 
        else if(Serial_Input == "SETDATE")
        {
            set_new_date(A,B,C);
        }
        else if(Serial_Input == "SETTIME")
        {
            set_new_time(A,B,C);    
        } 
        else if(Serial_Input == "SETT")
        {
            if(A >= 1){Sample_Rate = A;}
             
            Sampling_Timer.detach();
            Console_Output_Timer.detach();
            Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
        } 
        else if(Serial_Input == "STATE")
        {   
            if(A==0){Sampling_Timer.detach(); Console_Output_Timer.detach();}
            if(A==1){Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);}
        } 
        else if(Serial_Input == "LOGGING")
        {
            Log_Value = A;
        } 
        else if(Serial_Input == "HELP")// Use this to display all of the availble commands
        { 
            HELP();
        }
        else if(Serial_Input == "STOP")
        {
            Console_Output_Timer.detach(); //Stops Sampling   
        }
         else if(Serial_Input == "Blank Blank")
        {
           cout << "Blank Blank complete" << endl;
        }
        else 
        {
            cout << "Please enter an acceptable command" << endl;
        }
    }
}
void Serial_Comms_Stealth_Mode()                                                        //Change this name
{
    time_t Time = Data_Active.get_time();
    tm* Time_Pointer = localtime(&Time);
    strftime(scom_time_buffer, 32, "%I:%M %p\t", Time_Pointer);                         //Format time as a string
    pc.printf("Date = %d %d %d\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));
    pc.printf("Time = %s", scom_time_buffer);                                           //Print the string formatted time
    pc.printf("Temperature = %f\t", Data_Active.get_temperature());                     //Print Temperature
    pc.printf("Pressure = %f\t", Data_Active.get_pressure());                           //Print Pressure
    pc.printf("Light = %f\n\r", Data_Active.get_light());                               //Print Light
    
    Green_led.Toggle();
    
}
void Serial_Comms()//Thread for Serial Communications
{
    while(1) {
        Thread::signal_wait(SerialCommsTime);//Stupid 0 Bug
        Serial_Comms_Stealth_Mode();
    }
}

void Console_Output_ISR()   {t2.signal_set(SerialCommsTime);}
void Sampling_ISR()         {t1.signal_set(SamplingTime);}


void Sample()//Samples the hardware and prints the result to the LCD
{
    while(1) 
    {
        Thread::signal_wait(SamplingTime);      //Set the time between samples

        Data_Active.set_temperature(sensor.getTemperature());   //Read Temperature
        Data_Active.set_pressure(sensor.getPressure());         //Read Pressure
        Data_Active.set_light(adcIn.read());                    //Read Light
        
        Time_Lock_Main.lock();
        time_t buffer_time = time(NULL);        
        Time_Lock_Main.unlock();

        Data_Active.set_time(buffer_time);

        Red_led.Toggle();
        t1.signal_set(NotSamplingTime);
    }
}
int main()
{
    //Greeting
    pc.printf("Test Start");
    pc.printf("\n\r");

    set_time(1512940530);               //Set RTC time to December 10 2017
    
    pc.baud(9600);                      //Sets the Serial Comms Baud Rate

    LCD.Initialise();
    LCD.DDRAM_Address(0x00);
 
    post();     //Power on Self Test

    //Initialise the SD card (this needs to move)
    if ( sd.init() != 0) {
        printf("Init failed \n");
        LCD.Display_Clear();
        LCD.Write_String("CANNOT INIT SD");        //Change me
        errorCode(FATAL);
    }
    //Create a filing system for SD Card
    FATFileSystem fs("sd", &sd);

    //Open to WRITE
    FILE* fp = fopen("/sd/test.csv","a");
    if (fp == NULL) {
        error("Could not open file for write\n");
        LCD.Display_Clear();
        LCD.Write_String("CANNOT OPEN FILE\n\n");//Change me
        errorCode(FATAL);
    }
    //Last message before sampling begins
    LCD.Display_Clear();
    LCD.Write_String("Ready Player");
    LCD.DDRAM_Address(0x40);
    LCD.Write_String("One");
    LCD.DDRAM_Address(0x00);


    Sample_Rate = TimerInterval;
    //Run interrupt
    Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);

    t1.start(Sample);
    t2.start(Serial_Comms);
    t3.start(LCD_Output);
    //t4.start(Network);
    t5.start(Serial_Commands);

    //Main thread ID

    idMain = osThreadGetId();   //CMSIS RTOS call

    //Thread ID
    id1 = t1.gettid();
    id2 = t2.gettid();
    id3 = t3.gettid();
    id4 = t4.gettid();
    id5 = t5.gettid();

    while (onBoardSwitch == 0) {

    }

    //Close File
    /*
    fclose(fp);

    //Close down
    sd.deinit();
    printf("Unmounted...\n");
    lcd.cls();
    lcd.printf("Unmounted...\n\n");
    */

    while(true) {
        greenLED = 1;
        wait(0.5);
        greenLED = 0;
        wait(0.1);
    }
}