mirror of
https://github.com/KevinMidboe/python-gpiozero.git
synced 2025-10-29 17:50:37 +00:00
@@ -536,11 +536,11 @@ class PiLiterBarGraph(LEDBarGraph):
|
||||
|
||||
class TrafficLights(LEDBoard):
|
||||
"""
|
||||
Extends :class:`LEDBoard` for devices containing red, amber, and green
|
||||
Extends :class:`LEDBoard` for devices containing red, yellow, and green
|
||||
LEDs.
|
||||
|
||||
The following example initializes a device connected to GPIO pins 2, 3,
|
||||
and 4, then lights the amber LED attached to GPIO 3::
|
||||
and 4, then lights the amber (yellow) LED attached to GPIO 3::
|
||||
|
||||
from gpiozero import TrafficLights
|
||||
|
||||
@@ -566,9 +566,20 @@ class TrafficLights(LEDBoard):
|
||||
``None``, each device will be left in whatever state the pin is found
|
||||
in when configured for output (warning: this can be on). If ``True``,
|
||||
the device will be switched on initially.
|
||||
|
||||
:param int yellow:
|
||||
The GPIO pin that the yellow LED is attached to. This is merely an
|
||||
alias for the ``amber`` parameter - you can't specify both ``amber``
|
||||
and ``yellow``.
|
||||
"""
|
||||
def __init__(self, red=None, amber=None, green=None,
|
||||
pwm=False, initial_value=False):
|
||||
pwm=False, initial_value=False, yellow=None):
|
||||
if amber is not None and yellow is not None:
|
||||
raise OutputDeviceBadValue(
|
||||
'Only one of amber or yellow can be specified'
|
||||
)
|
||||
if amber is None:
|
||||
amber = yellow
|
||||
if not all(p is not None for p in [red, amber, green]):
|
||||
raise GPIOPinMissing(
|
||||
'red, amber and green pins must be provided'
|
||||
@@ -578,6 +589,16 @@ class TrafficLights(LEDBoard):
|
||||
pwm=pwm, initial_value=initial_value,
|
||||
_order=('red', 'amber', 'green'))
|
||||
|
||||
def __getattr__(self, name):
|
||||
if name == 'yellow':
|
||||
name = 'amber'
|
||||
return super(TrafficLights, self).__getattr__(name)
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
if name == 'yellow':
|
||||
name = 'amber'
|
||||
return super(TrafficLights, self).__setattr__(name, value)
|
||||
|
||||
|
||||
class PiTraffic(TrafficLights):
|
||||
"""
|
||||
|
||||
@@ -551,6 +551,11 @@ def test_traffic_lights():
|
||||
assert red_pin.state
|
||||
assert not amber_pin.state
|
||||
assert not green_pin.state
|
||||
with TrafficLights(red=red_pin, yellow=amber_pin, green=green_pin) as board:
|
||||
board.yellow.on()
|
||||
assert not red_pin.state
|
||||
assert amber_pin.state
|
||||
assert not green_pin.state
|
||||
|
||||
def test_traffic_lights_bad_init():
|
||||
with pytest.raises(ValueError):
|
||||
|
||||
Reference in New Issue
Block a user