Add OutputDevice.toggle method with locking for correct threaded
operation
This commit is contained in:
Dave Jones
2015-09-23 13:38:37 +01:00
parent e805eedd13
commit 7c647ec616

View File

@@ -1,5 +1,7 @@
from RPi import GPIO
from time import sleep from time import sleep
from threading import Lock
from RPi import GPIO
from .devices import GPIODeviceError, GPIODevice, GPIOThread from .devices import GPIODeviceError, GPIODevice, GPIOThread
@@ -24,6 +26,7 @@ class LED(OutputDevice):
def __init__(self, pin=None): def __init__(self, pin=None):
super(LED, self).__init__(pin) super(LED, self).__init__(pin)
self._blink_thread = None self._blink_thread = None
self._lock = Lock()
def blink(self, on_time=1, off_time=1): def blink(self, on_time=1, off_time=1):
self._stop_blink() self._stop_blink()
@@ -54,6 +57,13 @@ class LED(OutputDevice):
self._stop_blink() self._stop_blink()
super(LED, self).off() super(LED, self).off()
def toggle(self):
with self._lock:
if self.is_active:
self.off()
else:
self.on()
class Buzzer(OutputDevice): class Buzzer(OutputDevice):
pass pass