mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Add flash method to output devices and boards, close #22
This commit is contained in:
@@ -2,6 +2,8 @@ from .input_devices import Button
|
|||||||
from .output_devices import LED, Buzzer
|
from .output_devices import LED, Buzzer
|
||||||
from .devices import GPIODeviceError
|
from .devices import GPIODeviceError
|
||||||
|
|
||||||
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
class TrafficLights(object):
|
class TrafficLights(object):
|
||||||
def __init__(self, red=None, amber=None, green=None):
|
def __init__(self, red=None, amber=None, green=None):
|
||||||
@@ -33,6 +35,14 @@ class TrafficLights(object):
|
|||||||
for led in self._leds:
|
for led in self._leds:
|
||||||
led.blink(on_time, off_time)
|
led.blink(on_time, off_time)
|
||||||
|
|
||||||
|
def flash(self, on_time=1, off_time=1, n=1):
|
||||||
|
for i in range(n):
|
||||||
|
self.on()
|
||||||
|
sleep(on_time)
|
||||||
|
self.off()
|
||||||
|
if i+1 < n: # don't sleep on final iteration
|
||||||
|
sleep(off_time)
|
||||||
|
|
||||||
|
|
||||||
class PiTraffic(TrafficLights):
|
class PiTraffic(TrafficLights):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -73,6 +83,14 @@ class FishDish(TrafficLights):
|
|||||||
def toggle_lights(self):
|
def toggle_lights(self):
|
||||||
super(FishDish, self).toggle()
|
super(FishDish, self).toggle()
|
||||||
|
|
||||||
|
def flash_lights(self, on_time=1, off_time=1, n=1):
|
||||||
|
for i in range(n):
|
||||||
|
[led.on() for led in self.leds]
|
||||||
|
sleep(on_time)
|
||||||
|
[led.off() for led in self.leds]
|
||||||
|
if i+1 < n: # don't sleep on final iteration
|
||||||
|
sleep(off_time)
|
||||||
|
|
||||||
|
|
||||||
class TrafficHat(FishDish):
|
class TrafficHat(FishDish):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -107,3 +125,11 @@ class PiLiter(object):
|
|||||||
def blink(self, on_time=1, off_time=1):
|
def blink(self, on_time=1, off_time=1):
|
||||||
for led in self._leds:
|
for led in self._leds:
|
||||||
led.blink(on_time, off_time)
|
led.blink(on_time, off_time)
|
||||||
|
|
||||||
|
def flash(self, on_time=1, off_time=1, n=1):
|
||||||
|
for i in range(n):
|
||||||
|
self.on()
|
||||||
|
sleep(on_time)
|
||||||
|
self.off()
|
||||||
|
if i+1 < n: # don't sleep on final iteration
|
||||||
|
sleep(off_time)
|
||||||
|
|||||||
@@ -64,6 +64,14 @@ class DigitalOutputDevice(OutputDevice):
|
|||||||
else:
|
else:
|
||||||
self.on()
|
self.on()
|
||||||
|
|
||||||
|
def flash(self, on_time=1, off_time=1, n=1):
|
||||||
|
for i in range(n):
|
||||||
|
self.on()
|
||||||
|
sleep(on_time)
|
||||||
|
self.off()
|
||||||
|
if i+1 < n: # don't sleep on final iteration
|
||||||
|
sleep(off_time)
|
||||||
|
|
||||||
|
|
||||||
class LED(DigitalOutputDevice):
|
class LED(DigitalOutputDevice):
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user