Skip to content

Commit 8b006e9

Browse files
committed
Merge pull request gpiozero#339 from lurch/extra_init_params
Add extra init-method params
2 parents 261f823 + e832cfe commit 8b006e9

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
@@ -375,8 +375,8 @@ class LEDBarGraph(LEDCollection):
375375
376376
:param float initial_value:
377377
The initial :attr:`value` of the graph given as a float between -1 and
378-
+1. Defaults to 0.0. This parameter can only be specified as a keyword
379-
parameter.
378+
+1. Defaults to ``0.0``. This parameter can only be specified as a
379+
keyword parameter.
380380
"""
381381

382382
def __init__(self, *pins, **kwargs):
@@ -385,7 +385,7 @@ def __init__(self, *pins, **kwargs):
385385
assert not isinstance(pin, LEDCollection)
386386
pwm = kwargs.pop('pwm', False)
387387
active_high = kwargs.pop('active_high', True)
388-
initial_value = kwargs.pop('initial_value', 0)
388+
initial_value = kwargs.pop('initial_value', 0.0)
389389
if kwargs:
390390
raise TypeError('unexpected keyword argument: %s' % kwargs.popitem()[0])
391391
super(LEDBarGraph, self).__init__(*pins, pwm=pwm, active_high=active_high)
@@ -468,7 +468,7 @@ class LedBorg(RGBLED):
468468

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

473473

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

496-
def __init__(self, pwm=False):
497-
super(PiLiter, self).__init__(4, 17, 27, 18, 22, 23, 24, 25, pwm=pwm)
501+
def __init__(self, pwm=False, initial_value=False):
502+
super(PiLiter, self).__init__(4, 17, 27, 18, 22, 23, 24, 25,
503+
pwm=pwm, initial_value=initial_value)
498504

499505

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

525531
def __init__(self, pwm=False, initial_value=0.0):
526532
pins = (4, 17, 27, 18, 22, 23, 24, 25)
527-
super(PiLiterBarGraph, self).__init__(*pins, pwm=pwm,
528-
initial_value=initial_value)
533+
super(PiLiterBarGraph, self).__init__(*pins,
534+
pwm=pwm, initial_value=initial_value)
529535

530536

531537
class TrafficLights(LEDBoard):
@@ -554,14 +560,22 @@ class TrafficLights(LEDBoard):
554560
If ``True``, construct :class:`PWMLED` instances to represent each
555561
LED. If ``False`` (the default), construct regular :class:`LED`
556562
instances.
563+
564+
:param bool initial_value:
565+
If ``False`` (the default), all LEDs will be off initially. If
566+
``None``, each device will be left in whatever state the pin is found
567+
in when configured for output (warning: this can be on). If ``True``,
568+
the device will be switched on initially.
557569
"""
558-
def __init__(self, red=None, amber=None, green=None, pwm=False):
570+
def __init__(self, red=None, amber=None, green=None,
571+
pwm=False, initial_value=False):
559572
if not all([red, amber, green]):
560573
raise GPIOPinMissing(
561574
'red, amber and green pins must be provided'
562575
)
563576
super(TrafficLights, self).__init__(
564-
red=red, amber=amber, green=green, pwm=pwm,
577+
red=red, amber=amber, green=green,
578+
pwm=pwm, initial_value=initial_value,
565579
_order=('red', 'amber', 'green'))
566580

567581

@@ -582,11 +596,22 @@ class PiTraffic(TrafficLights):
582596
To use the PI-TRAFFIC board when attached to a non-standard set of pins,
583597
simply use the parent class, :class:`TrafficLights`.
584598
599+
:param bool pwm:
600+
If ``True``, construct :class:`PWMLED` instances to represent each
601+
LED. If ``False`` (the default), construct regular :class:`LED`
602+
instances.
603+
604+
:param bool initial_value:
605+
If ``False`` (the default), all LEDs will be off initially. If
606+
``None``, each device will be left in whatever state the pin is found
607+
in when configured for output (warning: this can be on). If ``True``,
608+
the device will be switched on initially.
609+
585610
.. _Low Voltage Labs PI-TRAFFIC: http://lowvoltagelabs.com/products/pi-traffic/
586611
"""
587-
588-
def __init__(self):
589-
super(PiTraffic, self).__init__(9, 10, 11)
612+
def __init__(self, pwm=False, initial_value=False):
613+
super(PiTraffic, self).__init__(9, 10, 11,
614+
pwm=pwm, initial_value=initial_value)
590615

591616

592617
class SnowPi(LEDBoard):
@@ -609,24 +634,34 @@ class SnowPi(LEDBoard):
609634
LED. If ``False`` (the default), construct regular :class:`LED`
610635
instances.
611636
637+
:param bool initial_value:
638+
If ``False`` (the default), all LEDs will be off initially. If
639+
``None``, each device will be left in whatever state the pin is found
640+
in when configured for output (warning: this can be on). If ``True``,
641+
the device will be switched on initially.
642+
612643
.. _Ryanteck SnowPi: https://ryanteck.uk/raspberry-pi/114-snowpi-the-gpio-snowman-for-raspberry-pi-0635648608303.html
613644
"""
614-
def __init__(self, pwm=False):
645+
def __init__(self, pwm=False, initial_value=False):
615646
super(SnowPi, self).__init__(
616647
arms=LEDBoard(
617648
left=LEDBoard(
618-
top=17, middle=18, bottom=22, pwm=pwm,
649+
top=17, middle=18, bottom=22,
650+
pwm=pwm, initial_value=initial_value,
619651
_order=('top', 'middle', 'bottom')),
620652
right=LEDBoard(
621-
top=7, middle=8, bottom=9, pwm=pwm,
653+
top=7, middle=8, bottom=9,
654+
pwm=pwm, initial_value=initial_value,
622655
_order=('top', 'middle', 'bottom')),
623656
_order=('left', 'right')
624657
),
625658
eyes=LEDBoard(
626-
left=23, right=24, pwm=pwm,
659+
left=23, right=24,
660+
pwm=pwm, initial_value=initial_value,
627661
_order=('left', 'right')
628662
),
629-
nose=25, pwm=pwm,
663+
nose=25,
664+
pwm=pwm, initial_value=initial_value,
630665
_order=('eyes', 'nose', 'arms')
631666
)
632667

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)