Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MCP23017 TCS3472_I2C WattBob_TextLCD mbed
commander.cpp@12:f485796016f8, 2015-11-21 (annotated)
- Committer:
- dreamselec 
- Date:
- Sat Nov 21 20:19:54 2015 +0000
- Revision:
- 12:f485796016f8
- Parent:
- 10:16ba52f8e025
- Child:
- 13:4f24da6e2f8e
Improved command parsing. Now supports multiple commands.
Who changed what in which revision?
| User | Revision | Line number | New 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 | 12:f485796016f8 | 14 | string CommandObjectValue [5] = { "mbed", "pc", "colour_sensor", "servos", "port" }; | 
| dreamselec | 12:f485796016f8 | 15 | string CommandObjectCommandsValue [5][kMaxCommandCount] = { | 
| dreamselec | 12:f485796016f8 | 16 | {"mbed", "haz-block", "read-current-block", "", "", "", "", "", "", ""}, | 
| dreamselec | 12:f485796016f8 | 17 | {"pc", "connect", "disconnect", "", "", "", "", "", "", "exit"}, | 
| dreamselec | 12:f485796016f8 | 18 | {"colour_sensor", "i-time", "preview", "", "", "", "", "", "", ""}, | 
| dreamselec | 12:f485796016f8 | 19 | {"servos", "test", "reset", "", "", "", "", "", "", ""}, | 
| dreamselec | 12:f485796016f8 | 20 | {"port", "init", "b-rate", "parity", "stopbit", "", "", "", "", ""}, | 
| dreamselec | 12:f485796016f8 | 21 | }; | 
| dreamselec | 8:e1da2ae62885 | 22 | |
| dreamselec | 6:98fe30430194 | 23 | Commander::Commander() { | 
| dreamselec | 8:e1da2ae62885 | 24 | this->resetVariables(); | 
| dreamselec | 6:98fe30430194 | 25 | } | 
| dreamselec | 6:98fe30430194 | 26 | |
| dreamselec | 6:98fe30430194 | 27 | Commander::~Commander() { | 
| dreamselec | 12:f485796016f8 | 28 | |
| dreamselec | 2:7a55cb10259f | 29 | } | 
| dreamselec | 2:7a55cb10259f | 30 | |
| dreamselec | 6:98fe30430194 | 31 | void Commander::decodeCommand(CommandTypeRaw type){ | 
| dreamselec | 12:f485796016f8 | 32 | this->resetVariables(); | 
| dreamselec | 6:98fe30430194 | 33 | this->typeRaw = type; | 
| dreamselec | 6:98fe30430194 | 34 | this->typeChar = CommandTypeValue[type]; | 
| dreamselec | 12:f485796016f8 | 35 | |
| dreamselec | 6:98fe30430194 | 36 | this->readCommandObject(); | 
| dreamselec | 8:e1da2ae62885 | 37 | if (this->objectRaw == InvalidObject) { return; } | 
| dreamselec | 12:f485796016f8 | 38 | |
| dreamselec | 12:f485796016f8 | 39 | if (this->readCommand(this->objectRaw) == false) { return; } | 
| dreamselec | 12:f485796016f8 | 40 | // else if ((connectedToPC == false) && (this->typeRaw != Set || this->objectRaw != PC || this->commandIndex[0] != 1)) { return; } | 
| dreamselec | 12:f485796016f8 | 41 | else if (connectedToPC == true || (this->typeRaw == Set && this->objectRaw == PC && this->commandIndex[0] == 1)) { this->executeCommand(); } | 
| dreamselec | 12:f485796016f8 | 42 | else { return; } | 
| dreamselec | 12:f485796016f8 | 43 | } | 
| dreamselec | 12:f485796016f8 | 44 | |
| dreamselec | 12:f485796016f8 | 45 | std::string Commander::description(){ | 
| dreamselec | 12:f485796016f8 | 46 | string str; | 
| dreamselec | 12:f485796016f8 | 47 | str.append("Command type:\t"); | 
| dreamselec | 12:f485796016f8 | 48 | str.append(&this->typeChar); | 
| dreamselec | 12:f485796016f8 | 49 | str.append("\nCommand object:\t" + this->object); | 
| dreamselec | 12:f485796016f8 | 50 | |
| dreamselec | 12:f485796016f8 | 51 | for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) { | 
| dreamselec | 12:f485796016f8 | 52 | if (this->command[i] == "") { break; } | 
| dreamselec | 12:f485796016f8 | 53 | str.append("Command:\t" + this->command[i].c_str() + "\nValue:\t" + this->commandValue[i].c_str() + " \n"); | 
| dreamselec | 12:f485796016f8 | 54 | } | 
| dreamselec | 2:7a55cb10259f | 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 | 12:f485796016f8 | 62 | |
| dreamselec | 6:98fe30430194 | 63 | char nextChar = '\0'; | 
| dreamselec | 12:f485796016f8 | 64 | |
| dreamselec | 6:98fe30430194 | 65 | do { | 
| dreamselec | 9:dc8f155b71c8 | 66 | nextChar = pc.getc(); | 
| dreamselec | 6:98fe30430194 | 67 | } while (nextChar != objectInitiator); | 
| dreamselec | 12:f485796016f8 | 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 | 12:f485796016f8 | 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 | 12:f485796016f8 | 93 | bool Commander::readCommand(CommandObjectRaw objectRaw){ | 
| dreamselec | 12:f485796016f8 | 94 | char nextChar = '\0'; | 
| dreamselec | 12:f485796016f8 | 95 | char commandCharArray [kMaxCommandCount - 1][79] = { "" }; | 
| dreamselec | 12:f485796016f8 | 96 | char commandValue [kMaxCommandCount -1][79] = { "" }; | 
| dreamselec | 8:e1da2ae62885 | 97 | |
| dreamselec | 8:e1da2ae62885 | 98 | int charIndex = 0; | 
| dreamselec | 12:f485796016f8 | 99 | int valueCharIndex = 0; | 
| dreamselec | 12:f485796016f8 | 100 | int commandValueIndex = 0; | 
| dreamselec | 12:f485796016f8 | 101 | bool commandComplete = false; | 
| dreamselec | 12:f485796016f8 | 102 | while (charIndex < 79 && valueCharIndex < 79) { | 
| dreamselec | 12:f485796016f8 | 103 | scanf("%1c", &nextChar); | 
| dreamselec | 12:f485796016f8 | 104 | if (nextChar == '\n' || nextChar == '\r' || nextChar == ' ') { continue; } | 
| dreamselec | 12:f485796016f8 | 105 | else if (nextChar == kCommandTerminator) { break; } | 
| dreamselec | 8:e1da2ae62885 | 106 | |
| dreamselec | 12:f485796016f8 | 107 | else if (nextChar == '=') { commandComplete = true; } | 
| dreamselec | 12:f485796016f8 | 108 | else if (nextChar == ',') { | 
| dreamselec | 12:f485796016f8 | 109 | commandComplete = false; | 
| dreamselec | 12:f485796016f8 | 110 | commandValueIndex++; | 
| dreamselec | 12:f485796016f8 | 111 | charIndex = 0; | 
| dreamselec | 12:f485796016f8 | 112 | valueCharIndex = 0; | 
| dreamselec | 12:f485796016f8 | 113 | } | 
| dreamselec | 12:f485796016f8 | 114 | |
| dreamselec | 12:f485796016f8 | 115 | if (commandComplete == false && nextChar != ',') { | 
| dreamselec | 12:f485796016f8 | 116 | commandCharArray[commandValueIndex][charIndex] = nextChar; | 
| dreamselec | 12:f485796016f8 | 117 | charIndex++; | 
| dreamselec | 12:f485796016f8 | 118 | }else if (commandComplete == true && nextChar != '='){ | 
| dreamselec | 12:f485796016f8 | 119 | commandValue[commandValueIndex][valueCharIndex] = nextChar; | 
| dreamselec | 12:f485796016f8 | 120 | valueCharIndex++; | 
| dreamselec | 12:f485796016f8 | 121 | } | 
| dreamselec | 12:f485796016f8 | 122 | |
| dreamselec | 12:f485796016f8 | 123 | } | 
| dreamselec | 8:e1da2ae62885 | 124 | |
| dreamselec | 12:f485796016f8 | 125 | for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) { | 
| dreamselec | 12:f485796016f8 | 126 | string tempCommandStr(commandCharArray[i]); | 
| dreamselec | 8:e1da2ae62885 | 127 | |
| dreamselec | 12:f485796016f8 | 128 | string tempCommandStr(commandCharArray); | 
| dreamselec | 12:f485796016f8 | 129 | if (tempCommandStr == "") { break; } | 
| dreamselec | 12:f485796016f8 | 130 | string tempValueStr(commandValue[i]); | 
| dreamselec | 12:f485796016f8 | 131 | |
| dreamselec | 12:f485796016f8 | 132 | int row = objectRaw; | 
| dreamselec | 12:f485796016f8 | 133 | int column = 1; | 
| dreamselec | 12:f485796016f8 | 134 | for (;column < kMaxCommandCount; column++){ | 
| dreamselec | 12:f485796016f8 | 135 | if (CommandObjectCommandsValue[row][column] == tempCommandStr) { | 
| dreamselec | 12:f485796016f8 | 136 | this->command[i] = tempCommandStr; | 
| dreamselec | 12:f485796016f8 | 137 | this->commandIndex[i] = column; | 
| dreamselec | 12:f485796016f8 | 138 | this->commandValue[i] = tempValueStr; | 
| dreamselec | 12:f485796016f8 | 139 | break; | 
| dreamselec | 12:f485796016f8 | 140 | } | 
| dreamselec | 12:f485796016f8 | 141 | } | 
| dreamselec | 12:f485796016f8 | 142 | if (this->commandIndex[i] == -1) { return false; } | 
| dreamselec | 12:f485796016f8 | 143 | } | 
| dreamselec | 12:f485796016f8 | 144 | |
| dreamselec | 12:f485796016f8 | 145 | return true; | 
| dreamselec | 12:f485796016f8 | 146 | } | 
| dreamselec | 2:7a55cb10259f | 147 | |
| dreamselec | 8:e1da2ae62885 | 148 | void Commander::executeCommand(){ | 
| dreamselec | 8:e1da2ae62885 | 149 | switch (this->objectRaw) { | 
| dreamselec | 12:f485796016f8 | 150 | case MBED: | 
| dreamselec | 12:f485796016f8 | 151 | for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) { | 
| dreamselec | 12:f485796016f8 | 152 | if (this->commandIndex[i] == -1 || this->commandIndex[i] == 0) { break; } | 
| dreamselec | 12:f485796016f8 | 153 | if (this->commandIndex[i] == 1) { | 
| dreamselec | 12:f485796016f8 | 154 | hazBlock(this->typeRaw); | 
| dreamselec | 12:f485796016f8 | 155 | }else if (this->commandIndex[i] == 2){ | 
| dreamselec | 12:f485796016f8 | 156 | getCurrentBlock(this->typeRaw); | 
| dreamselec | 12:f485796016f8 | 157 | } | 
| dreamselec | 12:f485796016f8 | 158 | } | 
| dreamselec | 12:f485796016f8 | 159 | break; | 
| dreamselec | 12:f485796016f8 | 160 | case PC: | 
| dreamselec | 12:f485796016f8 | 161 | for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) { | 
| dreamselec | 12:f485796016f8 | 162 | if (this->commandIndex[i] == -1 || this->commandIndex[i] == 0) { break; } | 
| dreamselec | 12:f485796016f8 | 163 | if (this->commandIndex[i] == 1){ | 
| dreamselec | 12:f485796016f8 | 164 | connectToPC(this->typeRaw); | 
| dreamselec | 12:f485796016f8 | 165 | }else if (this->commandIndex[i] == 2){ | 
| dreamselec | 12:f485796016f8 | 166 | disconnectToPC(this->typeRaw); | 
| dreamselec | 12:f485796016f8 | 167 | } | 
| dreamselec | 12:f485796016f8 | 168 | } | 
| dreamselec | 12:f485796016f8 | 169 | break; | 
| dreamselec | 12:f485796016f8 | 170 | case ColourSensor: | 
| dreamselec | 12:f485796016f8 | 171 | for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) { | 
| dreamselec | 12:f485796016f8 | 172 | if (this->commandIndex[i] == -1 || this->commandIndex[i] == 0) { break; } | 
| dreamselec | 12:f485796016f8 | 173 | if (this->commandIndex[i] == 1) { | 
| dreamselec | 12:f485796016f8 | 174 | int integrationTime; | 
| dreamselec | 12:f485796016f8 | 175 | sscanf(this->commandValue[i].c_str(), "%i", &integrationTime); | 
| dreamselec | 12:f485796016f8 | 176 | setIntegrationTime(integrationTime); | 
| dreamselec | 12:f485796016f8 | 177 | }else if (this->commandIndex[i] == 2){ | 
| dreamselec | 12:f485796016f8 | 178 | if (this->commandValue[i] == "ON"){ | 
| dreamselec | 12:f485796016f8 | 179 | previewOnPC(true); | 
| dreamselec | 12:f485796016f8 | 180 | }else if (this->commandValue[i] == "OFF"){ | 
| dreamselec | 12:f485796016f8 | 181 | previewOnPC(false); | 
| dreamselec | 12:f485796016f8 | 182 | } | 
| dreamselec | 12:f485796016f8 | 183 | }else if (this->commandIndex[i] == 3){ | 
| dreamselec | 10:16ba52f8e025 | 184 | readColourSensor(); | 
| dreamselec | 8:e1da2ae62885 | 185 | } | 
| dreamselec | 12:f485796016f8 | 186 | } | 
| dreamselec | 8:e1da2ae62885 | 187 | break; | 
| dreamselec | 12:f485796016f8 | 188 | case Servos: | 
| dreamselec | 12:f485796016f8 | 189 | for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) { | 
| dreamselec | 12:f485796016f8 | 190 | if (this->commandIndex[i] == -1 || this->commandIndex[i] == 0) { break; } | 
| dreamselec | 12:f485796016f8 | 191 | if (this->commandIndex[i] == 1){ | 
| dreamselec | 12:f485796016f8 | 192 | testServos(); | 
| dreamselec | 12:f485796016f8 | 193 | }else if (this->commandIndex[i] == 2){ | 
| dreamselec | 12:f485796016f8 | 194 | resetServos(); | 
| dreamselec | 12:f485796016f8 | 195 | } | 
| dreamselec | 12:f485796016f8 | 196 | } | 
| dreamselec | 12:f485796016f8 | 197 | break; | 
| dreamselec | 12:f485796016f8 | 198 | case Port: | 
| dreamselec | 12:f485796016f8 | 199 | for (int i = 0; i < sizeof(this->command)/sizeof(*this->command); i++) { | 
| dreamselec | 12:f485796016f8 | 200 | if (this->commandIndex[i] == -1 || this->commandIndex[i] == 0) { break; } | 
| dreamselec | 12:f485796016f8 | 201 | if (this->commandIndex[i] == 1) { | 
| dreamselec | 12:f485796016f8 | 202 | getPortInfo(); | 
| dreamselec | 12:f485796016f8 | 203 | }else if (this->commandIndex[i] == 2){ | 
| dreamselec | 12:f485796016f8 | 204 | float baudRate; | 
| dreamselec | 12:f485796016f8 | 205 | sscanf(this->commandValue[i].c_str(), "%f", &baudRate); | 
| dreamselec | 12:f485796016f8 | 206 | setPortBaudRate(baudRate); | 
| dreamselec | 12:f485796016f8 | 207 | } | 
| dreamselec | 12:f485796016f8 | 208 | } | 
| dreamselec | 12:f485796016f8 | 209 | break; | 
| dreamselec | 12:f485796016f8 | 210 | default: | 
| dreamselec | 12:f485796016f8 | 211 | break; | 
| dreamselec | 12:f485796016f8 | 212 | } | 
| dreamselec | 8:e1da2ae62885 | 213 | } | 
| dreamselec | 8:e1da2ae62885 | 214 | |
| dreamselec | 12:f485796016f8 | 215 | |
| dreamselec | 12:f485796016f8 | 216 | void Commander::resetVariables(){ | 
| dreamselec | 12:f485796016f8 | 217 | this->object = ""; | 
| dreamselec | 12:f485796016f8 | 218 | this->objectRaw = InvalidObject; | 
| dreamselec | 12:f485796016f8 | 219 | memset(this->command, 0, sizeof(this->command)); | 
| dreamselec | 12:f485796016f8 | 220 | memset(this->commandValue, 0, sizeof(this->command)); | 
| dreamselec | 12:f485796016f8 | 221 | memset(this->commandIndex, 0, sizeof(this->commandIndex)); | 
| dreamselec | 12:f485796016f8 | 222 | this->typeRaw = InvalidType; | 
| dreamselec | 12:f485796016f8 | 223 | this->typeChar = '\0'; | 
| dreamselec | 12:f485796016f8 | 224 | } | 
