AceRoutine
1.0.1
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
|
25 #ifndef ACE_ROUTINE_COMMAND_DISPATCHER_H
26 #define ACE_ROUTINE_COMMAND_DISPATCHER_H
29 #include <AceRoutine.h>
30 #include "CommandHandler.h"
31 #include "InputLine.h"
33 class __FlashStringHelper;
35 namespace ace_routine {
79 mNumCommands(numCommands),
95 static uint8_t
tokenize(
char* line,
const char** argv, uint8_t argvSize) {
96 char* token = strtok(line, DELIMS);
98 while (token !=
nullptr && argc < argvSize) {
101 token = strtok(
nullptr, DELIMS);
109 if (mPrompt !=
nullptr) {
110 mPrinter.print(mPrompt);
112 COROUTINE_CHANNEL_READ(mChannel, input);
114 if (input.status == InputLine::kStatusOverflow) {
119 if (mPrompt !=
nullptr) {
120 mPrinter.print(input.line);
136 for (uint8_t i = 0; i < mNumCommands; i++) {
158 void helpAll(Print& printer)
const;
161 bool helpSpecific(Print& printer,
const char* cmd)
const;
173 static const uint8_t STATUS_SUCCESS = 0;
174 static const uint8_t STATUS_BUFFER_OVERFLOW = 1;
175 static const uint8_t STATUS_FLUSH_TO_EOL = 2;
176 static const char DELIMS[];
181 uint8_t
const mNumCommands;
182 const char**
const mArgv;
183 uint8_t
const mArgvSize;
184 const char*
const mPrompt;
void helpAll(Print &printer) const
Print help on all commands.
static uint8_t tokenize(char *line, const char **argv, uint8_t argvSize)
Tokenize the line, separating tokens delimited by whitespace (space, formfeed, carriage return,...
void findAndRunCommand(const char *cmd, int argc, const char *const *argv) const
Find and run the given command.
static void printHelp(Print &printer, const CommandHandler *command)
Print help string for the given command.
#define COROUTINE_LOOP()
Mark the beginning of a coroutine loop.
virtual ~CommandDispatcher()
Destructor.
int compareTo(const FCString &that) const
Compare to another FCString.
A union of (const char*) and (const __FlashStringHelper*) with a discriminator.
void printLineError(const char *line, uint8_t statusCode) const
Print the input line that caused an error along with its status code.
void runCommand(char *line) const
Tokenize the given line and run the command handler.
An unbuffered synchronized channel.
Signature for a command handler.
CommandDispatcher(Channel< InputLine > &channel, Print &printer, const CommandHandler *const *commands, uint8_t numCommands, const char **argv, uint8_t argvSize, const char *prompt)
Constructor.
A coroutine that reads lines from the Serial port, tokenizes the line on whitespace boundaries,...
bool helpSpecific(Print &printer, const char *cmd) const
Print helpString of specific cmd.
Base class of all coroutines.
const CommandHandler * findCommand(const char *cmd) const
Find the CommandHandler of the given command name.
void helpCommandHandler(Print &printer, int argc, const char *const *argv) const
Handle the 'help' command.
int runCoroutine() override
The body of the coroutine.
FCString getName() const
Return the name of the command.