Chandan Siyag / Mbed 2 deprecated AlooMBED

Dependencies:   MCP23017 TCS3472_I2C WattBob_TextLCD mbed

Committer:
dreamselec
Date:
Mon Nov 16 23:44:44 2015 +0000
Revision:
9:dc8f155b71c8
Parent:
8:e1da2ae62885
Child:
10:16ba52f8e025
Testing of command parsing successful. Implementing ability to declare new has block.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreamselec 6:98fe30430194 1 //
dreamselec 6:98fe30430194 2 // commander.cpp
dreamselec 6:98fe30430194 3 // Created by Chandan Siyag on 14/11/2015.
dreamselec 6:98fe30430194 4 #include <iostream>
lordlycastle 5:6ef2d954fca3 5 #include <string>
dreamselec 2:7a55cb10259f 6 #include "commander.h"
dreamselec 2:7a55cb10259f 7
dreamselec 6:98fe30430194 8 using namespace std;
dreamselec 6:98fe30430194 9
dreamselec 9:dc8f155b71c8 10 //const int kCommandBufferSize = 40;
dreamselec 8:e1da2ae62885 11 const int kCommandValueBufferSize = 40;
dreamselec 8:e1da2ae62885 12 const int kObjectBufferSize = 20;
dreamselec 8:e1da2ae62885 13
dreamselec 8:e1da2ae62885 14 string CommandObjectValue [5] = { "mbed", "pc", "colour_sensor", "servos", "port" };
dreamselec 8:e1da2ae62885 15 string CommandObjectCommandsValue [5][kMaxCommandCount] = {
dreamselec 8:e1da2ae62885 16 {"mbed", "haz-block", "read-current-block", "", "", "", "", "", "", ""},
dreamselec 8:e1da2ae62885 17 {"pc", "connect", "disconnect", "", "", "", "", "", "", "exit"},
dreamselec 8:e1da2ae62885 18 {"colour_sensor", "i-time=", "preview=", "", "", "", "", "", "", ""},
dreamselec 8:e1da2ae62885 19 {"servos", "test", "reset", "", "", "", "", "", "", ""},
dreamselec 8:e1da2ae62885 20 {"port", "init", "b-rate=", "parity=", "stopbit=", "", "", "", "", ""},
dreamselec 8:e1da2ae62885 21 };
dreamselec 8:e1da2ae62885 22
dreamselec 6:98fe30430194 23 Commander::Commander() {
dreamselec 6:98fe30430194 24 typeChar = '\0';
dreamselec 8:e1da2ae62885 25 typeRaw = InvalidType;
dreamselec 8:e1da2ae62885 26 commandValueIndex = 0;
dreamselec 8:e1da2ae62885 27 objectRaw = InvalidObject;
dreamselec 8:e1da2ae62885 28 this->resetVariables();
dreamselec 6:98fe30430194 29 }
dreamselec 6:98fe30430194 30
dreamselec 6:98fe30430194 31 Commander::~Commander() {
dreamselec 6:98fe30430194 32
dreamselec 2:7a55cb10259f 33 }
dreamselec 2:7a55cb10259f 34
dreamselec 6:98fe30430194 35 void Commander::decodeCommand(CommandTypeRaw type){
dreamselec 6:98fe30430194 36 this->typeRaw = type;
dreamselec 6:98fe30430194 37 this->typeChar = CommandTypeValue[type];
dreamselec 6:98fe30430194 38
dreamselec 6:98fe30430194 39 this->readCommandObject();
dreamselec 8:e1da2ae62885 40 if (this->objectRaw == InvalidObject) { return; }
dreamselec 6:98fe30430194 41
dreamselec 8:e1da2ae62885 42 this->readCommand(this->objectRaw);
dreamselec 8:e1da2ae62885 43 if (this->commandValueIndex == -1) { return; }
dreamselec 8:e1da2ae62885 44 else if ((connectedToPC == false) && (this->typeRaw != Set || this->objectRaw != PC || this->commandValueIndex != 1)) { return; }
dreamselec 6:98fe30430194 45
dreamselec 8:e1da2ae62885 46 this->executeCommand();
dreamselec 6:98fe30430194 47 return;
dreamselec 2:7a55cb10259f 48 }
dreamselec 2:7a55cb10259f 49
dreamselec 6:98fe30430194 50 std::string Commander::description(){
dreamselec 6:98fe30430194 51 string str;
dreamselec 6:98fe30430194 52 str.append("Command type:\t");
dreamselec 6:98fe30430194 53 str.append(&this->typeChar);
dreamselec 6:98fe30430194 54 str.append("\nCommand object:\t" + this->object + "\n" + "Command:\t\t" + this->command + " \n");
dreamselec 6:98fe30430194 55
dreamselec 6:98fe30430194 56 return str;
dreamselec 6:98fe30430194 57 }
dreamselec 6:98fe30430194 58
dreamselec 6:98fe30430194 59 void Commander::readCommandObject(){
dreamselec 6:98fe30430194 60 char objectInitiator = '<';
dreamselec 6:98fe30430194 61 char objectTerminator = '>';
dreamselec 6:98fe30430194 62
dreamselec 6:98fe30430194 63 char nextChar = '\0';
dreamselec 6:98fe30430194 64
dreamselec 6:98fe30430194 65 do {
dreamselec 9:dc8f155b71c8 66 nextChar = pc.getc();
dreamselec 6:98fe30430194 67 } while (nextChar != objectInitiator);
dreamselec 6:98fe30430194 68
dreamselec 8:e1da2ae62885 69 char objectCharArray [kObjectBufferSize] = "";
dreamselec 6:98fe30430194 70 int i = 0;
dreamselec 8:e1da2ae62885 71 while (i < kObjectBufferSize) {
dreamselec 9:dc8f155b71c8 72 nextChar = pc.getc();
dreamselec 6:98fe30430194 73 if (nextChar == '\n' || nextChar == '\r' || nextChar == ' ') { continue; }
dreamselec 6:98fe30430194 74 if (nextChar == objectTerminator)
dreamselec 6:98fe30430194 75 break;
dreamselec 6:98fe30430194 76 objectCharArray[i] = nextChar;
dreamselec 6:98fe30430194 77 i++;
dreamselec 6:98fe30430194 78 }
dreamselec 6:98fe30430194 79 string tempStr(objectCharArray);
dreamselec 6:98fe30430194 80 this->object = tempStr;
dreamselec 6:98fe30430194 81
dreamselec 8:e1da2ae62885 82 for (int i = 0; i < (sizeof(CommandObjectValue)/sizeof(*CommandObjectValue)); i++) {
dreamselec 8:e1da2ae62885 83 if (CommandObjectValue[i] == this->object){
dreamselec 8:e1da2ae62885 84 this->objectRaw = static_cast<CommandObjectRaw>(i);
dreamselec 8:e1da2ae62885 85 return;
dreamselec 8:e1da2ae62885 86 }
dreamselec 8:e1da2ae62885 87 }
dreamselec 8:e1da2ae62885 88
dreamselec 8:e1da2ae62885 89 this->objectRaw = InvalidObject;
dreamselec 8:e1da2ae62885 90 return;
dreamselec 8:e1da2ae62885 91 }
dreamselec 8:e1da2ae62885 92
dreamselec 8:e1da2ae62885 93 void Commander::readCommand(CommandObjectRaw objectRaw){
dreamselec 8:e1da2ae62885 94 char nextChar = '\0';
dreamselec 8:e1da2ae62885 95 char commandCharArray [kCommandBufferSize] = "";
dreamselec 8:e1da2ae62885 96 char commandValue [kCommandValueBufferSize] = "";
dreamselec 8:e1da2ae62885 97
dreamselec 8:e1da2ae62885 98 int charIndex = 0;
dreamselec 8:e1da2ae62885 99 int valueIndex = 0;
dreamselec 8:e1da2ae62885 100 bool commandComplete = false;
dreamselec 8:e1da2ae62885 101 while (charIndex < kCommandBufferSize && valueIndex < kCommandValueBufferSize) {
dreamselec 9:dc8f155b71c8 102 nextChar = pc.getc();
dreamselec 8:e1da2ae62885 103 if (nextChar == '\n' || nextChar == '\r' || nextChar == ' ') { continue; }
dreamselec 8:e1da2ae62885 104 else if (nextChar == kCommandTerminator) { break; }
dreamselec 8:e1da2ae62885 105
dreamselec 8:e1da2ae62885 106 if (commandComplete == false) {
dreamselec 8:e1da2ae62885 107 commandCharArray[charIndex] = nextChar;
dreamselec 8:e1da2ae62885 108 charIndex++;
dreamselec 8:e1da2ae62885 109 }else if (commandComplete == true){
dreamselec 8:e1da2ae62885 110 commandValue[valueIndex] = nextChar;
dreamselec 8:e1da2ae62885 111 valueIndex++;
dreamselec 8:e1da2ae62885 112 }
dreamselec 8:e1da2ae62885 113
dreamselec 8:e1da2ae62885 114 if (nextChar == '=') { commandComplete = true; }
dreamselec 8:e1da2ae62885 115 }
dreamselec 8:e1da2ae62885 116
dreamselec 8:e1da2ae62885 117 string tempCommandStr(commandCharArray);
dreamselec 8:e1da2ae62885 118 string tempValueStr(commandValue);
dreamselec 8:e1da2ae62885 119
dreamselec 8:e1da2ae62885 120 int row = objectRaw;
dreamselec 8:e1da2ae62885 121 int column = 0;
dreamselec 8:e1da2ae62885 122
dreamselec 8:e1da2ae62885 123 for (;column < kMaxCommandCount; column++){
dreamselec 8:e1da2ae62885 124 if (CommandObjectCommandsValue[row][column] == tempCommandStr) {
dreamselec 8:e1da2ae62885 125 this->command = tempCommandStr;
dreamselec 8:e1da2ae62885 126 this->commandValueIndex = column;
dreamselec 8:e1da2ae62885 127 if (this->typeRaw == Set){
dreamselec 8:e1da2ae62885 128 this->commandValue = tempValueStr;
dreamselec 8:e1da2ae62885 129 }
dreamselec 8:e1da2ae62885 130 return;
dreamselec 8:e1da2ae62885 131 }
dreamselec 8:e1da2ae62885 132 }
dreamselec 8:e1da2ae62885 133
dreamselec 8:e1da2ae62885 134 this->commandValueIndex = -1;
dreamselec 6:98fe30430194 135 return;
dreamselec 2:7a55cb10259f 136 }
dreamselec 2:7a55cb10259f 137
dreamselec 8:e1da2ae62885 138 void Commander::executeCommand(){
dreamselec 8:e1da2ae62885 139 switch (this->objectRaw) {
dreamselec 8:e1da2ae62885 140 case MBED:
dreamselec 8:e1da2ae62885 141 if (this->commandValueIndex == 1) {
dreamselec 8:e1da2ae62885 142 hazBlock(this->typeRaw);
dreamselec 8:e1da2ae62885 143 }else if (this->commandValueIndex == 2){
dreamselec 8:e1da2ae62885 144 getCurrentBlock(this->typeRaw);
dreamselec 8:e1da2ae62885 145 }
dreamselec 8:e1da2ae62885 146 break;
dreamselec 8:e1da2ae62885 147 case PC:
dreamselec 8:e1da2ae62885 148 if (this->commandValueIndex == 1){
dreamselec 8:e1da2ae62885 149 connectToPC(this->typeRaw);
dreamselec 8:e1da2ae62885 150 }else if (this->commandValueIndex == 2){
dreamselec 8:e1da2ae62885 151 disconnectToPC(this->typeRaw);
dreamselec 8:e1da2ae62885 152 }
dreamselec 8:e1da2ae62885 153 break;
dreamselec 8:e1da2ae62885 154 case ColourSensor:
dreamselec 8:e1da2ae62885 155 if (this->commandValueIndex == 1) {
dreamselec 8:e1da2ae62885 156 int integrationTime;
dreamselec 8:e1da2ae62885 157 sscanf(this->commandValue.c_str(), "%i", &integrationTime);
dreamselec 8:e1da2ae62885 158 setIntegrationTime(integrationTime);
dreamselec 8:e1da2ae62885 159 }else if (this->commandValueIndex == 2){
dreamselec 8:e1da2ae62885 160 if (this->commandValue == "ON"){
dreamselec 8:e1da2ae62885 161 previewOnPC(true);
dreamselec 8:e1da2ae62885 162 }else if (this->commandValue == "OFF"){
dreamselec 8:e1da2ae62885 163 previewOnPC(false);
dreamselec 8:e1da2ae62885 164 }
dreamselec 8:e1da2ae62885 165 }
dreamselec 8:e1da2ae62885 166 break;
dreamselec 8:e1da2ae62885 167 case Servos:
dreamselec 8:e1da2ae62885 168 if (this->commandValueIndex == 1){
dreamselec 8:e1da2ae62885 169 testServos();
dreamselec 8:e1da2ae62885 170 }else if (this->commandValueIndex == 2){
dreamselec 8:e1da2ae62885 171 resetServos();
dreamselec 8:e1da2ae62885 172 }
dreamselec 8:e1da2ae62885 173 break;
dreamselec 8:e1da2ae62885 174 case Port:
dreamselec 8:e1da2ae62885 175 if (this->commandValueIndex == 1) {
dreamselec 8:e1da2ae62885 176 getPortInfo();
dreamselec 8:e1da2ae62885 177 }else if (this->commandValueIndex == 2){
dreamselec 8:e1da2ae62885 178 int baudRate;
dreamselec 8:e1da2ae62885 179 sscanf(this->commandValue.c_str(), "%i", &baudRate);
dreamselec 8:e1da2ae62885 180 setPortBaudRate(baudRate);
dreamselec 8:e1da2ae62885 181 }
dreamselec 8:e1da2ae62885 182 break;
dreamselec 8:e1da2ae62885 183 default:
dreamselec 8:e1da2ae62885 184 break;
dreamselec 8:e1da2ae62885 185 }
dreamselec 8:e1da2ae62885 186 }
dreamselec 8:e1da2ae62885 187
dreamselec 8:e1da2ae62885 188 void Commander::resetVariables(){
dreamselec 8:e1da2ae62885 189 this->object = "";
dreamselec 8:e1da2ae62885 190 this->objectRaw = InvalidObject;
dreamselec 8:e1da2ae62885 191 this->command = "";
dreamselec 8:e1da2ae62885 192 this->commandValue = "";
dreamselec 8:e1da2ae62885 193 this->commandValueIndex = 0;
dreamselec 8:e1da2ae62885 194 this->typeRaw = InvalidType;
dreamselec 8:e1da2ae62885 195 this->typeChar = '\0';
dreamselec 8:e1da2ae62885 196 }