mirror of
				https://github.com/KevinMidboe/python-gpiozero.git
				synced 2025-10-29 17:50:37 +00:00 
			
		
		
		
	Fix #23
Add OutputDevice.toggle method with locking for correct threaded operation
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user