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@14:73ef945b8def, 2014-04-24 (annotated)
- Committer:
- silverpanda
- Date:
- Thu Apr 24 23:27:17 2014 +0000
- Revision:
- 14:73ef945b8def
- Parent:
- 13:019e24491f32
- Child:
- 15:a32db434af65
added Pi
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 | 14:73ef945b8def | 161 | if(findString("generate")) parseGenerate(); |
silverpanda | 9:f9efd3a69c2d | 162 | } |
silverpanda | 9:f9efd3a69c2d | 163 | //----------------------------------------------------------------------------- |
silverpanda | 11:4c6c6d6c0ebe | 164 | |
silverpanda | 12:b846b64e3980 | 165 | void Shell::parseLEDState() |
silverpanda | 11:4c6c6d6c0ebe | 166 | { |
silverpanda | 11:4c6c6d6c0ebe | 167 | // example text: turn blue LED on |
silverpanda | 11:4c6c6d6c0ebe | 168 | |
silverpanda | 11:4c6c6d6c0ebe | 169 | eLEDColor ledColor = lcNone; |
silverpanda | 11:4c6c6d6c0ebe | 170 | if(findString("red")) ledColor = lcRed; |
silverpanda | 11:4c6c6d6c0ebe | 171 | if(findString("blue")) ledColor = lcBlue; |
silverpanda | 11:4c6c6d6c0ebe | 172 | if(findString("green")) ledColor = lcGreen; |
silverpanda | 11:4c6c6d6c0ebe | 173 | |
silverpanda | 12:b846b64e3980 | 174 | // check if color is specified |
silverpanda | 11:4c6c6d6c0ebe | 175 | if(ledColor == lcNone) { |
silverpanda | 11:4c6c6d6c0ebe | 176 | sendText(" -- missing LED color: red, blue or green"); |
silverpanda | 11:4c6c6d6c0ebe | 177 | sendExample(); |
silverpanda | 11:4c6c6d6c0ebe | 178 | } |
silverpanda | 11:4c6c6d6c0ebe | 179 | else { |
silverpanda | 12:b846b64e3980 | 180 | |
silverpanda | 12:b846b64e3980 | 181 | // check if LED is specified |
silverpanda | 11:4c6c6d6c0ebe | 182 | if(findString("LED")) { |
silverpanda | 11:4c6c6d6c0ebe | 183 | |
silverpanda | 12:b846b64e3980 | 184 | // check for state |
silverpanda | 11:4c6c6d6c0ebe | 185 | eState state = stNone; |
silverpanda | 11:4c6c6d6c0ebe | 186 | if(findString("on")) state = stOn; |
silverpanda | 11:4c6c6d6c0ebe | 187 | if(findString("off")) state = stOff; |
silverpanda | 11:4c6c6d6c0ebe | 188 | |
silverpanda | 11:4c6c6d6c0ebe | 189 | if(state == stNone) { |
silverpanda | 12:b846b64e3980 | 190 | |
silverpanda | 12:b846b64e3980 | 191 | // state not specified |
silverpanda | 11:4c6c6d6c0ebe | 192 | sendText(" -- missing state: on or off"); |
silverpanda | 11:4c6c6d6c0ebe | 193 | sendExample(); |
silverpanda | 11:4c6c6d6c0ebe | 194 | } |
silverpanda | 11:4c6c6d6c0ebe | 195 | else { |
silverpanda | 12:b846b64e3980 | 196 | |
silverpanda | 12:b846b64e3980 | 197 | // check if state has changed |
silverpanda | 12:b846b64e3980 | 198 | if(ledColors->getState(ledColor) == state) { |
silverpanda | 12:b846b64e3980 | 199 | |
silverpanda | 12:b846b64e3980 | 200 | // state not changed |
silverpanda | 12:b846b64e3980 | 201 | sendText(" -- already "); |
silverpanda | 12:b846b64e3980 | 202 | (state == stOn) ? sendText("on") : sendText("off"); |
silverpanda | 12:b846b64e3980 | 203 | } |
silverpanda | 12:b846b64e3980 | 204 | else { |
silverpanda | 12:b846b64e3980 | 205 | |
silverpanda | 12:b846b64e3980 | 206 | // state has changed |
silverpanda | 12:b846b64e3980 | 207 | sendText(" -- done!"); |
silverpanda | 12:b846b64e3980 | 208 | ledColors->turn(ledColor, state); |
silverpanda | 12:b846b64e3980 | 209 | } |
silverpanda | 11:4c6c6d6c0ebe | 210 | } |
silverpanda | 11:4c6c6d6c0ebe | 211 | } |
silverpanda | 11:4c6c6d6c0ebe | 212 | else { |
silverpanda | 12:b846b64e3980 | 213 | |
silverpanda | 12:b846b64e3980 | 214 | // LED was not specified |
silverpanda | 11:4c6c6d6c0ebe | 215 | sendText(" -- specify LED"); |
silverpanda | 11:4c6c6d6c0ebe | 216 | sendExample(); |
silverpanda | 11:4c6c6d6c0ebe | 217 | } |
silverpanda | 11:4c6c6d6c0ebe | 218 | } |
silverpanda | 11:4c6c6d6c0ebe | 219 | } |
silverpanda | 11:4c6c6d6c0ebe | 220 | //----------------------------------------------------------------------------- |
silverpanda | 11:4c6c6d6c0ebe | 221 | |
silverpanda | 11:4c6c6d6c0ebe | 222 | void Shell::sendExample() |
silverpanda | 11:4c6c6d6c0ebe | 223 | { |
silverpanda | 11:4c6c6d6c0ebe | 224 | sendText(" -> example: turn red LED on"); |
silverpanda | 11:4c6c6d6c0ebe | 225 | } |
silverpanda | 13:019e24491f32 | 226 | //----------------------------------------------------------------------------- |
silverpanda | 13:019e24491f32 | 227 | |
silverpanda | 14:73ef945b8def | 228 | void Shell::parseGenerate() |
silverpanda | 14:73ef945b8def | 229 | { |
silverpanda | 14:73ef945b8def | 230 | if(findString("pi")) generatePi(); |
silverpanda | 14:73ef945b8def | 231 | else sendText(" -- generate what?"); |
silverpanda | 14:73ef945b8def | 232 | } |
silverpanda | 14:73ef945b8def | 233 | //----------------------------------------------------------------------------- |
silverpanda | 14:73ef945b8def | 234 | |
silverpanda | 13:019e24491f32 | 235 | void Shell::reportPushButtonPress(ePushButton thisPushButton) |
silverpanda | 13:019e24491f32 | 236 | { |
silverpanda | 13:019e24491f32 | 237 | switch(thisPushButton) { |
silverpanda | 13:019e24491f32 | 238 | case pb2: sendText("SW2"); break; |
silverpanda | 13:019e24491f32 | 239 | case pb3: sendText("SW3"); break; |
silverpanda | 13:019e24491f32 | 240 | } |
silverpanda | 13:019e24491f32 | 241 | sendText(" pressed\n> "); |
silverpanda | 13:019e24491f32 | 242 | } |
silverpanda | 14:73ef945b8def | 243 | //----------------------------------------------------------------------------- |
silverpanda | 14:73ef945b8def | 244 | |
silverpanda | 14:73ef945b8def | 245 | #define n 1000//1125 |
silverpanda | 14:73ef945b8def | 246 | #define len 10*n/3 |
silverpanda | 14:73ef945b8def | 247 | |
silverpanda | 14:73ef945b8def | 248 | void Shell::generatePi() |
silverpanda | 14:73ef945b8def | 249 | { |
silverpanda | 14:73ef945b8def | 250 | int i, j, k, q, x, nines, predigit, digits; |
silverpanda | 14:73ef945b8def | 251 | int a[len]; |
silverpanda | 14:73ef945b8def | 252 | |
silverpanda | 14:73ef945b8def | 253 | sendText(" -- Pi = "); |
silverpanda | 14:73ef945b8def | 254 | |
silverpanda | 14:73ef945b8def | 255 | digits=0; |
silverpanda | 14:73ef945b8def | 256 | for (j=1; j<=len; j++) |
silverpanda | 14:73ef945b8def | 257 | a[j] = 2; |
silverpanda | 14:73ef945b8def | 258 | nines = 0; |
silverpanda | 14:73ef945b8def | 259 | predigit = 0;// {First predigit is a 0} |
silverpanda | 14:73ef945b8def | 260 | for (j=1; j<=n; j++) { |
silverpanda | 14:73ef945b8def | 261 | q = 0; |
silverpanda | 14:73ef945b8def | 262 | for(i=len; i>0; i--) { |
silverpanda | 14:73ef945b8def | 263 | x = 10*a[i] + q*i; |
silverpanda | 14:73ef945b8def | 264 | a[i] = x % (2*i - 1); |
silverpanda | 14:73ef945b8def | 265 | q = x / (2*i - 1); |
silverpanda | 14:73ef945b8def | 266 | |
silverpanda | 14:73ef945b8def | 267 | } |
silverpanda | 14:73ef945b8def | 268 | a[1] = q % 10; |
silverpanda | 14:73ef945b8def | 269 | q = q / 10; |
silverpanda | 14:73ef945b8def | 270 | if (q == 9) { |
silverpanda | 14:73ef945b8def | 271 | nines++; |
silverpanda | 14:73ef945b8def | 272 | } else { |
silverpanda | 14:73ef945b8def | 273 | if (q == 10) { |
silverpanda | 14:73ef945b8def | 274 | usbSerial->printf("%d",predigit+1); |
silverpanda | 14:73ef945b8def | 275 | for (k=1; k<=nines; k++) // to nines do |
silverpanda | 14:73ef945b8def | 276 | usbSerial->printf("0");// {zeros} |
silverpanda | 14:73ef945b8def | 277 | predigit = 0; |
silverpanda | 14:73ef945b8def | 278 | nines = 0; |
silverpanda | 14:73ef945b8def | 279 | } else { |
silverpanda | 14:73ef945b8def | 280 | usbSerial->printf("%d",predigit); |
silverpanda | 14:73ef945b8def | 281 | predigit = q; |
silverpanda | 14:73ef945b8def | 282 | if (nines != 0) { // then |
silverpanda | 14:73ef945b8def | 283 | for(k=1; k<=nines; k++) // do |
silverpanda | 14:73ef945b8def | 284 | usbSerial->printf("9"); |
silverpanda | 14:73ef945b8def | 285 | nines = 0; |
silverpanda | 14:73ef945b8def | 286 | } |
silverpanda | 14:73ef945b8def | 287 | } |
silverpanda | 14:73ef945b8def | 288 | digits++; |
silverpanda | 14:73ef945b8def | 289 | digits+=nines; |
silverpanda | 14:73ef945b8def | 290 | } |
silverpanda | 14:73ef945b8def | 291 | } |
silverpanda | 14:73ef945b8def | 292 | } |
silverpanda | 14:73ef945b8def | 293 | //----------------------------------------------------------------------------- |