From 3e52078450a4205fdfaa2d4ba2448bce3d3d19d7 Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Mon, 14 Sep 2015 13:46:08 +0100 Subject: [PATCH] Add wait_for_input and add_callback methods to InputDevice --- gpio_components/input_devices.py | 33 +++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/gpio_components/input_devices.py b/gpio_components/input_devices.py index 125cd9e..3155a4a 100644 --- a/gpio_components/input_devices.py +++ b/gpio_components/input_devices.py @@ -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