2

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.

Diddlik
  • 76
  • 1
  • 9

2 Answers2

1

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
0

I am not convinced that you are using drive_high and drive_low to simulate the button pushing. I have a almost identical problem. using Mock pins to develop a Pi program on windows, I find that the callback routines are not called.

from gpiozero.pins.mock import MockFactory
from gpiozero import Device, Button, LED
from time import sleep

Device.pin_factory = MockFactory()  # set default pin 
factory

btn = Button(16)

# Get a reference to mock pin 16 (used by the button)
btn_pin = Device.pin_factory.pin(16)

def pressed():       #  callback 
    print('pressed')

def released():       #  callback 
    print('released')    

btn.when_pressed  = pressed  
btn.when_released = released  # callback routine

for i in range(3):           # now try to signal sensor
    print('pushing the button')
    btn_pin.drive_high
    sleep(0.1)
    btn_pin.drive_low
    sleep(0.2)

The output has no callbacks, just

pushing the button

pushing the button

pushing the button
>>> 
Nick
  • 97
  • 10