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-10-30
- Revision:
- 2:7a55cb10259f
- Child:
- 3:843b830ee8bd
File content as of revision 2:7a55cb10259f:
#include "commander.h" #include "globals.h" //#include <regex> #include <string> #include <iostream> //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; commandType = pc.getc(); if (commandType == '?') return QUERY; else if (commandType == '!') return SET; else if (commandType == '=') return REPLY; else return INVALID; } void recieveCommand(Command *cmd, Type type){ // cmd->inputString; // cmd->outputString; cmd->typeEnum = type; 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){ 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 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; } } //string parseReply(Command *cmd){ // //}