From 064dda10bf8ab3c6c23255a84849bddaea4eab66 Mon Sep 17 00:00:00 2001 From: "DrZiplok@gmail.com" Date: Fri, 24 Sep 2010 06:18:59 +0000 Subject: [PATCH] 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 --- libraries/AP_Common/examples/menu/menu.pde | 7 +++++++ libraries/AP_Common/include/menu.h | 21 ++++++++++++++++++++- libraries/AP_Common/menu.cpp | 20 +++++++++++++++++--- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/libraries/AP_Common/examples/menu/menu.pde b/libraries/AP_Common/examples/menu/menu.pde index 4864e582d0..fea9efa188 100644 --- a/libraries/AP_Common/examples/menu/menu.pde +++ b/libraries/AP_Common/examples/menu/menu.pde @@ -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}, }; diff --git a/libraries/AP_Common/include/menu.h b/libraries/AP_Common/include/menu.h index 498cb407c5..a55fc2771d 100644 --- a/libraries/AP_Common/include/menu.h +++ b/libraries/AP_Common/include/menu.h @@ -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 diff --git a/libraries/AP_Common/menu.cpp b/libraries/AP_Common/menu.cpp index 0dc426be26..8fba5271af 100644 --- a/libraries/AP_Common/menu.cpp +++ b/libraries/AP_Common/menu.cpp @@ -31,10 +31,15 @@ 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; @@ -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])); +}