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
Diff: commander.cpp
- Revision:
- 8:e1da2ae62885
- Parent:
- 7:b6e31bfdb2af
- Child:
- 9:dc8f155b71c8
--- a/commander.cpp Mon Nov 16 18:46:31 2015 +0000 +++ b/commander.cpp Mon Nov 16 22:20:11 2015 +0000 @@ -7,9 +7,25 @@ using namespace std; +const int kCommandBufferSize = 40; +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=", "", "", "", "", "", "", ""}, + {"servos", "test", "reset", "", "", "", "", "", "", ""}, + {"port", "init", "b-rate=", "parity=", "stopbit=", "", "", "", "", ""}, +}; + Commander::Commander() { typeChar = '\0'; - typeRaw = Invalid; + typeRaw = InvalidType; + commandValueIndex = 0; + objectRaw = InvalidObject; + this->resetVariables(); } Commander::~Commander() { @@ -21,21 +37,13 @@ this->typeChar = CommandTypeValue[type]; this->readCommandObject(); + if (this->objectRaw == InvalidObject) { return; } - char nextChar = '\0'; - char commandCharArray [79] = ""; - int i = 0; - while (i < 79) { - scanf("%1c", &nextChar); - if (nextChar == '\n' || nextChar == '\r' || nextChar == ' ') { continue; } - else if (nextChar == kCommandTerminator) { break; } - commandCharArray[i] = nextChar; - i++; - } + this->readCommand(this->objectRaw); + if (this->commandValueIndex == -1) { return; } + else if ((connectedToPC == false) && (this->typeRaw != Set || this->objectRaw != PC || this->commandValueIndex != 1)) { return; } - string tempStr(commandCharArray); - this->command = tempStr; - + this->executeCommand(); return; } @@ -58,9 +66,9 @@ scanf("%1c", &nextChar); } while (nextChar != objectInitiator); - char objectCharArray [20] = ""; + char objectCharArray [kObjectBufferSize] = ""; int i = 0; - while (i < 20) { + while (i < kObjectBufferSize) { scanf("%1c", &nextChar); if (nextChar == '\n' || nextChar == '\r' || nextChar == ' ') { continue; } if (nextChar == objectTerminator) @@ -71,9 +79,118 @@ 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); + return; + } + } + + this->objectRaw = InvalidObject; + return; +} + +void Commander::readCommand(CommandObjectRaw objectRaw){ + char nextChar = '\0'; + char commandCharArray [kCommandBufferSize] = ""; + char commandValue [kCommandValueBufferSize] = ""; + + int charIndex = 0; + int valueIndex = 0; + bool commandComplete = false; + while (charIndex < kCommandBufferSize && valueIndex < kCommandValueBufferSize) { + scanf("%1c", &nextChar); + 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; } + } + + string tempCommandStr(commandCharArray); + string tempValueStr(commandValue); + + int row = objectRaw; + int column = 0; + + for (;column < kMaxCommandCount; column++){ + if (CommandObjectCommandsValue[row][column] == tempCommandStr) { + this->command = tempCommandStr; + this->commandValueIndex = column; + if (this->typeRaw == Set){ + this->commandValue = tempValueStr; + } + return; + } + } + + this->commandValueIndex = -1; return; } -//string parseReply(Command *cmd){ -// -//} +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); + } + } + 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; + } +} + +void Commander::resetVariables(){ + this->object = ""; + this->objectRaw = InvalidObject; + this->command = ""; + this->commandValue = ""; + this->commandValueIndex = 0; + this->typeRaw = InvalidType; + this->typeChar = '\0'; +}