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:
- 6:98fe30430194
- Parent:
- 5:6ef2d954fca3
- Child:
- 7:b6e31bfdb2af
--- a/commander.cpp Mon Nov 16 01:26:41 2015 +0000 +++ b/commander.cpp Mon Nov 16 05:10:17 2015 +0000 @@ -1,119 +1,79 @@ +// +// commander.cpp +// Created by Chandan Siyag on 14/11/2015. +#include <iostream> #include <string> -#include <iostream> #include "commander.h" -#include "globals.h" - -//enum Type { INVALID = -1, QUERY = 0, SET, REPLY }; -//enum CObject { MBED = 0, COLOUR_SENSOR, SERVOS }; -/** Listen serial port for either of the command prefix i.e. '?' or '!' or '=' - @return -1:not correct prefix, otherwise 0:QUERY, 1:SET, 2:REPLY*/ -Type listenForCommand(){ - char commandType; - pc.printf("In listen"); - commandType = pc.getc(); - - if (commandType == '?'){ - return QUERY; - } - else if (commandType == '!'){ - return SET; - } - else if (commandType == '='){ - return REPLY; - } - else{ - return INVALID; - } +using namespace std; + +Commander::Commander() { + typeChar = '\0'; + typeRaw = CommandTypeRaw::Invalid; +} + +Commander::~Commander() { + } -void recieveCommand(Command *cmd, Type type){ -// pc.printf("In recieve command\n"); - - switch (type) { - case QUERY: - cmd->inputString[0] = '?'; - cmd->commandType[0] = '?'; - break; - case SET: - cmd->inputString[0] = '!'; - cmd->commandType[0] = '!'; - break; - case REPLY: - cmd->inputString[0] = ':'; - cmd->commandType[0] = ':'; - break; - default: - break; - } - - char nextChar; - - int commandObjectIdx = 0; - bool recordCommandObject = false; - int commandStringIdx = 0; - bool recordCommandString = false; - - for (int i = 1; i < gCommandBufferSize; i++){ - nextChar = pc.getc(); - cmd->inputString[i] = nextChar; - - if (nextChar == gCommandTerminator){ - pc.printf("nextChar == Terminator\r\n"); - pc.printf("%s\r\n", cmd->inputString); -// cmd->commandString[commandStringIdx - 1] = '\0'; - break; - } - - if (recordCommandObject){ - cmd->commandObject[commandObjectIdx] = nextChar; - commandObjectIdx++; - } else if (recordCommandString) { - cmd->commandString[commandStringIdx] = nextChar; - commandStringIdx++; - } - - if (nextChar == '<') - recordCommandObject = true; - else if (nextChar == '>'){ - recordCommandObject = false; - cmd->commandObject[commandObjectIdx - 1] = '\0'; - recordCommandString = true; - } - } - - if (cmd->commandObject == "mbed") - cmd->objectEnum = MBED; - else if (cmd->commandObject == "colour_sensor") - cmd->objectEnum = COLOUR_SENSOR; +void Commander::decodeCommand(CommandTypeRaw type){ + this->typeRaw = type; + this->typeChar = CommandTypeValue[type]; + + this->readCommandObject(); + + 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++; + } + + string tempStr(commandCharArray); + this->command = tempStr; + + return; } -void decodeCommand(Command *cmd) -{ - switch (cmd->typeEnum) { - case QUERY: { - switch (cmd->objectEnum) { - case MBED: { - string commandString = cmd->commandString; - const string init = "init"; - if (cmd->commandString == "init") { - // Convert gParity from Pairty to string - pc.printf("%s<%s>baudRate:%f,parity:none,stopbits:%i;\r\n", cmd->commandType, cmd->commandObject, gBaudRate, gStopBits); - } - break; - } - default: - pc.printf("Going to 2rd default\r\n"); - break; - } - break; - } - default: - pc.printf("Going to 1rd default\r\n"); - break; - } +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 { + scanf("%1c", &nextChar); + } while (nextChar != objectInitiator); + + char objectCharArray [20] = ""; + int i = 0; + while (i < 20) { + scanf("%1c", &nextChar); + if (nextChar == '\n' || nextChar == '\r' || nextChar == ' ') { continue; } + if (nextChar == objectTerminator) + break; + objectCharArray[i] = nextChar; + i++; + } + string tempStr(objectCharArray); + this->object = tempStr; + + return; } //string parseReply(Command *cmd){ // -//} \ No newline at end of file +//}