Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Revision:
52:99915f5240b2
Parent:
51:47f5db68500b
Child:
53:71f59e195f06
--- a/main.cpp	Tue Jan 09 15:15:08 2018 +0000
+++ b/main.cpp	Tue Jan 09 22:27:49 2018 +0000
@@ -1,138 +1,96 @@
 /*
 ELEC 351 Group T
 Team Members : Christopher Hills, Thomas Morris
-Current Verision 23
-Overiew: Working Tasks 1,2,3,5,6,7,8,9,10,11,12,13,14,15
-EASTER EGG IS HERE!!
+Christopher Student Number : 10467175 
+Thomas Student Number : 10427070
 */
-
-
 #include "SETUP.hpp"
-#include "NETWORK.hpp"
-#include "FIFO.hpp"
-#include "SD_CARD.hpp"
-#include "SERIAL_COMMANDS.hpp"
-#include "SAMPLE.hpp"
-#include "THREADS.hpp"
-void SD_Card()//Writes data to the SD card
+
+//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(1);
-        Thread::wait(Sample_Rate*1000); //Waits until a new is taken before checking again
-        SD_Card_Write();
+        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();
+        Thread::wait(NetworkWaitTime);      //Waits Network Wait amount of time
+        Networking();                       //Write to the Network
     }
 }
 void LCD_Output()
 {
     while(1)
     {
-        Thread::wait(10);//Small wait
-        LCD_Print_Output();
+        LCD_Print_Output();                 //Writes to the LCD
     }
 }
 void Serial_Commands()
 {
     while(1)
     {
-        Serial_Commands_Output();   
+        Serial_Commands_Output();           //Enable Serial Commands
     }
-
 }
 void LED_Logging()
 {
     while(1)
     {
-        if(Log_Value == 0)
-        {
-            Thread::wait(3000);
-        }
-        else if(Log_Value == 1)
-        {
-            Yellow_led.flash(0.2);
-            Thread::wait(2800);
-        }
-        else if(Log_Value == 2)
-        {
-            Yellow_led.flash(0.2);
-            Thread::wait(200);
-            Yellow_led.flash(0.2);
-            Thread::wait(2400);
-        } 
-        else if(Log_Value == 3)
-        {
-            Yellow_led.flash(0.2);
-            Thread::wait(200);
-            Yellow_led.flash(0.2);
-            Thread::wait(200);
-            Yellow_led.flash(0.2);
-            Thread::wait(2000);
-        } 
-        else if(Log_Value == 4)
-        {
-            Yellow_led.flash(0.2);
-            Thread::wait(200);
-            Yellow_led.flash(0.2);
-            Thread::wait(200);
-            Yellow_led.flash(0.2);
-            Thread::wait(200);
-            Yellow_led.flash(0.2);
-            Thread::wait(1600);
-        } 
-        else if(Log_Value == 5)
-        {
-            Yellow_led.flash(0.2);
-            Thread::wait(200);
-            Yellow_led.flash(0.2);
-            Thread::wait(200);
-            Yellow_led.flash(0.2);
-            Thread::wait(200);
-            Yellow_led.flash(0.2);
-            Thread::wait(200);
-            Yellow_led.flash(0.2);
-            Thread::wait(1200);
-        } 
-        else
-        {
-            Log_Value = 0;   
-        }
+       Log_Leds();                          //Flashes the yellow led to indicate the logging mode
     }
 }
 void Sample()
 {
-    Sample_Event();   
+    Sample_Event();                         //Run Sample Event
 }
 int main()
 {
-    set_time(1515352584);               //Set RTC time to December 10 2017
-    
-    pc.baud(9600);                      //Sets the Serial Comms Baud Rate
+    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
 
-    LCD.Initialise();
-    LCD.DDRAM_Address(0x00);
- 
-    post();     //Power on Self Test
-
-    Sample_Rate = TimerInterval;
-    //Run interrupt
-    Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
-
+    //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
@@ -145,23 +103,31 @@
 
     while(true) 
     {
-        if(SD_CARD_DETECT.read() == 1)
+        if(SD_CARD_DETECT.read() == 1)              //Check if SD Card has been manually ejected
         {
-            t2.signal_set(DONT_WRITE_TO_SD_CARD);
-            errorCode(SD_CARD_REMOVED);
+            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)
+        else if(SD_CARD_DETECT.read() == 0)         //Check if SD Card is present
         {
-            t2.signal_set(SD_CARD_WRITE);
+            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(SD_CARD_WRITE);   
+            t2.signal_set(DONT_WRITE_TO_SD_CARD);   //Else Dont Write to SD Card an Error has occured   
         }
         if(onBoardSwitch == 1)
         {   
-            SD_Card_Eject();
-            Green_led.switchOn();
+            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 
         }
     }
 }
\ No newline at end of file