APM:Libraries
AP_Menu.h
Go to the documentation of this file.
1 #pragma once
14 
15 #include <inttypes.h>
16 #include <AP_HAL/AP_HAL.h>
17 
18 #define MENU_COMMANDLINE_MAX 32
19 #define MENU_ARGS_MAX 3
20 #define MENU_COMMAND_MAX 14
21 
22 class Menu {
24 public:
34  struct arg {
35  const char *str;
36  long i;
37  float f;
38  };
39 
54  FUNCTOR_TYPEDEF(func, int8_t, uint8_t, const struct arg *);
55 
56  static void set_port(AP_HAL::BetterStream *port) {
57  _port = port;
58  }
59 
67  FUNCTOR_TYPEDEF(preprompt, bool);
68 
71  struct command {
76 
88  FUNCTOR_DECLARE(func, int8_t, uint8_t, const struct arg *);
89  };
90 
100  Menu(const char *prompt, const struct command *commands, uint8_t entries, preprompt ppfunc = 0);
101 
103  void set_limits(uint8_t commandline_max, uint8_t args_max);
104 
106  void run(void);
107 
111  bool check_input(void);
112 
113 private:
116  void _help(void);
117 
123  int8_t _call(uint8_t n, uint8_t argc);
124 
125  const char * _prompt;
126  const command * _commands;
127  const uint8_t _entries;
128  const preprompt _ppfunc;
129 
130  static char *_inbuf;
131  static arg *_argv;
132 
134  uint8_t _args_max;
135 
136  // allocate inbuf and args buffers
137  void _allocate_buffers(void);
138 
139  // number of characters read so far from the port
140  uint8_t _input_len;
141 
142  // check for next input character
143  bool _check_for_input(void);
144 
145  // run one full entered command.
146  // return true if the menu loop should exit
147  bool _run_command(bool prompt_on_enter);
148 
149  void _display_prompt();
150 
151  // port to run on
153 };
154 
165 #define MENU(name, prompt, commands) \
166  static const char __menu_name__ ## name[] = prompt; \
167  static Menu name(__menu_name__ ## name, commands, ARRAY_SIZE(commands))
168 
169 #define MENU2(name, prompt, commands, preprompt) \
170  static const char __menu_name__ ## name[] = prompt; \
171  static Menu name(__menu_name__ ## name, commands, ARRAY_SIZE(commands), preprompt)
Menu_Commands commands
Definition: RCOutput.cpp:33
bool check_input(void)
Definition: AP_Menu.cpp:204
FUNCTOR_TYPEDEF(func, int8_t, uint8_t, const struct arg *)
#define FUNCTOR_DECLARE(name, rettype,...)
Definition: functor.h:25
bool _check_for_input(void)
Definition: AP_Menu.cpp:40
Menu(const char *prompt, const struct command *commands, uint8_t entries, preprompt ppfunc=0)
Definition: AP_Menu.cpp:21
float f
floating point form of the argument (if a number)
Definition: AP_Menu.h:37
const char * str
string form of the argument
Definition: AP_Menu.h:35
static arg * _argv
arguments
Definition: AP_Menu.h:131
void _allocate_buffers(void)
Definition: AP_Menu.cpp:260
void _help(void)
implements the &#39;help&#39; command
Definition: AP_Menu.cpp:222
void run(void)
menu runner
Definition: AP_Menu.cpp:164
uint8_t _commandline_max
Definition: AP_Menu.h:133
const preprompt _ppfunc
optional pre-prompt action
Definition: AP_Menu.h:128
const command * _commands
array of commands
Definition: AP_Menu.h:126
Class defining and handling one menu tree.
Definition: AP_Menu.h:23
bool _run_command(bool prompt_on_enter)
Definition: AP_Menu.cpp:87
static AP_HAL::BetterStream * _port
Definition: AP_Menu.h:152
uint8_t _args_max
Definition: AP_Menu.h:134
int8_t _call(uint8_t n, uint8_t argc)
Definition: AP_Menu.cpp:235
static void set_port(AP_HAL::BetterStream *port)
Definition: AP_Menu.h:56
const uint8_t _entries
size of the menu
Definition: AP_Menu.h:127
uint8_t _input_len
Definition: AP_Menu.h:140
#define MENU_COMMAND_MAX
maximum size of a command name
Definition: AP_Menu.h:20
long i
integer form of the argument (if a number)
Definition: AP_Menu.h:36
const char * _prompt
prompt to display
Definition: AP_Menu.h:125
static char * _inbuf
input buffer
Definition: AP_Menu.h:130
void set_limits(uint8_t commandline_max, uint8_t args_max)
set command line length limit
Definition: AP_Menu.cpp:244
void _display_prompt()
Definition: AP_Menu.cpp:80