Skip to content

Commit

Permalink
Wake the display when BL button is pressed when LCD is off
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsci committed May 28, 2022
1 parent 42e0d86 commit e4381f9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
2 changes: 2 additions & 0 deletions modules/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def on_activate(self):

def on_deactivate(self):
self._is_active = False
if buttons := self.buttons:
buttons.deactivate()

def on_tick(self):
# Only for things that need scheduler hooks, like UguiApp
Expand Down
2 changes: 2 additions & 0 deletions modules/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def _register_button(self, button):
elif self._is_registered(button):
del self._callbacks[k]
if self.is_active():
# print(f"Unregistering {button.pin}")
tidal_helpers.set_lightsleep_irq(button.pin, None, None)

def _is_registered(self, button):
Expand Down Expand Up @@ -177,6 +178,7 @@ def deactivate(self):
self._cancel_autorepeat()

for button in self._callbacks.values():
# print(f"Unregistering {button.pin}")
tidal_helpers.set_lightsleep_irq(button.pin, None, None)
if button.updown and button.state == 0:
# Simulate a button up
Expand Down
68 changes: 50 additions & 18 deletions modules/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def __init__(self):
self.no_sleep_before = time.ticks_ms() + (settings.get("boot_nosleep_time", 15) * 1000)
self._wake_lcd_buttons = None
self._current_backlight_val = None
self._temp_wake_lcd = False
self._deactivated_app = None

def switch_app(self, app):
"""Asynchronously switch to the specified app."""
Expand Down Expand Up @@ -107,7 +109,7 @@ def enter(self):
# Work out when we need to sleep until
now = time.ticks_ms()
lcd_sleep_time = self._last_activity_time + self.get_inactivity_time()
should_sleep_lcd = lcd_sleep_time <= now
should_sleep_lcd = (lcd_sleep_time <= now) and not self._temp_wake_lcd
can_sleep = self.can_sleep()
if can_sleep and should_sleep_lcd:
# Then we have to notify the app that we're going to switch off
Expand All @@ -116,6 +118,7 @@ def enter(self):
# this before calling peek_timer()
deactivated_app = self._current_app
if deactivated_app:
self._deactivated_app = deactivated_app
deactivated_app.on_deactivate()

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

if can_sleep:
# print(f"Sleepy time {t}")
# Make sure any debug prints show up on the USB UART
tidal_helpers.uart_tx_flush(0)
# print(f"Sleepy time {t} should_sleep_lcd={should_sleep_lcd}")
if should_sleep_lcd:
self.set_backlight_value(None)
tidal.lcd_power_off()
# Switch buttons so that (a) a press while the screen is off
# doesn't get passed to the app, and (b) so that any button
# wakes the screen, even ones the app hasn't registered an
# interrupt for.
self.wake_lcd_buttons.activate()

# Make sure any debug prints show up on the UART
tidal_helpers.uart_tx_flush(0)

wakeup_cause = tidal_helpers.lightsleep(t)
# print(f"Returned from lightsleep reason={wakeup_cause}")

if deactivated_app:
# This will also reactivate its buttons
deactivated_app.on_activate()
deactivated_app = None

# deactivated_app's buttons will be reactivated from reset_inactivity
else:
if t == 0:
# Add a bit of a sleep (which uses less power than straight-up looping)
Expand Down Expand Up @@ -183,17 +184,36 @@ def can_sleep(self):

def reset_inactivity(self):
# print("Reset inactivity")
if self._temp_wake_lcd:
if tidal._LCD_BLEN.value() == 0:
# print("Ignoring reset inactivity while backlight button is down")
pass
else:
# print("Unsetting _temp_wake_lcd")
self._temp_wake_lcd = False
return

if self.wake_lcd_buttons.is_active():
# Make sure we stop anything involving the backlight pin prior to potentially reconfiguring it
self.wake_lcd_buttons.deactivate()

if self._deactivated_app:
# This will also reactivate its buttons
# print("Reactivating previous app")
self._deactivated_app.on_activate()
self._deactivated_app = None

self._last_activity_time = time.ticks_ms()
backlight_val = settings.get("backlight_pwm")
if tidal.lcd_is_on():
# No need to reconfigure the pin, and don't reconfigure PWM unless
# necessary, as this restarts the PWM waveform causing a potential
# slight flicker.
if backlight_val != self._current_backlight_val:
self._current_backlight_val = backlight_val
tidal_helpers.set_backlight_pwm(tidal._LCD_BLEN, backlight_val)
else:
if not tidal.lcd_is_on():
tidal.lcd_power_on()
# Restore backlight setting if necessary
self.set_backlight_value(settings.get("backlight_pwm"))

def set_backlight_value(self, backlight_val):
# don't reconfigure PWM unless necessary, as this restarts the PWM
# waveform causing a potential slight flicker.
if backlight_val != self._current_backlight_val:
self._current_backlight_val = backlight_val
tidal_helpers.set_backlight_pwm(tidal._LCD_BLEN, backlight_val)

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

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

def backlight_button_pressed(self, pressed):
if pressed:
self._temp_wake_lcd = True
# print("LCD temp wake")
tidal.display.sleep_mode(0)
else:
# Don't clear _temp_wake_lcd here, it has to be done after the reset_inactivity from
# Buttons.check_buttons(). Yes this has become uglier than I'd like...
# print("LCD resleep")
tidal.display.sleep_mode(1)

def get_inactivity_time(self):
return settings.get("inactivity_time", 30) * 1000

Expand Down
4 changes: 4 additions & 0 deletions modules/tidal.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ def lcd_is_on():

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

def lcd_backlight_off():
Expand Down

0 comments on commit e4381f9

Please sign in to comment.