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
commander.cpp
- Committer:
- dreamselec
- Date:
- 2015-11-16
- Revision:
- 9:dc8f155b71c8
- Parent:
- 8:e1da2ae62885
- Child:
- 10:16ba52f8e025
File content as of revision 9:dc8f155b71c8:
// // commander.cpp // Created by Chandan Siyag on 14/11/2015. #include <iostream> #include <string> #include "commander.h" 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 = InvalidType; commandValueIndex = 0; objectRaw = InvalidObject; this->resetVariables(); } Commander::~Commander() { } void Commander::decodeCommand(CommandTypeRaw type){ 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; } this->executeCommand(); return; } 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) { nextChar = pc.getc(); if (nextChar == '\n' || nextChar == '\r' || nextChar == ' ') { continue; } if (nextChar == objectTerminator) break; objectCharArray[i] = nextChar; i++; } 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) { 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; } } 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; } 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'; }