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.
Fork of frdm_echo by
shell.cpp@12:b846b64e3980, 2014-04-17 (annotated)
- Committer:
- silverpanda
- Date:
- Thu Apr 17 15:38:08 2014 +0000
- Revision:
- 12:b846b64e3980
- Parent:
- 11:4c6c6d6c0ebe
- Child:
- 13:019e24491f32
changed name
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
silverpanda | 5:03b7c237c4c4 | 1 | #include "mbed.h" |
silverpanda | 7:19da09fe546b | 2 | #include "LEDColors.h" |
silverpanda | 5:03b7c237c4c4 | 3 | #include "shell.h" |
silverpanda | 5:03b7c237c4c4 | 4 | |
silverpanda | 7:19da09fe546b | 5 | extern LEDColors *ledColors; |
silverpanda | 7:19da09fe546b | 6 | |
silverpanda | 5:03b7c237c4c4 | 7 | // create contructor |
silverpanda | 5:03b7c237c4c4 | 8 | Shell::Shell(uint32_t thisBaudRate) |
silverpanda | 5:03b7c237c4c4 | 9 | { |
silverpanda | 5:03b7c237c4c4 | 10 | usbSerial = new Serial(USBTX, USBRX); |
silverpanda | 5:03b7c237c4c4 | 11 | usbSerial->baud(115200); |
silverpanda | 8:b715912d684b | 12 | characterCount = 0; |
silverpanda | 8:b715912d684b | 13 | characterPointer = 0; |
silverpanda | 5:03b7c237c4c4 | 14 | } |
silverpanda | 5:03b7c237c4c4 | 15 | //----------------------------------------------------------------------------- |
silverpanda | 5:03b7c237c4c4 | 16 | |
silverpanda | 5:03b7c237c4c4 | 17 | void Shell::sendText(char *thisText) |
silverpanda | 5:03b7c237c4c4 | 18 | { |
silverpanda | 5:03b7c237c4c4 | 19 | // this can send any text |
silverpanda | 5:03b7c237c4c4 | 20 | usbSerial->printf(thisText); |
silverpanda | 5:03b7c237c4c4 | 21 | } |
silverpanda | 5:03b7c237c4c4 | 22 | //----------------------------------------------------------------------------- |
silverpanda | 5:03b7c237c4c4 | 23 | |
silverpanda | 12:b846b64e3980 | 24 | void Shell::sendStartMessage() |
silverpanda | 5:03b7c237c4c4 | 25 | { |
silverpanda | 5:03b7c237c4c4 | 26 | // sends the first greeting |
silverpanda | 12:b846b64e3980 | 27 | sendText("> ** K64 LED controller **\n"); |
silverpanda | 12:b846b64e3980 | 28 | sendText(">\n"); |
silverpanda | 12:b846b64e3980 | 29 | sendText("> use commands like: turn blue LED on\n"); |
silverpanda | 12:b846b64e3980 | 30 | sendText("> or: turn red LED off\n"); |
silverpanda | 12:b846b64e3980 | 31 | sendText(">\n> "); |
silverpanda | 5:03b7c237c4c4 | 32 | } |
silverpanda | 5:03b7c237c4c4 | 33 | //----------------------------------------------------------------------------- |
silverpanda | 5:03b7c237c4c4 | 34 | |
silverpanda | 5:03b7c237c4c4 | 35 | void Shell::scanUSBSerialRx() |
silverpanda | 5:03b7c237c4c4 | 36 | { |
silverpanda | 5:03b7c237c4c4 | 37 | // check if there is something to read |
silverpanda | 5:03b7c237c4c4 | 38 | if(usbSerial->readable()) { |
silverpanda | 9:f9efd3a69c2d | 39 | |
silverpanda | 5:03b7c237c4c4 | 40 | // if so ... |
silverpanda | 5:03b7c237c4c4 | 41 | char character = usbSerial->getc(); |
silverpanda | 9:f9efd3a69c2d | 42 | |
silverpanda | 5:03b7c237c4c4 | 43 | // see if this is a semi colon or a carriage return |
silverpanda | 5:03b7c237c4c4 | 44 | // if so, give a new line cursor |
silverpanda | 7:19da09fe546b | 45 | if((character == ';') || (character == 13)) { |
silverpanda | 9:f9efd3a69c2d | 46 | finishCharacterBuffer(); |
silverpanda | 9:f9efd3a69c2d | 47 | parseCommands(); |
silverpanda | 7:19da09fe546b | 48 | usbSerial->printf("\n> "); |
silverpanda | 11:4c6c6d6c0ebe | 49 | // ledColors->flashWhite(10); |
silverpanda | 11:4c6c6d6c0ebe | 50 | ledColors->flash(lcBlue, 10); |
silverpanda | 7:19da09fe546b | 51 | } |
silverpanda | 9:f9efd3a69c2d | 52 | |
silverpanda | 5:03b7c237c4c4 | 53 | // if not, just print the character |
silverpanda | 9:f9efd3a69c2d | 54 | else if(addCharacterToBuffer(character)) { |
silverpanda | 9:f9efd3a69c2d | 55 | usbSerial->printf("%c", character); |
silverpanda | 11:4c6c6d6c0ebe | 56 | ledColors->flash(lcGreen, 10); |
silverpanda | 7:19da09fe546b | 57 | } |
silverpanda | 5:03b7c237c4c4 | 58 | } |
silverpanda | 5:03b7c237c4c4 | 59 | } |
silverpanda | 5:03b7c237c4c4 | 60 | //----------------------------------------------------------------------------- |
silverpanda | 9:f9efd3a69c2d | 61 | |
silverpanda | 9:f9efd3a69c2d | 62 | bool Shell::addCharacterToBuffer(char newCharacter) |
silverpanda | 8:b715912d684b | 63 | { |
silverpanda | 9:f9efd3a69c2d | 64 | bool accept = (characterPointer < ItsInputBufferSize_); |
silverpanda | 9:f9efd3a69c2d | 65 | if(accept) { |
silverpanda | 8:b715912d684b | 66 | inputBuffer[characterPointer++] = newCharacter; |
silverpanda | 8:b715912d684b | 67 | characterCount = characterPointer; |
silverpanda | 8:b715912d684b | 68 | } |
silverpanda | 9:f9efd3a69c2d | 69 | return accept; |
silverpanda | 8:b715912d684b | 70 | } |
silverpanda | 8:b715912d684b | 71 | //----------------------------------------------------------------------------- |
silverpanda | 8:b715912d684b | 72 | |
silverpanda | 8:b715912d684b | 73 | void Shell::finishCharacterBuffer() |
silverpanda | 8:b715912d684b | 74 | { |
silverpanda | 10:85a8da3b7e5e | 75 | inputBuffer[characterCount] = 0; |
silverpanda | 10:85a8da3b7e5e | 76 | characterCount = characterPointer; |
silverpanda | 8:b715912d684b | 77 | characterPointer = 0; |
silverpanda | 8:b715912d684b | 78 | } |
silverpanda | 9:f9efd3a69c2d | 79 | //----------------------------------------------------------------------------- |
silverpanda | 9:f9efd3a69c2d | 80 | |
silverpanda | 11:4c6c6d6c0ebe | 81 | char Shell::lowercase(char thisChar) |
silverpanda | 11:4c6c6d6c0ebe | 82 | { |
silverpanda | 11:4c6c6d6c0ebe | 83 | // check if this character is uppercase |
silverpanda | 11:4c6c6d6c0ebe | 84 | if((thisChar >= 'A') && (thisChar <= 'Z')) thisChar += 32; |
silverpanda | 11:4c6c6d6c0ebe | 85 | |
silverpanda | 11:4c6c6d6c0ebe | 86 | return thisChar; |
silverpanda | 11:4c6c6d6c0ebe | 87 | } |
silverpanda | 11:4c6c6d6c0ebe | 88 | //----------------------------------------------------------------------------- |
silverpanda | 11:4c6c6d6c0ebe | 89 | |
silverpanda | 11:4c6c6d6c0ebe | 90 | bool Shell::findString(char *thisString) |
silverpanda | 9:f9efd3a69c2d | 91 | { |
silverpanda | 9:f9efd3a69c2d | 92 | bool found = false; |
silverpanda | 11:4c6c6d6c0ebe | 93 | uint32_t startPointer = 0, characterPointer, newStartLocation, stringLength = 0; |
silverpanda | 9:f9efd3a69c2d | 94 | char firstChar, secondChar; |
silverpanda | 11:4c6c6d6c0ebe | 95 | |
silverpanda | 11:4c6c6d6c0ebe | 96 | // determine the length of thisString |
silverpanda | 11:4c6c6d6c0ebe | 97 | while(thisString[stringLength]) stringLength++; |
silverpanda | 11:4c6c6d6c0ebe | 98 | |
silverpanda | 10:85a8da3b7e5e | 99 | // start at the start position (0) on the input buffer |
silverpanda | 11:4c6c6d6c0ebe | 100 | while(!found |
silverpanda | 11:4c6c6d6c0ebe | 101 | && (characterCount >= stringLength) |
silverpanda | 11:4c6c6d6c0ebe | 102 | && (startPointer <= characterCount - stringLength)) { |
silverpanda | 11:4c6c6d6c0ebe | 103 | |
silverpanda | 10:85a8da3b7e5e | 104 | // start at the beginning of the compare string (thisString) |
silverpanda | 9:f9efd3a69c2d | 105 | found = true; |
silverpanda | 9:f9efd3a69c2d | 106 | characterPointer = 0; |
silverpanda | 9:f9efd3a69c2d | 107 | while(found && (characterPointer < stringLength)) { |
silverpanda | 11:4c6c6d6c0ebe | 108 | |
silverpanda | 10:85a8da3b7e5e | 109 | // get and compare characters |
silverpanda | 9:f9efd3a69c2d | 110 | firstChar = inputBuffer[startPointer + characterPointer]; |
silverpanda | 9:f9efd3a69c2d | 111 | secondChar = thisString[characterPointer]; |
silverpanda | 11:4c6c6d6c0ebe | 112 | found = (lowercase(firstChar) == lowercase(secondChar)); |
silverpanda | 11:4c6c6d6c0ebe | 113 | |
silverpanda | 10:85a8da3b7e5e | 114 | // for debugging |
silverpanda | 9:f9efd3a69c2d | 115 | // usbSerial->printf("(compare %c with %c)\n", firstChar, secondChar); |
silverpanda | 11:4c6c6d6c0ebe | 116 | |
silverpanda | 10:85a8da3b7e5e | 117 | // move up to next characters |
silverpanda | 9:f9efd3a69c2d | 118 | characterPointer++; |
silverpanda | 9:f9efd3a69c2d | 119 | } |
silverpanda | 11:4c6c6d6c0ebe | 120 | |
silverpanda | 10:85a8da3b7e5e | 121 | // move start position |
silverpanda | 10:85a8da3b7e5e | 122 | if(found) { |
silverpanda | 10:85a8da3b7e5e | 123 | newStartLocation = startPointer + stringLength; |
silverpanda | 11:4c6c6d6c0ebe | 124 | |
silverpanda | 10:85a8da3b7e5e | 125 | // clean up from buffer |
silverpanda | 10:85a8da3b7e5e | 126 | characterPointer = newStartLocation; |
silverpanda | 10:85a8da3b7e5e | 127 | while(characterPointer < characterCount) { |
silverpanda | 10:85a8da3b7e5e | 128 | firstChar = inputBuffer[characterPointer]; |
silverpanda | 10:85a8da3b7e5e | 129 | inputBuffer[characterPointer - newStartLocation] = firstChar; |
silverpanda | 10:85a8da3b7e5e | 130 | characterPointer++; |
silverpanda | 10:85a8da3b7e5e | 131 | } |
silverpanda | 10:85a8da3b7e5e | 132 | characterCount -= newStartLocation; |
silverpanda | 10:85a8da3b7e5e | 133 | inputBuffer[characterCount] = 0; |
silverpanda | 11:4c6c6d6c0ebe | 134 | |
silverpanda | 10:85a8da3b7e5e | 135 | // for debugging send buffer |
silverpanda | 10:85a8da3b7e5e | 136 | /* |
silverpanda | 10:85a8da3b7e5e | 137 | if(characterCount) { |
silverpanda | 10:85a8da3b7e5e | 138 | sendText(" is now \""); |
silverpanda | 10:85a8da3b7e5e | 139 | sendText(inputBuffer); |
silverpanda | 10:85a8da3b7e5e | 140 | sendText("\""); |
silverpanda | 10:85a8da3b7e5e | 141 | } |
silverpanda | 10:85a8da3b7e5e | 142 | */ |
silverpanda | 10:85a8da3b7e5e | 143 | } |
silverpanda | 11:4c6c6d6c0ebe | 144 | |
silverpanda | 9:f9efd3a69c2d | 145 | startPointer++; |
silverpanda | 9:f9efd3a69c2d | 146 | } |
silverpanda | 11:4c6c6d6c0ebe | 147 | |
silverpanda | 10:85a8da3b7e5e | 148 | // report |
silverpanda | 9:f9efd3a69c2d | 149 | return found; |
silverpanda | 9:f9efd3a69c2d | 150 | } |
silverpanda | 9:f9efd3a69c2d | 151 | //----------------------------------------------------------------------------- |
silverpanda | 9:f9efd3a69c2d | 152 | |
silverpanda | 9:f9efd3a69c2d | 153 | void Shell::parseCommands() |
silverpanda | 9:f9efd3a69c2d | 154 | { |
silverpanda | 11:4c6c6d6c0ebe | 155 | if(findString("set")) sendText(" -- \"set\" found!"); |
silverpanda | 11:4c6c6d6c0ebe | 156 | if(findString("Clark")) sendText(" -- \"Clark\" found!"); |
silverpanda | 11:4c6c6d6c0ebe | 157 | if(findString("Stefan")) sendText(" -- Hi \"Stefan\"!"); |
silverpanda | 11:4c6c6d6c0ebe | 158 | if(findString("Ralph")) sendText(" -- Hey, that's me!"); |
silverpanda | 11:4c6c6d6c0ebe | 159 | |
silverpanda | 12:b846b64e3980 | 160 | if(findString("turn")) parseLEDState(); |
silverpanda | 9:f9efd3a69c2d | 161 | } |
silverpanda | 9:f9efd3a69c2d | 162 | //----------------------------------------------------------------------------- |
silverpanda | 11:4c6c6d6c0ebe | 163 | |
silverpanda | 12:b846b64e3980 | 164 | void Shell::parseLEDState() |
silverpanda | 11:4c6c6d6c0ebe | 165 | { |
silverpanda | 11:4c6c6d6c0ebe | 166 | // example text: turn blue LED on |
silverpanda | 11:4c6c6d6c0ebe | 167 | |
silverpanda | 11:4c6c6d6c0ebe | 168 | eLEDColor ledColor = lcNone; |
silverpanda | 11:4c6c6d6c0ebe | 169 | if(findString("red")) ledColor = lcRed; |
silverpanda | 11:4c6c6d6c0ebe | 170 | if(findString("blue")) ledColor = lcBlue; |
silverpanda | 11:4c6c6d6c0ebe | 171 | if(findString("green")) ledColor = lcGreen; |
silverpanda | 11:4c6c6d6c0ebe | 172 | |
silverpanda | 12:b846b64e3980 | 173 | // check if color is specified |
silverpanda | 11:4c6c6d6c0ebe | 174 | if(ledColor == lcNone) { |
silverpanda | 11:4c6c6d6c0ebe | 175 | sendText(" -- missing LED color: red, blue or green"); |
silverpanda | 11:4c6c6d6c0ebe | 176 | sendExample(); |
silverpanda | 11:4c6c6d6c0ebe | 177 | } |
silverpanda | 11:4c6c6d6c0ebe | 178 | else { |
silverpanda | 12:b846b64e3980 | 179 | |
silverpanda | 12:b846b64e3980 | 180 | // check if LED is specified |
silverpanda | 11:4c6c6d6c0ebe | 181 | if(findString("LED")) { |
silverpanda | 11:4c6c6d6c0ebe | 182 | |
silverpanda | 12:b846b64e3980 | 183 | // check for state |
silverpanda | 11:4c6c6d6c0ebe | 184 | eState state = stNone; |
silverpanda | 11:4c6c6d6c0ebe | 185 | if(findString("on")) state = stOn; |
silverpanda | 11:4c6c6d6c0ebe | 186 | if(findString("off")) state = stOff; |
silverpanda | 11:4c6c6d6c0ebe | 187 | |
silverpanda | 11:4c6c6d6c0ebe | 188 | if(state == stNone) { |
silverpanda | 12:b846b64e3980 | 189 | |
silverpanda | 12:b846b64e3980 | 190 | // state not specified |
silverpanda | 11:4c6c6d6c0ebe | 191 | sendText(" -- missing state: on or off"); |
silverpanda | 11:4c6c6d6c0ebe | 192 | sendExample(); |
silverpanda | 11:4c6c6d6c0ebe | 193 | } |
silverpanda | 11:4c6c6d6c0ebe | 194 | else { |
silverpanda | 12:b846b64e3980 | 195 | |
silverpanda | 12:b846b64e3980 | 196 | // check if state has changed |
silverpanda | 12:b846b64e3980 | 197 | if(ledColors->getState(ledColor) == state) { |
silverpanda | 12:b846b64e3980 | 198 | |
silverpanda | 12:b846b64e3980 | 199 | // state not changed |
silverpanda | 12:b846b64e3980 | 200 | sendText(" -- already "); |
silverpanda | 12:b846b64e3980 | 201 | (state == stOn) ? sendText("on") : sendText("off"); |
silverpanda | 12:b846b64e3980 | 202 | } |
silverpanda | 12:b846b64e3980 | 203 | else { |
silverpanda | 12:b846b64e3980 | 204 | |
silverpanda | 12:b846b64e3980 | 205 | // state has changed |
silverpanda | 12:b846b64e3980 | 206 | sendText(" -- done!"); |
silverpanda | 12:b846b64e3980 | 207 | ledColors->turn(ledColor, state); |
silverpanda | 12:b846b64e3980 | 208 | } |
silverpanda | 11:4c6c6d6c0ebe | 209 | } |
silverpanda | 11:4c6c6d6c0ebe | 210 | } |
silverpanda | 11:4c6c6d6c0ebe | 211 | else { |
silverpanda | 12:b846b64e3980 | 212 | |
silverpanda | 12:b846b64e3980 | 213 | // LED was not specified |
silverpanda | 11:4c6c6d6c0ebe | 214 | sendText(" -- specify LED"); |
silverpanda | 11:4c6c6d6c0ebe | 215 | sendExample(); |
silverpanda | 11:4c6c6d6c0ebe | 216 | } |
silverpanda | 11:4c6c6d6c0ebe | 217 | } |
silverpanda | 11:4c6c6d6c0ebe | 218 | } |
silverpanda | 11:4c6c6d6c0ebe | 219 | //----------------------------------------------------------------------------- |
silverpanda | 11:4c6c6d6c0ebe | 220 | |
silverpanda | 11:4c6c6d6c0ebe | 221 | void Shell::sendExample() |
silverpanda | 11:4c6c6d6c0ebe | 222 | { |
silverpanda | 11:4c6c6d6c0ebe | 223 | sendText(" -> example: turn red LED on"); |
silverpanda | 11:4c6c6d6c0ebe | 224 | } |
silverpanda | 11:4c6c6d6c0ebe | 225 | //----------------------------------------------------------------------------- |