Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Revision:
23:3c85d7f657a2
Parent:
22:eb4cc12087b2
Child:
24:7d2da96e05ad
--- a/main.cpp	Fri Dec 29 21:46:49 2017 +0000
+++ b/main.cpp	Sat Dec 30 20:55:48 2017 +0000
@@ -12,74 +12,8 @@
 */
 
 //Includes and Definitions
-#include "sample_hardware.hpp"
-#include "Networkbits.hpp"
-#include "rtos.h"
-#include "LED.hpp"
-#include "DATA.hpp"
-#include "LCD.hpp"
-#define SamplingTime 1
-#define NotSamplingTime 0
-#define Print_Time_to_LCD 1
-#define Dont_Print_Time_to_LCD 0
-#define TimerInterval 1 //This is in seconds
-#define EDGE_RISEN 1
-#define EDGE_FALLEN 0
-#define mailsize 5
-#include <iostream>
-Serial pc(USBTX, USBRX);
-//SW1+SW2 are declared as interrupt ins in sample hardware.cpp
 
-
-//Thread IDs
-osThreadId idMain;
-osThreadId id1;
-osThreadId id2;
-osThreadId id3;
-osThreadId id4;
-
-
-Timeout sw1TimeOut;//Used to prevent switch bounce
-
-LED Red_led(PE_15);
-LED Yellow_led(PB_10);
-LED Green_led(PB_11);
-
-LCD LCD(D8,D9,D4,A0,A1,A2,A3,D7,D6,D3,D1); 
-char LCD_buffer [50];
-int LCD_sprintf;
-
-//Tickers
-
-Ticker Sample_timer;
-
-//Threads
-Thread t1;
-Thread t2;
-Thread t3;
-Thread t4;
-Thread t180;//cos yolo
-
-double temp = 0;
-double pressure = 0;
-double lux = 0;
-string buffer_time = 0;
-
-char buffer[32];
-char scom_time_buffer[32];
-char msel_time_buffer[32];
-
-void SW1FallingEdge();
-void SW1TimeOutHandler();
-
-
-
-using namespace std;
-
-int mode = 0;
-
-Mail<DATA, mailsize> mail_box;       //Mail Queue, Type DATA, Capacity mailsize(defined above), name mail_box
-
+#include "SETUP.hpp"
 void Network()                                  //Interrupt service routine for handling the timeout
 {
     osEvent evt_network = mail_box.get();                        //Get the latest entry from "mail_box"
@@ -95,7 +29,7 @@
         msg_network.set_light(Rec_Data_Network->get_light());                   //Copy light from mailbox to temporary instance   
         mail_box.free(Rec_Data_Network);                                        //Free space in the mailbox (delete earliest sample taken)
     
-        networktest();
+        //networktest();//Run the network
     }                                                
 }
 
@@ -104,7 +38,7 @@
     sw1TimeOut.detach();        //Stop the timeout counter firing
     SW1.fall(&SW1FallingEdge);  //Now wait for a falling edge
 }
-void SDWrite()//End of skype chat
+void SDWrite()
 {
     osEvent evt_sd = mail_box.get();                        //Get the latest entry from "mail_box"
         
@@ -138,7 +72,7 @@
 {
     while(1){
 
-        //Thread::wait(1000);
+        Thread::wait(100);//Wait the thread
         
         osEvent evt_lcd = mail_box.get();                        //Get the latest entry from "mail_box"
         
@@ -157,7 +91,7 @@
             {
                 //Write new data to LCD (not fast!)
                                       
-                /*LCD_sprintf = */sprintf (LCD_buffer, "%1.1f %1.1f %1.1f",msg_lcd.get_temperature(),msg_lcd.get_pressure(),msg_lcd.get_light());//Used for converting to a sting
+                sprintf (LCD_buffer, "%1.1f %1.1f %1.1f",msg_lcd.get_temperature(),msg_lcd.get_pressure(),msg_lcd.get_light());//Used for converting to a sting
                 
                 //LCD.Display_Clear();
                 LCD.DDRAM_Address(0x00);
@@ -176,7 +110,7 @@
                 time_t msel_time = msg_lcd.get_time();                                  //Declare local variable for time
                 strftime(scom_time_buffer, 32, "%I:%M %p", localtime(&msel_time));     //Format time as a string
                 LCD.Display_Clear();
-                LCD_sprintf = sprintf (LCD_buffer, "%s",scom_time_buffer);
+                sprintf (LCD_buffer, "%s",scom_time_buffer);
                 LCD.DDRAM_Address(0x00);
                 LCD.Write_String("Current Time:");
                 LCD.DDRAM_Address(0x40);
@@ -190,55 +124,81 @@
         }
     }
 }
+
+void Check_Serial_Input()
+{
+    t6.signal_set(1);
+    while(1)
+    {
+        Thread::signal_wait(1);
+        Thread::wait(100);//Waits 100ms
+        cout << "In Check_Serial_Input" << endl;
+        cin >> Serial_Enter_Check;//Only run this if Seriak Commands input is finished
+        if (Serial_Enter_Check == "c")
+        {
+            t5.signal_set(SerialTime);
+            t2.signal_set(0);
+            //Run Serial Commands window and stop the printing of the last command
+            Serial_Enter_Check = "0";   
+        }
+        else
+        {
+            t6.signal_set(1);   
+        }
+    }
+       
+}
 void Serial_Commands()
 {
-    string Serial_Input;
     while(1)
     {   
-        cout << "Please type in a command" << endl;
-       
+        t5.signal_set(NotSerialTime);
+        Thread::signal_wait(SerialTime);
+        cout << "Please type in a Serial command" << endl;
+        //Mutex this
+        Sample_timer.detach();
+        t6.signal_set(0);
         cin >> Serial_Input;
-        
+        t6.signal_set(1);
         if (Serial_Input == "Test")
         {
-            
+            cout << "Testing Serial Comms" <<endl;
         }
-        else if(Serial_Input == "READ ALL")
+        else if(Serial_Input == "READALL")
         {
-        
+            Serial_timer.attach(&Serial_Comms_set,1);
         }
-        else if(Serial_Input == "DELETE ALL")
+        else if(Serial_Input == "DELETE_ALL")
         {
         
         }
-        else if(Serial_Input == "READ <n>")
+        else if(Serial_Input == "READ_<n>")
         {
         
         }
-        else if(Serial_Input == "DELETE <n>")
+        else if(Serial_Input == "DELETE_<n>")
         {
         
         }
-        else if(Serial_Input == "SETDATE <dd> <mm> <yyyy>")
+        else if(Serial_Input == "SETDATE_<dd>_<mm>_<yyyy>")
         {
         
         }
-        else if(Serial_Input == "SETT <T>")
+        else if(Serial_Input == "SETT_<T>")
         {
         
         }
-        else if(Serial_Input == "STATE <x>")
+        else if(Serial_Input == "STATE_<x>")
         {
         
         }
-        else if(Serial_Input == "LOGGING <x>")
+        else if(Serial_Input == "LOGGING_<x>")
         {
         
         }
         else if(Serial_Input == "HELP")// Use this to display all of the availble commands   
         {
-            cout << "Avalible Commands are: "<< endl;
-            cout << "READ ALL , DELETE ALL READ<n> ,DELETE<n> , SETDATE<dd> <mm> <yyyy> , SETTIME <hh> <mm> <ss>, SETT <T> , STATE <x>, LOGGING <x>, HELP"<< endl;
+            HELP();   
         }    
         else
         {
@@ -246,11 +206,15 @@
         }
     }
 }
-void Serial_Comms()//Thread for Serial Communications
+void Serial_Comms()//Thread for Serial Communications //Alter this function
 {
-    cout << "In Serial Comms" << endl;
+    //cout << "In Serial Comms" << endl;
     while(1)
     {
+        cout << "In Serial Comms" << endl;
+        Thread::signal_wait(SERIAL_COMMS);
+        //t2.signal_set(SERIAL_COMMS);
+        cout << "In Serial Comms 2" << endl;
         osEvent evt_serial = mail_box.get();                                   //Get the latest entry from "mail_box"
         
         if (evt_serial.status == osEventMail)
@@ -272,11 +236,18 @@
             pc.printf("Pressure = %f\t", msg_serial.get_pressure());           //Print Pressure
             pc.printf("Light = %f\n\r", msg_serial.get_light());               //Print Light
         }
+        t2.signal_set(NotSERIAL_COMMS);
         Green_led.Toggle();
+        
         //Thread::wait(1000);   
     }
 }
 
+void Serial_Comms_set()
+{
+    t2.signal_set(SERIAL_COMMS);
+}
+
 void Sample_signal_set()            //Sets the Signal for when to sample the sensors
 {
     t1.signal_set(SamplingTime);    //Set the sampling thread signal high
@@ -317,9 +288,6 @@
 } 
 int main() 
 {  
-    
-
-    
     //Greeting
     pc.printf("Test Start");
     pc.printf("\n\r");
@@ -328,19 +296,27 @@
     pc.baud(9600);                      //Sets the Serial Comms Baud Rate
     
     LCD.Initialise();
-    LCD.DDRAM_Address(0x40);
+    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.cls();
-        lcd.printf("CANNOT INIT SD");        //Change me
+        LCD.Display_Clear();
+        LCD.Write_String("CANNOT INIT SD");
         errorCode(FATAL);
     } 
     
+    
+    
+    
+    LCD.Display_Clear();
+                sprintf (LCD_buffer, "%s",scom_time_buffer);
+                LCD.DDRAM_Address(0x00);
+                LCD.Write_String("Current Time:");
+                LCD.DDRAM_Address(0x40);
+                LCD.Write_String(LCD_buffer);
     //Create a filing system for SD Card
     FATFileSystem fs("sd", &sd);     
 
@@ -348,28 +324,33 @@
     FILE* fp = fopen("/sd/test.csv","a");
     if (fp == NULL) {
         error("Could not open file for write\n");
-        lcd.cls();
-        lcd.printf("CANNOT OPEN FILE\n\n");//Change me
+        LCD.Display_Clear();
+        LCD.Write_String("CANNOT OPEN FILE");
         errorCode(FATAL);
     }
     
     //Last message before sampling begins
-    lcd.cls();//change me
-    lcd.printf("READY\n\n");//change me
-    
+    LCD.Display_Clear();
+    LCD.Write_String("POST");
+    LCD.DDRAM_Address(0x40);
+    LCD.Write_String("SUCCESSFUL");
+    wait(1);
+    LCD.Display_Clear();
+    LCD.Write_String("READY");
     
-    //Run interrupt
+    t5.signal_set(NotSerialTime);
+    //Run interrupts
+    SW1.fall(&SW1FallingEdge);
     Sample_timer.attach(&Sample_signal_set,TimerInterval);
-    SW1.fall(&SW1FallingEdge);
     //Run Threads
     
-    Thread t4(osPriorityAboveNormal);//Sets the Priority bigly
+    //Thread t4(osPriorityAboveNormal);//Sets the Priority bigly
     t1.start(Sample);
     t2.start(Serial_Comms);
     t3.start(ModeSelection);
     t4.start(Network);
-    
-    
+    t5.start(Serial_Commands);
+    t6.start(Check_Serial_Input);
     //Main thread ID
     
     idMain = osThreadGetId();   //CMSIS RTOS call