DisplayoTron backlight to come on in response to key press?


#1

Hi all,

Been struggling with this for a while. I’d like the backlight of the DisplayoTron to come on in response to any button press and then switch off again say 30 seconds after the last input. I’m aware of the methods for turning on and off the backlight but I’m struggling to understand how I could achieve this without having to totally re-write the menu standard menu class.

Any ideas?!


#2

Eek, this is a good question.

First off if you’re using nav.bind_defaults(menu) you’re going to have to bind the inputs manually. bind_defaults() just binds each button to the corresponding action call on your menu- you can see what it does here: https://github.com/pimoroni/displayotron/blob/7f36f37d60663619bfc853350545b47a369e5f4e/library/dothat/touch.py#L89-L111

Then you’re going to have to acquaint yourself with Python’s threading.Timer to handle the timeout: https://docs.python.org/2/library/threading.html#timer-objects

You would want a global timer object which you cancel and reset every time a button is pushed. Here’s a rough (not functional) example:

BACKLIGHT_TIME_OUT = 30.0  # Seconds
backlight_timer = None

def turn_off_backlight():
    # code to turn off backlight here

def turn_on_backlight():
    # code to turn on backlight here

def call_this_when_a_button_is_pushed():
    global backlight_timer
    turn_on_backlight()
    backlight_timer.cancel()
    backlight_timer = Timer(BACKLIGHT_TIME_OUT, turn_off_backlight())

You would want to call call_this_when_a_button_is_pushed() from each button handler (the bindings in bind_defaults())


#3

Hmm- I recall that the Menu system has a screensaver feature already built in which could call a plugin that turns off the backlight when it’s run, and turns it back on again when it’s cleaned up.

Every button action in the Menu will check the idle status and call cleanup() on the idle_handler plugin when a button is pressed:

So a plugin that just does this set as the idle_handler may also work:

import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav

class IdleBacklight(MenuOption):
    def __init__(self, backlight):
        self.backlight = backlight
        MenuOption.__init__(self)

    def cancel(self):
        self.backlight.rgb(255, 255, 255)

    def begin(self):
        self.backlight.rgb(0, 0, 0)

menu =  Menu(
    structure=... blah blah,
    lcd=lcd,
    idle_handler=IdleBacklight(backlight),
    idle_timeout=3
)

nav.bind_defaults(menu)

while 1:
    menu.redraw()
    time.sleep(0.05)

#4

Okay, made an example to demonstrate this, tested it and pushed it into GitHub: https://github.com/pimoroni/displayotron/blob/master/examples/dothat/advanced/backlight-timeout.py

Note: my example differs slightly from the above solution since I ran into a couple of quirks and improved it to use the backlight setting set by the Backlight config plugin


#5

Thanks chaps! I’ll take a look at this and see if I can get it going and report back.

For ages I wanted to get some code working which controls my Phillips Hue lighting and integrates with the DisplayOTron, I’ve got it all working now and am just tinkering to try and improve the system and add a stack of features.

Learnt loads and having a blast :)


#6

Code works flawlessly!

Many thanks for your assistance, I’d have never have been able to come up with such an elegant solution.


#7

Took me a couple of tries- but the plugin solution was a lucky eureka moment. Enjoy!