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 = { const struct Menu::command top_menu_commands[] PROGMEM = {
{"*", menu_auto},
{"test", menu_test}, {"test", menu_test},
}; };

View File

@ -26,7 +26,17 @@ public:
/// Note that the array of menu commands is expected to be in program /// Note that the array of menu commands is expected to be in program
/// memory. /// memory.
struct command { 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 int (*func)(uint8_t argc, const struct arg *argv); ///< callback function
}; };
@ -42,7 +52,16 @@ public:
void run(void); void run(void);
private: private:
/// Implements the default 'help' command
///
void _help(void); ///< implements the '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 char *_prompt; ///< prompt to display
const command *_commands; ///< array of commands const command *_commands; ///< array of commands
const uint8_t _entries; ///< size of the menu const uint8_t _entries; ///< size of the menu

View File

@ -31,10 +31,15 @@ Menu::run(void)
uint8_t len, i, ret; uint8_t len, i, ret;
uint8_t argc; uint8_t argc;
int c; int c;
func fn;
// loop performing commands // loop performing commands
for (;;) { 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 // loop reading characters from the input
len = 0; len = 0;
@ -83,8 +88,7 @@ Menu::run(void)
// look for a command matching the first word (note that it may be empty) // look for a command matching the first word (note that it may be empty)
for (i = 0; i < _entries; i++) { for (i = 0; i < _entries; i++) {
if (!strcmp_P(_argv[0].str, _commands[i].command)) { if (!strcmp_P(_argv[0].str, _commands[i].command)) {
fn = (func)pgm_read_word(&_commands[i].func); ret = _call(i, argc);
ret = fn(argc, &_argv[0]);
if (-2 == ret) if (-2 == ret)
return; return;
} }
@ -106,3 +110,13 @@ Menu::_help(void)
for (i = 0; i < _entries; i++) for (i = 0; i < _entries; i++)
Serial.printf(" %S\n", _commands[i].command); 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]));
}