|
65 | 65 |
|
66 | 66 |
|
67 | 67 | 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() |
73 | 70 |
|
74 | 71 | # Do not stop the program now in case data transmission was in progress |
75 | 72 | # Instead, ask the scheduler to empty the action queue before stopping |
@@ -121,15 +118,26 @@ def on_clean_exit(*args): |
121 | 118 | def on_win32_ctrl_event(event): |
122 | 119 | """Handle Windows console control events (like Ctrl-C).""" |
123 | 120 | 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) |
125 | 122 | clean_stop() |
126 | 123 | return 0 |
127 | 124 |
|
128 | 125 |
|
129 | 126 | def on_win32_wm_event(hWnd, msg, wParam, lParam): |
130 | 127 | """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() |
133 | 141 |
|
134 | 142 | # Create a tray icon for the program, with an Exit entry in menu |
135 | 143 | try: |
@@ -214,7 +222,8 @@ def on_win32_wm_event(hWnd, msg, wParam, lParam): |
214 | 222 | win32con.WM_ENDSESSION: on_win32_wm_event, |
215 | 223 | win32con.WM_QUIT: on_win32_wm_event, |
216 | 224 | 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} |
218 | 227 |
|
219 | 228 | wndclass.lpfnWndProc = messageMap |
220 | 229 |
|
|
0 commit comments