mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Add wait_for_input and add_callback methods to InputDevice
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user