- Forum
- General Discussion
- python libgpiod - GPIOD - Event Callback
python libgpiod - GPIOD - Event Callback
-
Hi,
Does anyone able to share how to write a callback event on falling edge on an Input GPIO. I can't find any example on how the lib handle event instead of using pulling method to find falling event.
Please helps.
2
Posts1k
Views
-
Hello, you can refer to the following sample.
#!/usr/bin/env python # import gpiod import time # Define the chip and lines chipname = "gpiochip0" #Please use gpiochip0 if using Raspberry pi 4/3 hardware # chipname = "gpiochip4" #Please use gpiochip4 if using Raspberry pi 5 hardware line_offset = 6 # Open the GPIO chip chip = gpiod.Chip(chipname) # Get the input line line = chip.get_line(line_offset) line.request(consumer="my_program", type=gpiod.LINE_REQ_EV_BOTH_EDGES) def print_event(event): if event.type == gpiod.LineEvent.RISING_EDGE: print("---AC Power Loss OR Power Adapter Failure---") print("Shutdown in 5 seconds") time.sleep(5) elif event.type == gpiod.LineEvent.FALLING_EDGE: print("---AC Power OK, Power Adapter OK---") try: # Wait for events indefinitely while True: event = line.event_wait() if event: event = line.event_read() print_event(event) except KeyboardInterrupt: print("Exiting...") finally: chip.close()
Please login to reply to this topic!
