@@ -46,6 +46,8 @@ def __init__(self):
46
46
self .no_sleep_before = time .ticks_ms () + (settings .get ("boot_nosleep_time" , 15 ) * 1000 )
47
47
self ._wake_lcd_buttons = None
48
48
self ._current_backlight_val = None
49
+ self ._temp_wake_lcd = False
50
+ self ._deactivated_app = None
49
51
50
52
def switch_app (self , app ):
51
53
"""Asynchronously switch to the specified app."""
@@ -107,7 +109,7 @@ def enter(self):
107
109
# Work out when we need to sleep until
108
110
now = time .ticks_ms ()
109
111
lcd_sleep_time = self ._last_activity_time + self .get_inactivity_time ()
110
- should_sleep_lcd = lcd_sleep_time <= now
112
+ should_sleep_lcd = ( lcd_sleep_time <= now ) and not self . _temp_wake_lcd
111
113
can_sleep = self .can_sleep ()
112
114
if can_sleep and should_sleep_lcd :
113
115
# Then we have to notify the app that we're going to switch off
@@ -116,6 +118,7 @@ def enter(self):
116
118
# this before calling peek_timer()
117
119
deactivated_app = self ._current_app
118
120
if deactivated_app :
121
+ self ._deactivated_app = deactivated_app
119
122
deactivated_app .on_deactivate ()
120
123
121
124
if next_timer_task := self .peek_timer ():
@@ -136,25 +139,23 @@ def enter(self):
136
139
t = next_time - now
137
140
138
141
if can_sleep :
139
- # print(f"Sleepy time {t}")
140
- # Make sure any debug prints show up on the USB UART
141
- tidal_helpers .uart_tx_flush (0 )
142
+ # print(f"Sleepy time {t} should_sleep_lcd={should_sleep_lcd}")
142
143
if should_sleep_lcd :
144
+ self .set_backlight_value (None )
143
145
tidal .lcd_power_off ()
144
146
# Switch buttons so that (a) a press while the screen is off
145
147
# doesn't get passed to the app, and (b) so that any button
146
148
# wakes the screen, even ones the app hasn't registered an
147
149
# interrupt for.
148
150
self .wake_lcd_buttons .activate ()
149
151
152
+ # Make sure any debug prints show up on the UART
153
+ tidal_helpers .uart_tx_flush (0 )
154
+
150
155
wakeup_cause = tidal_helpers .lightsleep (t )
151
156
# print(f"Returned from lightsleep reason={wakeup_cause}")
152
157
153
- if deactivated_app :
154
- # This will also reactivate its buttons
155
- deactivated_app .on_activate ()
156
- deactivated_app = None
157
-
158
+ # deactivated_app's buttons will be reactivated from reset_inactivity
158
159
else :
159
160
if t == 0 :
160
161
# Add a bit of a sleep (which uses less power than straight-up looping)
@@ -183,17 +184,36 @@ def can_sleep(self):
183
184
184
185
def reset_inactivity (self ):
185
186
# print("Reset inactivity")
187
+ if self ._temp_wake_lcd :
188
+ if tidal ._LCD_BLEN .value () == 0 :
189
+ # print("Ignoring reset inactivity while backlight button is down")
190
+ pass
191
+ else :
192
+ # print("Unsetting _temp_wake_lcd")
193
+ self ._temp_wake_lcd = False
194
+ return
195
+
196
+ if self .wake_lcd_buttons .is_active ():
197
+ # Make sure we stop anything involving the backlight pin prior to potentially reconfiguring it
198
+ self .wake_lcd_buttons .deactivate ()
199
+
200
+ if self ._deactivated_app :
201
+ # This will also reactivate its buttons
202
+ # print("Reactivating previous app")
203
+ self ._deactivated_app .on_activate ()
204
+ self ._deactivated_app = None
205
+
186
206
self ._last_activity_time = time .ticks_ms ()
187
- backlight_val = settings .get ("backlight_pwm" )
188
- if tidal .lcd_is_on ():
189
- # No need to reconfigure the pin, and don't reconfigure PWM unless
190
- # necessary, as this restarts the PWM waveform causing a potential
191
- # slight flicker.
192
- if backlight_val != self ._current_backlight_val :
193
- self ._current_backlight_val = backlight_val
194
- tidal_helpers .set_backlight_pwm (tidal ._LCD_BLEN , backlight_val )
195
- else :
207
+ if not tidal .lcd_is_on ():
196
208
tidal .lcd_power_on ()
209
+ # Restore backlight setting if necessary
210
+ self .set_backlight_value (settings .get ("backlight_pwm" ))
211
+
212
+ def set_backlight_value (self , backlight_val ):
213
+ # don't reconfigure PWM unless necessary, as this restarts the PWM
214
+ # waveform causing a potential slight flicker.
215
+ if backlight_val != self ._current_backlight_val :
216
+ self ._current_backlight_val = backlight_val
197
217
tidal_helpers .set_backlight_pwm (tidal ._LCD_BLEN , backlight_val )
198
218
199
219
@property
@@ -204,6 +224,7 @@ def wake_lcd_buttons(self):
204
224
for button in tidal .ALL_BUTTONS :
205
225
# We don't need these button presses to do anything, they just have to exist
206
226
self ._wake_lcd_buttons .on_press (button , lambda : 0 )
227
+ self ._wake_lcd_buttons .on_up_down (tidal ._LCD_BLEN , self .backlight_button_pressed )
207
228
return self ._wake_lcd_buttons
208
229
209
230
def usb_plug_event (self , charging ):
@@ -212,6 +233,17 @@ def usb_plug_event(self, charging):
212
233
# Prevent sleep again to give USB chance to enumerate
213
234
self .no_sleep_before = time .ticks_ms () + (settings .get ("usb_nosleep_time" , 15 ) * 1000 )
214
235
236
+ def backlight_button_pressed (self , pressed ):
237
+ if pressed :
238
+ self ._temp_wake_lcd = True
239
+ # print("LCD temp wake")
240
+ tidal .display .sleep_mode (0 )
241
+ else :
242
+ # Don't clear _temp_wake_lcd here, it has to be done after the reset_inactivity from
243
+ # Buttons.check_buttons(). Yes this has become uglier than I'd like...
244
+ # print("LCD resleep")
245
+ tidal .display .sleep_mode (1 )
246
+
215
247
def get_inactivity_time (self ):
216
248
return settings .get ("inactivity_time" , 30 ) * 1000
217
249
0 commit comments