Skip to content

Commit e4381f9

Browse files
committed
Wake the display when BL button is pressed when LCD is off
1 parent 42e0d86 commit e4381f9

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

modules/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def on_activate(self):
4646

4747
def on_deactivate(self):
4848
self._is_active = False
49+
if buttons := self.buttons:
50+
buttons.deactivate()
4951

5052
def on_tick(self):
5153
# Only for things that need scheduler hooks, like UguiApp

modules/buttons.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def _register_button(self, button):
7474
elif self._is_registered(button):
7575
del self._callbacks[k]
7676
if self.is_active():
77+
# print(f"Unregistering {button.pin}")
7778
tidal_helpers.set_lightsleep_irq(button.pin, None, None)
7879

7980
def _is_registered(self, button):
@@ -177,6 +178,7 @@ def deactivate(self):
177178
self._cancel_autorepeat()
178179

179180
for button in self._callbacks.values():
181+
# print(f"Unregistering {button.pin}")
180182
tidal_helpers.set_lightsleep_irq(button.pin, None, None)
181183
if button.updown and button.state == 0:
182184
# Simulate a button up

modules/scheduler.py

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ def __init__(self):
4646
self.no_sleep_before = time.ticks_ms() + (settings.get("boot_nosleep_time", 15) * 1000)
4747
self._wake_lcd_buttons = None
4848
self._current_backlight_val = None
49+
self._temp_wake_lcd = False
50+
self._deactivated_app = None
4951

5052
def switch_app(self, app):
5153
"""Asynchronously switch to the specified app."""
@@ -107,7 +109,7 @@ def enter(self):
107109
# Work out when we need to sleep until
108110
now = time.ticks_ms()
109111
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
111113
can_sleep = self.can_sleep()
112114
if can_sleep and should_sleep_lcd:
113115
# Then we have to notify the app that we're going to switch off
@@ -116,6 +118,7 @@ def enter(self):
116118
# this before calling peek_timer()
117119
deactivated_app = self._current_app
118120
if deactivated_app:
121+
self._deactivated_app = deactivated_app
119122
deactivated_app.on_deactivate()
120123

121124
if next_timer_task := self.peek_timer():
@@ -136,25 +139,23 @@ def enter(self):
136139
t = next_time - now
137140

138141
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}")
142143
if should_sleep_lcd:
144+
self.set_backlight_value(None)
143145
tidal.lcd_power_off()
144146
# Switch buttons so that (a) a press while the screen is off
145147
# doesn't get passed to the app, and (b) so that any button
146148
# wakes the screen, even ones the app hasn't registered an
147149
# interrupt for.
148150
self.wake_lcd_buttons.activate()
149151

152+
# Make sure any debug prints show up on the UART
153+
tidal_helpers.uart_tx_flush(0)
154+
150155
wakeup_cause = tidal_helpers.lightsleep(t)
151156
# print(f"Returned from lightsleep reason={wakeup_cause}")
152157

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
158159
else:
159160
if t == 0:
160161
# Add a bit of a sleep (which uses less power than straight-up looping)
@@ -183,17 +184,36 @@ def can_sleep(self):
183184

184185
def reset_inactivity(self):
185186
# 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+
186206
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():
196208
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
197217
tidal_helpers.set_backlight_pwm(tidal._LCD_BLEN, backlight_val)
198218

199219
@property
@@ -204,6 +224,7 @@ def wake_lcd_buttons(self):
204224
for button in tidal.ALL_BUTTONS:
205225
# We don't need these button presses to do anything, they just have to exist
206226
self._wake_lcd_buttons.on_press(button, lambda: 0)
227+
self._wake_lcd_buttons.on_up_down(tidal._LCD_BLEN, self.backlight_button_pressed)
207228
return self._wake_lcd_buttons
208229

209230
def usb_plug_event(self, charging):
@@ -212,6 +233,17 @@ def usb_plug_event(self, charging):
212233
# Prevent sleep again to give USB chance to enumerate
213234
self.no_sleep_before = time.ticks_ms() + (settings.get("usb_nosleep_time", 15) * 1000)
214235

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+
215247
def get_inactivity_time(self):
216248
return settings.get("inactivity_time", 30) * 1000
217249

modules/tidal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,12 @@ def lcd_is_on():
143143

144144
def lcd_backlight_on(on=True):
145145
if on:
146+
# print("lcd_backlight_on pin is OUT")
146147
_LCD_BLEN.init(mode=Pin.OUT, drive=Pin.DRIVE_0, value=0)
148+
# This is overwritten somewhere along the line if we try and use BL as a button...
149+
tidal_helpers.gpio_sleep_sel(_LCD_BLEN, False)
147150
else:
151+
# print("lcd_backlight_off pin is IN")
148152
_LCD_BLEN.init(mode=Pin.IN, pull=Pin.PULL_UP)
149153

150154
def lcd_backlight_off():

0 commit comments

Comments
 (0)