mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
Add docstrings, close #26
This commit is contained in:
@@ -6,6 +6,9 @@ from time import sleep
|
||||
|
||||
|
||||
class LEDBoard(object):
|
||||
"""
|
||||
A Generic LED Board or collecfion of LEDs.
|
||||
"""
|
||||
def __init__(self, leds):
|
||||
self._leds = tuple(LED(led) for led in leds)
|
||||
|
||||
@@ -14,22 +17,53 @@ class LEDBoard(object):
|
||||
return self._leds
|
||||
|
||||
def on(self):
|
||||
"""
|
||||
Turn all the LEDs on.
|
||||
"""
|
||||
for led in self._leds:
|
||||
led.on()
|
||||
|
||||
def off(self):
|
||||
"""
|
||||
Turn all the LEDs off.
|
||||
"""
|
||||
for led in self._leds:
|
||||
led.off()
|
||||
|
||||
def toggle(self):
|
||||
"""
|
||||
Toggle all the LEDs. For each LED, if it's on, turn it off; if it's
|
||||
off, turn it on.
|
||||
"""
|
||||
for led in self._leds:
|
||||
led.toggle()
|
||||
|
||||
def blink(self, on_time=1, off_time=1):
|
||||
"""
|
||||
Make all the LEDs turn on and off repeatedly in the background.
|
||||
|
||||
on_time: 1
|
||||
Number of seconds to be on
|
||||
|
||||
off_time: 1
|
||||
Number of seconds to be off
|
||||
"""
|
||||
for led in self._leds:
|
||||
led.blink(on_time, off_time)
|
||||
|
||||
def flash(self, on_time=1, off_time=1, n=1):
|
||||
"""
|
||||
Turn all the LEDs on and off a given number of times.
|
||||
|
||||
on_time: 1
|
||||
Number of seconds on
|
||||
|
||||
off_time: 1
|
||||
Number of seconds off
|
||||
|
||||
n: 1
|
||||
Number of iterations
|
||||
"""
|
||||
for i in range(n):
|
||||
self.on()
|
||||
sleep(on_time)
|
||||
@@ -39,12 +73,27 @@ class LEDBoard(object):
|
||||
|
||||
|
||||
class PiLiter(LEDBoard):
|
||||
"""
|
||||
Ciseco Pi-LITEr: strip of 8 very bright LEDs.
|
||||
"""
|
||||
def __init__(self):
|
||||
leds = (4, 17, 27, 18, 22, 23, 24, 25)
|
||||
super(PiLiter, self).__init__(leds)
|
||||
|
||||
|
||||
class TrafficLights(LEDBoard):
|
||||
"""
|
||||
Generic Traffic Lights set.
|
||||
|
||||
red: None
|
||||
Red LED pin
|
||||
|
||||
amber: None
|
||||
Amber LED pin
|
||||
|
||||
green: None
|
||||
Green LED pin
|
||||
"""
|
||||
def __init__(self, red=None, amber=None, green=None):
|
||||
if not all([red, amber, green]):
|
||||
raise GPIODeviceError('Red, Amber and Green pins must be provided')
|
||||
@@ -56,12 +105,19 @@ class TrafficLights(LEDBoard):
|
||||
|
||||
|
||||
class PiTraffic(TrafficLights):
|
||||
"""
|
||||
Low Voltage Labs PI-TRAFFIC: vertical traffic lights board on pins 9, 10
|
||||
and 11.
|
||||
"""
|
||||
def __init__(self):
|
||||
red, amber, green = (9, 10, 11)
|
||||
super(PiTraffic, self).__init__(red, amber, green)
|
||||
|
||||
|
||||
class FishDish(TrafficLights):
|
||||
"""
|
||||
Pi Supply FishDish: horizontal traffic light LEDs, a button and a buzzer.
|
||||
"""
|
||||
def __init__(self):
|
||||
red, amber, green = (9, 22, 4)
|
||||
super(FishDish, self).__init__(red, amber, green)
|
||||
@@ -74,27 +130,59 @@ class FishDish(TrafficLights):
|
||||
return self._all
|
||||
|
||||
def on(self):
|
||||
"""
|
||||
Turn all the board's components on.
|
||||
"""
|
||||
for thing in self._all:
|
||||
thing.on()
|
||||
|
||||
def off(self):
|
||||
"""
|
||||
Turn all the board's components off.
|
||||
"""
|
||||
for thing in self._all:
|
||||
thing.off()
|
||||
|
||||
def toggle(self):
|
||||
"""
|
||||
Toggle all the board's components. For each component, if it's on, turn
|
||||
it off; if it's off, turn it on.
|
||||
"""
|
||||
for thing in self._all:
|
||||
thing.toggle()
|
||||
|
||||
def lights_on(self):
|
||||
"""
|
||||
Turn all the board's LEDs on.
|
||||
"""
|
||||
super(FishDish, self).on()
|
||||
|
||||
def lights_off(self):
|
||||
"""
|
||||
Turn all the board's LEDs off.
|
||||
"""
|
||||
super(FishDish, self).off()
|
||||
|
||||
def toggle_lights(self):
|
||||
"""
|
||||
Toggle all the board's LEDs. For each LED, if it's on, turn
|
||||
it off; if it's off, turn it on.
|
||||
"""
|
||||
super(FishDish, self).toggle()
|
||||
|
||||
def flash_lights(self, on_time=1, off_time=1, n=1):
|
||||
"""
|
||||
Turn all the LEDs on and off a given number of times.
|
||||
|
||||
on_time: 1
|
||||
Number of seconds on
|
||||
|
||||
off_time: 1
|
||||
Number of seconds off
|
||||
|
||||
n: 1
|
||||
Number of iterations
|
||||
"""
|
||||
for i in range(n):
|
||||
[led.on() for led in self.leds]
|
||||
sleep(on_time)
|
||||
@@ -104,6 +192,9 @@ class FishDish(TrafficLights):
|
||||
|
||||
|
||||
class TrafficHat(FishDish):
|
||||
"""
|
||||
Ryanteck Traffic HAT: horizontal traffic light LEDs, a button and a buzzer.
|
||||
"""
|
||||
def __init__(self):
|
||||
red, amber, green = (22, 23, 24)
|
||||
super(FishDish, self).__init__(red, amber, green)
|
||||
|
||||
Reference in New Issue
Block a user