pythonraspberry-pigpiogpiozero

Simulate "button pressed" an rise an event in gpiozero


I try to develop some code on a machine without GPIO. As GPIO library I selected a gpiozero to be able to write my code without access to gpio of raspberry pi. My problem, I cant get ride of .when_pressed event in the code. I simulate state change of the button, but the function is not called.

Device.pin_factory = MockFactory()

def interrupt_Event(channel):
   print("%s puted in the queue", channel)

InputPin.Device.pin_factory.pin(channel)
InputPin.when_pressed  = interrupt_Event

def main():
   try:
        while True:

            time.sleep(1)
                    InputPins[channel].pull=drive_high()
                    time.sleep(0.1) 
                    print("State CHANNEL %s" % channel)
                    print(InputPins[channel].state)
                    InputPins[channel].drive_low()

Till now I have no Idea what is wrong.


Solution

  • when_pressed function should not have arguments (see 2.7 in https://gpiozero.readthedocs.io/en/stable/recipes.html).

    You could define the callback using a loop :Creating functions in a loop (use channel=channel to force early binding of channel value as in example below)

    for channel in channels:
        def onpress(channel=channel):
            print("%s puted in the queue", channel)
        InputPins[channel].when_pressed = onpress