Displays user interactions with menus displayed on a console or a serial terminal.
Dependents: EVAL-AD568x-AD569x EVAL-AD7124 EVAL-AD5592R EVAL-AD717x-AD411x ... more
Diff: adi_console_menu.c
- Revision:
- 6:34034065d24b
- Parent:
- 5:052b9936f41f
- Child:
- 7:49c1587ecd29
--- a/adi_console_menu.c Fri Dec 03 18:07:28 2021 +0000 +++ b/adi_console_menu.c Tue Feb 15 15:43:54 2022 +0530 @@ -314,21 +314,24 @@ } /** - * @brief Handles the input from the user by displaying + * @brief Handles the integer type input from the user by displaying * the menu message and provides a set number of * input attempts for the user. * @param menu_prompt[in] - User specified prompt. * @param min_val[in] - minimum input value. * @param max_val[in] - maximum input value. * @param input_val[in, out] - User provided input value. + * @param input_len[in] - User provided input length. + * @param max_attempts[in] - Maximum number of input attempts. * @param clear_lines[in] - lines to clear in case of invalid input. * @return SUCCESS in case success. otherwise FAILURE */ -int32_t adi_handle_user_input(const char* menu_prompt, +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) { @@ -342,7 +345,7 @@ /* Gets the input from the user and allows * reattempts in-case of incorrect input */ printf("%s (%d - %d): ", menu_prompt, min_val, max_val); - *input_val = (uint16_t)adi_get_decimal_int(5); + *input_val = (uint16_t)adi_get_decimal_int(input_len); if ((*input_val >= min_val) && (*input_val <= max_val)) { // break out of the loop in-case of a correct input @@ -368,6 +371,65 @@ return SUCCESS; } +/** + * @brief Handles the float type input from the user by displaying + * the menu message and provides a set number of + * input attempts for the user. + * @param menu_prompt[in] - User specified prompt. + * @param min_val[in] - minimum input value. + * @param max_val[in] - maximum input value. + * @param input_val[in, out] - User provided input value. + * @param input_len[in] - User provided input length. + * @param max_attempts[in] - Maximum number of input attempts. + * @param clear_lines[in] - lines to clear in case of + invalid input. + * @return SUCCESS in case success. otherwise FAILURE + */ +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) +{ + uint8_t count = 0; + + if (menu_prompt == NULL || input_val == NULL) { + return FAILURE; + } + + do { + /* Gets the input from the user and allows + * reattempts in-case of incorrect input */ + printf("%s (%0.3f - %0.3f): ", menu_prompt, min_val, max_val); + *input_val = adi_get_decimal_float(input_len); + + if ((*input_val >= min_val) && (*input_val <= max_val)) { + // break out of the loop in-case of a correct input + break; + } + else { + if (count == max_attempts) { + printf(EOL "Maximum try limit exceeded" EOL); + adi_press_any_key_to_continue(); + return FAILURE; + } + + printf(EOL "Please enter a valid selection" EOL); + adi_press_any_key_to_continue(); + /* Moves up the cursor by specified lines and + * clears the lines below it */ + for (uint8_t i = 0; i < clear_lines; i++) { + printf(VT100_CLEAR_CURRENT_LINE); + printf(VT100_MOVE_UP_N_LINES, 1); + } + } + } while (++count <= max_attempts); + + return SUCCESS; +} + /*! * @brief Clears the console terminal *