Proj 324 Final

Fork of ELEC351_Group_T by Plymouth ELEC351 Group T

Revision:
38:8d86e0d8a816
Parent:
37:7c4d7f206039
Child:
39:5c499989d2b9
--- a/main.cpp	Sun Jan 07 19:21:11 2018 +0000
+++ b/main.cpp	Sun Jan 07 21:08:34 2018 +0000
@@ -12,9 +12,49 @@
 
 #include "SETUP.hpp"
 #include "NETWORK.hpp"
+#include "FIFO.hpp"
+
+#define Do_Read_Data 1
+#define Dont_Read_Data 0
+#define Do_Delete_Data 1
+#define Dont_Delete_Data 0
 
 Mutex Time_Lock_Main;
- 
+Mutex Data_Buffer_Lock;
+
+int Write_Data(DATA Data_Store, int Write_Pointer)
+{
+    Data_Buffer_Lock.lock();//Mutex Lock
+    
+    Data_Buffer[Write_Pointer] = Data_Store;
+    if(Write_Pointer < mailsize - 1)
+    {
+        Write_Pointer = Write_Pointer + 1;
+    }
+    else
+    {
+        Write_Pointer = 0;   
+    }
+    Data_Buffer_Lock.unlock();
+    return Write_Pointer;
+    
+}
+DATA Read_Data(int Read_Pointer)
+{
+    Thread::signal_wait(Do_Read_Data);
+    Data_Buffer_Lock.lock();
+    DATA Temp_Data = Data_Buffer[Read_Pointer];
+    Data_Buffer_Lock.unlock();
+    
+    return Temp_Data;
+}
+void Delete_Data(int Delete_Pointer)
+{
+    Thread::signal_wait(Do_Delete_Data);
+    Data_Buffer_Lock.lock();
+    Data_Buffer[Delete_Pointer].set_all_zero();
+    Data_Buffer_Lock.unlock();
+}
 void Network()
 {
     while(1)
@@ -376,85 +416,162 @@
 void Serial_Commands()//Used for getting input from the user to determine the opperations to perform
 {
     
-    string Serial_Input;
-    int A,B,C;//Place a thread wait here?
+    char input[100];
     while(1) {
+        
         if(Log_Value == 1){cout<<"In Serial_Commands"<<endl;}   
-        Serial_Input = "Blank";
+         for (int x = 0; x < 100; x++){input[x] = ' ';};
+        
         cout << "Please type in a command" << endl;//Log this 
         
-        cin >> Serial_Input >> A >> B >> C;
-
-        if (Serial_Input == "Test")
+        cin.getline(input,sizeof(input),'\r');
+        
+        //READ ALL FIX THIS
+        if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ' & input[5] == 'A' & input[6] == 'L' & input[7] == 'L' & input[8] == ' ')
         {
-            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")
+            if(Log_Value == 1){cout<<"READ ALL Confirmed"<<endl;}
+        }
+        
+        //DELETE ALL FIX THIS
+        else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ' & input[7] == 'A' & input[8] == 'L' & input[9] == 'L' & input[10] == ' ')
         {   
             Sampling_Timer.detach();
             Console_Output_Timer.detach();
-            for (int x = 0; x < mailsize; x++)//cout deleted or something
-            {
-    
-            }
-        Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
-        cout << "Sampling Restarted" << endl;
+            Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
+            cout << "Sampling Restarted" << endl;
+            if(Log_Value == 1){cout<<"DELETE ALL Confirmed"<<endl;}
+        }
+        
+        //READ look into this
+        else if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ')
+        {
+            int NumberOfChars = 0; int ArrayAddress = 0; string ReadNumber; int NumberToRead;
+            while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
+            for(int x=5; x < NumberOfChars; x++){ReadNumber += input[x];}
+            stringstream Number(ReadNumber);
+            Number >> NumberToRead;
+            
+            Serial_Comms_Data_Get();
+            if(Log_Value == 1){cout << "Read " << NumberToRead << " samples" << endl;}
+            
         } 
-        else if(Serial_Input == "READ") 
-        {
-            for (int x = 0; x < A; x++)
-            {
-                Serial_Comms_Data_Get();
-                if(Log_Value == 1){cout << "Read " << x << " samples" << endl;}
-            }
-        } 
-        else if(Serial_Input == "DELETE")
+        
+        //DELETE look into this
+        else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ')
         {
             Sampling_Timer.detach();
             Console_Output_Timer.detach();
-            for (int x = 0; x < A; x++){
-                if(Log_Value == 1){cout << "Deleted " << x << " samples" << endl;}
-            }
+            
+            
+            int NumberOfChars = 0; int ArrayAddress = 0; string DeleteNumber; int NumberToDelete;
+            while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
+            for(int x=7; x < NumberOfChars; x++){DeleteNumber += input[x];}
+            stringstream Number(DeleteNumber);
+            Number >> NumberToDelete;
+            
+            if(Log_Value == 1){cout << "Deleted " << NumberToDelete << " 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")
+        
+        //STATE
+        else if(input[0] == 'S' & input[1] == 'T' & input[2] == 'A' & input[3] == 'T' & input[4] == 'E' & input[5] == ' ')
         {
-            set_new_time(A,B,C);    
+            int NumberOfChars = 0; int ArrayAddress = 0; string StateNumber; int NumberToState;
+            while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
+            for(int x=6; x < NumberOfChars; x++){StateNumber += input[x];}
+            stringstream Number(StateNumber);
+            Number >> NumberToState;
+            
+            if(NumberToState==0){Sampling_Timer.detach(); Console_Output_Timer.detach();}
+            if(NumberToState==1){Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);}  
         } 
-        else if(Serial_Input == "SETT")
+        
+        //SETDATE
+        else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'D' & input[4] == 'A' & input[5] == 'T' & input[6] == 'E' & input[7] == ' ' & input[10] == ' ' & input[13] == ' ')
+        {
+            int NumberOfChars = 0; int ArrayAddress = 0; 
+            string DayNumber, MonthNumber, YearNumber;
+            int NumberToDay, NumberToMonth, NumberToYear;
+            
+            for(int x=8; x < 10; x++){DayNumber += input[x];}
+            stringstream Number_1(DayNumber);
+            Number_1 >> NumberToDay;
+            
+            for(int x=11; x < 13; x++){MonthNumber += input[x];}
+            stringstream Number_2(MonthNumber);
+            Number_2 >> NumberToMonth;
+            
+            for(int x=14; x < 18; x++){YearNumber += input[x];}
+            stringstream Number_3(YearNumber);
+            Number_3 >> NumberToYear;
+            
+            set_new_date(NumberToDay,NumberToMonth,NumberToYear);
+        }
+        
+        //SETTIME
+        else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == 'I' & input[5] == 'M' & input[6] == 'E' & input[7] == ' ' & input[10] == ' ' & input[13] == ' ')
         {
-            if(A >= 1){Sample_Rate = A;}
+            int NumberOfChars = 0; int ArrayAddress = 0; 
+            string HourNumber, MinuteNumber, SecondNumber;
+            int NumberToHour, NumberToMinute, NumberToSecond;
+            
+            for(int x=8; x < 10; x++){HourNumber += input[x];}
+            stringstream Number_1(HourNumber);
+            Number_1 >> NumberToHour;
+            
+            for(int x=11; x < 13; x++){MinuteNumber += input[x];}
+            stringstream Number_2(MinuteNumber);
+            Number_2 >> NumberToMinute;
+            
+            for(int x=14; x < 16; x++){SecondNumber += input[x];}
+            stringstream Number_3(SecondNumber);
+            Number_3 >> NumberToSecond;
+            
+            set_new_time(NumberToHour,NumberToMinute,NumberToSecond);    
+        } 
+        
+        //SETT
+        else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == ' ')
+        {
+            
+            int NumberOfChars = 0; int ArrayAddress = 0; string SettNumber; int NumberToSett;
+            while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
+            for(int x=5; x < NumberOfChars; x++){SettNumber += input[x];}
+            stringstream Number(SettNumber);
+            Number >> NumberToSett;
+               
+            if(NumberToSett >= 1){Sample_Rate = NumberToSett;}
              
             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);}
+        }
+        
+        //LOGGING
+        else if(input[0] == 'L' & input[1] == 'O' & input[2] == 'G' & input[3] == 'G' & input[4] == 'I' & input[5] == 'N' & input[6] == 'G' & input[7] == ' ')
+        {
+            int NumberOfChars = 0; int ArrayAddress = 0; string LoggingNumber; int NumberToLogging;
+            while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
+            for(int x=8; x < NumberOfChars; x++){LoggingNumber += input[x];}
+            stringstream Number(LoggingNumber);
+            Number >> NumberToLogging;
+            
+            cout << "Successfully entered logging" << endl;
+            cout << NumberToLogging  << endl; 
+            Log_Value = NumberToLogging;
         } 
-        else if(Serial_Input == "LOGGING")
-        {
-            Log_Value = A;//Look into a nicer way of doing this
-        } 
-        else if(Serial_Input == "HELP")// Use this to display all of the availble commands
+        
+        //HELP
+        else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P')// Use this to display all of the availble commands
         { 
             HELP();
         }
-        else if(Serial_Input == "STOP")
+        
+        //STOP
+        else if(input[0] == 'S' & input[1] == 'T' & input[2] == 'O' & input[3] == 'P')
         {
             Console_Output_Timer.detach(); //Stops Sampling   
         }
@@ -464,7 +581,7 @@
         }
     }
 }
-void Serial_Comms_Data_Get()                                                        //Change this name
+void Serial_Comms_Data_Get()
 {
     if(Log_Value == 1){cout<<"Getting Data"<<endl;} 
     time_t Time = Data_Active.get_time();