Skip to content

Gfy63/ParseCommands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ParseCommands

Evaluate commands entered over serial or as string and call a defined function.

  • Use serial stream as input.
  • Use string as input.
  • Config commands in a struct.
  • EOL auto detection. ( CRLF, LFCR, CR or LF )
  • Max input buffer size and argument count configurable.
  • Event callback for debugging, echo, log and more.
  • Error detection.

Caution

  • From version 1.7.0 the command setEOL() is obselate.
  • From version 1.5.0 the struct ParseCommand::command_t is change to pcmd_command_t

DOWNLOAD

Download from github.

BASIC USAGE

INSTANTIATE

#include <ParseCommands.h>

ParseCommands pCmd;         // Constructor.

// Define the commands and hear callback functions
struct pcmd_command_t commandList[] = {
// command, callback function
    "test", CmdTest,
    "test2", CmdTest2,
    NULL, NULL              // END OF LIST (NEEDED)
};

void setup()
{
    pCmd.begin( commandList );
}

// Create all in the struct defined callback functions
void CmdTest( int argc, char *argv[] )
{
    Serial.println( "test()");
}

SETUP

The commands and callback functions are defined in a struct. The commands are case sensitive.

// Define the commands and hear callback functions
struct pcmd_command_t commandList[] = {
// command, callback function
    ";", CmdComment,
    "test", CmdTest,
    "test2", CmdTest2,
    NULL, NULL              // END OF LIST (NEEDED)
};

The last entry in the command list must by NULL, NULL, this terminate the list.

LOOP

// Pass a serial input to the parser.
if( Serial.available() ) {pCmd.read( Serial.read() ); }

The CR finishes a input stream and start the parser. The LF is ignored.

Constructors

Constructor or begin() get command list, optional input buffer size and optional max argument count.

ParseCommands( pcmd_command_t *c, size_t bufferSize, size_t argCnt );
or
begin( pcmd_command_t *c, size_t bufferSize, size_t argCnt );

Constructor get command list, input buffer size.

ParseCommands( pcmd_command_t *c, size_t bufferSize );
or
begin( pcmd_command_t *c, size_t bufferSize );

Constructor get command list.

ParseCommands( pcmd_command_t *c );
or
begin( pcmd_command_t *c );

Default bufferSize is 16 char.
Default argCnt is 3.

Note

The buffer size is only limited to the memory size in the device. If the size exceed the device possibilities, you get an error, and no input is possible.

Loop method

This is the only method to be call from the loop. void read() read a char from any stream and complete the command. After the CR or LF detected the command is parsed and fires the appropriate callback function.

Arguments

The command can be followed by serval arguments. All arguments are separated by a space. If an argument need to contain a space, put it between quotes ("). You can use the escape character \ to put a quote in this string.

"Test \"aaa\"" ==> Test "aaa"

Comment string

If you use the comment string, the arguments aren't split and stay together in one argument. The default comment string is ;.

Exp: "; 1 2 3" ==> Comment ; with argument 0 be 1 2 3

The comment string can be change with setCommentString. It can be up to 8 char long.

The comment string command must be determined as a normal command in the command struct.

You can use the comment string to send infos as a help text.

Call by string

A command can also pass as string. (exp.: fired from a button push)

// Pass a string command with 3 arguments.
pCmd.doCommand( "test 1 2 3" );

Debugging event

The eventHandler() define a function that can be use for debugging, echo, log and more.

void PCmd_EventHandler( int event );

pCmd.EventCallback( PCmd_EventCallback );

void PCmd_EventCallback( int event )
{
    ...
}

Posible events are:

    PCMD_INPUT_CHAR_EVT
    PCMD_READ_COMMAND_EVT
    PCMD_DO_COMMAND_EVT
    PCMD_ERROR_EVT

Use getLastCharRead() and getLastCommand() to get the unfiltered input.

Callback function

After the command is parsed, the callback function is called.

He has 2 arguments. (exp. void test( int argc, char *argv[]) )

  • argc is the argument count. 0 for no arguments passed.
  • argv is the list of the arguments as string.

Errors

The read() or doCommand() return false if an error accoutered.

Use getError() to get the error code and getErrorText() to get the error text.

    PCMD_COMMAND_OK 
	PCMD_TOO_MANY_ARGUMENTS_ERR
    PCMD_CMD_NOT_FOUND_ERR
	PCMD_INPUT_TO_LONG_ERR
    PCMD_TOO_MANY_CHAR_ERR
    PCMD_EMPLY_LINE_ERR
	PCMD_MEM_ALLOCATION_ERR

About

Arduino library- Evaluate commands entered over serial or as string and call a defined function.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages