FINAL PROJECT isn't it

Fork of ELEC351 by Plymouth ELEC351 Group T

main.cpp

Committer:
thomasmorris
Date:
2018-01-09
Revision:
52:99915f5240b2
Parent:
51:47f5db68500b
Child:
53:71f59e195f06

File content as of revision 52:99915f5240b2:

/*
ELEC 351 Group T
Team Members : Christopher Hills, Thomas Morris
Christopher Student Number : 10467175 
Thomas Student Number : 10427070
*/
#include "SETUP.hpp"

//Interrupt service routine for handling the timeout of SW1
void SW1TimeOutHandler() {
    SW1TimeOut.detach();        //Stop the timeout counter firing
    SW1.fall(&SW1FallingEdge);  //Now wait for a falling edge
}
//Interrupt service routive for SW2 falling edge (release)
void SW1FallingEdge() {
    SW1.fall(NULL);                             //Disable this interrupt
    SW1TimeOut.attach(&SW1TimeOutHandler, SW1_SW2_Timeout_Time); //Start timeout counter    
}
//Interrupt service routine for handling the timeout of SW2
void SW2TimeOutHandler() {
    SW2TimeOut.detach();        //Stop the timeout counter firing
    SW2.fall(&SW2FallingEdge);  //Now wait for a falling edge
}
//Interrupt service routive for SW2 falling edge (release)
void SW2FallingEdge() {
    SW2.fall(NULL);                             //Disable this interrupt
    SW2TimeOut.attach(&SW2TimeOutHandler, SW1_SW2_Timeout_Time); //Start timeout counter    
}
void SD_Card()                              //Writes data to the SD card
{
    while(1)
    {
        Thread::signal_wait(SD_CARD_WRITE); //Wait till the SD Card Signal is set
        Thread::wait(Sample_Rate*1000);     //Waits until a new is taken before checking again
        SD_Card_Write();                    //Write Data to the SD Card
    }
}
void Network()
{
    while(1)
    {
        Thread::wait(NetworkWaitTime);      //Waits Network Wait amount of time
        Networking();                       //Write to the Network
    }
}
void LCD_Output()
{
    while(1)
    {
        LCD_Print_Output();                 //Writes to the LCD
    }
}
void Serial_Commands()
{
    while(1)
    {
        Serial_Commands_Output();           //Enable Serial Commands
    }
}
void LED_Logging()
{
    while(1)
    {
       Log_Leds();                          //Flashes the yellow led to indicate the logging mode
    }
}
void Sample()
{
    Sample_Event();                         //Run Sample Event
}
int main()
{
    set_time(1515530152);                                   //Sets time
    pc.baud(9600);                                          //Sets the Serial Comms Baud Rate
    LCD.Initialise();                                       //Initialise the LCD 
    LCD.DDRAM_Address(0x00);                                //Set the LCD string entry point to top left
    post();                                                 //Power on Self Test
    Sample_Rate = TimerInterval;                            //Define Sample Rate
    Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);       //Start the sampler

    //Start Threads
    t1.start(Sample);
    t2.start(SD_Card);
    t3.start(LCD_Output);
    t4.start(Network);
    t5.start(Serial_Commands);
    t6.start(LED_Logging);
    
    //Interrupts
    SW1.fall(&SW1FallingEdge);
    SW2.fall(&SW2FallingEdge);
    
    //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();
    id6 = t6.gettid();

    while(true) 
    {
        if(SD_CARD_DETECT.read() == 1)              //Check if SD Card has been manually ejected
        {
            SD_CARD_PRESENT = 0;                    //Sets the SD card to not present
            t2.signal_set(DONT_WRITE_TO_SD_CARD);   //Stop writing to SD Card
            errorCode(SD_CARD_REMOVED);             //Throws an error code
        } 
        else if(SD_CARD_DETECT.read() == 0)         //Check if SD Card is present
        {
            if(SD_CARD_PRESENT == 0)                //Checks if the SD card is set to present
            {
                SD_Init();//                        //Inits the SD card if it was not already initialised
                SD_CARD_PRESENT = 1;                //Makes SD Card Present 1 to prevent the SD init from running over and over
            }
            t2.signal_set(SD_CARD_WRITE);           //Enables the SD card to Write
        }
        else
        {
            t2.signal_set(DONT_WRITE_TO_SD_CARD);   //Else Dont Write to SD Card an Error has occured   
        }
        if(onBoardSwitch == 1)
        {   
            SD_CARD_PRESENT = 0;                    //Sets the SD card to not present
            t2.signal_set(DONT_WRITE_TO_SD_CARD);   //Stop writing to SD Card
            SD_Card_Eject();                        //Ejects the SD Card
            Green_led.switchOn();                   //Turns on the green led 
        }
    }
}