Skip to content

Commit 12932c6

Browse files
authored
Merge pull request gpiozero#441 from lurch/add_pistop_board
Add support for the Pi-Stop Traffic-light board
2 parents 0d7ea5a + c35741c commit 12932c6

File tree

8 files changed

+123
-31
lines changed

8 files changed

+123
-31
lines changed

docs/api_boards.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ PI-TRAFFIC
7171
:inherited-members:
7272
:members:
7373

74+
Pi-Stop
75+
=======
76+
77+
.. autoclass:: PiStop
78+
:inherited-members:
79+
:members:
80+
7481
TrafficLightsBuzzer
7582
===================
7683

docs/images/composite_device_hierarchy.dot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ digraph classes {
2525
PiLiterBarGraph->LEDBarGraph;
2626
TrafficLights->LEDBoard;
2727
PiTraffic->TrafficLights;
28+
PiStop->TrafficLights;
2829
TrafficLightsBuzzer->CompositeOutputDevice;
2930
FishDish->TrafficLightsBuzzer;
3031
TrafficHat->TrafficLightsBuzzer;
96 Bytes
Binary file not shown.
2.35 KB
Loading

docs/images/composite_device_hierarchy.svg

Lines changed: 41 additions & 31 deletions
Loading

gpiozero/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
PiLiterBarGraph,
117117
TrafficLights,
118118
PiTraffic,
119+
PiStop,
119120
SnowPi,
120121
TrafficLightsBuzzer,
121122
FishDish,

gpiozero/boards.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,55 @@ def __init__(self, pwm=False, initial_value=False):
753753
pwm=pwm, initial_value=initial_value)
754754

755755

756+
class PiStop(TrafficLights):
757+
"""
758+
Extends :class:`TrafficLights` for the `PiHardware Pi-Stop`_: a vertical
759+
traffic lights board.
760+
761+
The following example turns on the amber LED on a Pi-Stop
762+
connected to location ``A+``::
763+
764+
from gpiozero import PiStop
765+
766+
traffic = PiStop('A+')
767+
traffic.amber.on()
768+
769+
:param str location:
770+
The `location`_ on the GPIO header to which the Pi-Stop is connected.
771+
Must be one of: ``A``, ``A+``, ``B``, ``B+``, ``C``, ``D``.
772+
773+
:param bool pwm:
774+
If ``True``, construct :class:`PWMLED` instances to represent each
775+
LED. If ``False`` (the default), construct regular :class:`LED`
776+
instances.
777+
778+
:param bool initial_value:
779+
If ``False`` (the default), all LEDs will be off initially. If
780+
``None``, each device will be left in whatever state the pin is found
781+
in when configured for output (warning: this can be on). If ``True``,
782+
the device will be switched on initially.
783+
784+
.. _PiHardware Pi-Stop: https://pihw.wordpress.com/meltwaters-pi-hardware-kits/pi-stop/
785+
.. _location: https://github.com/PiHw/Pi-Stop/blob/master/markdown_source/markdown/Discover-PiStop.md
786+
"""
787+
LOCATIONS = {
788+
'A': (7, 8, 25),
789+
'A+': (21, 20, 16),
790+
'B': (10, 9, 11),
791+
'B+': (13, 19, 26),
792+
'C': (18, 15, 14),
793+
'D': (2, 3, 4),
794+
}
795+
796+
def __init__(self, location=None, pwm=False, initial_value=False):
797+
gpios = self.LOCATIONS.get(location, None)
798+
if gpios is None:
799+
raise ValueError('location must be one of: %s' %
800+
', '.join(sorted(self.LOCATIONS.keys())))
801+
super(PiStop, self).__init__(*gpios,
802+
pwm=pwm, initial_value=initial_value)
803+
804+
756805
class SnowPi(LEDBoard):
757806
"""
758807
Extends :class:`LEDBoard` for the `Ryanteck SnowPi`_ board.

tests/test_boards.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,30 @@ def test_pi_traffic():
580580
with PiTraffic() as board:
581581
assert [device.pin for device in board] == pins
582582

583+
def test_pi_stop():
584+
with pytest.raises(ValueError):
585+
PiStop()
586+
with pytest.raises(ValueError):
587+
PiStop('E')
588+
pins_a = [MockPin(n) for n in (7, 8, 25)]
589+
with PiStop('A') as board:
590+
assert [device.pin for device in board] == pins_a
591+
pins_aplus = [MockPin(n) for n in (21, 20, 16)]
592+
with PiStop('A+') as board:
593+
assert [device.pin for device in board] == pins_aplus
594+
pins_b = [MockPin(n) for n in (10, 9, 11)]
595+
with PiStop('B') as board:
596+
assert [device.pin for device in board] == pins_b
597+
pins_bplus = [MockPin(n) for n in (13, 19, 26)]
598+
with PiStop('B+') as board:
599+
assert [device.pin for device in board] == pins_bplus
600+
pins_c = [MockPin(n) for n in (18, 15, 14)]
601+
with PiStop('C') as board:
602+
assert [device.pin for device in board] == pins_c
603+
pins_d = [MockPin(n) for n in (2, 3, 4)]
604+
with PiStop('D') as board:
605+
assert [device.pin for device in board] == pins_d
606+
583607
def test_snow_pi():
584608
pins = [MockPin(n) for n in (23, 24, 25, 17, 18, 22, 7, 8, 9)]
585609
with SnowPi() as board:

0 commit comments

Comments
 (0)