When debugging code it can be handy to examine/alter variables and to check the state of input lines. So your main program can run and you have access to alter variables and run functions from a terminal. In this sample the main program is just a loop that flashes an LED. In that loop a periodic call is made to ShellTC which handles any commands from the serial terminal. The code is a bit quirky(it was originally written for a very resource limited device) but it works well enough to be useful, hence its published. More details in the main.cpp

Dependencies:   mbed

.

shell.cpp

Committer:
jont
Date:
2015-02-06
Revision:
4:107d2d3294da
Parent:
0:87e65dabdb95

File content as of revision 4:107d2d3294da:

/* Copyright (c) <year> <copyright holders>, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
/*++++++ SHELL.C +++++++*/
//jont@ninelocks.com
//(c)jont@ninelocks.com 1992-2012
//Nasty nasty code intedned to help debug other bits of the project
//If you dont like the code, fix it :-)

#include "string.h"
#include "sio.h"
#include "shell.h"
#include "mbed.h"
#include "mon.h"


char current_level;            /* current mode            */

void ShellCommand(void);        /* decides if command  */
void nu_command(void);            /* finds commmand        */
int  SplitCommand(void);            /* split up the command line    */
void ShellTC(void);                /* called by timed irq        */
/*======================================================================*/
// Init the flags etc
/*======================================================================*/
void ShellInit() {
    current_level = '%';   /* the prompt symbol*/
    commandWaiting = false;
     
}
/*======================================================================*/
/* first stop is command                          */
/*======================================================================*/

void ShellCommand(void) {
    /* for limited damage could strip results codes here
    * so we dont need to check level for each alterable varaiable
    */

    valueChange = SplitCommand();  /* split up the command line */
    //show how command line was broken up to help debug your commands
    if (valueChange != 0) {
        printf("\n\rCOMMAND %s", comBuff);
        printf("\n\rVALUE %s", comBuff+valueChange);
    }

    nu_command();  /* call command router */
    printf("\n\r%c", current_level);
   
}
/*======================================================================*/
/* SplitCommand separates arg and value               */
/*======================================================================*/
/*
* cuts command line in to command
* and an value argument for those which can be set
*/

int SplitCommand(void) {
    int index;
    int valueIndex;
    int length;
    length = strlen(comBuff);
//    printf("\n\rLength %d", length);
    for (index= 0; index <= length; index++) {
        if ( (comBuff[index] == 0x20 ) && (index != length - 1)) {
            comBuff[index]='\0';  /* terminate string */
            valueIndex = index+1;
            printf("\n\rVindex %d", valueIndex);
            return(valueIndex);
        }
    }
    return(0);
}
/*======================================================================*/
/*  nu_command finds and initiates command                */
/*======================================================================*/
/*
* this looks up a command and despatches to its handler
* new functions only need to be added to the array command_table
* and of course be declared before the table
*/
void nu_command(void) {
    int i = 0;
    //  printf("\n\rNC Says %s\n\r",comBuff);
    do {

        //    printf("\n\rNC2 Says %s",command_table[i].commandp);
        if (strcmp(comBuff, command_table[i].commandp) == 0) {
                command_table[i].func_p();

        }
        i++;
    } while (strcmp ( command_table[i].commandp, "") != 0);

}

/*======================================================================*/
/* the handler that does the actual command                */
/*======================================================================*/

void ShellTC() {

    if ( commandWaiting == true ) {
        ShellCommand();
        commandWaiting = false;
    }

}