Replies: 3 comments 4 replies
-
I think Alternatively, you could 'invert' your buttons_config dictionary, and do something like: from gpiozero import ButtonBoard
buttons_config = {
"next": 6,
"prev": 5,
"up": 19,
"down": 26,
"play": 0,
"home": 16,
"hiddenScreen": 21,
"reboot": 17
}
bb = ButtonBoard(hold_time=3, hold_repeat=False, **buttons_config) See https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists |
Beta Was this translation helpful? Give feedback.
-
Yes, what @lurch said. If you have a list on pin numbers, unpack it with If you have a dict mapping names to pins (the opposite of what you have), even better because you can do
This doesn't seem to be covered in the docs but the |
Beta Was this translation helpful? Give feedback.
-
Mapping names to pins is nice for one function pins. The **kwargs works but doesn't exactly do what I need. It forces me to define each and every single button of a defined number of buttons I might have in the script. This is good if I were putting out a multi button device with a fixed number of buttons in the world that just did one thing only and wouldn't be configurable. I could name the pins button1, button2 etc. I added print statements to see what values I get out. //works but not exactly what i want
from gpiozero import ButtonBoard
buttons_config = {
"next": 19,
"prev": 26,
}
bb = ButtonBoard(hold_time=3, hold_repeat=False, **buttons_config)
def do_pressed(this):
print("PRESSED: " + " value " + str(bb.value))
print(this)
print(this.value)
print(this.pin)
print(this.pin.number)
print(' ')
def do_held(this):
print("HELD: " + str(bb.held_time) + " value " + str(bb.value))
print(this)
print(this.value)
print(this.pin)
print(this.pin.number)
print(' ')
def do_released(this):
print("RELEASED" + " value bb:" + str(bb.value))
print(this)
print(this.value)
print(this.pin)
print(this.pin.number)
print(' ')
bb.next.when_pressed = do_pressed
bb.next.when_held = do_held
bb.next.when_released = do_released
bb.prev.when_pressed = do_pressed
bb.prev.when_held = do_held
bb.prev.when_released = do_released But if I don't know how many buttons there are going to be, could be 1 or many, I think Lurch's first example using *args is more what I need. I want to be able to pass both the gpio pins and a list of things the buttons to do in a config.js without having to change the script I write if the number of buttons change. What I want to do is the following but I found that ButtonBoard doesn't return the pin(s) or pin.number(s) when used this way. it throws // kinda works but I need pin.number and it doesn't exist
from gpiozero import ButtonBoard
buttons_config = {
"19":["next","admin"],
"26":["prev","softReboot"],
"17":["screenon","screenoff"]
}
bb = ButtonBoard(*buttons_config,hold_time=3,hold_repeat = False)
def do_pressed(this):
print("PRESSED " + " value " + str(bb.value))
print(this)
print(this.value)
print(this.pin)
print(this.pin.number)
print("I will get the first value for the key and do something but I need the pin.number to get the value")
print(' ')
def do_held(this):
print("HELD: " + str(bb.held_time) + " value " + str(bb.value))
print(this)
print(this.value)
print(this.pin)
print(this.pin.number)
print("I will get the second value for the key and do something but I need the pin.number to get the value")
print(' ')
def do_repleased(this):
print("HELD: " + str(bb.held_time) + " value " + str(bb.value))
print(this)
print(this.value)
print(this.pin)
print(this.pin.number)
print("I will get the third value for the key and do something but I need the pin.number to get the value")
print(' ')
bb.when_pressed = do_pressed
bb.when_held = do_held
bb.when_released = do_held |
Beta Was this translation helpful? Give feedback.
-
Short description of change: be able to define ButtonBoard pins with a list
Where would this change be made: ButtonBoard
Purpose of change:
Allow defining a ButtonBoard from a configuration list of values instead of having to hardcode them.
Please provide example usage of gpiozero with your suggested change:
Beta Was this translation helpful? Give feedback.
All reactions