Stupidav
Posts: 11
Joined: Wed Mar 16, 2016 10:02 pm

GPIO.add_event_detect

Wed Mar 23, 2016 11:26 pm

I am trying to make sure that I am understanding the GPIO.add_event_detect correctly, more accurately making sure that I use it properly.

Is GPIO.add_event_detect to be placed within my main loop, or is it more along the lines of a property?

ie:

Code: Select all

import something

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)

def btnPressed(23):
    print('Someone pressed my Button')
    break

While True:
    GPIO.add_event_detect(23, GPIO.RISING, callback=btnPressed, bouncetime=200)
    # perhaps do something else

Or would it be more like:

Code: Select all

import something

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)

def btnPressed(23):
    print('Someone pressed my Button')
    break

GPIO.add_event_detect(23, GPIO.RISING, callback=btnPressed, bouncetime=200)

While True:
    # perhaps do something


tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: GPIO.add_event_detect

Wed Mar 23, 2016 11:55 pm

Like a property, so your second example is the correct usage.

Stupidav
Posts: 11
Joined: Wed Mar 16, 2016 10:02 pm

Re: GPIO.add_event_detect

Thu Mar 24, 2016 8:36 pm

Another question on this, if one area of the program is doing something, like time.sleep(300) as an example and I am 10seconds into this, and I activate the input with the add_event_detect, does the program "Pause" the time.sleep(300) while it handles the callback or does it attempt to continue both.

Per my example code, if I activate an input in 10 seconds into the 300, leaving 290 to go, after what's in the callback, time.sleep(200), would it continue from where it left off for the additional 290, would it continue for the balance of 90, or would it consider the time.sleep(300) complete and move on to what ever is next?

Code: Select all

import something

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN)

def btnPressed(23):
    print('Someone pressed my Button')
    time.sleep(200)

GPIO.add_event_detect(23, GPIO.RISING, callback=btnPressed, bouncetime=200)

While True:
    print('Im doing something')
    time.sleep(300)


tom.slick
Posts: 190
Joined: Wed Jan 06, 2016 9:23 pm

Re: GPIO.add_event_detect

Fri Mar 25, 2016 5:00 am

The event detect is running is a separate thread. Your parent thread would continue to run.


To use your example, if you pressed your button at 10 seconds in (290 remaining) and your event took 200 seconds to complete, then when the event is finished you would have 90 seconds remaining on your call to sleep.

edit fixed spelling and numbers in example

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: GPIO.add_event_detect

Fri Mar 25, 2016 6:18 am

since all callbacks are in the same thread, it's a bad idea to have a "long" callback (since every event risen while the callback is running will be managed by the callback at the end of the previous).
It is a good practice to keep callbacks easy and fast

Return to “Python”