Skip to content

Commit e832cfe

Browse files
committed
Add extra init-method params
adds `initial_value` to PiLiter, TrafficLights, PiTraffic & SnowPi and adds `pwm` to PiTraffic
1 parent b581719 commit e832cfe

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

gpiozero/boards.py

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,8 @@ class LEDBarGraph(LEDCollection):
372372
373373
:param float initial_value:
374374
The initial :attr:`value` of the graph given as a float between -1 and
375-
+1. Defaults to 0.0. This parameter can only be specified as a keyword
376-
parameter.
375+
+1. Defaults to ``0.0``. This parameter can only be specified as a
376+
keyword parameter.
377377
"""
378378

379379
def __init__(self, *pins, **kwargs):
@@ -382,7 +382,7 @@ def __init__(self, *pins, **kwargs):
382382
assert not isinstance(pin, LEDCollection)
383383
pwm = kwargs.pop('pwm', False)
384384
active_high = kwargs.pop('active_high', True)
385-
initial_value = kwargs.pop('initial_value', 0)
385+
initial_value = kwargs.pop('initial_value', 0.0)
386386
if kwargs:
387387
raise TypeError('unexpected keyword argument: %s' % kwargs.popitem()[0])
388388
super(LEDBarGraph, self).__init__(*pins, pwm=pwm, active_high=active_high)
@@ -465,7 +465,7 @@ class LedBorg(RGBLED):
465465

466466
def __init__(self, initial_value=(0, 0, 0), pwm=True):
467467
super(LedBorg, self).__init__(red=17, green=27, blue=22,
468-
initial_value=initial_value, pwm=pwm)
468+
pwm=pwm, initial_value=initial_value)
469469

470470

471471
class PiLiter(LEDBoard):
@@ -484,14 +484,20 @@ class PiLiter(LEDBoard):
484484
485485
:param bool pwm:
486486
If ``True``, construct :class:`PWMLED` instances for each pin. If
487-
``False`` (the default), construct regular :class:`LED` instances. This
488-
parameter can only be specified as a keyword parameter.
487+
``False`` (the default), construct regular :class:`LED` instances.
488+
489+
:param: bool initial_value:
490+
If ``False`` (the default), all LEDs will be off initially. If
491+
``None``, each device will be left in whatever state the pin is found
492+
in when configured for output (warning: this can be on). If ``True``,
493+
the device will be switched on initially.
489494
490495
.. _Ciseco Pi-LITEr: http://shop.ciseco.co.uk/pi-liter-8-led-strip-for-the-raspberry-pi/
491496
"""
492497

493-
def __init__(self, pwm=False):
494-
super(PiLiter, self).__init__(4, 17, 27, 18, 22, 23, 24, 25, pwm=pwm)
498+
def __init__(self, pwm=False, initial_value=False):
499+
super(PiLiter, self).__init__(4, 17, 27, 18, 22, 23, 24, 25,
500+
pwm=pwm, initial_value=initial_value)
495501

496502

497503
class PiLiterBarGraph(LEDBarGraph):
@@ -514,15 +520,15 @@ class PiLiterBarGraph(LEDBarGraph):
514520
515521
:param float initial_value:
516522
The initial :attr:`value` of the graph given as a float between -1 and
517-
+1.
523+
+1. Defaults to ``0.0``.
518524
519525
.. _Ciseco Pi-LITEr: http://shop.ciseco.co.uk/pi-liter-8-led-strip-for-the-raspberry-pi/
520526
"""
521527

522528
def __init__(self, pwm=False, initial_value=0.0):
523529
pins = (4, 17, 27, 18, 22, 23, 24, 25)
524-
super(PiLiterBarGraph, self).__init__(*pins, pwm=pwm,
525-
initial_value=initial_value)
530+
super(PiLiterBarGraph, self).__init__(*pins,
531+
pwm=pwm, initial_value=initial_value)
526532

527533

528534
class TrafficLights(LEDBoard):
@@ -551,14 +557,22 @@ class TrafficLights(LEDBoard):
551557
If ``True``, construct :class:`PWMLED` instances to represent each
552558
LED. If ``False`` (the default), construct regular :class:`LED`
553559
instances.
560+
561+
:param bool initial_value:
562+
If ``False`` (the default), all LEDs will be off initially. If
563+
``None``, each device will be left in whatever state the pin is found
564+
in when configured for output (warning: this can be on). If ``True``,
565+
the device will be switched on initially.
554566
"""
555-
def __init__(self, red=None, amber=None, green=None, pwm=False):
567+
def __init__(self, red=None, amber=None, green=None,
568+
pwm=False, initial_value=False):
556569
if not all([red, amber, green]):
557570
raise GPIOPinMissing(
558571
'red, amber and green pins must be provided'
559572
)
560573
super(TrafficLights, self).__init__(
561-
red=red, amber=amber, green=green, pwm=pwm,
574+
red=red, amber=amber, green=green,
575+
pwm=pwm, initial_value=initial_value,
562576
_order=('red', 'amber', 'green'))
563577

564578

@@ -579,11 +593,22 @@ class PiTraffic(TrafficLights):
579593
To use the PI-TRAFFIC board when attached to a non-standard set of pins,
580594
simply use the parent class, :class:`TrafficLights`.
581595
596+
:param bool pwm:
597+
If ``True``, construct :class:`PWMLED` instances to represent each
598+
LED. If ``False`` (the default), construct regular :class:`LED`
599+
instances.
600+
601+
:param bool initial_value:
602+
If ``False`` (the default), all LEDs will be off initially. If
603+
``None``, each device will be left in whatever state the pin is found
604+
in when configured for output (warning: this can be on). If ``True``,
605+
the device will be switched on initially.
606+
582607
.. _Low Voltage Labs PI-TRAFFIC: http://lowvoltagelabs.com/products/pi-traffic/
583608
"""
584-
585-
def __init__(self):
586-
super(PiTraffic, self).__init__(9, 10, 11)
609+
def __init__(self, pwm=False, initial_value=False):
610+
super(PiTraffic, self).__init__(9, 10, 11,
611+
pwm=pwm, initial_value=initial_value)
587612

588613

589614
class SnowPi(LEDBoard):
@@ -606,24 +631,34 @@ class SnowPi(LEDBoard):
606631
LED. If ``False`` (the default), construct regular :class:`LED`
607632
instances.
608633
634+
:param bool initial_value:
635+
If ``False`` (the default), all LEDs will be off initially. If
636+
``None``, each device will be left in whatever state the pin is found
637+
in when configured for output (warning: this can be on). If ``True``,
638+
the device will be switched on initially.
639+
609640
.. _Ryanteck SnowPi: https://ryanteck.uk/raspberry-pi/114-snowpi-the-gpio-snowman-for-raspberry-pi-0635648608303.html
610641
"""
611-
def __init__(self, pwm=False):
642+
def __init__(self, pwm=False, initial_value=False):
612643
super(SnowPi, self).__init__(
613644
arms=LEDBoard(
614645
left=LEDBoard(
615-
top=17, middle=18, bottom=22, pwm=pwm,
646+
top=17, middle=18, bottom=22,
647+
pwm=pwm, initial_value=initial_value,
616648
_order=('top', 'middle', 'bottom')),
617649
right=LEDBoard(
618-
top=7, middle=8, bottom=9, pwm=pwm,
650+
top=7, middle=8, bottom=9,
651+
pwm=pwm, initial_value=initial_value,
619652
_order=('top', 'middle', 'bottom')),
620653
_order=('left', 'right')
621654
),
622655
eyes=LEDBoard(
623-
left=23, right=24, pwm=pwm,
656+
left=23, right=24,
657+
pwm=pwm, initial_value=initial_value,
624658
_order=('left', 'right')
625659
),
626-
nose=25, pwm=pwm,
660+
nose=25,
661+
pwm=pwm, initial_value=initial_value,
627662
_order=('eyes', 'nose', 'arms')
628663
)
629664

tests/test_boards.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
def setup_function(function):
1919
import gpiozero.devices
2020
# dirty, but it does the job
21-
if function.__name__ in ('test_robot', 'test_ryanteck_robot', 'test_camjam_kit_robot', 'test_led_borg'):
21+
if function.__name__ in ('test_robot', 'test_ryanteck_robot', 'test_camjam_kit_robot', 'test_led_borg', 'test_snow_pi_initial_value_pwm'):
2222
gpiozero.devices.pin_factory = MockPWMPin
2323
else:
2424
gpiozero.devices.pin_factory = MockPin
@@ -566,6 +566,22 @@ def test_snow_pi():
566566
with SnowPi() as board:
567567
assert [device.pin for device in board.leds] == pins
568568

569+
def test_snow_pi_initial_value():
570+
with SnowPi() as board:
571+
assert all(device.pin.state == False for device in board.leds)
572+
with SnowPi(initial_value=False) as board:
573+
assert all(device.pin.state == False for device in board.leds)
574+
with SnowPi(initial_value=True) as board:
575+
assert all(device.pin.state == True for device in board.leds)
576+
with SnowPi(initial_value=0.5) as board:
577+
assert all(device.pin.state == True for device in board.leds)
578+
579+
def test_snow_pi_initial_value_pwm():
580+
pins = [MockPWMPin(n) for n in (23, 24, 25, 17, 18, 22, 7, 8, 9)]
581+
with SnowPi(pwm=True, initial_value=0.5) as board:
582+
assert [device.pin for device in board.leds] == pins
583+
assert all(device.pin.state == 0.5 for device in board.leds)
584+
569585
def test_traffic_lights_buzzer():
570586
red_pin = MockPin(2)
571587
amber_pin = MockPin(3)

0 commit comments

Comments
 (0)