mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
@@ -155,12 +155,25 @@ class GPIODevice(object):
|
|||||||
return self._pin
|
return self._pin
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_active(self):
|
def value(self):
|
||||||
"""
|
"""
|
||||||
Returns `True` if the device is currently active and `False` otherwise.
|
Returns `True` if the device is currently active and `False` otherwise.
|
||||||
"""
|
"""
|
||||||
return self._read()
|
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):
|
def __repr__(self):
|
||||||
try:
|
try:
|
||||||
return "<gpiozero.%s object on pin=%d, is_active=%s>" % (
|
return "<gpiozero.%s object on pin=%d, is_active=%s>" % (
|
||||||
|
|||||||
@@ -26,10 +26,12 @@ class OutputDevice(GPIODevice):
|
|||||||
always does the opposite).
|
always does the opposite).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__slots__ = ('_active_high')
|
__slots__ = ('_active_high', '_source', '_source_thread')
|
||||||
|
|
||||||
def __init__(self, pin=None, active_high=True):
|
def __init__(self, pin=None, active_high=True):
|
||||||
self._active_high = active_high
|
self._active_high = active_high
|
||||||
|
self._source = None
|
||||||
|
self._source_thread = None
|
||||||
super(OutputDevice, self).__init__(pin)
|
super(OutputDevice, self).__init__(pin)
|
||||||
self._active_state = GPIO.HIGH if active_high else GPIO.LOW
|
self._active_state = GPIO.HIGH if active_high else GPIO.LOW
|
||||||
self._inactive_state = GPIO.LOW if active_high else GPIO.HIGH
|
self._inactive_state = GPIO.LOW if active_high else GPIO.HIGH
|
||||||
@@ -50,6 +52,10 @@ class OutputDevice(GPIODevice):
|
|||||||
self.close()
|
self.close()
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self.source = None
|
||||||
|
super(OutputDevice, self).close()
|
||||||
|
|
||||||
def _write(self, value):
|
def _write(self, value):
|
||||||
GPIO.output(self.pin, bool(value))
|
GPIO.output(self.pin, bool(value))
|
||||||
|
|
||||||
@@ -65,6 +71,34 @@ class OutputDevice(GPIODevice):
|
|||||||
"""
|
"""
|
||||||
self._write(self._inactive_state)
|
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
|
@property
|
||||||
def active_high(self):
|
def active_high(self):
|
||||||
return self._active_high
|
return self._active_high
|
||||||
@@ -94,6 +128,10 @@ class DigitalOutputDevice(OutputDevice):
|
|||||||
self._blink_thread = None
|
self._blink_thread = None
|
||||||
self._lock = Lock()
|
self._lock = Lock()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
self._stop_blink()
|
||||||
|
super(DigitalOutputDevice, self).close()
|
||||||
|
|
||||||
def on(self):
|
def on(self):
|
||||||
"""
|
"""
|
||||||
Turns the device on.
|
Turns the device on.
|
||||||
|
|||||||
Reference in New Issue
Block a user