Skip to Main Content
Programming Embedded Systems, 2nd Edition
book

Programming Embedded Systems, 2nd Edition

by Michael Barr, Anthony Massa
October 2006
Intermediate to advanced content levelIntermediate to advanced
336 pages
9h 13m
English
O'Reilly Media, Inc.
Content preview from Programming Embedded Systems, 2nd Edition

Command-Line Interface Processing

The command-line interface module is responsible for building the incoming command, parsing a completed command, and executing the function associated with the command. The command-line interface module contains two functions to handle these tasks: cliBuildCommand and cliProcessCommand.

The command_t struct has two members: the command name (defined by the pointer name), and the function to execute when the command is entered (defined by the pointer to a function function).

The command table, gCommandTable, is a command_t type array. The last command name and function in the table are set to NULL in order to aid the command lookup procedure. Additional commands can be added by following the format shown, but new commands must be added before the terminating last command.

You may notice that a macro called MAX_COMMAND_LEN is defined. Command names must be less than or equal to the maximum command length. The following code shows the command struct type and the command table:

#define MAX_COMMAND_LEN             (10)
#define COMMAND_TABLE_SIZE          (4)

typedef struct
{
    char const    *name;
    void          (*function)(void);
} command_t;

command_t const gCommandTable[COMMAND_TABLE_LEN] = 
{
    {"HELP",    commandsHelp,},
    {"LED",     commandsLed, },
    {"BUZZER",  commandsBuzzer, },
    {NULL,      NULL }
};

As characters are received from the serial port, the main processing loop calls the cliBuildCommand function, as we saw earlier in this chapter. Once a completed command is received, indicated by a carriage ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming with STM32: Getting Started with the Nucleo Board and C/C++

Programming with STM32: Getting Started with the Nucleo Board and C/C++

Donald Norris

Publisher Resources

ISBN: 0596009836Supplemental ContentErrata Page