Skip to content

Commit 1bb6117

Browse files
authored
Merge pull request #184 from mathoudebine/feature/181-turn-the-screen-off-before-computer-sleeps-and-back-on-after-resume
Turn screen off/on when computer goes to/resume from sleep (Windows)
2 parents 17f58f7 + 12399a8 commit 1bb6117

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

configure.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ def __init__(self):
109109
self.wl_cb.place(x=500, y=150, width=210)
110110

111111
self.lhm_admin_warning = ttk.Label(self.window,
112-
text="Admin rights needed, or select another Hardware monitoring",
113-
foreground='#00f')
112+
text="❌ Restart as admin. or select another Hardware monitoring",
113+
foreground='#f00')
114114
self.lhm_admin_warning.place(x=320, y=190)
115115

116116
sysmon_label = ttk.Label(self.window, text='Display configuration', font='bold')
@@ -142,7 +142,7 @@ def __init__(self):
142142
self.brightness_val_label.place(x=500, y=385)
143143
self.brightness_warning_label = ttk.Label(self.window,
144144
text="⚠ Turing / rev. A displays can get hot at high brightness!",
145-
foreground='#f00')
145+
foreground='#ff8c00')
146146
self.brightness_warning_label.place(x=320, y=420)
147147

148148
self.edit_theme_btn = ttk.Button(self.window, text="Edit theme", command=lambda: self.on_theme_editor_click())
@@ -295,12 +295,15 @@ def on_hwlib_change(self, e=None):
295295
self.eth_cb.configure(state="readonly", foreground="#000")
296296
self.wl_cb.configure(state="readonly", foreground="#000")
297297

298-
if hwlib == "LHM":
299-
self.lhm_admin_warning.place(x=320, y=190)
300-
elif hwlib == "AUTO" and sys.platform == "win32":
301-
self.lhm_admin_warning.place(x=320, y=190)
302-
else:
303-
self.lhm_admin_warning.place_forget()
298+
if sys.platform == "win32":
299+
import ctypes
300+
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
301+
if (hwlib == "LHM" or hwlib == "AUTO") and not is_admin:
302+
self.lhm_admin_warning.place(x=320, y=190)
303+
self.save_run_btn.state(["disabled"])
304+
else:
305+
self.lhm_admin_warning.place_forget()
306+
self.save_run_btn.state(["!disabled"])
304307

305308
def show_hide_brightness_warning(self, e=None):
306309
if int(self.brightness_slider.get()) > 50 and [k for k, v in revision_map.items() if v == self.model_cb.get()][

library/display.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ def initialize_display(self):
8282
# Send initialization commands
8383
self.lcd.InitializeComm()
8484

85+
# Turn on display, set brightness and LEDs for supported HW
86+
self.turn_on()
87+
88+
# Set orientation
89+
self.lcd.SetOrientation(_get_theme_orientation())
90+
91+
def turn_on(self):
8592
# Turn screen on in case it was turned off previously
8693
self.lcd.ScreenOn()
8794

@@ -91,8 +98,12 @@ def initialize_display(self):
9198
# Set backplate RGB LED color (for supported HW only)
9299
self.lcd.SetBackplateLedColor(config.THEME_DATA['display'].get("DISPLAY_RGB_LED", (255, 255, 255)))
93100

94-
# Set orientation
95-
self.lcd.SetOrientation(_get_theme_orientation())
101+
def turn_off(self):
102+
# Turn screen off
103+
self.lcd.ScreenOff()
104+
105+
# Turn off backplate RGB LED
106+
self.lcd.SetBackplateLedColor(led_color=(0, 0, 0))
96107

97108
def display_static_images(self):
98109
if config.THEME_DATA.get('static_images', False):

main.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,8 @@
6565

6666

6767
def clean_stop(tray_icon=None):
68-
# Turn screen off before stopping
69-
display.lcd.ScreenOff()
70-
71-
# Turn backplate LED off for supported devices
72-
display.lcd.SetBackplateLedColor(led_color=(0, 0, 0))
68+
# Turn screen and LEDs off before stopping
69+
display.turn_off()
7370

7471
# Do not stop the program now in case data transmission was in progress
7572
# Instead, ask the scheduler to empty the action queue before stopping
@@ -121,15 +118,26 @@ def on_clean_exit(*args):
121118
def on_win32_ctrl_event(event):
122119
"""Handle Windows console control events (like Ctrl-C)."""
123120
if event in (win32con.CTRL_C_EVENT, win32con.CTRL_BREAK_EVENT, win32con.CTRL_CLOSE_EVENT):
124-
logger.info("Caught Windows control event %s, exiting" % event)
121+
logger.debug("Caught Windows control event %s, exiting" % event)
125122
clean_stop()
126123
return 0
127124

128125

129126
def on_win32_wm_event(hWnd, msg, wParam, lParam):
130127
"""Handle Windows window message events (like ENDSESSION, CLOSE, DESTROY)."""
131-
logger.debug("Caught Windows window message event %s, exiting" % msg)
132-
clean_stop()
128+
logger.debug("Caught Windows window message event %s" % msg)
129+
if msg == win32con.WM_POWERBROADCAST:
130+
# WM_POWERBROADCAST is used to detect computer going to/resuming from sleep
131+
if wParam == win32con.PBT_APMSUSPEND:
132+
logger.info("Computer is going to sleep, display will turn off")
133+
display.turn_off()
134+
elif wParam == win32con.PBT_APMRESUMEAUTOMATIC:
135+
logger.info("Computer is resuming from sleep, display will turn on")
136+
display.turn_on()
137+
else:
138+
# For any other events, the program will stop
139+
logger.info("Program will now exit")
140+
clean_stop()
133141

134142
# Create a tray icon for the program, with an Exit entry in menu
135143
try:
@@ -214,7 +222,8 @@ def on_win32_wm_event(hWnd, msg, wParam, lParam):
214222
win32con.WM_ENDSESSION: on_win32_wm_event,
215223
win32con.WM_QUIT: on_win32_wm_event,
216224
win32con.WM_DESTROY: on_win32_wm_event,
217-
win32con.WM_CLOSE: on_win32_wm_event}
225+
win32con.WM_CLOSE: on_win32_wm_event,
226+
win32con.WM_POWERBROADCAST: on_win32_wm_event}
218227

219228
wndclass.lpfnWndProc = messageMap
220229

0 commit comments

Comments
 (0)