0

Is there an pigpio equivalent to GPIO's event_detected() function? I need something that is constantly watching for a button press. The code I currently have doesn't consistently detect it (python sample code below).

import pigpio
pi = pigpio.pi()
y = 1
while pi.read(21) == 1:
    y = y + 1
    print(y)

Thanks.

0

The pigpio term is callback.

def cbf(gpio, level, tick):
   print(gpio, level, tick)

cb1 = pi.callback(22, pigpio.EITHER_EDGE, cbf)

cb2 = pi.callback(4, pigpio.EITHER_EDGE)

cb3 = pi.callback(17)

print(cb3.tally())

cb3.reset_tally()

cb1.cancel() # To cancel callback cb1.

There is also wait_for_edge but I would prefer callbacks.

  • Thanks, that worked! – Monk Aug 17 '19 at 18:18
0

Thanks. This is the final code I wound up using in case anyone else has a similar question.

import pigpio
pi = pigpio.pi()

def cbf(gpio, level, tick):
    print("Button pressed")
cb1 = pi.callback(21, pigpio.FALLING_EDGE, cbf) # FALLING_EDGE, RISING_EDGE, or EITHER_EDGE

y = 1

while True:
    y = y + 1
    print(y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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