Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Revision:
16:067916791a25
Parent:
15:c1592fc1a501
Child:
17:37d883f40c3d
diff -r c1592fc1a501 -r 067916791a25 main.cpp
--- a/main.cpp	Sun Dec 24 14:15:57 2017 +0000
+++ b/main.cpp	Sun Dec 24 15:23:14 2017 +0000
@@ -25,6 +25,7 @@
 #define TimerInterval 1 //This is in seconds
 #define EDGE_RISEN 1
 #define EDGE_FALLEN 0
+#define mailsize 5
 
 
 Serial pc(USBTX, USBRX);
@@ -51,8 +52,6 @@
 //Threads
 Thread t1;
 Thread t2;
-Thread t3;
-Thread t4;
 Thread t5;
 Thread t6;
 
@@ -70,7 +69,7 @@
 
 int mode = 0;
 
-Mail<DATA, 120> mail_box;       //Mail Queue, Type DATA, Capacity 120, name mail_box
+Mail<DATA, mailsize> mail_box;       //Mail Queue, Type DATA, Capacity 120, name mail_box
 
 void Network()                                  //Interrupt service routine for handling the timeout
 {
@@ -85,6 +84,8 @@
         msg_network.set_temperature(Rec_Data_Network->get_temperature());
         msg_network.set_pressure(Rec_Data_Network->get_pressure());
         msg_network.set_light(Rec_Data_Network->get_light());
+        
+        mail_box.free(Rec_Data_Network);
     
         //NETWORK_Print();     //Runs the network
     }                                                
@@ -108,12 +109,14 @@
         msg_sd.set_temperature(Rec_Data_SD->get_temperature());
         msg_sd.set_pressure(Rec_Data_SD->get_pressure());
         msg_sd.set_light(Rec_Data_SD->get_light());
+        
+        mail_box.free(Rec_Data_SD);
     }    
 }
 //Interrupt service routive for SW1 falling edge (release)
 void SW1FallingEdge() {
     SW1.fall(NULL);                             //Disable this interrupt
-    Yellow_led.Toggle();                         //Toggle LED  
+    Yellow_led.Toggle();                        //Toggle LED  
     
     mode = mode +1;//Cycles through modes
     if(mode >1)
@@ -128,7 +131,7 @@
 {
     while(1){
 
-        Thread::wait(1000);
+        //Thread::wait(1000);
         
         osEvent evt_lcd = mail_box.get();                        //Get the latest entry from "mail_box"
         
@@ -142,6 +145,7 @@
             msg_lcd.set_pressure(Rec_Data_LCD->get_pressure());
             msg_lcd.set_light(Rec_Data_LCD->get_light());
 
+            mail_box.free(Rec_Data_LCD);
             
             if(mode == 0)//Print values to the LCD
             {
@@ -165,30 +169,6 @@
     }
 }
 
-void PrintTime()
-{
-    Thread::signal_wait(Print_Time_to_LCD);
-    while(1)
-    {
-        //lcd.printf("Current Time: \n %s", buffer);
-        Thread::wait(1000);//Waits the thread for 1 second
-    }
-}
-void Time()
-{
-    while (true) 
-    {
-        time_t seconds = time(NULL);
-        //pc.printf("Time as seconds since January 1, 1970 = %d\n", seconds);       
-        //pc.printf("Time as a basic string = %s", ctime(&seconds));
-        //strftime(buffer, 32, "%I:%M %p\n\r", localtime(&seconds));
-        //pc.printf("Current Time:%s", buffer);
-        
-        Thread::wait(1000);
-    }   
-}
-
-
 void Serial_Comms()//Thread for Serial Communications
 {
     while(1)
@@ -205,6 +185,8 @@
             msg_serial.set_pressure(Rec_Data_Serial->get_pressure());
             msg_serial.set_light(Rec_Data_Serial->get_light());  
             
+            mail_box.free(Rec_Data_Serial);
+            
             time_t scom_time = msg_serial.get_time();
             strftime(scom_time_buffer, 32, "%I:%M %p\t", localtime(&scom_time));
             pc.printf("Time = %s", scom_time_buffer);
@@ -214,7 +196,7 @@
             pc.printf("Light = %f\n\r", msg_serial.get_light());               //Print Light
         }
         Green_led.Toggle();
-        Thread::wait(1000);   
+        //Thread::wait(1000);   
     }
 }
 
@@ -233,12 +215,12 @@
         temp = sensor.getTemperature();         //Read Temperature
         pressure = sensor.getPressure();        //Read Pressure
         lux = adcIn.read();                     //Read Light
-        time_t buffer_time = time(NULL);                   //Read Time
+        time_t buffer_time = time(NULL);        //Read Time
         
-        DATA *Send_Data = mail_box.alloc();           //Allocate a block from the memory pool, Type Data
+        DATA *Send_Data = mail_box.alloc();     //Allocate a block from the memory pool, Type Data
         
-        if (Send_Data ==  NULL){                     //If Data is empty
-            //pc.printf("Out of memory\n\r");     //Print out of memory warning
+        if (Send_Data ==  NULL){                //If Data is empty
+            //pc.printf("Out of memory\n\r");   //Print out of memory warning
             return;
         }
         
@@ -262,7 +244,9 @@
 int main() 
 {  
     //Greeting
-    printf("Test Start\n\n\r");    
+    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
     
@@ -301,9 +285,7 @@
     
     t1.start(Sample);
     t2.start(Serial_Comms);
-    t3.start(Time);
-    //t4.start(PrintTime);
-    //t5.start(ModeSelection);
+    t5.start(ModeSelection);
     //t6.start(Network);
     //Main thread ID
     
@@ -312,8 +294,6 @@
     //Thread ID
     id1 = t1.gettid();
     id2 = t2.gettid();
-    id3 = t3.gettid();
-    id4 = t4.gettid();
     //id5 = t5.gettid();
     
     //Toggle Green LED after a button has been pressed