AceUtils
0.6.0
Useful Arduino utilties which are too small as separate libraries, but complex enough to be shared among multiple projects, and often have external dependencies to other libraries.
|
A class that tokenizes a line containing tokens separated on whitespace boundaries, and calls the appropriate command handler to handle the command. More...
#include <CommandDispatcher.h>
Public Member Functions | |
CommandDispatcher (const CommandHandler *const *commands, uint8_t numCommands, const char **argv, uint8_t argvSize) | |
Constructor. More... | |
void | runCommand (Print &printer, char *line) const |
Tokenize the given 'line', run the matching command handler, and send output to the 'printer'. | |
const CommandHandler * | findCommand (const char *cmd) const |
Find the CommandHandler of the given command name. More... | |
Static Public Member Functions | |
static uint8_t | tokenize (char *line, const char **argv, uint8_t argvSize) |
Tokenize the line, separating tokens delimited by whitespace (space, formfeed, carriage return, newline, tab, and vertical tab) and fill argv with each token until argvSize is reached. More... | |
A class that tokenizes a line containing tokens separated on whitespace boundaries, and calls the appropriate command handler to handle the command.
Command have the form "command arg1 arg2 ...", where the 'arg*' can be any string.
The calling code is expected to provide a mapping of the command string (e.g. "list") to its command handler (CommandHandler). The CommandHandler is called with the number of arguments (argc) and the array of tokens (argv), just like the arguments of the C-language main() function.
The template parameters can be either a 'char' for C-strings or '__FlashStringHelper' for F() flash strings.
Definition at line 50 of file CommandDispatcher.h.
|
inline |
Constructor.
commands | Array of CommandHandler pointers. |
numCommands | number of commands. |
argv | Array of (const char*) that will be used to hold the word tokens of a command line string. |
argvSize | The size of the argv array. Tokens which are beyond this limit will be silently dropped. |
Definition at line 62 of file CommandDispatcher.h.
|
inline |
Find the CommandHandler of the given command name.
VisibleForTesting.
NOTE: this is currently a linear O(N) scan which is good enough for small number of commands. If we sorted the handlers, we could do a binary search for O(log(N)) and handle larger number of commands.
Definition at line 108 of file CommandDispatcher.h.
|
inlinestatic |
Tokenize the line, separating tokens delimited by whitespace (space, formfeed, carriage return, newline, tab, and vertical tab) and fill argv with each token until argvSize is reached.
Return the number of tokens filled in.
VisibleForTesting.
Definition at line 88 of file CommandDispatcher.h.