Henk Meewis / Mbed 2 deprecated USB_serial_LED_controller

Dependencies:   mbed

Fork of frdm_echo by Henk Meewis

Revision:
10:85a8da3b7e5e
Parent:
9:f9efd3a69c2d
Child:
11:4c6c6d6c0ebe
--- a/shell.cpp	Mon Apr 14 21:02:10 2014 +0000
+++ b/shell.cpp	Tue Apr 15 03:12:29 2014 +0000
@@ -24,7 +24,7 @@
 void Shell::sendHelloWorld()
 {
     // sends the first greeting
-    sendText("** Hello World **\n\n> ");
+    sendText(" ** K64 shell **\n>\n> ");
 }
 //-----------------------------------------------------------------------------
 
@@ -67,6 +67,8 @@
 
 void Shell::finishCharacterBuffer()
 {
+    inputBuffer[characterCount] = 0;
+    characterCount = characterPointer;
     characterPointer = 0;
 }
 //-----------------------------------------------------------------------------
@@ -74,25 +76,59 @@
 bool Shell::findString(char *thisString, uint32_t stringLength)
 {
     bool found = false;
-    uint32_t startPointer = 0, characterPointer;
+    uint32_t startPointer = 0, characterPointer, newStartLocation;
     char firstChar, secondChar;
-
-    while(!found && (startPointer <= characterCount - stringLength)) {
-
+    
+    // start at the start position (0) on the input buffer
+    while(!found 
+        && (characterCount >= stringLength)
+        && (startPointer <= characterCount - stringLength)) {
+        
+        // start at the beginning of the compare string (thisString)
         found = true;
         characterPointer = 0;
         while(found && (characterPointer < stringLength)) {
+            
+            // get and compare characters
             firstChar = inputBuffer[startPointer + characterPointer];
             secondChar = thisString[characterPointer];
             found = (firstChar == secondChar);
             
+            // for debugging
             // usbSerial->printf("(compare %c with %c)\n", firstChar, secondChar);
             
+            // move up to next characters
             characterPointer++;
         }
+        
+        // move start position
+        if(found) {
+            newStartLocation = startPointer + stringLength;
+            
+            // clean up from buffer
+            characterPointer = newStartLocation;
+            while(characterPointer < characterCount) {
+                firstChar = inputBuffer[characterPointer];
+                inputBuffer[characterPointer - newStartLocation] = firstChar;
+                characterPointer++;
+            }
+            characterCount -= newStartLocation;
+            inputBuffer[characterCount] = 0;
+            
+            // for debugging send buffer
+            /*
+            if(characterCount) {
+                sendText(" is now \"");
+                sendText(inputBuffer);
+                sendText("\"");
+            }
+            */
+        }
+        
         startPointer++;
     }
-
+    
+    // report
     return found;
 }
 //-----------------------------------------------------------------------------
@@ -102,4 +138,3 @@
     if(findString("set", 3)) sendText(" -- \"set\" found!");
 }
 //-----------------------------------------------------------------------------
-