Add wait_for_input and add_callback methods to InputDevice

This commit is contained in:
Ben Nuttall
2015-09-14 13:46:08 +01:00
parent 4d19a10547
commit 3e52078450

View File

@@ -6,13 +6,36 @@ GPIO.setwarnings(False)
class InputDevice(object):
def __init__(self, pin):
self.pin = pin
GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
def __init__(self, pin=None):
if pin is None:
raise InputDeviceError('No GPIO pin number given')
def is_pressed(self):
return GPIO.input(self.pin) == 0
self.pin = pin
self.pull = GPIO.PUD_UP
self.edge = GPIO.FALLING
self.active = 0
self.inactive = 1
GPIO.setup(pin, GPIO.IN, self.pull)
def is_active(self):
return GPIO.input(self.pin) == self.active
def wait_for_input(self):
GPIO.wait_for_edge(self.pin, self.edge)
def add_callback(self, callback=None, bouncetime=1000):
if callback is None:
raise InputDeviceError('No callback function given')
GPIO.add_event_detect(self.pin, self.edge, callback, bouncetime)
def remove_callback(self):
GPIO.remove_event_detect(self.pin)
class Button(InputDevice):
pass
class InputDeviceError(Exception):
pass