Henk Meewis / Mbed 2 deprecated USB_serial_LED_controller

Dependencies:   mbed

Fork of frdm_echo by Henk Meewis

shell.cpp

Committer:
silverpanda
Date:
2014-04-15
Revision:
10:85a8da3b7e5e
Parent:
9:f9efd3a69c2d
Child:
11:4c6c6d6c0ebe

File content as of revision 10:85a8da3b7e5e:

#include "mbed.h"
#include "LEDColors.h"
#include "shell.h"

extern LEDColors *ledColors;

// create contructor
Shell::Shell(uint32_t thisBaudRate)
{
    usbSerial = new Serial(USBTX, USBRX);
    usbSerial->baud(115200);
    characterCount = 0;
    characterPointer = 0;
}
//-----------------------------------------------------------------------------

void Shell::sendText(char *thisText)
{
    // this can send any text
    usbSerial->printf(thisText);
}
//-----------------------------------------------------------------------------

void Shell::sendHelloWorld()
{
    // sends the first greeting
    sendText(" ** K64 shell **\n>\n> ");
}
//-----------------------------------------------------------------------------

void Shell::scanUSBSerialRx()
{
    // check if there is something to read
    if(usbSerial->readable()) {

        // if so ...
        char character = usbSerial->getc();

        // see if this is a semi colon or a carriage return
        // if so, give a new line cursor
        if((character == ';') || (character == 13)) {
            finishCharacterBuffer();
            parseCommands();
            usbSerial->printf("\n> ");
            ledColors->flashWhite(10);
        }

        // if not, just print the character
        else if(addCharacterToBuffer(character)) {
            usbSerial->printf("%c", character);
            ledColors->flashGreen(10);
        }
    }
}
//-----------------------------------------------------------------------------

bool Shell::addCharacterToBuffer(char newCharacter)
{
    bool accept = (characterPointer < ItsInputBufferSize_);
    if(accept) {
        inputBuffer[characterPointer++] = newCharacter;
        characterCount = characterPointer;
    }
    return accept;
}
//-----------------------------------------------------------------------------

void Shell::finishCharacterBuffer()
{
    inputBuffer[characterCount] = 0;
    characterCount = characterPointer;
    characterPointer = 0;
}
//-----------------------------------------------------------------------------

bool Shell::findString(char *thisString, uint32_t stringLength)
{
    bool found = false;
    uint32_t startPointer = 0, characterPointer, newStartLocation;
    char firstChar, secondChar;
    
    // 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;
}
//-----------------------------------------------------------------------------

void Shell::parseCommands()
{
    if(findString("set", 3)) sendText(" -- \"set\" found!");
}
//-----------------------------------------------------------------------------