The CommandProcessor is the interface to install a run-time menu into an embedded system.

Dependents:   A_CANAdapter USB2I2C

Revision:
6:1a0512faa75d
Parent:
5:a98bd1f2fd59
Child:
7:0f058d664b21
--- a/CommandProcessor.c	Thu Mar 31 00:33:18 2011 +0000
+++ b/CommandProcessor.c	Sat Apr 02 17:12:39 2011 +0000
@@ -39,8 +39,9 @@
 
 static struct
 {
-    int bufferSize;            // size of the buffer
     int caseinsensitive;    // FALSE=casesensitive, TRUE=insensitive
+    int echo;               // TRUE=echo on, FALSE=echo off
+    int bufferSize;         // size of the buffer
     int (*kbhit)(void);
     int (*getch)(void);
     int (*putch)(int ch);
@@ -50,6 +51,7 @@
 static INITRESULT_T CommandProcessor_Init(
     int defaultMenu,
     int caseinsensitive,
+    int echo,
     int maxCmdLen, 
     int (*kbhit)(void),
     int (*getch)(void),
@@ -59,21 +61,25 @@
 static ADDRESULT_T CommandProcessor_Add(CMD_T *m);
 static RUNRESULT_T CommandProcessor_Run(void);
 static RUNRESULT_T CommandProcessor_End(void);
+static RUNRESULT_T CommandProcessor_Echo(int echo);
 
 static CMDP_T CommandProcessor = 
 {
     CommandProcessor_Init,
     CommandProcessor_Add,
     CommandProcessor_Run,
+    CommandProcessor_Echo,
     CommandProcessor_End
 };
 
 static RUNRESULT_T Help(char *p);
+static RUNRESULT_T Echo(char *p);
 static RUNRESULT_T Exit(char *p);
 static RUNRESULT_T About(char *p);
 
 static CMD_T HelpMenu = {"Help", "Shows this help, 'Help ?' shows more details.", Help, visible};
 static CMD_T QuestionMenu = {"?", "Shows this help, '? ?' shows more details.", Help, visible};
+static CMD_T EchoMenu = {"Echo", "Echo [1|on|0|off] turns echo on or off.", Echo, visible};
 static CMD_T AboutMenu = {"About", "About this CommandProcessor", About, visible};
 static CMD_T ExitMenu = {"Exit", "Exits the program", Exit, visible};
 
@@ -100,6 +106,20 @@
     return runok;
 }
 
+static RUNRESULT_T Echo(char *p) {
+    if (*p) {
+        if (*p == '1' || mystrnicmp(p, "on", 2) == 0)
+            CommandProcessor_Echo(1);
+        if (*p == '0' || mystrnicmp(p, "off", 3) == 0)
+            CommandProcessor_Echo(0);
+    }
+    if (cfg.echo)
+        cfg.puts("\r\nEcho is on");
+    else
+        cfg.puts("\r\nEcho is off");
+    return runok;
+}
+
 static RUNRESULT_T Exit(char *p)
 {
     (void)p;
@@ -330,6 +350,7 @@
 INITRESULT_T CommandProcessor_Init(
     int addDefaultMenu, 
     int caseinsensitive,
+    int echo,
     int maxCmdLen, 
     int (*kbhit)(void),
     int (*getch)(void),
@@ -343,15 +364,18 @@
     cfg.bufferSize = maxCmdLen;
     if (buffer)
     {
-        if (addDefaultMenu & 0x0004)
+        if (addDefaultMenu & 0x0008)
             CommandProcessor.Add(&QuestionMenu);
+        if (addDefaultMenu & 0x0008)
+            CommandProcessor.Add(&HelpMenu);
         if (addDefaultMenu & 0x0004)
-            CommandProcessor.Add(&HelpMenu);
+            CommandProcessor.Add(&EchoMenu);            
         if (addDefaultMenu & 0x0002)
             CommandProcessor.Add(&AboutMenu);
         if (addDefaultMenu & 0x0001)
             CommandProcessor.Add(&ExitMenu);
         cfg.caseinsensitive = caseinsensitive;
+        cfg.echo = echo;
         cfg.kbhit = kbhit;
         cfg.getch = getch;
         cfg.putch = putch;
@@ -430,7 +454,7 @@
     CMD_T *cbk = NULL;
     char * params = NULL;
 
-    if (showPrompt)
+    if (showPrompt && cfg.echo)
     {
         cfg.putch('>');
         showPrompt = FALSE;
@@ -528,6 +552,11 @@
 }
 
 
+static RUNRESULT_T CommandProcessor_Echo(int echo) {
+    cfg.echo = echo;
+    return runok;
+}
+
 /// End the CommandProcessor by freeing all the memory that was allocated
 ///
 ///    returns runok