mannok
Posts: 63
Joined: Sun Jan 11, 2015 11:19 am

Any idea to use add_event_detect's callback func with args?

Wed Jan 14, 2015 1:08 pm

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!!! :)

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 1:21 pm

Docs for RPi.GPIO's add_event_detect() function are here: http://sourceforge.net/p/raspberry-gpio ... ki/Inputs/
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 1:32 pm

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)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

mannok
Posts: 63
Joined: Sun Jan 11, 2015 11:19 am

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 1:51 pm

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!!!

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 2:48 pm

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:

Code: Select all

def cb(channel):
    etc.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

mannok
Posts: 63
Joined: Sun Jan 11, 2015 11:19 am

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 3:30 pm

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:

Code: Select all

def cb(channel):
    etc.
I get this error:
TypeError: <lambda>() takes exactly 2 arguments (1 given)

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 3:36 pm

What does your lambda function look like?
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

mannok
Posts: 63
Joined: Sun Jan 11, 2015 11:19 am

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 3:51 pm

elParaguayo wrote:What does your lambda function look like?
callback=lambda echoPin, startTime: func(echoPin, startTime)

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 4:08 pm

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 4:14 pm

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 4:44 pm

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
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Any idea to use add_event_detect's callback func with ar

Wed Jan 14, 2015 4:50 pm

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

davecollinsjr
Posts: 2
Joined: Wed Dec 18, 2013 12:52 pm

Re: Any idea to use add_event_detect's callback func with args?

Wed Feb 20, 2019 3:28 am

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)

Return to “Python”