Command Processor
This is a relatively simple command processor. It has a few objectives -
- Permit flexible IO assignment for serial input and output (serial, USB, etc.).
- Does not "block" the CPU by waiting for a keystroke (process individual chars and return).
- Compiles on mbed target and desktop under Visual C++ 2010 Express.
You can read more about the APIs and get the library at CommandProcessor.
Sample in Action
Here's a sample of the program in action, and further below is the source to this sample that makes it happen.
This program was built Oct 1 2011 15:16:17. >Help Echo : Echo [1|on|0|off] turns echo on or off. Exit : Exits the program Help : Help or '?' shows this help, 'Help ?' shows more details. History: Show command history who : Shows who is logged on, or 'who id' for specifics >who who... >About This program was built Oct 1 2011 15:16:17. >
Sample Source Code
And here's the code that makes it happen.
// CommandLine.cpp : Defines the entry point for the
// console application.
//
#ifdef WIN32
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "assert.h"
#else
#include "mbed.h"
#endif
extern "C" {
#include "CommandProcessor.h"
}
#ifdef WIN32
#else
Serial pc(USBTX, USBRX);
#endif
RUNRESULT_T Who(char *p);
const CMD_T WhoCmd = {
"who",
"Shows who is logged on, or 'who id' for specifics",
Who,
visible
};
RUNRESULT_T SignOnBanner(char *p);
const CMD_T SignOnBannerCmd = {
"About",
"About this program",
SignOnBanner,
invisible
};
RUNRESULT_T SignOnBanner(char *p)
{
puts("\r\nThis program was built " __DATE__ " " __TIME__ ".\r\n");
return runok;
}
RUNRESULT_T Who(char *p) {
printf("\r\nwho...\r\n");
if (*p)
printf(" Sorry, no help for [%s]\r\n", p);
return runok;
}
// Provide the serial interface methods for the command processor
#ifdef WIN32
int mReadable() { return _kbhit(); }
int mGetCh() { return _getch(); }
int mPutCh(int a) { return _putch(a); }
int mPutS(const char * s) { return printf("%s\r\n", s); }
#else
Serial pc(USBTX, USBRX);
int mReadable() { return pc.readable(); }
int mGetCh() { return pc.getc(); }
int mPutCh(int a) { return pc.putc(a); }
int mPutS(const char * s) { return pc.printf("%s\r\n", s); }
#endif
int main(int argc, char* argv[])
{
CMDP_T * cp = GetCommandProcessor();
#ifdef WIN32
// nothing special for Win32
#else
pc.baud(921600);
#endif
cp->Init(
&SignOnBannerCmd,
CFG_ENABLE_TERMINATE | CFG_ENABLE_SYSTEM
| CFG_ECHO_ON | CFG_CASE_INSENSITIVE, // options
50, // Command Buffer length
5, // History depth # of commands
mReadable, // User provided API (kbhit())
mGetCh, // User provided API
mPutCh, // User provided API
mPutS); // User provided API
// Start adding custom commands now
cp->Add(&WhoCmd);
// Should never "wait" in here
while (cp->Run() == runok) // run this timeslice
{
// do non-blocking things here
;
}
cp->End(); // cleanup
return 0;
}
Please log in to post comments.
