Provide an easy way to print stuff before displaying the prompt.

git-svn-id: https://arducopter.googlecode.com/svn/trunk@545 f9c3cf11-9bcb-44bc-f272-b75c42450872
This commit is contained in:
DrZiplok@gmail.com 2010-09-24 06:18:59 +00:00
parent eb6113e685
commit 064dda10bf
3 changed files with 44 additions and 4 deletions

View File

@ -18,7 +18,14 @@ menu_test(uint8_t argc, const Menu::arg *argv)
}
}
int
menu_auto(uint8_t argc, const Menu::arg *argv)
{
Serial.println("auto text");
}
const struct Menu::command top_menu_commands[] PROGMEM = {
{"*", menu_auto},
{"test", menu_test},
};

View File

@ -26,7 +26,17 @@ public:
/// Note that the array of menu commands is expected to be in program
/// memory.
struct command {
const char command[MENU_COMMAND_MAX]; ///< name of the command
/// Name of the command, as typed or received.
/// Command names are limited in size to keep this structure compact.
/// If the first command in the menu is named '*', it will be called
/// each time before printing the prompt.
const char command[MENU_COMMAND_MAX];
/// The function to call when the command is received.
/// The \a argc argument will be at least 1, and no more than
/// MENU_ARGS_MAX. The argv array will be populated with
/// arguments typed/received up to MENU_ARGS_MAX. The command
/// name will always be in argv[0].
int (*func)(uint8_t argc, const struct arg *argv); ///< callback function
};
@ -42,7 +52,16 @@ public:
void run(void);
private:
/// Implements the default 'help' command
///
void _help(void); ///< implements the 'help' command
/// calls the function for the n'th menu item
///
/// @param n Index for the menu item to call
/// @param argc Number of arguments prepared for the menu item
///
int _call(uint8_t n, uint8_t argc);
const char *_prompt; ///< prompt to display
const command *_commands; ///< array of commands
const uint8_t _entries; ///< size of the menu

View File

@ -31,11 +31,16 @@ Menu::run(void)
uint8_t len, i, ret;
uint8_t argc;
int c;
func fn;
// loop performing commands
for (;;) {
// if the first command is called '*', call it before displaying the menu
if ("*", _commands[0].command) {
if (-2 == _call(0, 0))
return;
}
// loop reading characters from the input
len = 0;
Serial.printf("%S] ", _prompt);
@ -83,8 +88,7 @@ Menu::run(void)
// look for a command matching the first word (note that it may be empty)
for (i = 0; i < _entries; i++) {
if (!strcmp_P(_argv[0].str, _commands[i].command)) {
fn = (func)pgm_read_word(&_commands[i].func);
ret = fn(argc, &_argv[0]);
ret = _call(i, argc);
if (-2 == ret)
return;
}
@ -106,3 +110,13 @@ Menu::_help(void)
for (i = 0; i < _entries; i++)
Serial.printf(" %S\n", _commands[i].command);
}
// run the n'th command in the menu
int
Menu::_call(uint8_t n, uint8_t argc)
{
func fn;
fn = (func)pgm_read_word(&_commands[n].func);
return(fn(argc, &_argv[0]));
}