3rd year group project. Electronic and Electrical Engineering. Heriot-Watt University. This is the code for the mbed for the Automatic Little Object Organiser (ALOO).

Dependencies:   MCP23017 TCS3472_I2C WattBob_TextLCD mbed

Revision:
12:f485796016f8
Parent:
10:16ba52f8e025
Child:
13:4f24da6e2f8e
--- a/commander.cpp	Sat Nov 21 18:52:07 2015 +0000
+++ b/commander.cpp	Sat Nov 21 20:19:54 2015 +0000
@@ -11,60 +11,61 @@
 const int kCommandValueBufferSize = 40;
 const int kObjectBufferSize = 20;
 
-string CommandObjectValue [5] = { "mbed", "pc", "colour_sensor", "servos", "port" };
-string CommandObjectCommandsValue [5][kMaxCommandCount] = {
-	{"mbed", "haz-block", "read-current-block", "", "", "", "", "", "", ""},
-	{"pc", "connect", "disconnect", "", "", "", "", "", "", "exit"},
-	{"colour_sensor", "i-time=", "preview=", "read", "", "", "", "", "", ""},
-	{"servos", "test", "reset", "", "", "", "", "", "", ""},
-	{"port", "init", "b-rate=", "parity=", "stopbit=", "", "", "", "", ""},
-};
+string CommandObjectValue [5] = { "mbed", "pc", "colour_sensor", "servos", "port" };
+string CommandObjectCommandsValue [5][kMaxCommandCount] = {
+		{"mbed", "haz-block", "read-current-block", "", "", "", "", "", "", ""},
+		{"pc", "connect", "disconnect", "", "", "", "", "", "", "exit"},
+		{"colour_sensor", "i-time", "preview", "", "", "", "", "", "", ""},
+		{"servos", "test", "reset", "", "", "", "", "", "", ""},
+		{"port", "init", "b-rate", "parity", "stopbit", "", "", "", "", ""},
+};
 
 Commander::Commander() {
-	typeChar = '\0';
-	typeRaw = InvalidType;
-	commandValueIndex = 0;
-	objectRaw = InvalidObject;
 	this->resetVariables();
 }
 
 Commander::~Commander() {
-	
+
 }
 
 void Commander::decodeCommand(CommandTypeRaw type){
+	this->resetVariables();
 	this->typeRaw = type;
 	this->typeChar = CommandTypeValue[type];
-	
+
 	this->readCommandObject();
 	if (this->objectRaw == InvalidObject) { return; }
-	
-	this->readCommand(this->objectRaw);
-	if (this->commandValueIndex == -1) { return; }
-//	else if ((connectedToPC == false) && (this->typeRaw != Set || this->objectRaw != PC || this->commandValueIndex != 1)) { return; }
-	else if (connectedToPC == true || (this->typeRaw == Set && this->objectRaw == PC && this->commandValueIndex == 1)) { this->executeCommand(); }
-	else { return; }
-}
+
+	if (this->readCommand(this->objectRaw) == false) { return; }
+	//	else if ((connectedToPC == false) && (this->typeRaw != Set || this->objectRaw != PC || this->commandIndex[0] != 1)) { return; }
+	else if (connectedToPC == true || (this->typeRaw == Set && this->objectRaw == PC && this->commandIndex[0] == 1)) { this->executeCommand(); }
+	else { return; }
+}
+
+std::string Commander::description(){
+	string str;
+	str.append("Command type:\t");
+	str.append(&this->typeChar);
+	str.append("\nCommand object:\t" + this->object);
+	
+	for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) {
+		if (this->command[i] == "") { break; }
+	str.append("Command:\t" + this->command[i].c_str() + "\nValue:\t" + this->commandValue[i].c_str() + " \n");
+	}
 
-std::string Commander::description(){
-	string str;
-	str.append("Command type:\t");
-	str.append(&this->typeChar);
-	str.append("\nCommand object:\t" + this->object + "\n" + "Command:\t\t" + this->command + " \n");
-	
 	return str;
 }
 
 void Commander::readCommandObject(){
 	char objectInitiator = '<';
 	char objectTerminator = '>';
-	
+
 	char nextChar = '\0';
-	
+
 	do {
 		nextChar = pc.getc();
 	} while (nextChar != objectInitiator);
-	
+
 	char objectCharArray [kObjectBufferSize] = "";
 	int i = 0;
 	while (i < kObjectBufferSize) {
@@ -77,7 +78,7 @@
 	}
 	string tempStr(objectCharArray);
 	this->object = tempStr;
-	
+
 	for (int i = 0; i < (sizeof(CommandObjectValue)/sizeof(*CommandObjectValue)); i++) {
 		if (CommandObjectValue[i] == this->object){
 			this->objectRaw = static_cast<CommandObjectRaw>(i);
@@ -89,109 +90,135 @@
 	return;
 }
 
-void Commander::readCommand(CommandObjectRaw objectRaw){
-	char nextChar = '\0';
-	char commandCharArray [kCommandBufferSize] = "";
-	char commandValue [kCommandValueBufferSize] = "";
+bool Commander::readCommand(CommandObjectRaw objectRaw){
+	char nextChar = '\0';
+	char commandCharArray [kMaxCommandCount - 1][79] = { "" };
+	char commandValue [kMaxCommandCount -1][79] = { "" };
 
 	int charIndex = 0;
-	int valueIndex = 0;
-	bool commandComplete = false;
-	while (charIndex < kCommandBufferSize && valueIndex < kCommandValueBufferSize) {
-		nextChar = pc.getc();
-		if (nextChar == '\n' || nextChar == '\r' || nextChar == ' ') { continue; }
-		else if (nextChar == kCommandTerminator) { break; }
-
-		if (commandComplete == false) {
-			commandCharArray[charIndex] = nextChar;
-			charIndex++;
-		}else if (commandComplete == true){
-			commandValue[valueIndex] = nextChar;
-			valueIndex++;
-		}
-
-		if (nextChar == '=') { commandComplete = true; }
-	}
+	int valueCharIndex = 0;
+	int commandValueIndex = 0;
+	bool commandComplete = false;
+	while (charIndex < 79 && valueCharIndex < 79) {
+		scanf("%1c", &nextChar);
+		if (nextChar == '\n' || nextChar == '\r' || nextChar == ' ') { continue; }
+		else if (nextChar == kCommandTerminator) { break; }
 
-	string tempCommandStr(commandCharArray);
-	string tempValueStr(commandValue);
-
-	int row = objectRaw;
-	int column = 0;
+		else if (nextChar == '=') { commandComplete = true; }
+		else if (nextChar == ',') {
+			commandComplete = false;
+			commandValueIndex++;
+			charIndex = 0;
+			valueCharIndex = 0;
+		}
+
+		if (commandComplete == false && nextChar != ',') {
+			commandCharArray[commandValueIndex][charIndex] = nextChar;
+			charIndex++;
+		}else if (commandComplete == true && nextChar != '='){
+			commandValue[commandValueIndex][valueCharIndex] = nextChar;
+			valueCharIndex++;
+		}
+
+	}
 
-	for (;column < kMaxCommandCount; column++){
-		if (CommandObjectCommandsValue[row][column] == tempCommandStr) {
-			this->command = tempCommandStr;
-			this->commandValueIndex = column;
-			if (this->typeRaw == Set){
-				this->commandValue = tempValueStr;
-			}
-			return;
-		}
-	}
+	for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) {
+		string tempCommandStr(commandCharArray[i]);
 
-	this->commandValueIndex = -1;
-	return;
-}
+		string tempCommandStr(commandCharArray);
+		if (tempCommandStr == "") { break; }
+		string tempValueStr(commandValue[i]);
+
+		int row = objectRaw;
+		int column = 1;
+		for (;column < kMaxCommandCount; column++){
+			if (CommandObjectCommandsValue[row][column] == tempCommandStr) {
+				this->command[i] = tempCommandStr;
+				this->commandIndex[i] = column;
+				this->commandValue[i] = tempValueStr;
+				break;
+			}
+		}
+		if (this->commandIndex[i] == -1) { return false; }
+	}
+
+	return true;
+}
 
 void Commander::executeCommand(){
 	switch (this->objectRaw) {
-		case MBED:
-			if (this->commandValueIndex == 1) {
-				hazBlock(this->typeRaw);
-			}else if (this->commandValueIndex == 2){
-				getCurrentBlock(this->typeRaw);
-			}
-			break;
-		case PC:
-			if (this->commandValueIndex == 1){
-				connectToPC(this->typeRaw);
-			}else if (this->commandValueIndex == 2){
-				disconnectToPC(this->typeRaw);
-			}
-			break;
-		case ColourSensor:
-			if (this->commandValueIndex == 1) {
-				int integrationTime;
-				sscanf(this->commandValue.c_str(), "%i", &integrationTime);
-				setIntegrationTime(integrationTime);
-			}else if (this->commandValueIndex == 2){
-				if (this->commandValue == "ON"){
-					previewOnPC(true);
-				}else if (this->commandValue == "OFF"){
-					previewOnPC(false);
-				}
-			}else if (this->commandValueIndex == 3){
+	case MBED:
+		for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) {
+			if (this->commandIndex[i] == -1 || this->commandIndex[i] == 0) { break; }
+			if (this->commandIndex[i] == 1) {
+				hazBlock(this->typeRaw);
+			}else if (this->commandIndex[i] == 2){
+				getCurrentBlock(this->typeRaw);
+			}
+		}
+		break;
+	case PC:
+		for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) {
+			if (this->commandIndex[i] == -1 || this->commandIndex[i] == 0) { break; }
+			if (this->commandIndex[i] == 1){
+				connectToPC(this->typeRaw);
+			}else if (this->commandIndex[i] == 2){
+				disconnectToPC(this->typeRaw);
+			}
+		}
+		break;
+	case ColourSensor:
+		for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) {
+			if (this->commandIndex[i] == -1 || this->commandIndex[i] == 0) { break; }
+			if (this->commandIndex[i] == 1) {
+				int integrationTime;
+				sscanf(this->commandValue[i].c_str(), "%i", &integrationTime);
+				setIntegrationTime(integrationTime);
+			}else if (this->commandIndex[i] == 2){
+				if (this->commandValue[i] == "ON"){
+					previewOnPC(true);
+				}else if (this->commandValue[i] == "OFF"){
+					previewOnPC(false);
+				}
+			}else if (this->commandIndex[i] == 3){
 				readColourSensor();
 			}
-			break;
-		case Servos:
-			if (this->commandValueIndex == 1){
-				testServos();
-			}else if (this->commandValueIndex == 2){
-				resetServos();
-			}
+		}
 			break;
-		case Port:
-			if (this->commandValueIndex == 1) {
-				getPortInfo();
-			}else if (this->commandValueIndex == 2){
-				int baudRate;
-				sscanf(this->commandValue.c_str(), "%i", &baudRate);
-				setPortBaudRate(baudRate);
-			}
-			break;
-		default:
-			break;
+	case Servos:
+		for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) {
+			if (this->commandIndex[i] == -1 || this->commandIndex[i] == 0) { break; }
+			if (this->commandIndex[i] == 1){
+				testServos();
+			}else if (this->commandIndex[i] == 2){
+				resetServos();
+			}
+		}
+		break;
+	case Port:
+		for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) {
+			if (this->commandIndex[i] == -1 || this->commandIndex[i] == 0) { break; }
+			if (this->commandIndex[i] == 1) {
+				getPortInfo();
+			}else if (this->commandIndex[i] == 2){
+				float baudRate;
+				sscanf(this->commandValue[i].c_str(), "%f", &baudRate);
+				setPortBaudRate(baudRate);
+			}
+		}
+		break;
+	default:
+		break;
+		}
 	}
-}
 
-void Commander::resetVariables(){
-	this->object = "";
-	this->objectRaw = InvalidObject;
-	this->command = "";
-	this->commandValue = "";
-	this->commandValueIndex = 0;
-	this->typeRaw = InvalidType;
-	this->typeChar = '\0';
-}
+
+	void Commander::resetVariables(){
+		this->object = "";
+		this->objectRaw = InvalidObject;
+		memset(this->command, 0, sizeof(this->command));
+		memset(this->commandValue, 0, sizeof(this->command));
+		memset(this->commandIndex, 0, sizeof(this->commandIndex));
+		this->typeRaw = InvalidType;
+		this->typeChar = '\0';
+	}