Plymouth ELEC351 Group T / Mbed OS ELEC351

Dependencies:   BME280 BMP280 TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SERIAL_COMMANDS.cpp Source File

SERIAL_COMMANDS.cpp

00001 #include "SERIAL_COMMANDS.hpp"
00002 int NetworkWaitTime;
00003 char input[100] = {};
00004 
00005 Ticker Console_Output_Timer;
00006 Ticker Sampling_Timer;//Controls Sampling
00007 
00008 void Serial_Commands_Output()//Used for getting input from the user to determine the opperations to perform
00009 {
00010         
00011         if(Log_Value == 4){pc.printf("In Serial_Commands\n");}   
00012          for (int x = 0; x < 100; x++){input[x] = ' ';};
00013         
00014         pc.printf("Please type in a command\n");//Log this 
00015         
00016         cin.getline(input,sizeof(input),'\r');
00017         
00018         //READ ALL
00019         if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ' & input[5] == 'A' & input[6] == 'L' & input[7] == 'L')
00020         {
00021             if(Log_Value == 4){pc.printf("READ ALL Confirmed\n");}
00022             
00023             Data_Buffer_Lock.lock();//Lock data buffer due to critical section
00024             int Start_Address_Read_All = Write_Pointer;
00025             Data_Buffer_Lock.unlock();//unLock data buffer due to critical section
00026              
00027             for(int x_Read_All = (Start_Address_Read_All - mailsize); x_Read_All != Start_Address_Read_All; x_Read_All = x_Read_All )
00028             {
00029                 if(x_Read_All < 0){x_Read_All = x_Read_All + mailsize;}
00030                 else              {x_Read_All = x_Read_All;}
00031                 if(Log_Value == 4){pc.printf("Address Reading From: %d\n",x_Read_All);}
00032                 DATA Serial_Read_Data = Read_Data(x_Read_All);
00033                 
00034                 time_t Time = Serial_Read_Data.get_time();
00035                 tm* Time_Pointer = localtime(&Time);
00036                
00037                 pc.printf("Date = %02d %02d %d ,\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));
00038                 pc.printf("Time = %02d:%02d:%02d ,\t", Time_Pointer->tm_hour, Time_Pointer->tm_min, Time_Pointer->tm_sec);                                           //Print the string formatted time
00039                 pc.printf("Temperature = %f ,\t", Serial_Read_Data.get_temperature());                     //Print Temperature
00040                 pc.printf("Pressure = %f ,\t", Serial_Read_Data.get_pressure());                           //Print Pressure
00041                 pc.printf("Light = %f ,\n", Serial_Read_Data.get_light());                               //Print Light
00042 
00043                 if(x_Read_All == mailsize - 1){x_Read_All = 0;}
00044                 else                          {x_Read_All = x_Read_All + 1;}
00045             }
00046         }
00047         
00048         //DELETE ALL FIX THIS - Might need to disable sampling during the delete
00049         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')
00050         {   
00051             if(Log_Value == 4){pc.printf("DELETE ALL Confirmed\n");}
00052             
00053             Data_Buffer_Lock.lock();
00054             int Start_Address = Write_Pointer;
00055             Data_Buffer_Lock.unlock();
00056              
00057             for(int x_Delete_All = (Start_Address - mailsize); x_Delete_All != Start_Address; x_Delete_All = x_Delete_All)
00058             {
00059                 if(Log_Value == 4){pc.printf("Address Deleting From: %d\n",x_Delete_All);}
00060                 if(x_Delete_All < 0){x_Delete_All = x_Delete_All + mailsize;}
00061                 else                {x_Delete_All = x_Delete_All;}
00062                 
00063                 Delete_Data(x_Delete_All);   
00064                 
00065                 if(x_Delete_All == mailsize - 1){x_Delete_All = 0;}
00066                 else                            {x_Delete_All = x_Delete_All + 1;}
00067 
00068             }
00069             pc.printf("DELETED %d RECORDS\n",mailsize);
00070         }
00071         
00072         //READ
00073         else if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ')
00074         {
00075             if(Log_Value == 4){pc.printf("READ n Confirmed\n");}
00076             int NumberOfChars = 0; int ArrayAddress = 0; string ReadNumber; int NumberToRead;
00077             while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
00078             for(int x=5; x < NumberOfChars; x++){ReadNumber += input[x];}
00079             stringstream Number(ReadNumber);
00080             Number >> NumberToRead;
00081             if(Log_Value == 4){pc.printf("Number of samples to Read %d\n",NumberToRead);}
00082             if(NumberToRead > mailsize)
00083             {
00084                 NumberToRead = mailsize;
00085             }
00086             Data_Buffer_Lock.lock();//Lock data buffer due to critical section
00087             int Start_Address = Write_Pointer;
00088             Data_Buffer_Lock.unlock();//unLock data buffer due to critical section
00089              
00090             for(int x = (Start_Address - NumberToRead); x != Start_Address; x = x )
00091             {
00092                 if(Log_Value == 4){pc.printf("Address Reading From: %d\n",x);}
00093                 if(x < 0){x = x + mailsize;}
00094                 else     {x = x;}
00095                 
00096                 DATA Serial_Read_Data = Read_Data(x);
00097                 
00098                 time_t Time = Serial_Read_Data.get_time();
00099                 tm* Time_Pointer = localtime(&Time);
00100         
00101                 pc.printf("Date = %02d %02d %d ,\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));
00102                 pc.printf("Time = %02d:%02d:%02d ,\t", Time_Pointer->tm_hour, Time_Pointer->tm_min, Time_Pointer->tm_sec);//Print the string formatted time
00103                 pc.printf("Temperature = %f ,\t", Serial_Read_Data.get_temperature());                     //Print Temperature
00104                 pc.printf("Pressure = %f ,\t", Serial_Read_Data.get_pressure());                           //Print Pressure
00105                 pc.printf("Light = %f ,\n", Serial_Read_Data.get_light());                               //Print Light
00106     
00107                 if(x == mailsize - 1){x = 0;}
00108                 else                 {x = x + 1;}
00109             }
00110             if(Log_Value == 4){pc.printf("Read %d samples\n",NumberToRead);}
00111         } 
00112         
00113         //DELETE look into this mgiht need to turn off sampling
00114         else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ')
00115         {
00116             if(Log_Value == 4){pc.printf("DELETE N Confirmed\n");}
00117             int NumberOfChars = 0; int ArrayAddress = 0; string DeleteNumber; int NumberToDelete;
00118             while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
00119             for(int x=7; x < NumberOfChars; x++){DeleteNumber += input[x];}
00120             stringstream Number(DeleteNumber);
00121             Number >> NumberToDelete;
00122             
00123             if(NumberToDelete > mailsize)
00124             {
00125                 NumberToDelete = mailsize;
00126             }
00127             if(Log_Value == 4){pc.printf("Deleted %d samples\n",NumberToDelete);}
00128             
00129             Data_Buffer_Lock.lock();
00130             int Start_Address = Write_Pointer;
00131             Data_Buffer_Lock.unlock();
00132              
00133             for(int x_Delete = (Start_Address - NumberToDelete); x_Delete != Start_Address; x_Delete = x_Delete )
00134             {
00135                 if(Log_Value == 4){pc.printf("Address Deleting From: %d\n",x_Delete);}
00136                 if(x_Delete < 0){x_Delete = x_Delete + mailsize;}
00137                 else            {x_Delete = x_Delete;}
00138                 
00139                 Delete_Data(x_Delete);   
00140                 
00141                 if(x_Delete == mailsize - 1){x_Delete = 0;}
00142                 else                        {x_Delete = x_Delete + 1;}
00143 
00144             }
00145         pc.printf("DELETED %d RECORDS\n",NumberToDelete);
00146         if(Log_Value == 4){pc.printf("Deleted %d samples\n",NumberToDelete);}
00147         }
00148         
00149         //STATE
00150         else if(input[0] == 'S' & input[1] == 'T' & input[2] == 'A' & input[3] == 'T' & input[4] == 'E' & input[5] == ' ')
00151         {
00152             if(Log_Value == 4){pc.printf("STATE Confirmed\n");}
00153             int NumberOfChars = 0; int ArrayAddress = 0; string StateNumber; int NumberToState;
00154             while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
00155             for(int x=6; x < NumberOfChars; x++){StateNumber += input[x];}
00156             stringstream Number(StateNumber);
00157             Number >> NumberToState;
00158             if(Log_Value == 4){pc.printf("STATE Set to %d\n",NumberToState);}
00159             
00160             if(NumberToState==0)        {Sampling_Timer.detach(); Console_Output_Timer.detach(); pc.printf("SAMPLING 0\n");}//Stop Sampling
00161             else if(NumberToState==1)   {Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);       pc.printf("SAMPLING 1\n");}//Start Sampling
00162             else                        {pc.printf("Invalid State\n");}
00163             
00164         } 
00165         
00166         //SETDATE
00167         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] == ' ')
00168         {
00169             if(Log_Value == 4){pc.printf("SETDATE Confirmed\n");}
00170             //int NumberOfChars = 0; int ArrayAddress = 0; 
00171             string DayNumber, MonthNumber, YearNumber;
00172             int NumberToDay, NumberToMonth, NumberToYear;
00173             
00174             for(int x=8; x < 10; x++){DayNumber += input[x];}
00175             stringstream Number_1(DayNumber);
00176             Number_1 >> NumberToDay;
00177             
00178             for(int x=11; x < 13; x++){MonthNumber += input[x];}
00179             stringstream Number_2(MonthNumber);
00180             Number_2 >> NumberToMonth;
00181             
00182             for(int x=14; x < 18; x++){YearNumber += input[x];}
00183             stringstream Number_3(YearNumber);
00184             Number_3 >> NumberToYear;
00185             if(Log_Value == 4){pc.printf("Year Input %d Month Input %d Day Input %d\n",NumberToYear,NumberToMonth,NumberToDay);}
00186             set_new_date(NumberToDay,NumberToMonth,NumberToYear);
00187         pc.printf("DATE UPDATED TO %02d %02d %d\n",NumberToDay,NumberToMonth,NumberToYear);
00188         }
00189         
00190         //SETTIME
00191         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] == ' ')
00192         {
00193             if(Log_Value == 4){pc.printf("SETTIME Confirmed\n");}
00194             //int NumberOfChars = 0; int ArrayAddress = 0; 
00195             string HourNumber, MinuteNumber, SecondNumber;
00196             int NumberToHour, NumberToMinute, NumberToSecond;
00197             
00198             for(int x=8; x < 10; x++){HourNumber += input[x];}
00199             stringstream Number_1(HourNumber);
00200             Number_1 >> NumberToHour;
00201             
00202             for(int x=11; x < 13; x++){MinuteNumber += input[x];}
00203             stringstream Number_2(MinuteNumber);
00204             Number_2 >> NumberToMinute;
00205             
00206             for(int x=14; x < 16; x++){SecondNumber += input[x];}
00207             stringstream Number_3(SecondNumber);
00208             Number_3 >> NumberToSecond;
00209             if(Log_Value == 4){pc.printf("Hour Input %d Minute Input %d Seconds Input %d",NumberToHour,NumberToMinute,NumberToSecond);}
00210             set_new_time(NumberToHour,NumberToMinute,NumberToSecond); 
00211             pc.printf("TIME UPDATED TO %02d %02d %02d\n",NumberToHour,NumberToMinute,NumberToSecond);   
00212         } 
00213         
00214         //SETT
00215         else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == ' ')
00216         {
00217             if(Log_Value == 4){pc.printf("SETT Confirmed\n");}
00218             int NumberOfChars = 0; int ArrayAddress = 0; 
00219             string SettNumber; double NumberToSett; 
00220             int Decimal_Divider = 10; 
00221             float Sample_Rate_Integer = 0; string Sample_Rate_Integer_String;
00222             float Sample_Rate_Decimal = 0; string Sample_Rate_Decimal_String;
00223             while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
00224          
00225                         
00226             if (Decimal_Check(input) == true)
00227             {
00228                 for(int x = 5; x < Decimal_Position(input); x++){Sample_Rate_Integer_String += input[x];}
00229                 stringstream Number_1(Sample_Rate_Integer_String);
00230                 Number_1 >> Sample_Rate_Integer;
00231         
00232                 for(int x = Decimal_Position(input) + 1; x < NumberOfChars; x++){Sample_Rate_Decimal_String += input[x];}
00233                 stringstream Number_2(Sample_Rate_Decimal_String);
00234                 Number_2 >> Sample_Rate_Decimal;
00235                 
00236                 for(int tens_power = 1; tens_power != NumberOfChars - Decimal_Position(input) - 1; tens_power++)
00237                 {
00238                     Decimal_Divider = Decimal_Divider * 10;   
00239                 }
00240                 NumberToSett = Sample_Rate_Integer + (Sample_Rate_Decimal / float(Decimal_Divider));
00241             }
00242             else                        
00243             {
00244                 for(int x=5; x < NumberOfChars; x++){SettNumber += input[x];}
00245                 stringstream Number_3(SettNumber);
00246                 Number_3 >> NumberToSett;
00247             }
00248             
00249             if(Log_Value == 4){pc.printf("Sample Rate Input %d\n",NumberToSett);}
00250             if(Log_Value == 4){pc.printf("Decimal Rate Input %d\n",Sample_Rate_Decimal);}
00251             if(Log_Value == 4){pc.printf("Integer Rate Input %d\n",Sample_Rate_Integer);}
00252             
00253             if(NumberToSett >= 0.1 & NumberToSett <= 60)
00254             {
00255                 Sample_Rate = NumberToSett;
00256                 pc.printf("T UPDATED TO %1.1f \n",NumberToSett);
00257             }
00258             else
00259             {
00260                 Sample_Rate = Sample_Rate;//No change
00261                 pc.printf("OUT OF RANGE\n");
00262             }
00263             Sampling_Timer.detach();
00264             Console_Output_Timer.detach();
00265             Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
00266         }
00267         //LOGGING
00268         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] == ' ')
00269         {
00270             int NumberOfChars = 0; int ArrayAddress = 0; string LoggingNumber; int NumberToLogging;
00271             while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
00272             for(int x=8; x < NumberOfChars; x++){LoggingNumber += input[x];}
00273             stringstream Number(LoggingNumber);
00274             Number >> NumberToLogging;
00275             if      (NumberToLogging == 0){pc.printf("NOT LOGGING\n");}
00276             else if (NumberToLogging == 1){pc.printf("LOGGING LCD\n");}
00277             else if (NumberToLogging == 2){pc.printf("LOGGING NETWORKING\n");}
00278             else if (NumberToLogging == 3){pc.printf("LOGGING SAMPLING\n");}
00279             else if (NumberToLogging == 4){pc.printf("LOGGING SERIAL COMMANDS\n");}
00280             else if (NumberToLogging == 5){pc.printf("LOGGING SD CARD\n");}
00281             else if (NumberToLogging >= 6){pc.printf("INVALID LOGGING COMMAND\n");}
00282 
00283             if (NumberToLogging <= 5){Log_Value = NumberToLogging;}  
00284         } 
00285         
00286         //HELP
00287         else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P')// Use this to display all of the availble commands
00288         { 
00289             pc.printf("Avalible Commands are: \n");
00290             
00291             pc.printf("READ ALL\nDELETE ALL\nREAD <n>\nDELETE <n>\nSETDATE <dd> <mm> <yyyy>\nSETTIME <hh> <mm> <ss>\nSETT <T>\nSTATE <x>\nLOGGING <x>\nNETSET <n>\nHELP\n");
00292 
00293         }
00294         
00295         //NETSET
00296         else if(input[0] == 'N' & input[1] == 'E' & input[2] == 'T' & input[3] == 'S' & input[4] == 'E' & input[5] == 'T' & input[6] == ' ')// Use this to display all of the availble commands
00297         { 
00298             if(Log_Value == 4){pc.printf("NETSET confirmed\n");}
00299             int NumberOfChars = 0; int ArrayAddress = 0; string NetSetNumber; int NumberToNetSet;
00300             while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
00301             for(int x=7; x < NumberOfChars; x++){NetSetNumber += input[x];}
00302             stringstream Number(NetSetNumber);
00303             Number >> NumberToNetSet;
00304             if(Log_Value == 4){pc.printf("NETSET Number %d",NumberToNetSet);}
00305             if(NumberToNetSet < 1)
00306             {
00307                 NumberToNetSet = 1;
00308             }
00309             else
00310             {
00311                 NumberToNetSet = NumberToNetSet;   
00312             }
00313             NetworkWaitTime = (NumberToNetSet * 1000);
00314         }
00315         
00316         else 
00317         {
00318             pc.printf("Please enter an acceptable command\n");
00319         }
00320     
00321 }