I want to set a callback function to run when an event occurs in a GPIO. I'm designing a class and I want to have the callback method inside it too. Here's an excerpt of the code:
import RPi.GPIO as gpio
class WheelEncoder:
'Encapsulates the attributes and methods to use a wheel encoder sensor'
inputPin = 0
ticks = 0
def __init__(self, inputPin):
self.inputPin = inputPin
gpio.setmode(gpio.BOARD)
gpio.setup(self.inputPin, gpio.IN, pull_up_down=gpio.PUD_UP)
gpio.add_event_detect(self.inputPin, gpio.RISING, event_callback)
def getTicks(self):
return self.ticks
def resetTicks(self):
self.ticks = 0
def event_callback(channel):
self.ticks += 1
And here is the output of calling it (from another file):
Traceback (most recent call last):
File "test-WheelEncoder.py", line 5, in <module>
sensor = WheelEncoder(3, 10, 3)
File "/home/pi/codes/sensors/WheelEncoder.py", line 20, in __init__
gpio.add_event_detect(self.inputPin, gpio.RISING, event_callback)
NameError: global name 'event_callback' is not defined
Im not sure what it could be.