From ba4b9b99614d5e2fee6a8bbfc5a18e742a6895ba Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 21 Nov 2012 16:17:16 +1100 Subject: [PATCH] AP_Menu: allow menus to work on ports other than UART0 --- libraries/AP_Menu/AP_Menu.cpp | 33 +++++++++++++++++++++------------ libraries/AP_Menu/AP_Menu.h | 7 +++++++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/libraries/AP_Menu/AP_Menu.cpp b/libraries/AP_Menu/AP_Menu.cpp index ab912dac2d..54a38756c8 100644 --- a/libraries/AP_Menu/AP_Menu.cpp +++ b/libraries/AP_Menu/AP_Menu.cpp @@ -16,6 +16,8 @@ // statics char Menu::_inbuf[MENU_COMMANDLINE_MAX]; Menu::arg Menu::_argv[MENU_ARGS_MAX + 1]; +FastSerial *Menu::_port; + // constructor Menu::Menu(const prog_char *prompt, const Menu::command *commands, uint8_t entries, preprompt ppfunc) : @@ -36,6 +38,11 @@ Menu::run(void) int c; char *s; + if (_port == NULL) { + // default to main serial port + _port = &Serial; + } + // loop performing commands for (;; ) { @@ -45,32 +52,32 @@ Menu::run(void) // loop reading characters from the input len = 0; - Serial.printf("%S] ", FPSTR(_prompt)); + _port->printf_P(PSTR("%S] "), FPSTR(_prompt)); for (;; ) { - c = Serial.read(); + c = _port->read(); if (-1 == c) continue; // carriage return -> process command if ('\r' == c) { _inbuf[len] = '\0'; - Serial.write('\r'); - Serial.write('\n'); + _port->write('\r'); + _port->write('\n'); break; } // backspace if ('\b' == c) { if (len > 0) { len--; - Serial.write('\b'); - Serial.write(' '); - Serial.write('\b'); + _port->write('\b'); + _port->write(' '); + _port->write('\b'); continue; } } // printable character if (isprint(c) && (len < (MENU_COMMANDLINE_MAX - 1))) { _inbuf[len++] = c; - Serial.write((char)c); + _port->write((char)c); continue; } } @@ -127,7 +134,7 @@ Menu::run(void) if (cmd_found==false) { - Serial.println("Invalid command, type 'help'"); + _port->println_P(PSTR("Invalid command, type 'help'")); } } @@ -139,9 +146,11 @@ Menu::_help(void) { int i; - Serial.println("Commands:"); - for (i = 0; i < _entries; i++) - Serial.printf(" %S\n", FPSTR(_commands[i].command)); + _port->println_P(PSTR("Commands:")); + for (i = 0; i < _entries; i++) { + delay(10); + _port->printf_P(PSTR(" %S\n"), FPSTR(_commands[i].command)); + } } // run the n'th command in the menu diff --git a/libraries/AP_Menu/AP_Menu.h b/libraries/AP_Menu/AP_Menu.h index 7a95403769..2eb7fe2a1a 100644 --- a/libraries/AP_Menu/AP_Menu.h +++ b/libraries/AP_Menu/AP_Menu.h @@ -56,6 +56,10 @@ public: /// typedef int8_t (*func)(uint8_t argc, const struct arg *argv); + static void set_port(FastSerial *port) { + _port = port; + } + /// menu pre-prompt function /// /// Called immediately before waiting for the user to type a command; can be @@ -120,6 +124,9 @@ private: static char _inbuf[MENU_COMMANDLINE_MAX]; ///< input buffer static arg _argv[MENU_ARGS_MAX + 1]; ///< arguments + + // port to run on + static FastSerial *_port; }; /// Macros used to define a menu.