Page 1 of 1
Any idea to use add_event_detect's callback func with args?
Posted: Wed Jan 14, 2015 1:08 pm
by mannok
Do anyone come up with an idea to let me pass parameter to call back function in add_event_detect'?
something like GPIO.add_event_detect(23, GPIO.RISING , callback=ssEchoHandler, args=(arg1,))
Thanks!!!

Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 1:21 pm
by DougieLawson
Docs for RPi.GPIO's add_event_detect() function are here:
http://sourceforge.net/p/raspberry-gpio ... ki/Inputs/
Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 1:32 pm
by scotty101
The documentation doesn't answer the OPs question.
I suspect you'd have to use lamba function to add arguments to the callback. The below code won't work but it might get you somewhere.
Code: Select all
cb = lambda arg1=valueToPass: functionToCall(arg1)
GPIO.add_event_callback(channel, cb, bouncetime=200)
Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 1:51 pm
by mannok
scotty101 wrote:The documentation doesn't answer the OPs question.
I suspect you'd have to use lamba function to add arguments to the callback. The below code won't work but it might get you somewhere.
Code: Select all
cb = lambda arg1=valueToPass: functionToCall(arg1)
GPIO.add_event_callback(channel, cb, bouncetime=200)
This is the answer! Thanks!!!
Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 2:48 pm
by elParaguayo
Does that work?
I thought the add_event_callback passed the channel as the first argument so, if you're using a lambda and don't care about the channel, you'd need to do
Code: Select all
cb = lambda channel, arg1=valueToPass: functionToCall(arg1)
GPIO.add_event_callback(channel, cb, bouncetime=200)
Compare:
Code: Select all
a = lambda x=2: type(x)
a("Hello")
=> <type 'str'>
a = lambda ignore_this, x=2: type(x)
a("Hello")
=> <type 'int'>
If, however, you just want the channel then you don't need the lambda. Your callback function would just be:
Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 3:30 pm
by mannok
elParaguayo wrote:Does that work?
I thought the add_event_callback passed the channel as the first argument so, if you're using a lambda and don't care about the channel, you'd need to do
Code: Select all
cb = lambda channel, arg1=valueToPass: functionToCall(arg1)
GPIO.add_event_callback(channel, cb, bouncetime=200)
Compare:
Code: Select all
a = lambda x=2: type(x)
a("Hello")
=> <type 'str'>
a = lambda ignore_this, x=2: type(x)
a("Hello")
=> <type 'int'>
If, however, you just want the channel then you don't need the lambda. Your callback function would just be:
I get this error:
TypeError: <lambda>() takes exactly 2 arguments (1 given)
Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 3:36 pm
by elParaguayo
What does your lambda function look like?
Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 3:51 pm
by mannok
elParaguayo wrote:What does your lambda function look like?
callback=lambda echoPin, startTime: func(echoPin, startTime)
Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 4:08 pm
by elParaguayo
mannok wrote:elParaguayo wrote:What does your lambda function look like?
callback=lambda echoPin, startTime: func(echoPin, startTime)
That's not going to work. If you want to pass the echoPin and startTime then I think you'll need to do this:
Code: Select all
callback=lambda channel, ep=echoPin, st=startTime: func(sp, st)
Please note, my understanding is that ep and st will be set with the value of startTime when the callback is set, not when it is triggered.
Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 4:14 pm
by elParaguayo
Looking at your other post, you have this:
Code: Select all
callback=lambda echoPin: print("Distance", round((time.time()-startEchoTime)*17150), "cm"))
That looks ok to me.
Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 4:44 pm
by DougieLawson
elParaguayo wrote:Looking at your other post, you have this:
Code: Select all
callback=lambda echoPin: print("Distance", round((time.time()-startEchoTime)*17150), "cm"))
That looks ok to me.
It looks way too complex when compared to the somewhat simpler code at
https://www.dropbox.com/s/cir2860vdkipv ... _sensor.py
Re: Any idea to use add_event_detect's callback func with ar
Posted: Wed Jan 14, 2015 4:50 pm
by elParaguayo
DougieLawson wrote:elParaguayo wrote:Looking at your other post, you have this:
Code: Select all
callback=lambda echoPin: print("Distance", round((time.time()-startEchoTime)*17150), "cm"))
That looks ok to me.
It looks way too complex when compared to the somewhat simpler code at
https://www.dropbox.com/s/cir2860vdkipv ... _sensor.py
Can't see the link, but you're right, if the only parameter being passed is echoPin then there's no need for a lambda at all here.
Re: Any idea to use add_event_detect's callback func with args?
Posted: Wed Feb 20, 2019 3:28 am
by davecollinsjr
This worked well for me, where bookshelf is an instance of a class maintaining state, and handle_book_pull is the handler/callback that needs the channel and the bookshelf instance:
Code: Select all
callback = lambda channel, bookshelf=bookshelf: handle_book_pull(channel, bookshelf)
GPIO.add_event_detect(BOOK, GPIO.BOTH, callback, bouncetime=200)