A light Command Dispatcher Library with commands linked to your functions.
You can register your commands and the functions linked.
On each execution of a command (char array), it will parse the array and send all the parameters to the functions.
Here is a quick example :
#include "mbed.h" #include "CommandDispatcher.h" Serial pc(USBTX, USBRX); void echoCommand(unsigned int argc, char* argv[], char* result); int main() { CommandDispatcher disp = CommandDispatcher(); char buffer[50]; char result[50]; int i=0; // register a command disp.addCommand("echo", echoCommand); pc.printf("Example Command Dispatcher\n\n"); while(true) { // get a complete line from serial buffer[i++] = pc.getc(); if (buffer[i-1]=='\n') { buffer[i-1]='\0'; i=0; // send it to the dispatcher and print result if (disp.executeCommand(buffer, result)) { pc.printf("%s\n", result); } else { pc.printf("Command not found.\n"); } } } } // the actual function called void echoCommand(unsigned int argc, char* argv[], char* result) { int i; sprintf(result, ""); for (i=1; i<argc; i++) { sprintf(result, "%s %s", result, argv[i]); } sprintf(result, "%s\n", result); }
Revision 2:695d21706b72, committed 2014-09-03
- Comitter:
- rominos2
- Date:
- Wed Sep 03 09:59:28 2014 +0000
- Parent:
- 1:855efbf6d7ae
- Child:
- 3:8784fbd35d29
- Commit message:
- Little fix in the API documentation
Changed in this revision
CommandDispatcher.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/CommandDispatcher.h Wed Sep 03 09:50:45 2014 +0000 +++ b/CommandDispatcher.h Wed Sep 03 09:59:28 2014 +0000 @@ -83,13 +83,13 @@ /** Execute a command. It will check each registered command with its name. @param command the command with the arguments in a single char array. @param result a char array alocated by the user and sent to the function for feedback use. + @return true if a registered command was found, else false. */ bool executeCommand(char* command, char* result); /** A Utility function to transform a char array to lower case (useful for argument checks). \n WARNING : This function modifies the word. @param word the word to modify. - */ static void toLowerCase(char* word); }; \ No newline at end of file