Skip to content

Commit

Permalink
✨ LCD Backlight Timer (MarlinFirmware#23768)
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
  • Loading branch information
2 people authored and Omkar Dhekne committed Mar 25, 2024
1 parent 0221771 commit 02bc407
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Marlin/Configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -2824,7 +2824,7 @@
#define BUTTON_DELAY_EDIT 50 // (ms) Button repeat delay for edit screens
#define BUTTON_DELAY_MENU 250 // (ms) Button repeat delay for menus

//#define TOUCH_IDLE_SLEEP 300 // (secs) Turn off the TFT backlight if set (5mn)
//#define TOUCH_IDLE_SLEEP 300 // (s) Turn off the TFT backlight if set (5mn)

#define TOUCH_SCREEN_CALIBRATION
Expand Down
5 changes: 5 additions & 0 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,11 @@
#define FEEDRATE_CHANGE_BEEP_FREQUENCY 440
#endif

//
// LCD Backlight Timeout
//
//#define LCD_BACKLIGHT_TIMEOUT 30 // (s) Timeout before turning off the backlight

#if HAS_BED_PROBE && EITHER(HAS_MARLINUI_MENU, HAS_TFT_LVGL_UI)
//#define PROBE_OFFSET_WIZARD // Add a Probe Z Offset calibration option to the LCD menu
#if ENABLED(PROBE_OFFSET_WIZARD)
Expand Down
8 changes: 8 additions & 0 deletions Marlin/src/inc/SanityCheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -2857,6 +2857,14 @@ static_assert(Y_MAX_LENGTH >= Y_BED_SIZE, "Movement bounds (Y_MIN_POS, Y_MAX_POS
#endif
#endif

#if LCD_BACKLIGHT_TIMEOUT
#if !HAS_ENCODER_ACTION
#error "LCD_BACKLIGHT_TIMEOUT requires an LCD with encoder or keypad."
#elif !PIN_EXISTS(LCD_BACKLIGHT)
#error "LCD_BACKLIGHT_TIMEOUT requires LCD_BACKLIGHT_PIN."
#endif
#endif

/**
* Some boards forbid the use of -1 Native USB
*/
Expand Down
1 change: 1 addition & 0 deletions Marlin/src/lcd/language/language_en.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ namespace Language_en {
LSTR MSG_ADVANCE_K_E = _UxGT("Advance K *");
LSTR MSG_CONTRAST = _UxGT("LCD Contrast");
LSTR MSG_BRIGHTNESS = _UxGT("LCD Brightness");
LSTR MSG_LCD_BKL_TIMEOUT = _UxGT("LCD Sleep (s)");
LSTR MSG_STORE_EEPROM = _UxGT("Store Settings");
LSTR MSG_LOAD_EEPROM = _UxGT("Load Settings");
LSTR MSG_RESTORE_DEFAULTS = _UxGT("Restore Defaults");
Expand Down
1 change: 1 addition & 0 deletions Marlin/src/lcd/language/language_fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ namespace Language_fr {
LSTR MSG_ADVANCE_K_E = _UxGT("Avance K *");
LSTR MSG_BRIGHTNESS = _UxGT("Luminosité LCD");
LSTR MSG_CONTRAST = _UxGT("Contraste LCD");
LSTR MSG_LCD_BKL_TIMEOUT = _UxGT("Veille LCD (s)");
LSTR MSG_STORE_EEPROM = _UxGT("Enregistrer config.");
LSTR MSG_LOAD_EEPROM = _UxGT("Charger config.");
LSTR MSG_RESTORE_DEFAULTS = _UxGT("Restaurer défauts");
Expand Down
24 changes: 22 additions & 2 deletions Marlin/src/lcd/marlinui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ constexpr uint8_t epps = ENCODER_PULSES_PER_STEP;
volatile int8_t encoderDiff; // Updated in update_buttons, added to encoderPosition every LCD update
#endif

#if LCD_BACKLIGHT_TIMEOUT
uint16_t MarlinUI::lcd_backlight_timeout; // Initialized by settings.load()
millis_t MarlinUI::backlight_off_ms = 0;
void MarlinUI::refresh_backlight_timeout() {
backlight_off_ms = lcd_backlight_timeout ? millis() + lcd_backlight_timeout * 1000UL : 0;
WRITE(LCD_BACKLIGHT_PIN, HIGH);
}
#endif

void MarlinUI::init() {

init_lcd();
Expand Down Expand Up @@ -1033,14 +1042,18 @@ void MarlinUI::init() {

reset_status_timeout(ms);

#if LCD_BACKLIGHT_TIMEOUT
refresh_backlight_timeout();
#endif

refresh(LCDVIEW_REDRAW_NOW);

#if LED_POWEROFF_TIMEOUT > 0
if (!powerManager.psu_on) leds.reset_timeout(ms);
#endif
}
} // encoder activity

#endif
#endif // HAS_ENCODER_ACTION

// This runs every ~100ms when idling often enough.
// Instead of tracking changes just redraw the Status Screen once per second.
Expand Down Expand Up @@ -1137,6 +1150,13 @@ void MarlinUI::init() {
return_to_status();
#endif

#if LCD_BACKLIGHT_TIMEOUT
if (backlight_off_ms && ELAPSED(ms, backlight_off_ms)) {
WRITE(LCD_BACKLIGHT_PIN, LOW); // Backlight off
backlight_off_ms = 0;
}
#endif

// Change state of drawing flag between screen updates
if (!drawing_screen) switch (lcdDrawUpdate) {
case LCDVIEW_CLEAR_CALL_REDRAW:
Expand Down
8 changes: 8 additions & 0 deletions Marlin/src/lcd/marlinui.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,14 @@ class MarlinUI {
FORCE_INLINE static void refresh_brightness() { set_brightness(brightness); }
#endif

#if LCD_BACKLIGHT_TIMEOUT
#define LCD_BKL_TIMEOUT_MIN 1
#define LCD_BKL_TIMEOUT_MAX (60*60*18) // 18 hours max within uint16_t
static uint16_t lcd_backlight_timeout;
static millis_t backlight_off_ms;
static void refresh_backlight_timeout();
#endif

#if HAS_DWIN_E3V2_BASIC
static void refresh();
#else
Expand Down
4 changes: 4 additions & 0 deletions Marlin/src/lcd/menu/menu_configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,10 @@ void menu_configuration() {
#if HAS_LCD_CONTRAST && LCD_CONTRAST_MIN < LCD_CONTRAST_MAX
EDIT_ITEM_FAST(uint8, MSG_CONTRAST, &ui.contrast, LCD_CONTRAST_MIN, LCD_CONTRAST_MAX, ui.refresh_contrast, true);
#endif
#if LCD_BACKLIGHT_TIMEOUT && LCD_BKL_TIMEOUT_MIN < LCD_BKL_TIMEOUT_MAX
EDIT_ITEM(uint16_4, MSG_LCD_BKL_TIMEOUT, &ui.lcd_backlight_timeout, LCD_BKL_TIMEOUT_MIN, LCD_BKL_TIMEOUT_MAX, ui.refresh_backlight_timeout);
#endif

#if ENABLED(FWRETRACT)
SUBMENU(MSG_RETRACT, menu_config_retract);
#endif
Expand Down
32 changes: 32 additions & 0 deletions Marlin/src/module/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,13 @@ typedef struct SettingsDataStruct {
//
uint8_t lcd_brightness; // M256 B

//
// LCD_BACKLIGHT_TIMEOUT
//
#if LCD_BACKLIGHT_TIMEOUT
uint16_t lcd_backlight_timeout; // (G-code needed)
#endif

//
// Controller fan settings
//
Expand Down Expand Up @@ -607,6 +614,10 @@ void MarlinSettings::postprocess() {
// Moved as last update due to interference with Neopixel init
TERN_(HAS_LCD_CONTRAST, ui.refresh_contrast());
TERN_(HAS_LCD_BRIGHTNESS, ui.refresh_brightness());

#if LCD_BACKLIGHT_TIMEOUT
ui.refresh_backlight_timeout();
#endif
}

#if BOTH(PRINTCOUNTER, EEPROM_SETTINGS)
Expand Down Expand Up @@ -1108,6 +1119,13 @@ void MarlinSettings::postprocess() {
EEPROM_WRITE(lcd_brightness);
}

//
// LCD Backlight Timeout
//
#if LCD_BACKLIGHT_TIMEOUT
EEPROM_WRITE(ui.lcd_backlight_timeout);
#endif

//
// Controller Fan
//
Expand Down Expand Up @@ -2015,6 +2033,13 @@ void MarlinSettings::postprocess() {
TERN_(HAS_LCD_BRIGHTNESS, if (!validating) ui.brightness = lcd_brightness);
}

//
// LCD Backlight Timeout
//
#if LCD_BACKLIGHT_TIMEOUT
EEPROM_READ(ui.lcd_backlight_timeout);
#endif

//
// Controller Fan
//
Expand Down Expand Up @@ -3040,6 +3065,13 @@ void MarlinSettings::reset() {
//
TERN_(HAS_LCD_BRIGHTNESS, ui.brightness = LCD_BRIGHTNESS_DEFAULT);

//
// LCD Backlight Timeout
//
#if LCD_BACKLIGHT_TIMEOUT
ui.lcd_backlight_timeout = LCD_BACKLIGHT_TIMEOUT;
#endif

//
// Controller Fan
//
Expand Down

0 comments on commit 02bc407

Please sign in to comment.