3

I have a button object that when pressed needs to perform something, and for this I'd need to get a property of the button. I'd like it to work like:

...
GPIO.add_event_detect(button, GPIO.RISING, callback=button_callback(button)) #I believe I can't add a parameter to button_callback like this
...

and then

def button_callback(button):
    do_something(button.id)

Is this possible? How can I access the button object from within the callback?

2
  • Could you explain or give an example of what you are trying to achieve? What you suggest does not work but if we understand the problem we may have a solution.
    – joan
    Oct 3, 2019 at 15:06
  • Surely 'button' is just an integer representing a GPIO channel or pin?
    – CoderMike
    Oct 3, 2019 at 15:06

2 Answers 2

6

I ran into the same issue. You can solve it without using another library.

This is how you do it.

def button_callback(button):
    print('Button {} pressed'.format(button))

GPIO.add_event_detect(button, GPIO.FALLING, callback=lambda x: button_callback(1))

By using the lambda, you are executing an anonymous function which then calls your button_callback function. Just pass the button id so you know which button was pressed.

Hope this helps! Also, this is my first answer ever on the Stack so please upvote and save as best answer :)

0

gpzero has support for events, very much what you are looking for event-wise:

from gpiozero import Button

def pushed_17():
    print("Don't push the button!")

b = Button(17)
b.when_pressed = pushed_17

From GPIO perspective the buttons don't really have properties other than the PIN they are assigned to: this way even if you had multiple buttons assigned to the same PIN for some reason, you won't be able to track which one exactly was pressed. Unless you wire them to different PINs or build a small adapter to stay framework agnostic:

def button_push(id):
    #do something based on the id passed
    print("Button {} pressed".format(id))

def push_15():
    return button_push(15)
2
  • The button variable passes a channel so I'm able to identify which button was pressed based on index from a global button list. I'm just trying to remove the global variable as that only makes the code less clean. Basically if I could pass the button variable to the button_callback, since button contains the channel info, my problem would be solved. Currently it is working, I'm just trying to remove a global and I just need to pass the button as a callback parameter. Is that possible?
    – lte__
    Oct 4, 2019 at 12:44
  • So, doing a button_push(id) is exactly my problem, that's what I want to do, except I call it button_callback. The question is how can I pass that id? I have callback=button_callback(button) but this is not how it works, I don't think I can do callback=button_callback(button), only callback=button_callback so where do I pass the parameters?
    – lte__
    Oct 8, 2019 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.