precision-converters-firmware
adi_console_menu.h
Go to the documentation of this file.
1 
18 #ifndef ADI_CONSOLE_MENU_H_
19 #define ADI_CONSOLE_MENU_H_
20 
21 /******************************************************************************/
22 /***************************** Include Files **********************************/
23 /******************************************************************************/
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <limits.h>
27 
28 /******************************************************************************/
29 /********************** Macros and Constants Definition ***********************/
30 /******************************************************************************/
31 
32 #define MENU_ESCAPED INT_MAX
33 #define MENU_CONTINUE INT_MAX-1
34 #define MENU_DONE INT_MAX-2
35 
36 #define ESCAPE_KEY_CODE (char)0x1B
37 
38 #define EOL "\r\n"
39 
40 /* ANSI VT100 escape sequence codes */
41 #define VT100_MOVE_UP_1_LINE "\033[A"
42 #define VT100_MOVE_UP_N_LINES "\x1B[%dA"
43 #define VT100_CLEAR_CURRENT_LINE "\x1B[J"
44 #define VT100_CLEAR_CONSOLE "\x1B[2J"
45 #define VT100_MOVE_TO_HOME "\x1B[H"
46 #define VT100_COLORED_TEXT "\x1B[%dm"
47 
48 #ifndef ARRAY_SIZE
49 #define ARRAY_SIZE(x) ((sizeof (x)) / (sizeof ((x)[0])))
50 #endif
51 
52 /* ANSI VT100 color codes */
55  VT_FG_RED = 31,
58  VT_FG_BLUE = 34,
60  VT_FG_CYAN = 36,
61  VT_FG_WHITE = 37
62 };
63 
64 /******************************************************************************/
65 /********************** Variables and User Defined Data Types *****************/
66 /******************************************************************************/
67 /* This define the state of the console menu library*/
68 typedef struct {
69  // Stores the error code from the last menu action.
70  int32_t last_error_code;
72 
73 /* Type Definitions */
74 // Each menu item is defined by this struct
75 typedef struct {
76  // String displayed for menu item
77  char * text;
78  // character that can be pressed to select menu item
80  // Function to be called when menu item is selected, if NULL, no function is called
81  int32_t (*action)(uint32_t option);
82  // Submenu to be called when menu item is selected, if NULL, no sub menu is displayed
84  // id value passed as the option value when calling menuAction
85  uint32_t id;
87 
88 // This defines a complete menu with items
89 typedef struct {
90  // String to be displayed as the menu title
91  char * title;
92  // Array of all the menu items
94  // Number of menuItems
95  uint8_t itemCount;
96  // Function alled before Menu title is displayed if defined
97  void (*headerItem)(void);
98  // Function called after menu items are displayed if defined
99  void (*footerItem)(void);
100  // Should the escape key to exit the menu be enabled?
102 } console_menu;
103 
104 /******************************************************************************/
105 /***************************** Public Declarations ***************************/
106 /******************************************************************************/
107 int32_t adi_do_console_menu(const console_menu * menu);
108 int32_t adi_get_decimal_int(uint8_t input_len);
109 uint32_t adi_get_hex_integer(uint8_t input_len);
110 float adi_get_decimal_float(uint8_t input_len);
111 int32_t adi_handle_user_input_integer(const char* menu_prompt,
112  uint16_t min_val,
113  uint16_t max_val,
114  uint16_t *input_val,
115  uint8_t input_len,
116  uint8_t max_attempts,
117  uint8_t clear_lines);
118 int32_t adi_handle_user_input_float(const char* menu_prompt,
119  float min_val,
120  float max_val,
121  float *input_val,
122  uint8_t input_len,
123  uint8_t max_attempts,
124  uint8_t clear_lines);
125 void adi_clear_console(void);
126 void adi_clear_last_menu_error(void);
127 int32_t adi_get_last_menu_error(void);
129 
131 
132 #endif /* ADI_CONSOLE_MENU_H_ */
int32_t adi_get_last_menu_error(void)
Returns the error code from the last menu.
Definition: adi_console_menu.c:472
vt100_colors
Definition: adi_console_menu.h:53
@ VT_FG_BLUE
Definition: adi_console_menu.h:58
@ VT_FG_MAGENTA
Definition: adi_console_menu.h:59
@ VT_FG_WHITE
Definition: adi_console_menu.h:61
@ VT_FG_CYAN
Definition: adi_console_menu.h:60
@ VT_FG_GREEN
Definition: adi_console_menu.h:56
@ VT_FG_DEFAULT
Definition: adi_console_menu.h:54
@ VT_FG_YELLOW
Definition: adi_console_menu.h:57
@ VT_FG_RED
Definition: adi_console_menu.h:55
void adi_clear_console(void)
Clears the console terminal.
Definition: adi_console_menu.c:441
int32_t adi_handle_user_input_float(const char *menu_prompt, float min_val, float max_val, float *input_val, uint8_t input_len, uint8_t max_attempts, uint8_t clear_lines)
Handles the float type input from the user by displaying the menu message and provides a set number o...
Definition: adi_console_menu.c:390
void adi_press_any_key_to_continue(void)
waits for any key to be pressed, and displays a prompt to the user
Definition: adi_console_menu.c:482
int32_t adi_get_decimal_int(uint8_t input_len)
Reads a decimal string from the user.
Definition: adi_console_menu.c:203
console_menu_state adi_console_menu_state
Definition: adi_console_menu.c:42
float adi_get_decimal_float(uint8_t input_len)
Reads a floating string from the user.
Definition: adi_console_menu.c:287
int32_t adi_handle_user_input_integer(const char *menu_prompt, uint16_t min_val, uint16_t max_val, uint16_t *input_val, uint8_t input_len, uint8_t max_attempts, uint8_t clear_lines)
Handles the integer type input from the user by displaying the menu message and provides a set number...
Definition: adi_console_menu.c:332
uint32_t adi_get_hex_integer(uint8_t input_len)
Reads a hexadecimal number from the user.
Definition: adi_console_menu.c:244
void adi_clear_last_menu_error(void)
Clears the error code from the last menu.
Definition: adi_console_menu.c:462
int32_t adi_do_console_menu(const console_menu *menu)
Display a consoleMenu and handle User interaction.
Definition: adi_console_menu.c:111
Definition: adi_console_menu.h:75
struct console_menu * submenu
Definition: adi_console_menu.h:83
char shortcutKey
Definition: adi_console_menu.h:79
char * text
Definition: adi_console_menu.h:77
uint32_t id
Definition: adi_console_menu.h:85
Definition: adi_console_menu.h:68
int32_t last_error_code
Definition: adi_console_menu.h:70
Definition: adi_console_menu.h:89
console_menu_item * items
Definition: adi_console_menu.h:93
void(* headerItem)(void)
Definition: adi_console_menu.h:97
uint8_t itemCount
Definition: adi_console_menu.h:95
void(* footerItem)(void)
Definition: adi_console_menu.h:99
bool enableEscapeKey
Definition: adi_console_menu.h:101
char * title
Definition: adi_console_menu.h:91