Skip to content

Commit 9f989d6

Browse files
thinkyheadborland1
authored andcommitted
✨ DOGM Display Sleep (MarlinFirmware#23992)
Co-authored-by: borland1 <[email protected]>
1 parent 85a38c9 commit 9f989d6

13 files changed

+141
-2
lines changed

Marlin/Configuration_adv.h

+11
Original file line numberDiff line numberDiff line change
@@ -1696,6 +1696,17 @@
16961696
// Western only. Not available for Cyrillic, Kana, Turkish, Greek, or Chinese.
16971697
//#define USE_SMALL_INFOFONT
16981698

1699+
/**
1700+
* Graphical Display Sleep
1701+
*
1702+
* The U8G library provides sleep / wake functions for SH1106, SSD1306,
1703+
* SSD1309, and some other DOGM displays.
1704+
* Enable this option to save energy and prevent OLED pixel burn-in.
1705+
* Adds the menu item Configuration > LCD Timeout (m) to set a wait period
1706+
* from 0 (disabled) to 99 minutes.
1707+
*/
1708+
//#define DISPLAY_SLEEP_MINUTES 2 // (minutes) Timeout before turning off the screen
1709+
16991710
/**
17001711
* ST7920-based LCDs can emulate a 16 x 4 character display using
17011712
* the ST7920 character-generator for very fast screen updates.

Marlin/src/core/language.h

+1
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@
303303
#define STR_MATERIAL_HEATUP "Material heatup parameters"
304304
#define STR_LCD_CONTRAST "LCD Contrast"
305305
#define STR_LCD_BRIGHTNESS "LCD Brightness"
306+
#define STR_DISPLAY_SLEEP "Display Sleep"
306307
#define STR_UI_LANGUAGE "UI Language"
307308
#define STR_Z_PROBE_OFFSET "Z-Probe Offset"
308309
#define STR_TEMPERATURE_UNITS "Temperature Units"

Marlin/src/gcode/gcode.h

+6
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
* M226 - Wait until a pin is in a given state: "M226 P<pin> S<state>" (Requires DIRECT_PIN_CONTROL)
203203
* M240 - Trigger a camera to take a photograph. (Requires PHOTO_GCODE)
204204
* M250 - Set LCD contrast: "M250 C<contrast>" (0-63). (Requires LCD support)
205+
* M255 - Set LCD sleep time: "M255 S<minutes>" (0-99). (Requires an LCD with brightness or sleep/wake)
205206
* M256 - Set LCD brightness: "M256 B<brightness>" (0-255). (Requires an LCD with brightness control)
206207
* M260 - i2c Send Data (Requires EXPERIMENTAL_I2CBUS)
207208
* M261 - i2c Request Data (Requires EXPERIMENTAL_I2CBUS)
@@ -879,6 +880,11 @@ class GcodeSuite {
879880
static void M250_report(const bool forReplay=true);
880881
#endif
881882

883+
#if HAS_DISPLAY_SLEEP
884+
static void M255();
885+
static void M255_report(const bool forReplay=true);
886+
#endif
887+
882888
#if HAS_LCD_BRIGHTNESS
883889
static void M256();
884890
static void M256_report(const bool forReplay=true);

Marlin/src/gcode/lcd/M255.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2022 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
#include "../../inc/MarlinConfig.h"
23+
24+
#if HAS_GCODE_M255
25+
26+
#include "../gcode.h"
27+
#include "../../lcd/marlinui.h"
28+
29+
/**
30+
* M255: Set the LCD sleep timeout (in minutes)
31+
* S<minutes> - Period of inactivity required for display / backlight sleep
32+
*/
33+
void GcodeSuite::M255() {
34+
if (parser.seenval('S')) {
35+
#if HAS_DISPLAY_SLEEP
36+
const int m = parser.value_int();
37+
ui.sleep_timeout_minutes = constrain(m, SLEEP_TIMEOUT_MIN, SLEEP_TIMEOUT_MAX);
38+
#else
39+
const int s = parser.value_int() * 60;
40+
ui.lcd_backlight_timeout = constrain(s, LCD_BKL_TIMEOUT_MIN, LCD_BKL_TIMEOUT_MAX);
41+
#endif
42+
}
43+
else
44+
M255_report();
45+
}
46+
47+
void GcodeSuite::M255_report(const bool forReplay/*=true*/) {
48+
report_heading_etc(forReplay, F(STR_DISPLAY_SLEEP));
49+
SERIAL_ECHOLNPGM(" M255 S",
50+
#if HAS_DISPLAY_SLEEP
51+
ui.sleep_timeout_minutes, " ; (minutes)"
52+
#else
53+
ui.lcd_backlight_timeout, " ; (seconds)"
54+
#endif
55+
);
56+
}
57+
58+
#endif // HAS_GCODE_M255

Marlin/src/inc/Conditionals_adv.h

+6
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,12 @@
628628
#if ALL(HAS_RESUME_CONTINUE, PRINTER_EVENT_LEDS, SDSUPPORT)
629629
#define HAS_LEDS_OFF_FLAG 1
630630
#endif
631+
#ifdef DISPLAY_SLEEP_MINUTES
632+
#define HAS_DISPLAY_SLEEP 1
633+
#endif
634+
#if HAS_DISPLAY_SLEEP || LCD_BACKLIGHT_TIMEOUT
635+
#define HAS_GCODE_M255 1
636+
#endif
631637

632638
#if EITHER(DIGIPOT_MCP4018, DIGIPOT_MCP4451)
633639
#define HAS_MOTOR_CURRENT_I2C 1

Marlin/src/inc/SanityCheck.h

+11
Original file line numberDiff line numberDiff line change
@@ -2991,6 +2991,17 @@ static_assert(Y_MAX_LENGTH >= Y_BED_SIZE, "Movement bounds (Y_MIN_POS, Y_MAX_POS
29912991
#endif
29922992
#endif
29932993

2994+
/**
2995+
* Display Sleep is not supported by these common displays
2996+
*/
2997+
#if HAS_DISPLAY_SLEEP
2998+
#if ANY(IS_U8GLIB_LM6059_AF, IS_U8GLIB_ST7565_64128, REPRAPWORLD_GRAPHICAL_LCD, FYSETC_MINI, ENDER2_STOCKDISPLAY, MINIPANEL)
2999+
#error "DISPLAY_SLEEP_MINUTES is not supported by your display."
3000+
#elif !WITHIN(DISPLAY_SLEEP_MINUTES, 0, 255)
3001+
#error "DISPLAY_SLEEP_MINUTES must be between 0 and 255."
3002+
#endif
3003+
#endif
3004+
29943005
/**
29953006
* Some boards forbid the use of -1 Native USB
29963007
*/

Marlin/src/lcd/dogm/marlinui_DOGM.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,11 @@ void MarlinUI::draw_kill_screen() {
342342

343343
void MarlinUI::clear_lcd() { } // Automatically cleared by Picture Loop
344344

345+
#if HAS_DISPLAY_SLEEP
346+
void MarlinUI::sleep_on() { u8g.sleepOn(); }
347+
void MarlinUI::sleep_off() { u8g.sleepOff(); }
348+
#endif
349+
345350
#if HAS_LCD_BRIGHTNESS
346351

347352
void MarlinUI::_set_brightness() {

Marlin/src/lcd/language/language_en.h

+1
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ namespace Language_en {
422422
LSTR MSG_CONTRAST = _UxGT("LCD Contrast");
423423
LSTR MSG_BRIGHTNESS = _UxGT("LCD Brightness");
424424
LSTR MSG_LCD_TIMEOUT_SEC = _UxGT("LCD Timeout (s)");
425+
LSTR MSG_SCREEN_TIMEOUT = _UxGT("LCD Timeout (m)");
425426
LSTR MSG_BRIGHTNESS_OFF = _UxGT("Backlight Off");
426427
LSTR MSG_STORE_EEPROM = _UxGT("Store Settings");
427428
LSTR MSG_LOAD_EEPROM = _UxGT("Load Settings");

Marlin/src/lcd/marlinui.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,15 @@ constexpr uint8_t epps = ENCODER_PULSES_PER_STEP;
192192
WRITE(LCD_BACKLIGHT_PIN, HIGH);
193193
}
194194

195+
#elif HAS_DISPLAY_SLEEP
196+
197+
uint8_t MarlinUI::sleep_timeout_minutes; // Initialized by settings.load()
198+
millis_t MarlinUI::screen_timeout_millis = 0;
199+
void MarlinUI::refresh_screen_timeout() {
200+
screen_timeout_millis = sleep_timeout_minutes ? millis() + sleep_timeout_minutes * 60UL * 1000UL : 0;
201+
sleep_off();
202+
}
203+
195204
#endif
196205

197206
void MarlinUI::init() {
@@ -1061,6 +1070,8 @@ void MarlinUI::init() {
10611070

10621071
#if LCD_BACKLIGHT_TIMEOUT
10631072
refresh_backlight_timeout();
1073+
#elif HAS_DISPLAY_SLEEP
1074+
refresh_screen_timeout();
10641075
#endif
10651076

10661077
refresh(LCDVIEW_REDRAW_NOW);
@@ -1172,6 +1183,9 @@ void MarlinUI::init() {
11721183
WRITE(LCD_BACKLIGHT_PIN, LOW); // Backlight off
11731184
backlight_off_ms = 0;
11741185
}
1186+
#elif HAS_DISPLAY_SLEEP
1187+
if (screen_timeout_millis && ELAPSED(ms, screen_timeout_millis))
1188+
sleep_on();
11751189
#endif
11761190

11771191
// Change state of drawing flag between screen updates

Marlin/src/lcd/marlinui.h

+8
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,14 @@ class MarlinUI {
279279
static uint16_t lcd_backlight_timeout;
280280
static millis_t backlight_off_ms;
281281
static void refresh_backlight_timeout();
282+
#elif HAS_DISPLAY_SLEEP
283+
#define SLEEP_TIMEOUT_MIN 0
284+
#define SLEEP_TIMEOUT_MAX 99
285+
static uint8_t sleep_timeout_minutes;
286+
static millis_t screen_timeout_millis;
287+
static void refresh_screen_timeout();
288+
static void sleep_on();
289+
static void sleep_off();
282290
#endif
283291

284292
#if HAS_DWIN_E3V2_BASIC

Marlin/src/lcd/menu/menu_configuration.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,8 @@ void menu_configuration() {
547547
//
548548
#if LCD_BACKLIGHT_TIMEOUT && LCD_BKL_TIMEOUT_MIN < LCD_BKL_TIMEOUT_MAX
549549
EDIT_ITEM(uint16_4, MSG_LCD_TIMEOUT_SEC, &ui.lcd_backlight_timeout, LCD_BKL_TIMEOUT_MIN, LCD_BKL_TIMEOUT_MAX, ui.refresh_backlight_timeout);
550+
#elif HAS_DISPLAY_SLEEP
551+
EDIT_ITEM(uint8, MSG_SCREEN_TIMEOUT, &ui.sleep_timeout_minutes, SLEEP_TIMEOUT_MIN, SLEEP_TIMEOUT_MAX, ui.refresh_screen_timeout);
550552
#endif
551553

552554
#if ENABLED(FWRETRACT)

Marlin/src/module/settings.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,9 @@ typedef struct SettingsDataStruct {
402402
// Display Sleep
403403
//
404404
#if LCD_BACKLIGHT_TIMEOUT
405-
uint16_t lcd_backlight_timeout; // (G-code needed)
405+
uint16_t lcd_backlight_timeout; // M255 S
406+
#elif HAS_DISPLAY_SLEEP
407+
uint8_t sleep_timeout_minutes; // M255 S
406408
#endif
407409

408410
//
@@ -631,6 +633,8 @@ void MarlinSettings::postprocess() {
631633

632634
#if LCD_BACKLIGHT_TIMEOUT
633635
ui.refresh_backlight_timeout();
636+
#elif HAS_DISPLAY_SLEEP
637+
ui.refresh_screen_timeout();
634638
#endif
635639
}
636640

@@ -1146,6 +1150,8 @@ void MarlinSettings::postprocess() {
11461150
//
11471151
#if LCD_BACKLIGHT_TIMEOUT
11481152
EEPROM_WRITE(ui.lcd_backlight_timeout);
1153+
#elif HAS_DISPLAY_SLEEP
1154+
EEPROM_WRITE(ui.sleep_timeout_minutes);
11491155
#endif
11501156

11511157
//
@@ -2095,6 +2101,8 @@ void MarlinSettings::postprocess() {
20952101
//
20962102
#if LCD_BACKLIGHT_TIMEOUT
20972103
EEPROM_READ(ui.lcd_backlight_timeout);
2104+
#elif HAS_DISPLAY_SLEEP
2105+
EEPROM_READ(ui.sleep_timeout_minutes);
20982106
#endif
20992107

21002108
//
@@ -3172,6 +3180,8 @@ void MarlinSettings::reset() {
31723180
//
31733181
#if LCD_BACKLIGHT_TIMEOUT
31743182
ui.lcd_backlight_timeout = LCD_BACKLIGHT_TIMEOUT;
3183+
#elif HAS_DISPLAY_SLEEP
3184+
ui.sleep_timeout_minutes = DISPLAY_SLEEP_MINUTES;
31753185
#endif
31763186

31773187
//
@@ -3502,6 +3512,11 @@ void MarlinSettings::reset() {
35023512
//
35033513
TERN_(HAS_LCD_CONTRAST, gcode.M250_report(forReplay));
35043514

3515+
//
3516+
// Display Sleep
3517+
//
3518+
TERN_(HAS_GCODE_M255, gcode.M255_report(forReplay));
3519+
35053520
//
35063521
// LCD Brightness
35073522
//

ini/features.ini

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ USES_LIQUIDCRYSTAL_I2C = marcoschwartz/[email protected]
3737
USES_LIQUIDTWI2 = [email protected]
3838
HAS_LCDPRINT = src_filter=+<src/lcd/lcdprint.cpp>
3939
HAS_MARLINUI_HD44780 = src_filter=+<src/lcd/HD44780>
40-
HAS_MARLINUI_U8GLIB = U8glib-HAL@~0.5.0
40+
HAS_MARLINUI_U8GLIB = U8glib-HAL@~0.5.2
4141
src_filter=+<src/lcd/dogm>
4242
HAS_(FSMC|SPI|LTDC)_TFT = src_filter=+<src/HAL/STM32/tft> +<src/HAL/STM32F1/tft> +<src/lcd/tft_io>
4343
HAS_FSMC_TFT = src_filter=+<src/HAL/STM32/tft/tft_fsmc.cpp> +<src/HAL/STM32F1/tft/tft_fsmc.cpp>
@@ -202,6 +202,7 @@ HAS_RESUME_CONTINUE = src_filter=+<src/gcode/lcd/M0_M1.cpp>
202202
LCD_SET_PROGRESS_MANUALLY = src_filter=+<src/gcode/lcd/M73.cpp>
203203
HAS_STATUS_MESSAGE = src_filter=+<src/gcode/lcd/M117.cpp>
204204
HAS_LCD_CONTRAST = src_filter=+<src/gcode/lcd/M250.cpp>
205+
HAS_GCODE_M255 = src_filter=+<src/gcode/lcd/M255.cpp>
205206
HAS_LCD_BRIGHTNESS = src_filter=+<src/gcode/lcd/M256.cpp>
206207
HAS_BUZZER = src_filter=+<src/gcode/lcd/M300.cpp>
207208
TOUCH_SCREEN_CALIBRATION = src_filter=+<src/gcode/lcd/M995.cpp>

0 commit comments

Comments
 (0)