Matt Matt - 1 year ago 141
Python Question

In Python, how do I pass parameters in a threaded callback?

Basically I am watching logic levels for some sort of change and then timing how long that lasts. Since I have many different lines to watch, I started to create a function for each line. So I thought, why don't I just create one function that uses different parameters depending on the line that gets triggered.

def myCallBack(pin, bool, Ftimer, Etimer):
## some more code here for checking bools and using timers

GPIO.add_event_detect(pin1, GPIO.both, callback=myCallBack(pin1, bool1, Ftimer1, Etimer1), bouncetime=200)
GPIO.add_event_detect(pin2, GPIO.both, callback=myCallBack(pin2, bool2, Ftimer2, Etimer2), bouncetime=200)

Currently, I am getting the error "TypeError: Parameter must be callable"
Is this approach even possible? Am I going about this the right way?
Any help would be appreciated.

Answer Source

I am not sure why this fixes my problem, but it does work. I created a class to make objects since I am doing the same set of functions on each logic line. I found this stackoverflow link and followed the same pattern. This works just fine for what I need to get done.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download