![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Proj 324 Final
Fork of ELEC351_Group_T by
SERIAL_COMMANDS.cpp
- Committer:
- thomasmorris
- Date:
- 2018-08-15
- Revision:
- 57:aba1296e51b1
- Parent:
- 56:bc5345bc6650
File content as of revision 57:aba1296e51b1:
#include "SERIAL_COMMANDS.hpp" int NetworkWaitTime; //Waiting time to update the network char input[100] = {}; //Character array initialised to NULL int steps = 0; int direction = 1; int Motor_To_Select = 1; float angle = 0; bool move_loop = 0; bool List_move_loop = 0; //Mutex Locks 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 //LOGGING 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 SPI\n");} //Logging SPI if (NumberToLogging <= 5){Log_Value = NumberToLogging;} //If inputted value is within bounds equate it to the log state } //Motor Control else if(input[0] == 'M' & input[1] == 'o' & input[2] == 't' & input[3] == 'o' & input[4] == 'r' & input[5] == ' ') { int NumberOfChars = 0; int ArrayAddress = 0; string MotorNumber; int Motor_Number; //Declare required variables while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters entered into the console for(int x=6; x < NumberOfChars; x++){MotorNumber += input[x];} //Concatenate the characters between the space and the end stringstream Number(MotorNumber); //Convert string to stringstream Number >> Motor_Number; //Convert stringstream to integer if(Motor_Number < 1 || Motor_Number > 6) { Motor_Number =0; pc.printf("Please Select a motor from 1-6\n"); } Motor_To_Select = Motor_Number; //Step Control pc.printf("Enter the angle to rotate\n"); //Request command in the terminal scanf("%f",&angle); //cin.getline(input,sizeof(input),'\r'); //Scan into input from the start of the line to the return character //if(input[0] == 'S' & input[1] == 't' & input[2] == 'e' & input[3] == 'p' & input[4] == 's' & input[5] == ' ') { //angle = input; if(angle < 0) { direction = 0;//Anti Clockwise } else { direction = 1;//Clockwise } float new_angle = abs(angle/1.8);//Converts input to steps steps = new_angle; pc.printf("The Motor is: %d\n The Number to step is: %d\n", Motor_Number, steps); } } //MOVE else if(input[0] == 'M' & input[1] == 'O' & input[2] == 'V' & input[3] == 'E') { move_loop = 1; pc.printf("Enter the Move\n"); //Request command in the terminal while(move_loop == 1) { cin.getline(input,sizeof(input),'\r'); if(input[0] == 'A' & input[1] == 'F'){STEPPER_MOTOR_1.Rotate_Steps(50 ,1);} else if(input[0] == 'C' & input[1] == 'F'){STEPPER_MOTOR_1.Rotate_Steps(50 ,0);} else if(input[0] == 'A' & input[1] == 'B'){STEPPER_MOTOR_2.Rotate_Steps(50 ,1);} else if(input[0] == 'C' & input[1] == 'B'){STEPPER_MOTOR_2.Rotate_Steps(50 ,0);} else if(input[0] == 'A' & input[1] == 'L'){STEPPER_MOTOR_3.Rotate_Steps(50 ,1);} else if(input[0] == 'C' & input[1] == 'L'){STEPPER_MOTOR_3.Rotate_Steps(50 ,0);} else if(input[0] == 'A' & input[1] == 'R'){STEPPER_MOTOR_4.Rotate_Steps(50 ,1);} else if(input[0] == 'C' & input[1] == 'R'){STEPPER_MOTOR_4.Rotate_Steps(50 ,0);} else if(input[0] == 'A' & input[1] == 'U'){STEPPER_MOTOR_5.Rotate_Steps(50 ,1);} else if(input[0] == 'C' & input[1] == 'U'){STEPPER_MOTOR_5.Rotate_Steps(50 ,0);} else if(input[0] == 'A' & input[1] == 'D'){STEPPER_MOTOR_6.Rotate_Steps(50 ,1);} else if(input[0] == 'C' & input[1] == 'D'){STEPPER_MOTOR_6.Rotate_Steps(50 ,0);} else if(input[0] == 'M' & input[1] == 'O' & input[2] == 'V' & input[3] == 'E') { move_loop = 0; pc.printf("Exiting Move loop\n"); //Exit moves function } else { pc.printf("Error please enter a allowed move\n"); //Error case } } } //LIST else if(input[0] == 'L' & input[1] == 'I' & input[2] == 'S' & input[3] == 'T')//This is where the move list shall be added to { List_move_loop = 1; pc.printf("Entered the Move_List Section\n"); //Request command in the terminal while(List_move_loop == 1) { cin.getline(input,sizeof(input),'\r'); if(input[0] == 'A' & input[1] == 'F'){Move_list[Move_list_pointer] = 1; Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'C' & input[1] == 'F'){Move_list[Move_list_pointer] = 2; Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'A' & input[1] == 'B'){Move_list[Move_list_pointer] = 3; Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'C' & input[1] == 'B'){Move_list[Move_list_pointer] = 4; Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'A' & input[1] == 'L'){Move_list[Move_list_pointer] = 5; Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'C' & input[1] == 'L'){Move_list[Move_list_pointer] = 6; Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'A' & input[1] == 'R'){Move_list[Move_list_pointer] = 7; Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'C' & input[1] == 'R'){Move_list[Move_list_pointer] = 8; Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'A' & input[1] == 'U'){Move_list[Move_list_pointer] = 9; Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'C' & input[1] == 'U'){Move_list[Move_list_pointer] = 10;Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'A' & input[1] == 'D'){Move_list[Move_list_pointer] = 11;Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'C' & input[1] == 'D'){Move_list[Move_list_pointer] = 12;Move_list_pointer = Move_list_pointer + 1;} else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] =='T' & input[5] == 'E') { pc.printf("Delete Clear Test\n"); memset(Move_list, 0, sizeof(Move_list)); Move_list_pointer = 0; pc.printf("Move List array and pointer reset\n"); List_move_loop = 0; pc.printf("Exiting Move List loop\n"); } else if(input[0] == 'L' & input[1] == 'I' & input[2] == 'S' & input[3] == 'T') { List_move_loop = 0; pc.printf("Exiting Move List loop\n"); //Exit moves function } else if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D') { pc.printf("Printing out the move list\n"); for(int i=0;i <= Move_list_pointer; i+=1) { pc.printf("Value %d is %d\n",i,Move_list[i]); } pc.printf("Move List test passed\n"); List_move_loop = 0; pc.printf("Exiting Move List loop\n"); //Exit moves function } else if(input[0] == 'R' & input[1] == 'U' & input[2] == 'N') { List_move_loop = 0; { pc.printf("Move point value is : %d\n",Move_list_pointer); for(int i = 0; i<= Move_list_pointer; i += 1) { if(Move_list[i] == 0){pc.printf("Move List Finished\n");} else if (Move_list[i] == 1){STEPPER_MOTOR_1.Rotate_Steps(50 ,1);} else if (Move_list[i] == 2){STEPPER_MOTOR_1.Rotate_Steps(50 ,0);} else if (Move_list[i] == 3){STEPPER_MOTOR_2.Rotate_Steps(50 ,1);} else if (Move_list[i] == 4){STEPPER_MOTOR_2.Rotate_Steps(50 ,0);} else if (Move_list[i] == 5){STEPPER_MOTOR_3.Rotate_Steps(50 ,1);} else if (Move_list[i] == 6){STEPPER_MOTOR_3.Rotate_Steps(50 ,0);} else if (Move_list[i] == 7){STEPPER_MOTOR_4.Rotate_Steps(50 ,1);} else if (Move_list[i] == 8){STEPPER_MOTOR_4.Rotate_Steps(50 ,0);} else if (Move_list[i] == 9){STEPPER_MOTOR_5.Rotate_Steps(50 ,1);} else if (Move_list[i] == 10){STEPPER_MOTOR_5.Rotate_Steps(50 ,0);} else if (Move_list[i] == 11){STEPPER_MOTOR_6.Rotate_Steps(50 ,1);} else if (Move_list[i] == 12){STEPPER_MOTOR_6.Rotate_Steps(50 ,0);} else pc.printf("Finished\n"); } pc.printf("List Run sucessful\n"); } pc.printf("List execution finished\n"); } //TEST RUN else if(input[0] == 'T' & input[1] == 'E' & input[2] == 'S' & input[3] == 'T' & input[4] == '_' & input[5] == 'R' & input[6] == 'U' & input[7] == 'N')//This is the test run code { memset(Move_list, 0, sizeof(Move_list)); Move_list_pointer = 0; Move_list[Move_list_pointer] = 1 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 2 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 3 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 4 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 5 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 6 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 7 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 8 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 9 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 10 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 11 ;Move_list_pointer = Move_list_pointer + 1; Move_list[Move_list_pointer] = 12 ;Move_list_pointer = Move_list_pointer + 1; pc.printf("Array filled for test\n"); List_move_loop = 0; pc.printf("Exiting Move List loop\n"); } else { pc.printf("Error please enter a allowed move\n"); //Error case } } } //Colours if(input[0] == 'C' & input[1] == 'O' & input[2] == 'L' & input[3] == 'O' & input[4] == 'U' & input[5] == 'R' & input[6] == 'S' & input[7] == ' ') { int NumberOfChars = 0; int ArrayAddress = 0; string COLOURSNUBMER; int NUMBERTOCOLOUR; //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++){COLOURSNUBMER += input[x];} //Concatenate the characters between the space and the end stringstream Number(COLOURSNUBMER); //Convert string to stringstream Number >> NUMBERTOCOLOUR; int n = NUMBERTOCOLOUR -1 ; //Convert stringstream to integer pc.printf("Showing side %D\n",NUMBERTOCOLOUR); pc.printf("%c,%c,%c\n%c,%c,%c\n%c,%c,%c\n", Value_convert(CubeMap[n][0][0]),Value_convert(CubeMap[n][0][1]),Value_convert(CubeMap[n][0][2]) ,Value_convert(CubeMap[n][1][0]),Value_convert(CubeMap[n][1][1]),Value_convert(CubeMap[n][1][2]) ,Value_convert(CubeMap[n][2][0]),Value_convert(CubeMap[n][2][1]),Value_convert(CubeMap[n][2][2]));;//Side n } //SIDE_ADD if(input[0] == 'S' & input[1] == 'I' & input[2] == 'D' & input[3] == 'E' & input[4] == '_' & input[5] == 'A' & input[6] == 'D' & input[7] == 'D' & input[8] == ' ') { int NumberOfChars = 0; int ArrayAddress = 0; string SIDENUMBER; int NUMBERTOSIDE; //Declare required variables while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;} //Count the number of characters entered into the console for(int x=9; x < NumberOfChars; x++){SIDENUMBER += input[x];} //Concatenate the characters between the space and the end stringstream Number(SIDENUMBER); //Convert string to stringstream Number >> NUMBERTOSIDE; pc.printf("Please enter in the colour of segment 1:\n"); int Colour_value; cin >>Colour_value; NUMBERTOSIDE = NUMBERTOSIDE - 1; CubeMap[NUMBERTOSIDE][0][0] = convert(Colour_value); pc.printf("Please enter in the colour of segment 2:\n"); cin >>Colour_value; CubeMap[NUMBERTOSIDE][0][1] = convert(Colour_value); pc.printf("Please enter in the colour of segment 3:\n"); cin >>Colour_value; CubeMap[NUMBERTOSIDE][0][2] = convert(Colour_value); pc.printf("Please enter in the colour of segment 4:\n"); cin >>Colour_value; CubeMap[NUMBERTOSIDE][1][0] = convert(Colour_value); pc.printf("Please enter in the colour of segment 5:\n"); cin >>Colour_value; CubeMap[NUMBERTOSIDE][1][1] = convert(Colour_value); pc.printf("Please enter in the colour of segment 6:\n"); cin >>Colour_value; CubeMap[NUMBERTOSIDE][1][2] = convert(Colour_value); pc.printf("Please enter in the colour of segment 7:\n"); cin >>Colour_value; CubeMap[NUMBERTOSIDE][2][0] = convert(Colour_value); pc.printf("Please enter in the colour of segment 8:\n"); cin >>Colour_value; CubeMap[NUMBERTOSIDE][2][1] = convert(Colour_value); pc.printf("Please enter in the colour of segment 9:\n"); cin >>Colour_value; CubeMap[NUMBERTOSIDE][2][2] = convert(Colour_value); pc.printf("Side filled\n"); cin.getline(input,sizeof(input),'\r'); } //SIDE_CLEAR if(input[0] == 'S' & input[1] == 'I' & input[2] == 'D' & input[3] == 'E' & input[4] == '_' & input[5] == 'C' & input[6] == 'L' & input[7] == 'E' & input[8] == 'A' & input[9] == 'R') { for(int i =0; i<=6;i++) { CubeMap[i][0][0] = convert(0); CubeMap[i][0][1] = convert(0); CubeMap[i][0][2] = convert(0); CubeMap[i][1][0] = convert(0); CubeMap[i][1][1] = convert(0); CubeMap[i][1][2] = convert(0); CubeMap[i][2][0] = convert(0); CubeMap[i][2][1] = convert(0); CubeMap[i][2][2] = convert(0); } pc.printf("All sides cleared of data\n"); } //HELP else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P') { pc.printf("Please specify the function you would like help in\nMOTOR , LOGGING , MOVE \n"); //Print introduction line cin.getline(input,sizeof(input),'\r'); if(input[0] == 'M' & input[1] == 'O' & input[2] == 'T' & input[3] == 'O' & input[4] == 'R'){pc.printf("Motor x selects a motor in the range of 1-6\nThen you issue the angle which is a number either positive or negative\n");} else if(input[0] == 'L' & input[1] == 'O' & input[2] == 'G' & input[3] == 'G' & input[4] == 'I' & input[5] == 'N' & input[6] == 'G'){pc.printf("Logging enables to user to get more information from the specific area which is logged\n");} else if(input[0] == 'M' & input[1] == 'O' & input[2] == 'V' & input[3] == 'E'){pc.printf("Allows the user to enter in a movement\n");} else if(input[0] == 'L' & input[1] == 'I' & input[2] == 'S' & input[3] == 'T'){pc.printf("Allows the user to to view / set a list of movements that can be performed\n");} else if(input[0] == 'C' & input[1] == 'O' & input[2] == 'L' & input[3] == 'O' & input[4] == 'U' & input[5] == 'R' & input[6] == 'S'){pc.printf("Allows the user to view and set the information upon the cubemap\n");} else{pc.printf("Enter the specific help area for more information\n");} } else { pc.printf("Please enter an acceptable command\n"); } }