![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
FINAL PROJECT isn't it
Fork of ELEC351 by
SERIAL_COMMANDS.cpp
- Committer:
- thomasmorris
- Date:
- 2018-01-09
- Revision:
- 52:99915f5240b2
- Parent:
- 48:244d6d81bb52
- Child:
- 53:71f59e195f06
File content as of revision 52:99915f5240b2:
#include "SERIAL_COMMANDS.hpp" int NetworkWaitTime; //Waiting time to update the network char input[100] = {}; //Character array initialised to NULL float Sample_Rate = 15.0; //Initial sampling rate Ticker Sampling_Timer; //Controls Sampling Rate //Mutex Locks Mutex SERIAL_COMMANDS_Lock; void Serial_Commands_Output() //Used for getting input from the user to determine the opperations to perform { if(Log_Value == 4){pc.printf("In Serial_Commands\n");} //If logging is enabled, print debug statement for (int x = 0; x < 100; x++){input[x] = ' ';}; //Fill input with spaces pc.printf("Please type in a command\n"); //Request command in the terminal cin.getline(input,sizeof(input),'\r'); //Scan into input from the start of the line to the return character //READ ALL if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ' & input[5] == 'A' & input[6] == 'L' & input[7] == 'L') { if(Log_Value == 4){pc.printf("READ ALL Confirmed\n");} //If logging is enabled, print debug statement SERIAL_COMMANDS_Lock.lock(); //Apply MUTEX lock int Start_Address_Read_All = Write_Pointer; //Store Write_Pointer in temporary variable SERIAL_COMMANDS_Lock.unlock(); //Release MUTEX lock for(int x_Read_All = (Start_Address_Read_All - mailsize); x_Read_All != Start_Address_Read_All; x_Read_All = x_Read_All ) //From the oldest sample to the newest sample { if(x_Read_All < 0){x_Read_All = x_Read_All + mailsize;} //If the address has gone below 0 add mailsize to it else {x_Read_All = x_Read_All;} //If the adress hasn't gone below 0 keep it the same if(Log_Value == 4){pc.printf("Address Reading From: %d\n",x_Read_All);} //If logging is enabled, print debug statement DATA Serial_Read_Data = Read_Data(x_Read_All); //Read from target address into temporary variable time_t Time = Serial_Read_Data.get_time(); //Assign the sampled time to Time tm* Time_Pointer = localtime(&Time); //Create pointer to Time pc.printf("Date = %02d %02d %d ,\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900)); //Print Date String pc.printf("Time = %02d:%02d:%02d ,\t", Time_Pointer->tm_hour, Time_Pointer->tm_min, Time_Pointer->tm_sec); //Print Time sTRING pc.printf("Temperature = %f ,\t", Serial_Read_Data.get_temperature()); //Print Temperature pc.printf("Pressure = %f ,\t", Serial_Read_Data.get_pressure()); //Print Pressure pc.printf("Light = %f ,\n", Serial_Read_Data.get_light()); //Print Light if(x_Read_All == mailsize - 1){x_Read_All = 0;} //If read address is mailsize - 1 set the read adress to 0 else {x_Read_All = x_Read_All + 1;} //If read address is not mailsize increment the read address } } //DELETE ALL 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') { Sampling_Timer.detach(); //Detach the sampling interrupt if(Log_Value == 4){pc.printf("DELETE ALL Confirmed\n");} //If logging is enabled, print debug statement SERIAL_COMMANDS_Lock.lock(); //Apply MUTEX lock int Start_Address = Write_Pointer; //Store Write_Pointer in a temporary variable SERIAL_COMMANDS_Lock.unlock(); //Release MUTEX lock for(int x_Delete_All = (Start_Address - mailsize); x_Delete_All != Start_Address; x_Delete_All = x_Delete_All) //From the oldest sample to the newest sample { if(x_Delete_All < 0){x_Delete_All = x_Delete_All + mailsize;} //If the target is less than 0 add mailsize to the target else {x_Delete_All = x_Delete_All;} //If the target is not less than 0 keep it the same if(Log_Value == 4){pc.printf("Address Deleting From: %d\n",x_Delete_All);} //If logging is enabled, print debug statement Delete_Data(x_Delete_All); //Delete DATA in the target address if(x_Delete_All == mailsize - 1){x_Delete_All = 0;} //If the target is mailsize - 1 then set the new target to 0 else {x_Delete_All = x_Delete_All + 1;} //If the target is not mailsize - 1 then increment the target } pc.printf("DELETED %d RECORDS\n",mailsize); //Print number of records which have been deleted Sampling_Timer.attach(&Sampling_ISR,Sample_Rate); //Reattach the sampling interrupt } //READ else if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ') { if(Log_Value == 4){pc.printf("READ n Confirmed\n");} //If logging is enabled, print the debug statement int NumberOfChars = 0; int ArrayAddress = 0; string ReadNumber; int NumberToRead; //Declare required variables while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters typed in the console for(int x=5; x < NumberOfChars; x++){ReadNumber += input[x];} //Concatenate characters from console into a string stringstream Number(ReadNumber); //Convert string into a stringstream Number >> NumberToRead; //Convert stringstream into an integer if(Log_Value == 4){pc.printf("Number of samples to Read %d\n",NumberToRead);} //If logging is enabled, print the debug statement if(NumberToRead > mailsize){NumberToRead = mailsize;} //If more records are chosen than exist in the buffer limit the selection to the buffer size SERIAL_COMMANDS_Lock.lock(); //Apply MUTEX lock int Start_Address = Write_Pointer; //Copy Write_Pointer into temporary variable SERIAL_COMMANDS_Lock.unlock(); //Release MUTEX lock for(int x = (Start_Address - NumberToRead); x != Start_Address; x = x ) //From newest - input number to newest { if(Log_Value == 4){pc.printf("Address Reading From: %d\n",x);} //If logging is enabled, print the debug statement if(x < 0){x = x + mailsize;} //If target is less than 0 add mailsize else {x = x;} //If target is not less than 0 keep it the same DATA Serial_Read_Data = Read_Data(x); //Read DATA from target time_t Time = Serial_Read_Data.get_time(); //Assign sampled time to Time tm* Time_Pointer = localtime(&Time); //Create a pointer to Time pc.printf("Date = %02d %02d %d ,\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900)); //Print the Date pc.printf("Time = %02d:%02d:%02d ,\t", Time_Pointer->tm_hour, Time_Pointer->tm_min, Time_Pointer->tm_sec); //Print the Time pc.printf("Temperature = %f ,\t", Serial_Read_Data.get_temperature()); //Print the Temperature pc.printf("Pressure = %f ,\t", Serial_Read_Data.get_pressure()); //Print the Pressure pc.printf("Light = %f ,\n", Serial_Read_Data.get_light()); //Print the Light if(x == mailsize - 1){x = 0;} //If target is mailsize - 1 set the new target to 0 else {x = x + 1;} //If target is not mailsize - 1 increment the target } if(Log_Value == 4){pc.printf("Read %d samples\n",NumberToRead);} //If logging is enabled, print the debug statement } //DELETE 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(); //Detach sampling interrupt if(Log_Value == 4){pc.printf("DELETE N Confirmed\n");} //If logging is enabled, print debug statement int NumberOfChars = 0; int ArrayAddress = 0; string DeleteNumber; int NumberToDelete; //Declare required variables while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters typed in the console for(int x=7; x < NumberOfChars; x++){DeleteNumber += input[x];} //Concatenate characters from console into a string stringstream Number(DeleteNumber); //Convert string into a stringstream Number >> NumberToDelete; //Convert stringstream into an integer if(NumberToDelete > mailsize){NumberToDelete = mailsize;} //If more records are chosen than exist in the buffer limit the selection to the buffer size if(Log_Value == 4){pc.printf("Deleted %d samples\n",NumberToDelete);} //If logging is enabled, print debug statement SERIAL_COMMANDS_Lock.lock(); //Apply MUTEX lock int Start_Address = Write_Pointer; //Copy Write_Pointer into temporary variable SERIAL_COMMANDS_Lock.unlock(); //Release MUTEX lock for(int x_Delete = (Start_Address - NumberToDelete); x_Delete != Start_Address; x_Delete = x_Delete ) //From newest - input number to newest { if(Log_Value == 4){pc.printf("Address Deleting From: %d\n",x_Delete);} //If logging is enabled, print debug statement if(x_Delete < 0){x_Delete = x_Delete + mailsize;} //If target is less than 0 add mailsize else {x_Delete = x_Delete;} //If target is not less than 0 keep it the same Delete_Data(x_Delete); //Delete DATA at specified address if(x_Delete == mailsize - 1){x_Delete = 0;} //If target is mailsize - 1 set target to 0 else {x_Delete = x_Delete + 1;} //If target is not mailsize - 1 increment the target } pc.printf("DELETED %d RECORDS\n",NumberToDelete); //Print number of records which have been deleted if(Log_Value == 4){pc.printf("Deleted %d samples\n",NumberToDelete);} //If logging is enabled, print the debug statement Sampling_Timer.attach(&Sampling_ISR,Sample_Rate); //Attach sampling interrupt } //STATE else if(input[0] == 'S' & input[1] == 'T' & input[2] == 'A' & input[3] == 'T' & input[4] == 'E' & input[5] == ' ') { if(Log_Value == 4){pc.printf("STATE Confirmed\n");} //If logging is enabled, print debug statement int NumberOfChars = 0; int ArrayAddress = 0; string StateNumber; int NumberToState; //Declare required variables while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters typed in the console for(int x=6; x < NumberOfChars; x++){StateNumber += input[x];} //Concatenate characters from console into a string stringstream Number(StateNumber); //Convert string into a stringstream Number >> NumberToState; //Convert stringstream into an integer if(Log_Value == 4){pc.printf("STATE Set to %d\n",NumberToState);} //If logging is enabled, print debug statement if(NumberToState==0) {Sampling_Timer.detach(); pc.printf("SAMPLING 0\n");} //If input is 0 stop sampling else if(NumberToState==1) {Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);pc.printf("SAMPLING 1\n");} //If input is 1 start sampling else {pc.printf("Invalid State\n");} //If other input received return error message } //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] == ' ') { if(Log_Value == 4){pc.printf("SETDATE Confirmed\n");} //If logging is enabled, print debug statement string DayNumber, MonthNumber, YearNumber; //Declare required variables int NumberToDay, NumberToMonth, NumberToYear; //Declare required variables for(int x=8; x < 10; x++){DayNumber += input[x];} //Concatenate characters from the first space to the second stringstream Number_1(DayNumber); //Convert string to stringstream Number_1 >> NumberToDay; //Convert stringstream to integer for(int x=11; x < 13; x++){MonthNumber += input[x];} //Concatenate characters from the second space to the third stringstream Number_2(MonthNumber); //Convert string to stringstream Number_2 >> NumberToMonth; //Convert stringstream to integer for(int x=14; x < 18; x++){YearNumber += input[x];} //Concatenate characters from the third space to the end stringstream Number_3(YearNumber); //Convert string to stringstream Number_3 >> NumberToYear; //Convert stringstream to string if(Log_Value == 4){pc.printf("Year Input %d Month Input %d Day Input %d\n",NumberToYear,NumberToMonth,NumberToDay);} //If logging is enabled, print debug statement set_new_date(NumberToDay,NumberToMonth,NumberToYear); //Set New Date pc.printf("DATE UPDATED TO %02d %02d %d\n",NumberToDay,NumberToMonth,NumberToYear); //Print Updated Date } //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(Log_Value == 4){pc.printf("SETTIME Confirmed\n");} //If logging is enabled, print debug statement string HourNumber, MinuteNumber, SecondNumber; //Declare required variables int NumberToHour, NumberToMinute, NumberToSecond; //Declare required variables for(int x=8; x < 10; x++){HourNumber += input[x];} //Concatenate characters from the third space to the end stringstream Number_1(HourNumber); //Convert string to stringstream Number_1 >> NumberToHour; //Convert stringstream to string for(int x=11; x < 13; x++){MinuteNumber += input[x];} //Concatenate characters from the third space to the end stringstream Number_2(MinuteNumber); //Convert string to stringstream Number_2 >> NumberToMinute; //Convert stringstream to string for(int x=14; x < 16; x++){SecondNumber += input[x];} //Concatenate characters from the third space to the end stringstream Number_3(SecondNumber); //Convert string to stringstream Number_3 >> NumberToSecond; //Convert stringstream to string if(Log_Value == 4){pc.printf("Hour Input %d Minute Input %d Seconds Input %d",NumberToHour,NumberToMinute,NumberToSecond);} //If logging is enabled, print debug statement set_new_time(NumberToHour,NumberToMinute,NumberToSecond); //Set New Time pc.printf("TIME UPDATED TO %02d %02d %02d\n",NumberToHour,NumberToMinute,NumberToSecond); //Print Updated Time } //SETT else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == ' ') { if(Log_Value == 4){pc.printf("SETT Confirmed\n");} //If logging is enabled, print debug statement int NumberOfChars = 0; int ArrayAddress = 0; //Declare required variables string SettNumber; double NumberToSett; //Declare required variables int Decimal_Divider = 10; //Declare required variables float Sample_Rate_Integer = 0; string Sample_Rate_Integer_String; //Declare required variables float Sample_Rate_Decimal = 0; string Sample_Rate_Decimal_String; //Declare required variables while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters entered into the console if (Decimal_Check(input) == true) //If a decimal is present { for(int x = 5; x < Decimal_Position(input); x++){Sample_Rate_Integer_String += input[x];} //Concatenate characters from the space to the decimal stringstream Number_1(Sample_Rate_Integer_String); //Convert string to a stringstream Number_1 >> Sample_Rate_Integer; //Convert stringstream to a integer for(int x = Decimal_Position(input) + 1; x < NumberOfChars; x++){Sample_Rate_Decimal_String += input[x];} //Concatenate characters from the decimal to the end stringstream Number_2(Sample_Rate_Decimal_String); //Convert string to a stringstream Number_2 >> Sample_Rate_Decimal; //Convert stringstream to a integer for(int tens_power = 1; tens_power != NumberOfChars - Decimal_Position(input) - 1; tens_power++) //For each position after the decimal place { Decimal_Divider = Decimal_Divider * 10; //Multiply the divider by another power of 10 } NumberToSett = Sample_Rate_Integer + (Sample_Rate_Decimal / float(Decimal_Divider)); //Divide the number after the decimal by the number of characters after the decimal } else //If a decimal is not present { for(int x=5; x < NumberOfChars; x++){SettNumber += input[x];} //Concatenate characters from the space to the end stringstream Number_3(SettNumber); //Convert string to a stringstream Number_3 >> NumberToSett; //Convert stringstream to an integer } if(Log_Value == 4){pc.printf("Sample Rate Input %d\n",NumberToSett);} //If logging is enabled, print debug statement if(Log_Value == 4){pc.printf("Decimal Rate Input %d\n",Sample_Rate_Decimal);} //If logging is enabled, print debug statement if(Log_Value == 4){pc.printf("Integer Rate Input %d\n",Sample_Rate_Integer);} //If logging is enabled, print debug statement if(NumberToSett >= 0.1 & NumberToSett <= 60) //If inputted number is between 0.1 and 60.0 { Sample_Rate = NumberToSett; //Set the sample rate to that input number pc.printf("T UPDATED TO %1.1f \n",NumberToSett); //Print updated sample rate } else //If inputted number is not within bounds { Sample_Rate = Sample_Rate; //Don't change the sample rate pc.printf("OUT OF RANGE\n"); //Print out of range error warning } Sampling_Timer.detach(); //Detach the sampling interrupt Sampling_Timer.attach(&Sampling_ISR,Sample_Rate); //Reattach the sampling interrupt with the new 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; //Declare required variables while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters entered into the console for(int x=8; x < NumberOfChars; x++){LoggingNumber += input[x];} //Concatenate the characters between the space and the end stringstream Number(LoggingNumber); //Convert string to stringstream Number >> NumberToLogging; //Convert stringstream to integer if (NumberToLogging == 0){pc.printf("NOT LOGGING\n");} //Not Logging else if (NumberToLogging == 1){pc.printf("LOGGING LCD\n");} //Logging LCD else if (NumberToLogging == 2){pc.printf("LOGGING NETWORKING\n");} //Logging Networking else if (NumberToLogging == 3){pc.printf("LOGGING SAMPLING\n");} //Logging Sampling else if (NumberToLogging == 4){pc.printf("LOGGING SERIAL COMMANDS\n");} //Logging serial commands else if (NumberToLogging == 5){pc.printf("LOGGING SD CARD\n");} //Logging SD card else if (NumberToLogging >= 6){pc.printf("INVALID LOGGING COMMAND\n");} //Invalud Logging Command if (NumberToLogging <= 5){Log_Value = NumberToLogging;} //If inputted value is within bounds equate it to the log state } //HELP else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P') { pc.printf("Avalible Commands are: \n"); //Print introduction line pc.printf("READ ALL\nDELETE ALL\nREAD <n>\nDELETE <n>\nSETDATE <dd> <mm> <yyyy>\nSETTIME <hh> <mm> <ss>\n"); //Print list of commands pc.printf("SETT <T>\nSTATE <x>\nLOGGING <x>\t 1:LCD\t 2:Networking\t 3:Sampling\t 4:Serial Commands\t 5:SD Card\nNETSET <n>\nHELP\n"); //Print list of commands } //NETSET 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 { if(Log_Value == 4){pc.printf("NETSET confirmed\n");} int NumberOfChars = 0; int ArrayAddress = 0; string NetSetNumber; int NumberToNetSet; while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} for(int x=7; x < NumberOfChars; x++){NetSetNumber += input[x];} stringstream Number(NetSetNumber); Number >> NumberToNetSet; if(Log_Value == 4){pc.printf("NETSET Number %d",NumberToNetSet);} if(NumberToNetSet < 1) { NumberToNetSet = 1; } else { NumberToNetSet = NumberToNetSet; } NetworkWaitTime = (NumberToNetSet * 1000); } else { pc.printf("Please enter an acceptable command\n"); } }