FINAL PROJECT isn't it

Fork of ELEC351 by Plymouth ELEC351 Group T

Committer:
thomasmorris
Date:
Mon May 07 15:44:34 2018 +0000
Revision:
53:71f59e195f06
Parent:
52:99915f5240b2
Child:
55:e0e684531825
To test version of code;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thomasmorris 47:6d128e500875 1 #include "SERIAL_COMMANDS.hpp"
thomasmorris 52:99915f5240b2 2 int NetworkWaitTime; //Waiting time to update the network
thomasmorris 52:99915f5240b2 3 char input[100] = {}; //Character array initialised to NULL
thomasmorris 53:71f59e195f06 4 int steps = 0;
thomasmorris 53:71f59e195f06 5 int direction = 1;
thomasmorris 53:71f59e195f06 6 int Motor_To_Select = 1;
thomasmorris 52:99915f5240b2 7 float Sample_Rate = 15.0; //Initial sampling rate
thomasmorris 53:71f59e195f06 8 float angle = 0;
thomasmorris 52:99915f5240b2 9 Ticker Sampling_Timer; //Controls Sampling Rate
thomasmorris 52:99915f5240b2 10 //Mutex Locks
thomasmorris 52:99915f5240b2 11
thomasmorris 52:99915f5240b2 12 Mutex SERIAL_COMMANDS_Lock;
thomasmorris 52:99915f5240b2 13 void Serial_Commands_Output() //Used for getting input from the user to determine the opperations to perform
thomasmorris 47:6d128e500875 14 {
thomasmorris 52:99915f5240b2 15 if(Log_Value == 4){pc.printf("In Serial_Commands\n");} //If logging is enabled, print debug statement
thomasmorris 47:6d128e500875 16
thomasmorris 52:99915f5240b2 17 for (int x = 0; x < 100; x++){input[x] = ' ';}; //Fill input with spaces
thomasmorris 47:6d128e500875 18
thomasmorris 52:99915f5240b2 19 pc.printf("Please type in a command\n"); //Request command in the terminal
thomasmorris 52:99915f5240b2 20 cin.getline(input,sizeof(input),'\r'); //Scan into input from the start of the line to the return character
thomasmorris 53:71f59e195f06 21 //cout << "Input is : " << input << endl;
thomasmorris 47:6d128e500875 22
thomasmorris 52:99915f5240b2 23 //READ ALL
thomasmorris 52:99915f5240b2 24 if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ' & input[5] == 'A' & input[6] == 'L' & input[7] == 'L')
thomasmorris 52:99915f5240b2 25 {
thomasmorris 52:99915f5240b2 26 if(Log_Value == 4){pc.printf("READ ALL Confirmed\n");} //If logging is enabled, print debug statement
thomasmorris 53:71f59e195f06 27
thomasmorris 53:71f59e195f06 28 }
thomasmorris 52:99915f5240b2 29 //SETDATE
thomasmorris 52:99915f5240b2 30 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] == ' ')
thomasmorris 52:99915f5240b2 31 {
thomasmorris 52:99915f5240b2 32 if(Log_Value == 4){pc.printf("SETDATE Confirmed\n");} //If logging is enabled, print debug statement
thomasmorris 52:99915f5240b2 33 string DayNumber, MonthNumber, YearNumber; //Declare required variables
thomasmorris 52:99915f5240b2 34 int NumberToDay, NumberToMonth, NumberToYear; //Declare required variables
thomasmorris 52:99915f5240b2 35
thomasmorris 52:99915f5240b2 36 for(int x=8; x < 10; x++){DayNumber += input[x];} //Concatenate characters from the first space to the second
thomasmorris 52:99915f5240b2 37 stringstream Number_1(DayNumber); //Convert string to stringstream
thomasmorris 52:99915f5240b2 38 Number_1 >> NumberToDay; //Convert stringstream to integer
thomasmorris 52:99915f5240b2 39
thomasmorris 52:99915f5240b2 40 for(int x=11; x < 13; x++){MonthNumber += input[x];} //Concatenate characters from the second space to the third
thomasmorris 52:99915f5240b2 41 stringstream Number_2(MonthNumber); //Convert string to stringstream
thomasmorris 52:99915f5240b2 42 Number_2 >> NumberToMonth; //Convert stringstream to integer
thomasmorris 52:99915f5240b2 43
thomasmorris 52:99915f5240b2 44 for(int x=14; x < 18; x++){YearNumber += input[x];} //Concatenate characters from the third space to the end
thomasmorris 52:99915f5240b2 45 stringstream Number_3(YearNumber); //Convert string to stringstream
thomasmorris 52:99915f5240b2 46 Number_3 >> NumberToYear; //Convert stringstream to string
thomasmorris 52:99915f5240b2 47
thomasmorris 52:99915f5240b2 48 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
thomasmorris 52:99915f5240b2 49
thomasmorris 52:99915f5240b2 50 set_new_date(NumberToDay,NumberToMonth,NumberToYear); //Set New Date
thomasmorris 52:99915f5240b2 51 pc.printf("DATE UPDATED TO %02d %02d %d\n",NumberToDay,NumberToMonth,NumberToYear); //Print Updated Date
thomasmorris 52:99915f5240b2 52 }
thomasmorris 52:99915f5240b2 53
thomasmorris 52:99915f5240b2 54 //SETTIME
thomasmorris 52:99915f5240b2 55 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] == ' ')
thomasmorris 52:99915f5240b2 56 {
thomasmorris 52:99915f5240b2 57 if(Log_Value == 4){pc.printf("SETTIME Confirmed\n");} //If logging is enabled, print debug statement
thomasmorris 52:99915f5240b2 58 string HourNumber, MinuteNumber, SecondNumber; //Declare required variables
thomasmorris 52:99915f5240b2 59 int NumberToHour, NumberToMinute, NumberToSecond; //Declare required variables
thomasmorris 52:99915f5240b2 60
thomasmorris 52:99915f5240b2 61 for(int x=8; x < 10; x++){HourNumber += input[x];} //Concatenate characters from the third space to the end
thomasmorris 52:99915f5240b2 62 stringstream Number_1(HourNumber); //Convert string to stringstream
thomasmorris 52:99915f5240b2 63 Number_1 >> NumberToHour; //Convert stringstream to string
thomasmorris 52:99915f5240b2 64
thomasmorris 52:99915f5240b2 65 for(int x=11; x < 13; x++){MinuteNumber += input[x];} //Concatenate characters from the third space to the end
thomasmorris 52:99915f5240b2 66 stringstream Number_2(MinuteNumber); //Convert string to stringstream
thomasmorris 52:99915f5240b2 67 Number_2 >> NumberToMinute; //Convert stringstream to string
thomasmorris 52:99915f5240b2 68
thomasmorris 52:99915f5240b2 69 for(int x=14; x < 16; x++){SecondNumber += input[x];} //Concatenate characters from the third space to the end
thomasmorris 52:99915f5240b2 70 stringstream Number_3(SecondNumber); //Convert string to stringstream
thomasmorris 52:99915f5240b2 71 Number_3 >> NumberToSecond; //Convert stringstream to string
thomasmorris 52:99915f5240b2 72
thomasmorris 52:99915f5240b2 73 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
thomasmorris 52:99915f5240b2 74
thomasmorris 52:99915f5240b2 75 set_new_time(NumberToHour,NumberToMinute,NumberToSecond); //Set New Time
thomasmorris 52:99915f5240b2 76 pc.printf("TIME UPDATED TO %02d %02d %02d\n",NumberToHour,NumberToMinute,NumberToSecond); //Print Updated Time
thomasmorris 52:99915f5240b2 77 }
thomasmorris 52:99915f5240b2 78 //LOGGING
thomasmorris 52:99915f5240b2 79 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] == ' ')
thomasmorris 52:99915f5240b2 80 {
thomasmorris 52:99915f5240b2 81 int NumberOfChars = 0; int ArrayAddress = 0; string LoggingNumber; int NumberToLogging; //Declare required variables
thomasmorris 52:99915f5240b2 82 while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters entered into the console
thomasmorris 52:99915f5240b2 83 for(int x=8; x < NumberOfChars; x++){LoggingNumber += input[x];} //Concatenate the characters between the space and the end
thomasmorris 52:99915f5240b2 84 stringstream Number(LoggingNumber); //Convert string to stringstream
thomasmorris 52:99915f5240b2 85 Number >> NumberToLogging; //Convert stringstream to integer
thomasmorris 52:99915f5240b2 86 if (NumberToLogging == 0){pc.printf("NOT LOGGING\n");} //Not Logging
thomasmorris 52:99915f5240b2 87 else if (NumberToLogging == 1){pc.printf("LOGGING LCD\n");} //Logging LCD
thomasmorris 52:99915f5240b2 88 else if (NumberToLogging == 2){pc.printf("LOGGING NETWORKING\n");} //Logging Networking
thomasmorris 52:99915f5240b2 89 else if (NumberToLogging == 3){pc.printf("LOGGING SAMPLING\n");} //Logging Sampling
thomasmorris 52:99915f5240b2 90 else if (NumberToLogging == 4){pc.printf("LOGGING SERIAL COMMANDS\n");} //Logging serial commands
thomasmorris 52:99915f5240b2 91 else if (NumberToLogging == 5){pc.printf("LOGGING SD CARD\n");} //Logging SD card
thomasmorris 52:99915f5240b2 92 else if (NumberToLogging >= 6){pc.printf("INVALID LOGGING COMMAND\n");} //Invalud Logging Command
thomasmorris 52:99915f5240b2 93
thomasmorris 52:99915f5240b2 94 if (NumberToLogging <= 5){Log_Value = NumberToLogging;} //If inputted value is within bounds equate it to the log state
thomasmorris 52:99915f5240b2 95 }
thomasmorris 53:71f59e195f06 96 //Steps
thomasmorris 53:71f59e195f06 97 /*
thomasmorris 53:71f59e195f06 98 else if(input[0] == 'S' & input[1] == 't' & input[2] == 'e' & input[3] == 'p' & input[4] == 's' & input[5] == ' ')
thomasmorris 53:71f59e195f06 99 {
thomasmorris 53:71f59e195f06 100 int NumberOfChars = 0; int ArrayAddress = 0; string SteppingNumber; int NumberToStep; //Declare required variables
thomasmorris 53:71f59e195f06 101 while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters entered into the console
thomasmorris 53:71f59e195f06 102 for(int x=6; x < NumberOfChars; x++){SteppingNumber += input[x];} //Concatenate the characters between the space and the end
thomasmorris 53:71f59e195f06 103 stringstream Number(SteppingNumber); //Convert string to stringstream
thomasmorris 53:71f59e195f06 104 Number >> NumberToStep; //Convert stringstream to integer
thomasmorris 53:71f59e195f06 105 if(NumberToStep <0)
thomasmorris 53:71f59e195f06 106 {
thomasmorris 53:71f59e195f06 107 NumberToStep =0;
thomasmorris 53:71f59e195f06 108 }
thomasmorris 53:71f59e195f06 109 steps = NumberToStep;
thomasmorris 53:71f59e195f06 110 pc.printf("Number to step is %d\n", steps);
thomasmorris 53:71f59e195f06 111 }
thomasmorris 53:71f59e195f06 112 */
thomasmorris 53:71f59e195f06 113 //Motor Control
thomasmorris 53:71f59e195f06 114 else if(input[0] == 'M' & input[1] == 'o' & input[2] == 't' & input[3] == 'o' & input[4] == 'r' & input[5] == ' ')
thomasmorris 53:71f59e195f06 115 {
thomasmorris 53:71f59e195f06 116 int NumberOfChars = 0; int ArrayAddress = 0; string MotorNumber; int Motor_Number; //Declare required variables
thomasmorris 53:71f59e195f06 117 while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters entered into the console
thomasmorris 53:71f59e195f06 118 for(int x=6; x < NumberOfChars; x++){MotorNumber += input[x];} //Concatenate the characters between the space and the end
thomasmorris 53:71f59e195f06 119 stringstream Number(MotorNumber); //Convert string to stringstream
thomasmorris 53:71f59e195f06 120 Number >> Motor_Number; //Convert stringstream to integer
thomasmorris 53:71f59e195f06 121 if(Motor_Number < 1 || Motor_Number > 6)
thomasmorris 53:71f59e195f06 122 {
thomasmorris 53:71f59e195f06 123 Motor_Number =0;
thomasmorris 53:71f59e195f06 124 pc.printf("Please Select a motor from 1-6\n");
thomasmorris 53:71f59e195f06 125 }
thomasmorris 53:71f59e195f06 126 Motor_To_Select = Motor_Number;
thomasmorris 53:71f59e195f06 127 //Step Control
thomasmorris 53:71f59e195f06 128 pc.printf("Enter the angle to rotate\n"); //Request command in the terminal
thomasmorris 53:71f59e195f06 129 scanf("%f",&angle);
thomasmorris 53:71f59e195f06 130 //cin.getline(input,sizeof(input),'\r'); //Scan into input from the start of the line to the return character
thomasmorris 53:71f59e195f06 131 //if(input[0] == 'S' & input[1] == 't' & input[2] == 'e' & input[3] == 'p' & input[4] == 's' & input[5] == ' ')
thomasmorris 53:71f59e195f06 132 {
thomasmorris 53:71f59e195f06 133 //angle = input;
thomasmorris 53:71f59e195f06 134 if(angle < 0)
thomasmorris 53:71f59e195f06 135 {
thomasmorris 53:71f59e195f06 136 direction = 0;
thomasmorris 53:71f59e195f06 137 }
thomasmorris 53:71f59e195f06 138 else
thomasmorris 53:71f59e195f06 139 {
thomasmorris 53:71f59e195f06 140 direction = 1;
thomasmorris 53:71f59e195f06 141 }
thomasmorris 53:71f59e195f06 142 float new_angle = abs(angle/1.8);
thomasmorris 53:71f59e195f06 143 steps = new_angle;
thomasmorris 53:71f59e195f06 144 pc.printf("The Motor is: %d\n The Number to step is: %d\n", Motor_Number, steps);
thomasmorris 53:71f59e195f06 145 }
thomasmorris 53:71f59e195f06 146
thomasmorris 53:71f59e195f06 147 }
thomasmorris 53:71f59e195f06 148 //Motor Direction
thomasmorris 53:71f59e195f06 149 else if(strcmp(input,"Motor") == 0)
thomasmorris 53:71f59e195f06 150 {
thomasmorris 53:71f59e195f06 151 motor_direction = 1;
thomasmorris 53:71f59e195f06 152 pc.printf("Motor direction set to clockwise\n");
thomasmorris 53:71f59e195f06 153 }
thomasmorris 53:71f59e195f06 154 else if(strcmp(input,"AntiClockwise") == 0)
thomasmorris 53:71f59e195f06 155 {
thomasmorris 53:71f59e195f06 156 motor_direction = 0;
thomasmorris 53:71f59e195f06 157 pc.printf("Motor direction set to Anti clockwise\n");
thomasmorris 53:71f59e195f06 158 }
thomasmorris 53:71f59e195f06 159 //Test
thomasmorris 53:71f59e195f06 160 else if(strcmp(input,"Test") == 0)
thomasmorris 53:71f59e195f06 161 {
thomasmorris 53:71f59e195f06 162 pc.printf("Test Passed\n");
thomasmorris 53:71f59e195f06 163 }
thomasmorris 52:99915f5240b2 164 //HELP
thomasmorris 52:99915f5240b2 165 else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P')
thomasmorris 52:99915f5240b2 166 {
thomasmorris 52:99915f5240b2 167 pc.printf("Avalible Commands are: \n"); //Print introduction line
thomasmorris 52:99915f5240b2 168 pc.printf("READ ALL\nDELETE ALL\nREAD <n>\nDELETE <n>\nSETDATE <dd> <mm> <yyyy>\nSETTIME <hh> <mm> <ss>\n"); //Print list of commands
thomasmorris 52:99915f5240b2 169 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
thomasmorris 52:99915f5240b2 170
thomasmorris 52:99915f5240b2 171 }
thomasmorris 52:99915f5240b2 172 else
thomasmorris 52:99915f5240b2 173 {
thomasmorris 52:99915f5240b2 174 pc.printf("Please enter an acceptable command\n");
thomasmorris 52:99915f5240b2 175 }
thomasmorris 53:71f59e195f06 176 }