mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Make buzzer blink, close #19
This commit is contained in:
@@ -22,12 +22,20 @@ class OutputDevice(GPIODevice):
|
|||||||
GPIO.output(self.pin, False)
|
GPIO.output(self.pin, False)
|
||||||
|
|
||||||
|
|
||||||
class LED(OutputDevice):
|
class DigitalOutputDevice(OutputDevice):
|
||||||
def __init__(self, pin=None):
|
def __init__(self, pin=None):
|
||||||
super(LED, self).__init__(pin)
|
super(DigitalOutputDevice, self).__init__(pin)
|
||||||
self._blink_thread = None
|
self._blink_thread = None
|
||||||
self._lock = Lock()
|
self._lock = Lock()
|
||||||
|
|
||||||
|
def on(self):
|
||||||
|
self._stop_blink()
|
||||||
|
super(DigitalOutputDevice, self).on()
|
||||||
|
|
||||||
|
def off(self):
|
||||||
|
self._stop_blink()
|
||||||
|
super(DigitalOutputDevice, self).off()
|
||||||
|
|
||||||
def blink(self, on_time=1, off_time=1):
|
def blink(self, on_time=1, off_time=1):
|
||||||
self._stop_blink()
|
self._stop_blink()
|
||||||
self._blink_thread = GPIOThread(
|
self._blink_thread = GPIOThread(
|
||||||
@@ -42,21 +50,13 @@ class LED(OutputDevice):
|
|||||||
|
|
||||||
def _blink_led(self, on_time, off_time):
|
def _blink_led(self, on_time, off_time):
|
||||||
while True:
|
while True:
|
||||||
super(LED, self).on()
|
super(DigitalOutputDevice, self).on()
|
||||||
if self._blink_thread.stopping.wait(on_time):
|
if self._blink_thread.stopping.wait(on_time):
|
||||||
break
|
break
|
||||||
super(LED, self).off()
|
super(DigitalOutputDevice, self).off()
|
||||||
if self._blink_thread.stopping.wait(off_time):
|
if self._blink_thread.stopping.wait(off_time):
|
||||||
break
|
break
|
||||||
|
|
||||||
def on(self):
|
|
||||||
self._stop_blink()
|
|
||||||
super(LED, self).on()
|
|
||||||
|
|
||||||
def off(self):
|
|
||||||
self._stop_blink()
|
|
||||||
super(LED, self).off()
|
|
||||||
|
|
||||||
def toggle(self):
|
def toggle(self):
|
||||||
with self._lock:
|
with self._lock:
|
||||||
if self.is_active:
|
if self.is_active:
|
||||||
@@ -65,7 +65,11 @@ class LED(OutputDevice):
|
|||||||
self.on()
|
self.on()
|
||||||
|
|
||||||
|
|
||||||
class Buzzer(OutputDevice):
|
class LED(DigitalOutputDevice):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Buzzer(DigitalOutputDevice):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user