From a5c9592516893031c519ffdd316b5ec81293fcce Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 31 Oct 2023 13:04:26 +0000 Subject: [PATCH] Examples: Migrate interrupt example to gpiod. --- examples/proximity-interrupt.py | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/examples/proximity-interrupt.py b/examples/proximity-interrupt.py index fa64f3e..67b7138 100755 --- a/examples/proximity-interrupt.py +++ b/examples/proximity-interrupt.py @@ -1,7 +1,6 @@ #!/usr/bin/env python -import signal - -import RPi.GPIO as GPIO +import gpiod +from gpiod.line import Bias, Edge from ltr559 import LTR559 @@ -16,17 +15,25 @@ """) +# /dev/gpiochip4 on a Raspberry Pi 5 +GPIOCHIP = "/dev/gpiochip4" + # Breakout garden uses BCM4 as a shared interrupt pin INTERRUPT_PIN = 4 -# Tell RPi.GPIO we'll be working with BCM pin numbering -GPIO.setmode(GPIO.BCM) - # Below we're setting up the LTR559 INTERRUPT_PIN in active LOW mode # This means it should be pulled "UP", which keeps it HIGH via a weak resistor # and when the LTR559 asserts the interrupt pin it will pull i=t LOW giving # us a "falling edge" transition to watch for. -GPIO.setup(INTERRUPT_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) +request = gpiod.request_lines( + GPIOCHIP, + consumer="LTR559", + config={ + INTERRUPT_PIN: gpiod.LineSettings( + edge_detection=Edge.FALLING, bias=Bias.PULL_UP + ) + }, +) # Enable interrupts and set the pin to active LOW mode ltr559 = LTR559(enable_interrupts=True, interrupt_pin_polarity=0) @@ -50,7 +57,7 @@ def interrupt_handler(pin): # Watch the INTERRUPT_PIN for a falling edge (HIGH/LOW transition) -GPIO.add_event_detect(INTERRUPT_PIN, callback=interrupt_handler, edge=GPIO.FALLING) - -# Prevent out Python script from exiting abruptly -signal.pause() +while True: + for event in request.read_edge_events(): + if event.line_offset == INTERRUPT_PIN: + interrupt_handler(event.line_offset)