Hi,
I’m trying to create a toggle switch for the backlight of the DotHat using the ButtonShim.
I tried several things :
- a toggle variable set to True and check with an if statement.
- a for r, g, b in backlight.rgb(r, g, b)…
This is my last try :
@buttonshim.on_press(buttonshim.BUTTON_A)
def button_a(button, pressed,):
r, g, b = backlight.rgb(r, g, b)
if r > 0 or g > 0 or b > 0:
backlight.off()
else :
backlight.rgb(255, 42, 255)
Sometimes i can turn off the backlight but never set an rgb value when it is off…
Does someone have an idea how to solve this ?
Thanks. :)
PictorSomni
The r, g, b = backlight.rgb(r, g, b)
line wont accomplish anything, since rgb
doesn’t return a value.
Internally the Display-o-Tron backlight is 19 individual LEDs, with no concept of an RGB colour that applies to them all.
So you should either keep track of the r, g and b values yourself, or the on/off state or perhaps even both.
r, g, b, on = 255, 42, 255, True
@buttonshim.on_press(buttonshim.BUTTON_A)
def button_a(button, pressed):
global on
on = not on
backlight.rgb(r * on, g * on, b * on)
It’s a naughty thing to rely upon, but Python will silently treat “True” and “False” as 1 and 0 for the purpose of mathematical operations, so:
>>> 42 * False
0
>>> 42 * True
42
The above would mean your r, g, b backlight colour and the on/off state are separate properties so you can change the r, g, b elsewhere and toggle the backlight on/off.
1 Like
Thank you Phil, you truly deserves that exquisite moustache of yours. ;)
1 Like