diff --git a/gpiozero/output_devices.py b/gpiozero/output_devices.py index 3a5840e..9175f58 100644 --- a/gpiozero/output_devices.py +++ b/gpiozero/output_devices.py @@ -1,5 +1,7 @@ -from RPi import GPIO from time import sleep +from threading import Lock + +from RPi import GPIO from .devices import GPIODeviceError, GPIODevice, GPIOThread @@ -24,6 +26,7 @@ class LED(OutputDevice): def __init__(self, pin=None): super(LED, self).__init__(pin) self._blink_thread = None + self._lock = Lock() def blink(self, on_time=1, off_time=1): self._stop_blink() @@ -54,6 +57,13 @@ class LED(OutputDevice): self._stop_blink() super(LED, self).off() + def toggle(self): + with self._lock: + if self.is_active: + self.off() + else: + self.on() + class Buzzer(OutputDevice): pass