Skip to content

Commit e0fd33b

Browse files
thinkyheadptoal
authored andcommitted
♻️ Refactor status screen timeout
1 parent d7b6e8d commit e0fd33b

File tree

9 files changed

+56
-54
lines changed

9 files changed

+56
-54
lines changed

Marlin/src/inc/Conditionals_post.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -3397,9 +3397,14 @@
33973397
#endif
33983398
#endif
33993399

3400-
// LCD timeout to status screen default is 15s
3401-
#ifndef LCD_TIMEOUT_TO_STATUS
3402-
#define LCD_TIMEOUT_TO_STATUS 15000
3400+
#if HAS_LCD_MENU
3401+
// LCD timeout to status screen default is 15s
3402+
#ifndef LCD_TIMEOUT_TO_STATUS
3403+
#define LCD_TIMEOUT_TO_STATUS 15000
3404+
#endif
3405+
#if LCD_TIMEOUT_TO_STATUS
3406+
#define SCREENS_CAN_TIME_OUT 1
3407+
#endif
34033408
#endif
34043409

34053410
// Add commands that need sub-codes to this list

Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/base_screen.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bool BaseScreen::buttonStyleCallback(CommandProcessor &cmd, uint8_t tag, uint8_t
4545
return false;
4646
}
4747

48-
#if LCD_TIMEOUT_TO_STATUS > 0
48+
#if SCREENS_CAN_TIME_OUT
4949
if (EventLoop::get_pressed_tag() != 0) {
5050
reset_menu_timeout();
5151
}
@@ -65,7 +65,7 @@ bool BaseScreen::buttonStyleCallback(CommandProcessor &cmd, uint8_t tag, uint8_t
6565
}
6666

6767
void BaseScreen::onIdle() {
68-
#if LCD_TIMEOUT_TO_STATUS > 0
68+
#if SCREENS_CAN_TIME_OUT
6969
if ((millis() - last_interaction) > LCD_TIMEOUT_TO_STATUS) {
7070
reset_menu_timeout();
7171
#if ENABLED(TOUCH_UI_DEBUG)
@@ -77,12 +77,10 @@ void BaseScreen::onIdle() {
7777
}
7878

7979
void BaseScreen::reset_menu_timeout() {
80-
#if LCD_TIMEOUT_TO_STATUS > 0
81-
last_interaction = millis();
82-
#endif
80+
TERN_(SCREENS_CAN_TIME_OUT, last_interaction = millis());
8381
}
8482

85-
#if LCD_TIMEOUT_TO_STATUS > 0
83+
#if SCREENS_CAN_TIME_OUT
8684
uint32_t BaseScreen::last_interaction;
8785
#endif
8886

Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/base_screen.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
class BaseScreen : public UIScreen {
2929
protected:
30-
#if LCD_TIMEOUT_TO_STATUS > 0
30+
#if SCREENS_CAN_TIME_OUT
3131
static uint32_t last_interaction;
3232
#endif
3333

Marlin/src/lcd/marlinui.cpp

+8-16
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ constexpr uint8_t epps = ENCODER_PULSES_PER_STEP;
165165
#endif
166166
#endif
167167

168-
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS > 0
168+
#if SCREENS_CAN_TIME_OUT
169169
bool MarlinUI::defer_return_to_status;
170+
millis_t MarlinUI::return_to_status_ms = 0;
170171
#endif
171172

172173
uint8_t MarlinUI::lcd_status_update_delay = 1; // First update one loop delayed
@@ -815,9 +816,6 @@ void MarlinUI::quick_feedback(const bool clear_buttons/*=true*/) {
815816

816817
LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
817818
millis_t next_lcd_update_ms;
818-
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
819-
millis_t MarlinUI::return_to_status_ms = 0;
820-
#endif
821819

822820
inline bool can_encode() {
823821
return !BUTTON_PRESSED(ENC_EN); // Update encoder only when ENC_EN is not LOW (pressed)
@@ -828,12 +826,6 @@ void MarlinUI::update() {
828826
static uint16_t max_display_update_time = 0;
829827
millis_t ms = millis();
830828

831-
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS > 0
832-
#define RESET_STATUS_TIMEOUT() (return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS)
833-
#else
834-
#define RESET_STATUS_TIMEOUT() NOOP
835-
#endif
836-
837829
#ifdef LED_BACKLIGHT_TIMEOUT
838830
leds.update_timeout(powersupply_on);
839831
#endif
@@ -859,7 +851,7 @@ void MarlinUI::update() {
859851

860852
#if HAS_TOUCH_BUTTONS
861853
if (touch_buttons) {
862-
RESET_STATUS_TIMEOUT();
854+
reset_status_timeout(ms);
863855
if (touch_buttons & (EN_A | EN_B)) { // Menu arrows, in priority
864856
if (ELAPSED(ms, next_button_update_ms)) {
865857
encoderDiff = (ENCODER_STEPS_PER_MENU_ITEM) * epps * encoderDirection;
@@ -914,7 +906,7 @@ void MarlinUI::update() {
914906
TERN_(HAS_SLOW_BUTTONS, slow_buttons = read_slow_buttons()); // Buttons that take too long to read in interrupt context
915907

916908
if (TERN0(IS_RRW_KEYPAD, handle_keypad()))
917-
RESET_STATUS_TIMEOUT();
909+
reset_status_timeout(ms);
918910

919911
uint8_t abs_diff = ABS(encoderDiff);
920912

@@ -980,7 +972,7 @@ void MarlinUI::update() {
980972
encoderDiff = 0;
981973
}
982974

983-
RESET_STATUS_TIMEOUT();
975+
reset_status_timeout(ms);
984976

985977
refresh(LCDVIEW_REDRAW_NOW);
986978

@@ -1006,7 +998,7 @@ void MarlinUI::update() {
1006998
lcd_status_update_delay = ++filename_scroll_pos >= filename_scroll_max ? 12 : 4; // Long delay at end and start
1007999
if (filename_scroll_pos > filename_scroll_max) filename_scroll_pos = 0;
10081000
refresh(LCDVIEW_REDRAW_NOW);
1009-
RESET_STATUS_TIMEOUT();
1001+
reset_status_timeout(ms);
10101002
}
10111003
#endif
10121004

@@ -1075,10 +1067,10 @@ void MarlinUI::update() {
10751067
NOLESS(max_display_update_time, millis() - ms);
10761068
}
10771069

1078-
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS > 0
1070+
#if SCREENS_CAN_TIME_OUT
10791071
// Return to Status Screen after a timeout
10801072
if (on_status_screen() || defer_return_to_status)
1081-
RESET_STATUS_TIMEOUT();
1073+
reset_status_timeout(ms);
10821074
else if (ELAPSED(ms, return_to_status_ms))
10831075
return_to_status();
10841076
#endif

Marlin/src/lcd/marlinui.h

+19-14
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,13 @@ class MarlinUI {
449449
static PGM_P get_preheat_label(const uint8_t m);
450450
#endif
451451

452+
#if SCREENS_CAN_TIME_OUT
453+
static inline void reset_status_timeout(const millis_t ms) { return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS; }
454+
#else
455+
static inline void reset_status_timeout(const millis_t) {}
456+
#endif
457+
452458
#if HAS_LCD_MENU
453-
#if LCD_TIMEOUT_TO_STATUS
454-
static millis_t return_to_status_ms;
455-
#endif
456459

457460
#if HAS_TOUCH_BUTTONS
458461
static uint8_t touch_buttons;
@@ -483,7 +486,7 @@ class MarlinUI {
483486
static screenFunc_t currentScreen;
484487
static bool screen_changed;
485488
static void goto_screen(const screenFunc_t screen, const uint16_t encoder=0, const uint8_t top=0, const uint8_t items=0);
486-
static void save_previous_screen();
489+
static void push_current_screen();
487490

488491
// goto_previous_screen and go_back may also be used as menu item callbacks
489492
static void _goto_previous_screen(TERN_(TURBO_BACK_MENU_ITEM, const bool is_back));
@@ -498,12 +501,12 @@ class MarlinUI {
498501
static void lcd_in_status(const bool inStatus);
499502
#endif
500503

504+
FORCE_INLINE static bool screen_is_sticky() {
505+
return TERN1(SCREENS_CAN_TIME_OUT, defer_return_to_status);
506+
}
507+
501508
FORCE_INLINE static void defer_status_screen(const bool defer=true) {
502-
#if LCD_TIMEOUT_TO_STATUS > 0
503-
defer_return_to_status = defer;
504-
#else
505-
UNUSED(defer);
506-
#endif
509+
TERN(SCREENS_CAN_TIME_OUT, defer_return_to_status = defer, UNUSED(defer));
507510
}
508511

509512
static inline void goto_previous_screen_no_defer() {
@@ -655,16 +658,18 @@ class MarlinUI {
655658

656659
private:
657660

661+
#if SCREENS_CAN_TIME_OUT
662+
static millis_t return_to_status_ms;
663+
static bool defer_return_to_status;
664+
#else
665+
static constexpr bool defer_return_to_status = false;
666+
#endif
667+
658668
#if HAS_STATUS_MESSAGE
659669
static void finish_status(const bool persist);
660670
#endif
661671

662672
#if HAS_WIRED_LCD
663-
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS > 0
664-
static bool defer_return_to_status;
665-
#else
666-
static constexpr bool defer_return_to_status = false;
667-
#endif
668673
static void draw_status_screen();
669674
#if HAS_GRAPHICAL_TFT
670675
static void tft_idle();

Marlin/src/lcd/menu/menu.cpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@
5050
int8_t encoderTopLine, encoderLine, screen_items;
5151

5252
typedef struct {
53-
screenFunc_t menu_function;
54-
uint32_t encoder_position;
55-
int8_t top_line, items;
53+
screenFunc_t menu_function; // The screen's function
54+
uint32_t encoder_position; // The position of the encoder
55+
int8_t top_line, items; // The amount of scroll, and the number of items
56+
#if SCREENS_CAN_TIME_OUT
57+
bool sticky; // The screen is sticky
58+
#endif
5659
} menuPosition;
5760
menuPosition screen_history[6];
5861
uint8_t screen_history_depth = 0;
@@ -75,9 +78,9 @@ bool MenuEditItemBase::liveEdit;
7578

7679
void MarlinUI::return_to_status() { goto_screen(status_screen); }
7780

78-
void MarlinUI::save_previous_screen() {
81+
void MarlinUI::push_current_screen() {
7982
if (screen_history_depth < COUNT(screen_history))
80-
screen_history[screen_history_depth++] = { currentScreen, encoderPosition, encoderTopLine, screen_items };
83+
screen_history[screen_history_depth++] = { currentScreen, encoderPosition, encoderTopLine, screen_items OPTARG(SCREENS_CAN_TIME_OUT, screen_is_sticky()) };
8184
}
8285

8386
void MarlinUI::_goto_previous_screen(TERN_(TURBO_BACK_MENU_ITEM, const bool is_back/*=false*/)) {
@@ -90,6 +93,7 @@ void MarlinUI::_goto_previous_screen(TERN_(TURBO_BACK_MENU_ITEM, const bool is_b
9093
is_back ? 0 : sh.top_line,
9194
sh.items
9295
);
96+
defer_status_screen(TERN_(SCREENS_CAN_TIME_OUT, sh.sticky));
9397
}
9498
else
9599
return_to_status();
@@ -147,7 +151,7 @@ void MenuEditItemBase::goto_edit_screen(
147151
) {
148152
TERN_(HAS_TOUCH_BUTTONS, ui.on_edit_screen = true);
149153
ui.screen_changed = true;
150-
ui.save_previous_screen();
154+
ui.push_current_screen();
151155
ui.refresh();
152156
editLabel = el;
153157
editValue = ev;
@@ -237,7 +241,7 @@ void MarlinUI::goto_screen(screenFunc_t screen, const uint16_t encoder/*=0*/, co
237241
//
238242
void MarlinUI::synchronize(PGM_P const msg/*=nullptr*/) {
239243
static PGM_P sync_message = msg ?: GET_TEXT(MSG_MOVING);
240-
save_previous_screen();
244+
push_current_screen();
241245
goto_screen([]{
242246
if (should_draw()) MenuItem_static::draw(LCD_HEIGHT >= 4, sync_message);
243247
});
@@ -371,14 +375,14 @@ void MenuItem_confirm::select_screen(
371375
selectFunc_t yesFunc, selectFunc_t noFunc,
372376
PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/
373377
) {
378+
ui.defer_status_screen();
374379
const bool ui_selection = ui.update_selection(), got_click = ui.use_click();
375380
if (got_click || ui.should_draw()) {
376381
draw_select_screen(yes, no, ui_selection, pref, string, suff);
377382
if (got_click) {
378383
selectFunc_t callFunc = ui_selection ? yesFunc : noFunc;
379384
if (callFunc) callFunc(); else ui.goto_previous_screen();
380385
}
381-
ui.defer_status_screen();
382386
}
383387
}
384388

Marlin/src/lcd/menu/menu_item.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class MenuItem_submenu : public MenuItemBase {
3939
FORCE_INLINE static void draw(const bool sel, const uint8_t row, PGM_P const pstr, ...) {
4040
_draw(sel, row, pstr, '>', LCD_STR_ARROW_RIGHT[0]);
4141
}
42-
static inline void action(PGM_P const, const screenFunc_t func) { ui.save_previous_screen(); ui.goto_screen(func); }
42+
static inline void action(PGM_P const, const screenFunc_t func) { ui.push_current_screen(); ui.goto_screen(func); }
4343
};
4444

4545
// Any menu item that invokes an immediate action
@@ -406,7 +406,7 @@ class MenuItem_bool : public MenuEditItemBase {
406406

407407
#define _CONFIRM_ITEM_INNER_P(PLABEL, V...) do { \
408408
if (encoderLine == _thisItemNr && ui.use_click()) { \
409-
ui.save_previous_screen(); \
409+
ui.push_current_screen(); \
410410
ui.goto_screen([]{MenuItem_confirm::select_screen(V);}); \
411411
return; \
412412
} \

Marlin/src/lcd/menu/menu_password.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ void Password::menu_password() {
177177
START_MENU();
178178
BACK_ITEM(MSG_ADVANCED_SETTINGS);
179179
SUBMENU(MSG_CHANGE_PASSWORD, screen_set_password);
180-
ACTION_ITEM(MSG_REMOVE_PASSWORD, []{ ui.save_previous_screen(); remove_password(); } );
180+
ACTION_ITEM(MSG_REMOVE_PASSWORD, []{ ui.push_current_screen(); remove_password(); } );
181181
#if ENABLED(EEPROM_SETTINGS)
182182
ACTION_ITEM(MSG_STORE_EEPROM, ui.store_settings);
183183
#endif

Marlin/src/lcd/tft/touch.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ void Touch::idle() {
9393
}
9494
#endif
9595

96-
#if LCD_TIMEOUT_TO_STATUS
97-
ui.return_to_status_ms = last_touch_ms + LCD_TIMEOUT_TO_STATUS;
98-
#endif
96+
ui.reset_status_timeout(last_touch_ms);
9997

10098
if (touch_time) {
10199
#if ENABLED(TOUCH_SCREEN_CALIBRATION)

0 commit comments

Comments
 (0)