PFW-1397 Implement ON_MENU_ENTER to simplify code

ON_MENU_ENTER runs code within the brackets only once
after a new menu is entered.

For the tool change menu, this allows us to display the data and
read from EEPROM only once.
This commit is contained in:
Guðni Már Gilbert 2022-10-23 15:21:49 +00:00 committed by DRracer
parent a896dfd4e0
commit 82bd9db1d6
3 changed files with 19 additions and 6 deletions

View File

@ -29,6 +29,7 @@ uint8_t menu_line = 0;
uint8_t menu_item = 0;
uint8_t menu_row = 0;
uint8_t menu_top = 0;
static bool menu_changed; // flag to indicate a new menu was entered
uint8_t menu_clicked = 0;
@ -53,6 +54,7 @@ void menu_goto(menu_func_t menu, const uint32_t encoder, const bool feedback, bo
menu_menu = menu;
lcd_encoder = encoder;
menu_top = 0; //reset menu view. Needed if menu_back() is called from deep inside a menu, such as Support
menu_changed = true;
CRITICAL_SECTION_END;
if (reset_menu_state)
menu_data_reset();
@ -347,6 +349,11 @@ bool __attribute__((noinline)) menu_item_leave(){
return ((menu_item == menu_line) && menu_clicked && (lcd_encoder == menu_item)) || menu_leaving;
}
bool menu_item_enter() {
menu_changed = false;
return !menu_changed;
}
uint8_t menu_item_function_P(const char* str, menu_func_t func)
{
if (menu_item == menu_line)

View File

@ -114,6 +114,11 @@ extern uint8_t menu_item_back_P(const char* str);
#define ON_MENU_LEAVE(func) do { if (menu_item_leave()){ func } } while (0)
extern bool menu_item_leave();
/// Entering a new menu
/// @param func lines of code to run once upon enter a menu or submenu
#define ON_MENU_ENTER(func) do { if (menu_item_enter()){ func } } while (0)
extern bool menu_item_enter();
#define MENU_ITEM_FUNCTION_P(str, func) do { if (menu_item_function_P(str, func)) return; } while (0)
extern uint8_t menu_item_function_P(const char* str, menu_func_t func);

View File

@ -1230,12 +1230,13 @@ static void lcd_menu_fails_stats_mmu_total()
//! @endcode
static void lcd_menu_toolchange_stats_mmu_total()
{
lcd_clear();
uint32_t toolchanges = eeprom_read_dword((uint32_t*)EEPROM_TOTAL_TOOLCHANGE_COUNT);
lcd_set_cursor(0, 0);
lcd_puts_P(PSTR("Toolchange count:"));
lcd_set_cursor(10, 1);
lcd_print(toolchanges);
ON_MENU_ENTER(
lcd_set_cursor(0, 0);
lcd_puts_P(PSTR("Toolchange count:"));
lcd_set_cursor(10, 1);
lcd_print(eeprom_read_dword((uint32_t*)EEPROM_TOTAL_TOOLCHANGE_COUNT));
);
menu_back_if_clicked_fb();
}