0
votes

I read some GPIO interrupt documentation on the web like this and there is one question left for me: Needs using GPIO.add_event_detect(<PIN>, <GPIO.EDGE>, callback=<some callback function>, bouncetime=<int>)in a python script to have a while true loop in the same script to run this script "endless" in order to handle the callback routine?

Let's make it more clear. My script should look like this:

    import RPi.GPIO as GPIO

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(6, GPIO.OUT)

    def my_interrupt_routine(channel):
         #do something here
    GPIO.add_event_detect(6, GPIO.FALLING, callback=my_interrupt_routine, bouncetime=200)
    try:
        while True:
            pass

    except KeyboardInterrupt:
        #do something here

    finally:
        GPIO.cleanup() 

So with this script - is it necessary to have the while true: loop to let the interrupt handler wait for the falling edg on Pin6 or can I just activate the handler and end the program? I'm asking this because I read many articles about the advantage of using interrupt handler versus polling the GPIO-Pin. And this while true: loop looks pretty much the same to me like polling around on Pins.
I used to develop in Java. There you define the interrupt handler/listener and somewhere at the other end of the world you can then handle the event. I assume this won't work in Python, right?

1

1 Answers

0
votes

I tried it out and the result is that it is absolutely necessary to have a while True loop in a script using the GPIO.add_event_detect() command. Otherwise the script just ends and no event listener is running in the background (as I expected). Obviously the need for system ressources in this loop is very low.
This script will be my listener for every interrupt happening on the GPIO interface and therefore I assume that I have to start it during the start-up of my Raspberry Pi and let it run all the time.
Would appreciate if someone has a suggestion how to "fire and forget" and catching listeners in Python(3).