A light Command Dispatcher Library with commands linked to your functions.

Dependents:   Rocket

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);
}

History

Add #ifndef on headers default tip

2014-09-03, by rominos2 [Wed, 03 Sep 2014 19:33:41 +0000] rev 4

Add #ifndef on headers


Add License headers.; Remove some deprecated #define.; Optimize memory managment of Commands.

2014-09-03, by rominos2 [Wed, 03 Sep 2014 10:53:38 +0000] rev 3

Add License headers.; Remove some deprecated #define.; Optimize memory managment of Commands.


Little fix in the API documentation

2014-09-03, by rominos2 [Wed, 03 Sep 2014 09:59:28 +0000] rev 2

Little fix in the API documentation


Change Library name for better understanding.; Modified some data types for better reading.

2014-09-03, by rominos2 [Wed, 03 Sep 2014 09:50:45 +0000] rev 1

Change Library name for better understanding.; Modified some data types for better reading.


Initial Release

2014-09-02, by rominos2 [Tue, 02 Sep 2014 22:07:18 +0000] rev 0

Initial Release