A Command Interpreter with support for used defined commands, subsystems, macros, help and parameter parsing.

Committer:
wvd_vegt
Date:
Fri Feb 11 10:26:33 2011 +0000
Revision:
7:269c2445b8f5
Child:
8:83edd6addbd5
Version 0.7

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wvd_vegt 7:269c2445b8f5 1 #include <vector>
wvd_vegt 7:269c2445b8f5 2
wvd_vegt 7:269c2445b8f5 3 #include "mbed.h"
wvd_vegt 7:269c2445b8f5 4 #include "cmdb.h"
wvd_vegt 7:269c2445b8f5 5
wvd_vegt 7:269c2445b8f5 6 DigitalOut myled(LED1);
wvd_vegt 7:269c2445b8f5 7
wvd_vegt 7:269c2445b8f5 8 //We'll be using the Usb Serial port
wvd_vegt 7:269c2445b8f5 9 Serial serial(USBTX, USBRX); //tx, rx
wvd_vegt 7:269c2445b8f5 10
wvd_vegt 7:269c2445b8f5 11 #define CID_TEST 1
wvd_vegt 7:269c2445b8f5 12 #define CID_INT 2
wvd_vegt 7:269c2445b8f5 13
wvd_vegt 7:269c2445b8f5 14 /** Sample User Command Dispatcher.
wvd_vegt 7:269c2445b8f5 15 *
wvd_vegt 7:269c2445b8f5 16 * @parm cmdb the command interpreter object.
wvd_vegt 7:269c2445b8f5 17 * @parm cid the command id.
wvd_vegt 7:269c2445b8f5 18 */
wvd_vegt 7:269c2445b8f5 19 void my_dispatcher(Cmdb& cmdb, int cid) {
wvd_vegt 7:269c2445b8f5 20 cmdb.printf("my_dispatcher: cid=%d\r\n", cid);
wvd_vegt 7:269c2445b8f5 21
wvd_vegt 7:269c2445b8f5 22 switch (cid) {
wvd_vegt 7:269c2445b8f5 23 case CID_INT :
wvd_vegt 7:269c2445b8f5 24 cmdb.printf("my_dispatcher: parm 0=%d\r\n",cmdb.INTPARM(0));
wvd_vegt 7:269c2445b8f5 25 break;
wvd_vegt 7:269c2445b8f5 26 }
wvd_vegt 7:269c2445b8f5 27 }
wvd_vegt 7:269c2445b8f5 28
wvd_vegt 7:269c2445b8f5 29 const cmd c1("Test",SUBSYSTEM,CID_TEST,"" ,"* Test Subsystem");
wvd_vegt 7:269c2445b8f5 30 const cmd c2("Int" ,CID_TEST ,CID_INT ,"%i","* Int as parameter" ,"dummy");
wvd_vegt 7:269c2445b8f5 31
wvd_vegt 7:269c2445b8f5 32 int main() {
wvd_vegt 7:269c2445b8f5 33 // Set the Baudrate.
wvd_vegt 7:269c2445b8f5 34 serial.baud(115200);
wvd_vegt 7:269c2445b8f5 35
wvd_vegt 7:269c2445b8f5 36 // Test the serial connection by
wvd_vegt 7:269c2445b8f5 37 serial.printf("\r\n\r\nCmdb Command Interpreter Demo Version %0.2f.\r\n\r\n", Cmdb::version());
wvd_vegt 7:269c2445b8f5 38
wvd_vegt 7:269c2445b8f5 39 //Create a Command Table Vector.
wvd_vegt 7:269c2445b8f5 40 std::vector<cmd> cmds;
wvd_vegt 7:269c2445b8f5 41
wvd_vegt 7:269c2445b8f5 42 //Add some of our own first...
wvd_vegt 7:269c2445b8f5 43 cmds.push_back(c1); //Test Subsystem is handled by Cmdb internally.
wvd_vegt 7:269c2445b8f5 44 cmds.push_back(c2); //The Int Command is handled by our 'my_dispatcher' method.
wvd_vegt 7:269c2445b8f5 45
wvd_vegt 7:269c2445b8f5 46 //Add some predefined...
wvd_vegt 7:269c2445b8f5 47 cmds.push_back(BOOT); //Handled by Cmdb internally.
wvd_vegt 7:269c2445b8f5 48
wvd_vegt 7:269c2445b8f5 49 cmds.push_back(ECHO); //Handled by Cmdb internally.
wvd_vegt 7:269c2445b8f5 50 cmds.push_back(BOLD); //Handled by Cmdb internally.
wvd_vegt 7:269c2445b8f5 51 cmds.push_back(CLS); //Handled by Cmdb internally.
wvd_vegt 7:269c2445b8f5 52
wvd_vegt 7:269c2445b8f5 53 //Add some predefined and mandatory...
wvd_vegt 7:269c2445b8f5 54 cmds.push_back(IDLE); //Handled by Cmdb internally.
wvd_vegt 7:269c2445b8f5 55 cmds.push_back(HELP); //Handled by Cmdb internally.
wvd_vegt 7:269c2445b8f5 56
wvd_vegt 7:269c2445b8f5 57 //Create and initialize the Command Interpreter.
wvd_vegt 7:269c2445b8f5 58 Cmdb cmdb(serial, cmds, &my_dispatcher);
wvd_vegt 7:269c2445b8f5 59
wvd_vegt 7:269c2445b8f5 60 while (1) {
wvd_vegt 7:269c2445b8f5 61 //Check for input...
wvd_vegt 7:269c2445b8f5 62 if (cmdb.hasnext()==true) {
wvd_vegt 7:269c2445b8f5 63
wvd_vegt 7:269c2445b8f5 64 //Supply input to Command Interpreter
wvd_vegt 7:269c2445b8f5 65 if (cmdb.scan(cmdb.next())) {
wvd_vegt 7:269c2445b8f5 66
wvd_vegt 7:269c2445b8f5 67 //Flash led when a command has been parsed and dispatched.
wvd_vegt 7:269c2445b8f5 68 myled = 1;
wvd_vegt 7:269c2445b8f5 69 wait(0.2);
wvd_vegt 7:269c2445b8f5 70
wvd_vegt 7:269c2445b8f5 71 //cmdb.print("Command Parsed and Dispatched\r\n");
wvd_vegt 7:269c2445b8f5 72
wvd_vegt 7:269c2445b8f5 73 myled = 0;
wvd_vegt 7:269c2445b8f5 74 wait(0.2);
wvd_vegt 7:269c2445b8f5 75 }
wvd_vegt 7:269c2445b8f5 76 }
wvd_vegt 7:269c2445b8f5 77
wvd_vegt 7:269c2445b8f5 78 }
wvd_vegt 7:269c2445b8f5 79 }