Browse Source

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.
Guðni Már Gilbert 2 years ago
parent
commit
82bd9db1d6
3 changed files with 19 additions and 6 deletions
  1. 7 0
      Firmware/menu.cpp
  2. 5 0
      Firmware/menu.h
  3. 7 6
      Firmware/ultralcd.cpp

+ 7 - 0
Firmware/menu.cpp

@@ -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)

+ 5 - 0
Firmware/menu.h

@@ -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);
 

+ 7 - 6
Firmware/ultralcd.cpp

@@ -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();
 }