Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Jul 27, 2004 10:07 am |
|
|
Here is an example that may help a bit. I am not posting all the code but data is received and put into a buffer through an ISR. This function is called from main to receive commands
Code: |
void TerminalHandler()
{
static uint8_t index = 0; /* index of buffer */
int i; /* character from buffer */
char ch; /* character from buffer */
static char buf[MAX_LINE_LEN]; /* for the line entry */
while (TRUE)
{
i = HostReadChar();
/* port error */
if (i < 0)
return;
ch = (char)i;
switch (ch)
{
/* illegal characters */
case '\a':
case '\f':
case '\n':
case '\t':
case '\v':
/* escape */
case 0x1B:
break;
/* backspace */
case '\b':
if (index)
{
--index;
/* do a destructive backspace */
HostWriteChar('\b');
HostWriteChar(' ');
HostWriteChar('\b');
}
break;
/* enter */
case '\r':
HostWriteChar('\r');
HostWriteChar('\n');
buf[index] = '\0';
/* did we get something? */
if (index)
{
if (!CommandHandler(buf, &CommandData[0]))
HostWriteRomString("Invalid Command. Type ? for help.\r\n");
}
buf[0] = '\0';
index = 0;
/* write the prompt */
HostWriteChar('>');
break;
/* all the rest of the characters */
default:
/* leave room for null at the end */
if (index < (MAX_LINE_LEN - 1))
{
buf[index] = ch;
if (!ch)
ch = 0;
HostWriteChar(ch);
index++;
}
break;
}
}
}
|
Here is the command handler:
Code: |
static bool CommandHandler(
char *pArgs, /* FIX ME: add comment */
COMMAND_DATA *pCommandData) /* FIX ME: add comment */
{
static char cmd[MAX_LINE_LEN]; /* local buffer */
bool found = 0; /* valid command? */
if (pArgs)
{
/* parse and remove the only the first command */
pArgs = stptok(pArgs, cmd, sizeof(cmd), " ");
/* find and execute command function */
while (pCommandData->pStr != NULL)
{
if (strcmpipgm2ram(cmd, pCommandData->pStr) == 0)
{
if (pCommandData->pFunction != NULL)
{
(void)strupr(cmd);
(void)pCommandData->pFunction(cmd, pArgs);
}
found = 1;
break;
}
pCommandData++;
}
}
return (found);
}
|
And here are the commands:
Code: |
typedef rom struct _cmd_t
{
const rom char *pStr; /* command */
void (*pFunction) (char *pCmd, char *pArgs); /* command function */
const rom char *pDescription; /* command description (for help) */
} COMMAND_DATA;
static COMMAND_DATA CommandData[] =
{
/* hidden commands - description is NULL */
{ { "help" }, VHelpCommand, { NULL } },
{ { "TEST" }, SelfTestCommand, { NULL } },
{ { "VIRGINIZE" }, VirginCommand, { NULL } },
{ { "bootmode" }, BootCommand, { NULL } },
/* application commands */
{ { "ver" }, VersionCommand, { "sofware version" } },
{ { "abus" }, ABUSCommand, { "sends command out abus" } },
{ { "input" }, InputCommand, { "input status" } },
{ { "photocell" }, PhotocellCommand, { "photocell status" } },
{ { "data" }, DataCommand, { "dump program data" } },
{ { "restore" }, ReInitCommand, { "restore defaults" } },
{ { "restart" }, RestartCommand, { "restart device" } },
{ { "dump" }, EEPROMDumpCommand,{ "dumps the contents of the eeprom" } },
{ { "host" }, HostModeCommand, { "allows this device to act as host" } },
{ { "??" }, VHelpCommand, { "verbose help" } },
{ { "?" }, THelpCommand, { "terse help" } },
{ { NULL }, NULL, { NULL } },
};
|
Not sure how well CCS handles pointers to functions. Also note that since CCS does not allow constant strings the above would not work with CCS and you would have to change it a bit to check for the commands. This code is for the C18 compiler BTW. |
|