Skip to content

Commit

Permalink
♻️ Refactor status screen timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jun 29, 2021
1 parent e3ae76d commit 2b37a71
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 54 deletions.
11 changes: 8 additions & 3 deletions Marlin/src/inc/Conditionals_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -3397,9 +3397,14 @@
#endif
#endif

// LCD timeout to status screen default is 15s
#ifndef LCD_TIMEOUT_TO_STATUS
#define LCD_TIMEOUT_TO_STATUS 15000
#if HAS_LCD_MENU
// LCD timeout to status screen default is 15s
#ifndef LCD_TIMEOUT_TO_STATUS
#define LCD_TIMEOUT_TO_STATUS 15000
#endif
#if LCD_TIMEOUT_TO_STATUS
#define SCREENS_CAN_TIME_OUT 1
#endif
#endif

// Add commands that need sub-codes to this list
Expand Down
10 changes: 4 additions & 6 deletions Marlin/src/lcd/extui/ftdi_eve_touch_ui/generic/base_screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ bool BaseScreen::buttonStyleCallback(CommandProcessor &cmd, uint8_t tag, uint8_t
return false;
}

#if LCD_TIMEOUT_TO_STATUS > 0
#if SCREENS_CAN_TIME_OUT
if (EventLoop::get_pressed_tag() != 0) {
reset_menu_timeout();
}
Expand All @@ -65,7 +65,7 @@ bool BaseScreen::buttonStyleCallback(CommandProcessor &cmd, uint8_t tag, uint8_t
}

void BaseScreen::onIdle() {
#if LCD_TIMEOUT_TO_STATUS > 0
#if SCREENS_CAN_TIME_OUT
if ((millis() - last_interaction) > LCD_TIMEOUT_TO_STATUS) {
reset_menu_timeout();
#if ENABLED(TOUCH_UI_DEBUG)
Expand All @@ -77,12 +77,10 @@ void BaseScreen::onIdle() {
}

void BaseScreen::reset_menu_timeout() {
#if LCD_TIMEOUT_TO_STATUS > 0
last_interaction = millis();
#endif
TERN_(SCREENS_CAN_TIME_OUT, last_interaction = millis());
}

#if LCD_TIMEOUT_TO_STATUS > 0
#if SCREENS_CAN_TIME_OUT
uint32_t BaseScreen::last_interaction;
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class BaseScreen : public UIScreen {
protected:
#if LCD_TIMEOUT_TO_STATUS > 0
#if SCREENS_CAN_TIME_OUT
static uint32_t last_interaction;
#endif

Expand Down
24 changes: 8 additions & 16 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ constexpr uint8_t epps = ENCODER_PULSES_PER_STEP;
#endif
#endif

#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS > 0
#if SCREENS_CAN_TIME_OUT
bool MarlinUI::defer_return_to_status;
millis_t MarlinUI::return_to_status_ms = 0;
#endif

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

LCDViewAction MarlinUI::lcdDrawUpdate = LCDVIEW_CLEAR_CALL_REDRAW;
millis_t next_lcd_update_ms;
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS
millis_t MarlinUI::return_to_status_ms = 0;
#endif

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

#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS > 0
#define RESET_STATUS_TIMEOUT() (return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS)
#else
#define RESET_STATUS_TIMEOUT() NOOP
#endif

#ifdef LED_BACKLIGHT_TIMEOUT
leds.update_timeout(powersupply_on);
#endif
Expand All @@ -859,7 +851,7 @@ void MarlinUI::update() {

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

if (TERN0(IS_RRW_KEYPAD, handle_keypad()))
RESET_STATUS_TIMEOUT();
reset_status_timeout(ms);

uint8_t abs_diff = ABS(encoderDiff);

Expand Down Expand Up @@ -980,7 +972,7 @@ void MarlinUI::update() {
encoderDiff = 0;
}

RESET_STATUS_TIMEOUT();
reset_status_timeout(ms);

refresh(LCDVIEW_REDRAW_NOW);

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

Expand Down Expand Up @@ -1075,10 +1067,10 @@ void MarlinUI::update() {
NOLESS(max_display_update_time, millis() - ms);
}

#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS > 0
#if SCREENS_CAN_TIME_OUT
// Return to Status Screen after a timeout
if (on_status_screen() || defer_return_to_status)
RESET_STATUS_TIMEOUT();
reset_status_timeout(ms);
else if (ELAPSED(ms, return_to_status_ms))
return_to_status();
#endif
Expand Down
33 changes: 19 additions & 14 deletions Marlin/src/lcd/marlinui.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,13 @@ class MarlinUI {
static PGM_P get_preheat_label(const uint8_t m);
#endif

#if SCREENS_CAN_TIME_OUT
static inline void reset_status_timeout(const millis_t ms) { return_to_status_ms = ms + LCD_TIMEOUT_TO_STATUS; }
#else
static inline void reset_status_timeout(const millis_t) {}
#endif

#if HAS_LCD_MENU
#if LCD_TIMEOUT_TO_STATUS
static millis_t return_to_status_ms;
#endif

#if HAS_TOUCH_BUTTONS
static uint8_t touch_buttons;
Expand Down Expand Up @@ -479,7 +482,7 @@ class MarlinUI {
static screenFunc_t currentScreen;
static bool screen_changed;
static void goto_screen(const screenFunc_t screen, const uint16_t encoder=0, const uint8_t top=0, const uint8_t items=0);
static void save_previous_screen();
static void push_current_screen();

// goto_previous_screen and go_back may also be used as menu item callbacks
static void _goto_previous_screen(TERN_(TURBO_BACK_MENU_ITEM, const bool is_back));
Expand All @@ -494,12 +497,12 @@ class MarlinUI {
static void lcd_in_status(const bool inStatus);
#endif

FORCE_INLINE static bool screen_is_sticky() {
return TERN1(SCREENS_CAN_TIME_OUT, defer_return_to_status);
}

FORCE_INLINE static void defer_status_screen(const bool defer=true) {
#if LCD_TIMEOUT_TO_STATUS > 0
defer_return_to_status = defer;
#else
UNUSED(defer);
#endif
TERN(SCREENS_CAN_TIME_OUT, defer_return_to_status = defer, UNUSED(defer));
}

static inline void goto_previous_screen_no_defer() {
Expand Down Expand Up @@ -651,16 +654,18 @@ class MarlinUI {

private:

#if SCREENS_CAN_TIME_OUT
static millis_t return_to_status_ms;
static bool defer_return_to_status;
#else
static constexpr bool defer_return_to_status = false;
#endif

#if HAS_STATUS_MESSAGE
static void finish_status(const bool persist);
#endif

#if HAS_WIRED_LCD
#if HAS_LCD_MENU && LCD_TIMEOUT_TO_STATUS > 0
static bool defer_return_to_status;
#else
static constexpr bool defer_return_to_status = false;
#endif
static void draw_status_screen();
#if HAS_GRAPHICAL_TFT
static void tft_idle();
Expand Down
20 changes: 12 additions & 8 deletions Marlin/src/lcd/menu/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@
int8_t encoderTopLine, encoderLine, screen_items;

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

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

void MarlinUI::save_previous_screen() {
void MarlinUI::push_current_screen() {
if (screen_history_depth < COUNT(screen_history))
screen_history[screen_history_depth++] = { currentScreen, encoderPosition, encoderTopLine, screen_items };
screen_history[screen_history_depth++] = { currentScreen, encoderPosition, encoderTopLine, screen_items OPTARG(SCREENS_CAN_TIME_OUT, screen_is_sticky()) };
}

void MarlinUI::_goto_previous_screen(TERN_(TURBO_BACK_MENU_ITEM, const bool is_back/*=false*/)) {
Expand All @@ -90,6 +93,7 @@ void MarlinUI::_goto_previous_screen(TERN_(TURBO_BACK_MENU_ITEM, const bool is_b
is_back ? 0 : sh.top_line,
sh.items
);
defer_status_screen(TERN_(SCREENS_CAN_TIME_OUT, sh.sticky));
}
else
return_to_status();
Expand Down Expand Up @@ -147,7 +151,7 @@ void MenuEditItemBase::goto_edit_screen(
) {
TERN_(HAS_TOUCH_BUTTONS, ui.on_edit_screen = true);
ui.screen_changed = true;
ui.save_previous_screen();
ui.push_current_screen();
ui.refresh();
editLabel = el;
editValue = ev;
Expand Down Expand Up @@ -237,7 +241,7 @@ void MarlinUI::goto_screen(screenFunc_t screen, const uint16_t encoder/*=0*/, co
//
void MarlinUI::synchronize(PGM_P const msg/*=nullptr*/) {
static PGM_P sync_message = msg ?: GET_TEXT(MSG_MOVING);
save_previous_screen();
push_current_screen();
goto_screen([]{
if (should_draw()) MenuItem_static::draw(LCD_HEIGHT >= 4, sync_message);
});
Expand Down Expand Up @@ -371,14 +375,14 @@ void MenuItem_confirm::select_screen(
selectFunc_t yesFunc, selectFunc_t noFunc,
PGM_P const pref, const char * const string/*=nullptr*/, PGM_P const suff/*=nullptr*/
) {
ui.defer_status_screen();
const bool ui_selection = ui.update_selection(), got_click = ui.use_click();
if (got_click || ui.should_draw()) {
draw_select_screen(yes, no, ui_selection, pref, string, suff);
if (got_click) {
selectFunc_t callFunc = ui_selection ? yesFunc : noFunc;
if (callFunc) callFunc(); else ui.goto_previous_screen();
}
ui.defer_status_screen();
}
}

Expand Down
4 changes: 2 additions & 2 deletions Marlin/src/lcd/menu/menu_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MenuItem_submenu : public MenuItemBase {
FORCE_INLINE static void draw(const bool sel, const uint8_t row, PGM_P const pstr, ...) {
_draw(sel, row, pstr, '>', LCD_STR_ARROW_RIGHT[0]);
}
static inline void action(PGM_P const, const screenFunc_t func) { ui.save_previous_screen(); ui.goto_screen(func); }
static inline void action(PGM_P const, const screenFunc_t func) { ui.push_current_screen(); ui.goto_screen(func); }
};

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

#define _CONFIRM_ITEM_INNER_P(PLABEL, V...) do { \
if (encoderLine == _thisItemNr && ui.use_click()) { \
ui.save_previous_screen(); \
ui.push_current_screen(); \
ui.goto_screen([]{MenuItem_confirm::select_screen(V);}); \
return; \
} \
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/lcd/menu/menu_password.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void Password::menu_password() {
START_MENU();
BACK_ITEM(MSG_ADVANCED_SETTINGS);
SUBMENU(MSG_CHANGE_PASSWORD, screen_set_password);
ACTION_ITEM(MSG_REMOVE_PASSWORD, []{ ui.save_previous_screen(); remove_password(); } );
ACTION_ITEM(MSG_REMOVE_PASSWORD, []{ ui.push_current_screen(); remove_password(); } );
#if ENABLED(EEPROM_SETTINGS)
ACTION_ITEM(MSG_STORE_EEPROM, ui.store_settings);
#endif
Expand Down
4 changes: 1 addition & 3 deletions Marlin/src/lcd/tft/touch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ void Touch::idle() {
}
#endif

#if LCD_TIMEOUT_TO_STATUS
ui.return_to_status_ms = last_touch_ms + LCD_TIMEOUT_TO_STATUS;
#endif
ui.reset_status_timeout(last_touch_ms);

if (touch_time) {
#if ENABLED(TOUCH_SCREEN_CALIBRATION)
Expand Down

0 comments on commit 2b37a71

Please sign in to comment.