diff --git a/gpiozero/devices.py b/gpiozero/devices.py index e5465f2..260760c 100644 --- a/gpiozero/devices.py +++ b/gpiozero/devices.py @@ -155,12 +155,25 @@ class GPIODevice(object): return self._pin @property - def is_active(self): + def value(self): """ Returns `True` if the device is currently active and `False` otherwise. """ return self._read() + is_active = value + + @property + def values(self): + """ + An infinite iterator of values read from `value`. + """ + while True: + try: + yield self.value + except GPIODeviceClosed: + break + def __repr__(self): try: return "" % ( diff --git a/gpiozero/output_devices.py b/gpiozero/output_devices.py index 15dc17b..474726f 100644 --- a/gpiozero/output_devices.py +++ b/gpiozero/output_devices.py @@ -26,10 +26,12 @@ class OutputDevice(GPIODevice): always does the opposite). """ - __slots__ = ('_active_high') + __slots__ = ('_active_high', '_source', '_source_thread') def __init__(self, pin=None, active_high=True): self._active_high = active_high + self._source = None + self._source_thread = None super(OutputDevice, self).__init__(pin) self._active_state = GPIO.HIGH if active_high else GPIO.LOW self._inactive_state = GPIO.LOW if active_high else GPIO.HIGH @@ -50,6 +52,10 @@ class OutputDevice(GPIODevice): self.close() raise + def close(self): + self.source = None + super(OutputDevice, self).close() + def _write(self, value): GPIO.output(self.pin, bool(value)) @@ -65,6 +71,34 @@ class OutputDevice(GPIODevice): """ self._write(self._inactive_state) + @property + def value(self): + return super(OutputDevice, self).value + + @value.setter + def value(self, value): + self._write(value) + + def _copy_values(self, source): + for v in source: + self.value = v + if self._source_thread.stopping.wait(0): + break + + @property + def source(self): + return self._source + + @source.setter + def source(self, value): + if self._source_thread is not None: + self._source_thread.stop() + self._source_thread = None + self._source = value + if value is not None: + self._source_thread = GPIOThread(target=self._copy_values, args=(value,)) + self._source_thread.start() + @property def active_high(self): return self._active_high @@ -94,6 +128,10 @@ class DigitalOutputDevice(OutputDevice): self._blink_thread = None self._lock = Lock() + def close(self): + self._stop_blink() + super(DigitalOutputDevice, self).close() + def on(self): """ Turns the device on.