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

Dependents:   A_CANAdapter USB2I2C

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Sat Apr 02 17:12:39 2011 +0000
Parent:
5:a98bd1f2fd59
Child:
7:0f058d664b21
Commit message:
Added ability to turn prompt echo on or off

Changed in this revision

CommandProcessor.c Show annotated file Show diff for this revision Revisions of this file
CommandProcessor.h Show annotated file Show diff for this revision Revisions of this file
--- 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
--- a/CommandProcessor.h	Thu Mar 31 00:33:18 2011 +0000
+++ b/CommandProcessor.h	Sat Apr 02 17:12:39 2011 +0000
@@ -161,25 +161,27 @@
     /// This function has a number of parameters, which make the CommandProcessor quite flexible.
     /// The user can enable a default menu, which can consist of the following functions.
     /// Note that when the [bit] is set, that menu item is enabled.
-    /// * [2] Help - which in turn will show all the menu items and their brief descriptions
+    /// * [3] Help - which in turn will show all the menu items and their brief descriptions
+    /// * [2] Echo - which adds the echo command help
     /// * [1] About - just a tiny statement about the CommandProcessor itself
     /// * [0] Exit - a method to permit a consistent means to exit the CommandProcessor
     ///
     ///    @param defaultMenu enables various default menu items, based on the bit values.
-    /// @param caseinsensitive when TRUE, as the name implies, permits "help" and "HeLp" to function the same
-    ///    @param maxCmdLen sets the memory allocation for the command buffer. This should be sized
-    ///            to the maximum command, including any passed in text as parameters.
     ///    @param kbhit is a user provided function to detect if a character is available for the CommandProcessor,
     ///            and when using standard io, you can typically use kbhit, or _kbhit as your system provides.
     ///    @param getch is a user provided function that provides a single character to the CommandProcessor
     ///    @param putch is a user provided function that permits the CommandProcessor to output a character
     ///    @param puts is a user provided function that permits the CommandProcessor to output a string
     ///        to which is automatically appended a \\n
+    ///    @param caseinsensitive when TRUE, as the name implies, permits "help" and "HeLp" to function the same
+    ///    @param maxCmdLen sets the memory allocation for the command buffer. This should be sized
+    ///            to the maximum command, including any passed in text as parameters.
     /// @returns INITRESULT_T to indicate if the init was successful or failed
     INITRESULT_T (*Init)(
         int defaultMenu, 
         int caseinsensitive,
-        int maxCmdLen, 
+        int echo,
+        int maxCmdLen,
         int (*kbhit)(void),
         int (*getch)(void),
         int (*putch)(int ch),
@@ -191,8 +193,8 @@
     ///    This passes in a reference to a user provided CMD_T item, which is
     ///    added to the menu system.
     ///
-    ///    @param m is a pointer to the user provided menu
-    ///    @returns ADDRESULT_T to indicate if the add was successful or failed
+    /// @param m is a pointer to the user provided menu
+    /// @returns ADDRESULT_T to indicate if the add was successful or failed
     ///
     ADDRESULT_T (*Add)(CMD_T * m);
 
@@ -209,6 +211,18 @@
     ///            command that was executed is requesting the CommandProcessor to exit.
     ///
     RUNRESULT_T (*Run)(void);
+    
+    /// Echo command permits turning the echo on and off
+    ///
+    /// When interactive with the user, it is best to have echo on, so they can see
+    /// the prompt, but if this is simply slaved to another program, then the echo
+    /// might need to be off to best manage the stream.
+    ///
+    /// @param echo turns the echo on (non-zero) or off (zero)
+    /// @returns RUNRESULT_T to indicate if the CommandProcessor should remain active or if the 
+    ///            command that was executed is requesting the CommandProcessor to exit.
+    ///
+    RUNRESULT_T (*Echo)(int echo);
 
     /// End if the function to be called when you want to gracefully end the CommandProcessor.
     ///