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); }
CommandDispatcher.h@2:695d21706b72, 2014-09-03 (annotated)
- Committer:
- rominos2
- Date:
- Wed Sep 03 09:59:28 2014 +0000
- Revision:
- 2:695d21706b72
- Parent:
- 1:855efbf6d7ae
- Child:
- 3:8784fbd35d29
Little fix in the API documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rominos2 | 1:855efbf6d7ae | 1 | #include "mbed.h" |
rominos2 | 1:855efbf6d7ae | 2 | |
rominos2 | 1:855efbf6d7ae | 3 | #define MAX_ARG_SIZE 20 |
rominos2 | 1:855efbf6d7ae | 4 | #define DEFAULT_BUFFER_SIZE 50 |
rominos2 | 1:855efbf6d7ae | 5 | |
rominos2 | 1:855efbf6d7ae | 6 | /** A light Command Dispatcher Library \n |
rominos2 | 1:855efbf6d7ae | 7 | You can link commands to functions. \n |
rominos2 | 1:855efbf6d7ae | 8 | On each execution of a command (char array), it will parse the array and send all the parameters to the functions. \n |
rominos2 | 1:855efbf6d7ae | 9 | Here is an quick example class that to show the purpose of this library. |
rominos2 | 1:855efbf6d7ae | 10 | @code |
rominos2 | 1:855efbf6d7ae | 11 | #include "mbed.h" |
rominos2 | 1:855efbf6d7ae | 12 | #include "CommandDispatcher.h" |
rominos2 | 1:855efbf6d7ae | 13 | |
rominos2 | 1:855efbf6d7ae | 14 | Serial pc(USBTX, USBRX); |
rominos2 | 1:855efbf6d7ae | 15 | |
rominos2 | 1:855efbf6d7ae | 16 | void echoCommand(unsigned int argc, char* argv[], char* result); |
rominos2 | 1:855efbf6d7ae | 17 | |
rominos2 | 1:855efbf6d7ae | 18 | int main() { |
rominos2 | 1:855efbf6d7ae | 19 | CommandDispatcher disp = CommandDispatcher(); |
rominos2 | 1:855efbf6d7ae | 20 | char buffer[50]; |
rominos2 | 1:855efbf6d7ae | 21 | char result[50]; |
rominos2 | 1:855efbf6d7ae | 22 | int i=0; |
rominos2 | 1:855efbf6d7ae | 23 | |
rominos2 | 1:855efbf6d7ae | 24 | disp.addCommand("echo", echoCommand); |
rominos2 | 1:855efbf6d7ae | 25 | pc.printf("Example Command Dispatcher\n\n"); |
rominos2 | 1:855efbf6d7ae | 26 | |
rominos2 | 1:855efbf6d7ae | 27 | while(true) { |
rominos2 | 1:855efbf6d7ae | 28 | buffer[i++] = pc.getc(); |
rominos2 | 1:855efbf6d7ae | 29 | if (buffer[i-1]=='\n') { |
rominos2 | 1:855efbf6d7ae | 30 | buffer[i-1]='\0'; |
rominos2 | 1:855efbf6d7ae | 31 | i=0; |
rominos2 | 1:855efbf6d7ae | 32 | |
rominos2 | 1:855efbf6d7ae | 33 | if (disp.executeCommand(buffer, result)) { |
rominos2 | 1:855efbf6d7ae | 34 | pc.printf("%s\n", result); |
rominos2 | 1:855efbf6d7ae | 35 | } else { |
rominos2 | 1:855efbf6d7ae | 36 | pc.printf("Command not found.\n"); |
rominos2 | 1:855efbf6d7ae | 37 | } |
rominos2 | 1:855efbf6d7ae | 38 | } |
rominos2 | 1:855efbf6d7ae | 39 | } |
rominos2 | 1:855efbf6d7ae | 40 | } |
rominos2 | 1:855efbf6d7ae | 41 | |
rominos2 | 1:855efbf6d7ae | 42 | void echoCommand(unsigned int argc, char* argv[], char* result) { |
rominos2 | 1:855efbf6d7ae | 43 | int i; |
rominos2 | 1:855efbf6d7ae | 44 | sprintf(result, ""); |
rominos2 | 1:855efbf6d7ae | 45 | for (i=1; i<argc; i++) { |
rominos2 | 1:855efbf6d7ae | 46 | sprintf(result, "%s %s", result, argv[i]); |
rominos2 | 1:855efbf6d7ae | 47 | } |
rominos2 | 1:855efbf6d7ae | 48 | sprintf(result, "%s\n", result); |
rominos2 | 1:855efbf6d7ae | 49 | } |
rominos2 | 1:855efbf6d7ae | 50 | @endcode |
rominos2 | 1:855efbf6d7ae | 51 | */ |
rominos2 | 1:855efbf6d7ae | 52 | class CommandDispatcher { |
rominos2 | 1:855efbf6d7ae | 53 | private: |
rominos2 | 1:855efbf6d7ae | 54 | class Command { |
rominos2 | 1:855efbf6d7ae | 55 | private: |
rominos2 | 1:855efbf6d7ae | 56 | char _name[MAX_ARG_SIZE]; |
rominos2 | 1:855efbf6d7ae | 57 | void(*_function)(unsigned int argc, char* argv[], char* result); |
rominos2 | 1:855efbf6d7ae | 58 | Command* _next; |
rominos2 | 1:855efbf6d7ae | 59 | |
rominos2 | 1:855efbf6d7ae | 60 | friend class CommandDispatcher; |
rominos2 | 1:855efbf6d7ae | 61 | }; |
rominos2 | 1:855efbf6d7ae | 62 | |
rominos2 | 1:855efbf6d7ae | 63 | CommandDispatcher::Command* _first_command; |
rominos2 | 1:855efbf6d7ae | 64 | |
rominos2 | 1:855efbf6d7ae | 65 | int parse_buffer(char* buffer, char*** argv); |
rominos2 | 1:855efbf6d7ae | 66 | |
rominos2 | 1:855efbf6d7ae | 67 | public: |
rominos2 | 1:855efbf6d7ae | 68 | /** Constructor of the Command Dispatcher |
rominos2 | 1:855efbf6d7ae | 69 | */ |
rominos2 | 1:855efbf6d7ae | 70 | CommandDispatcher(); |
rominos2 | 1:855efbf6d7ae | 71 | |
rominos2 | 1:855efbf6d7ae | 72 | /** Add a command to the Dispatcher. |
rominos2 | 1:855efbf6d7ae | 73 | @param commandName the name of the command. Not case sensitive. |
rominos2 | 1:855efbf6d7ae | 74 | @param commandFunction the pointer to the function called when function is executed. \n |
rominos2 | 1:855efbf6d7ae | 75 | The 2 first parameters are liked a C code main (number and value of arguments). \n |
rominos2 | 1:855efbf6d7ae | 76 | The last (result) is got on command execution and transport a potential result to the executor. \n |
rominos2 | 1:855efbf6d7ae | 77 | */ |
rominos2 | 1:855efbf6d7ae | 78 | void addCommand(char* commandName, void(*commandFunction)(unsigned int argc, char* argv[], char* result)); |
rominos2 | 1:855efbf6d7ae | 79 | /** Clean all the commands (free memory allocated). |
rominos2 | 1:855efbf6d7ae | 80 | */ |
rominos2 | 1:855efbf6d7ae | 81 | void cleanCommands(); |
rominos2 | 1:855efbf6d7ae | 82 | |
rominos2 | 1:855efbf6d7ae | 83 | /** Execute a command. It will check each registered command with its name. |
rominos2 | 1:855efbf6d7ae | 84 | @param command the command with the arguments in a single char array. |
rominos2 | 1:855efbf6d7ae | 85 | @param result a char array alocated by the user and sent to the function for feedback use. |
rominos2 | 2:695d21706b72 | 86 | @return true if a registered command was found, else false. |
rominos2 | 1:855efbf6d7ae | 87 | */ |
rominos2 | 1:855efbf6d7ae | 88 | bool executeCommand(char* command, char* result); |
rominos2 | 1:855efbf6d7ae | 89 | |
rominos2 | 1:855efbf6d7ae | 90 | /** A Utility function to transform a char array to lower case (useful for argument checks). \n |
rominos2 | 1:855efbf6d7ae | 91 | WARNING : This function modifies the word. |
rominos2 | 1:855efbf6d7ae | 92 | @param word the word to modify. |
rominos2 | 1:855efbf6d7ae | 93 | */ |
rominos2 | 1:855efbf6d7ae | 94 | static void toLowerCase(char* word); |
rominos2 | 1:855efbf6d7ae | 95 | }; |