From 7c647ec616235eb0b774537256a8304ef9803023 Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Wed, 23 Sep 2015 13:38:37 +0100 Subject: [PATCH] Fix #23 Add OutputDevice.toggle method with locking for correct threaded operation --- gpiozero/output_devices.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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