Merge pull request #339 from lurch/extra_init_params

Add extra init-method params
This commit is contained in:
Ben Nuttall
2016-05-28 12:39:25 +01:00
2 changed files with 73 additions and 22 deletions

View File

@@ -375,8 +375,8 @@ class LEDBarGraph(LEDCollection):
:param float initial_value: :param float initial_value:
The initial :attr:`value` of the graph given as a float between -1 and The initial :attr:`value` of the graph given as a float between -1 and
+1. Defaults to 0.0. This parameter can only be specified as a keyword +1. Defaults to ``0.0``. This parameter can only be specified as a
parameter. keyword parameter.
""" """
def __init__(self, *pins, **kwargs): def __init__(self, *pins, **kwargs):
@@ -385,7 +385,7 @@ class LEDBarGraph(LEDCollection):
assert not isinstance(pin, LEDCollection) assert not isinstance(pin, LEDCollection)
pwm = kwargs.pop('pwm', False) pwm = kwargs.pop('pwm', False)
active_high = kwargs.pop('active_high', True) active_high = kwargs.pop('active_high', True)
initial_value = kwargs.pop('initial_value', 0) initial_value = kwargs.pop('initial_value', 0.0)
if kwargs: if kwargs:
raise TypeError('unexpected keyword argument: %s' % kwargs.popitem()[0]) raise TypeError('unexpected keyword argument: %s' % kwargs.popitem()[0])
super(LEDBarGraph, self).__init__(*pins, pwm=pwm, active_high=active_high) super(LEDBarGraph, self).__init__(*pins, pwm=pwm, active_high=active_high)
@@ -468,7 +468,7 @@ class LedBorg(RGBLED):
def __init__(self, initial_value=(0, 0, 0), pwm=True): def __init__(self, initial_value=(0, 0, 0), pwm=True):
super(LedBorg, self).__init__(red=17, green=27, blue=22, super(LedBorg, self).__init__(red=17, green=27, blue=22,
initial_value=initial_value, pwm=pwm) pwm=pwm, initial_value=initial_value)
class PiLiter(LEDBoard): class PiLiter(LEDBoard):
@@ -487,14 +487,20 @@ class PiLiter(LEDBoard):
:param bool pwm: :param bool pwm:
If ``True``, construct :class:`PWMLED` instances for each pin. If If ``True``, construct :class:`PWMLED` instances for each pin. If
``False`` (the default), construct regular :class:`LED` instances. This ``False`` (the default), construct regular :class:`LED` instances.
parameter can only be specified as a keyword parameter.
:param: bool initial_value:
If ``False`` (the default), all LEDs will be off initially. If
``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.
.. _Ciseco Pi-LITEr: http://shop.ciseco.co.uk/pi-liter-8-led-strip-for-the-raspberry-pi/ .. _Ciseco Pi-LITEr: http://shop.ciseco.co.uk/pi-liter-8-led-strip-for-the-raspberry-pi/
""" """
def __init__(self, pwm=False): def __init__(self, pwm=False, initial_value=False):
super(PiLiter, self).__init__(4, 17, 27, 18, 22, 23, 24, 25, pwm=pwm) super(PiLiter, self).__init__(4, 17, 27, 18, 22, 23, 24, 25,
pwm=pwm, initial_value=initial_value)
class PiLiterBarGraph(LEDBarGraph): class PiLiterBarGraph(LEDBarGraph):
@@ -517,15 +523,15 @@ class PiLiterBarGraph(LEDBarGraph):
:param float initial_value: :param float initial_value:
The initial :attr:`value` of the graph given as a float between -1 and The initial :attr:`value` of the graph given as a float between -1 and
+1. +1. Defaults to ``0.0``.
.. _Ciseco Pi-LITEr: http://shop.ciseco.co.uk/pi-liter-8-led-strip-for-the-raspberry-pi/ .. _Ciseco Pi-LITEr: http://shop.ciseco.co.uk/pi-liter-8-led-strip-for-the-raspberry-pi/
""" """
def __init__(self, pwm=False, initial_value=0.0): def __init__(self, pwm=False, initial_value=0.0):
pins = (4, 17, 27, 18, 22, 23, 24, 25) pins = (4, 17, 27, 18, 22, 23, 24, 25)
super(PiLiterBarGraph, self).__init__(*pins, pwm=pwm, super(PiLiterBarGraph, self).__init__(*pins,
initial_value=initial_value) pwm=pwm, initial_value=initial_value)
class TrafficLights(LEDBoard): class TrafficLights(LEDBoard):
@@ -554,14 +560,22 @@ class TrafficLights(LEDBoard):
If ``True``, construct :class:`PWMLED` instances to represent each If ``True``, construct :class:`PWMLED` instances to represent each
LED. If ``False`` (the default), construct regular :class:`LED` LED. If ``False`` (the default), construct regular :class:`LED`
instances. instances.
:param bool initial_value:
If ``False`` (the default), all LEDs will be off initially. If
``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.
""" """
def __init__(self, red=None, amber=None, green=None, pwm=False): def __init__(self, red=None, amber=None, green=None,
pwm=False, initial_value=False):
if not all([red, amber, green]): if not all([red, amber, green]):
raise GPIOPinMissing( raise GPIOPinMissing(
'red, amber and green pins must be provided' 'red, amber and green pins must be provided'
) )
super(TrafficLights, self).__init__( super(TrafficLights, self).__init__(
red=red, amber=amber, green=green, pwm=pwm, red=red, amber=amber, green=green,
pwm=pwm, initial_value=initial_value,
_order=('red', 'amber', 'green')) _order=('red', 'amber', 'green'))
@@ -582,11 +596,22 @@ class PiTraffic(TrafficLights):
To use the PI-TRAFFIC board when attached to a non-standard set of pins, To use the PI-TRAFFIC board when attached to a non-standard set of pins,
simply use the parent class, :class:`TrafficLights`. simply use the parent class, :class:`TrafficLights`.
:param bool pwm:
If ``True``, construct :class:`PWMLED` instances to represent each
LED. If ``False`` (the default), construct regular :class:`LED`
instances.
:param bool initial_value:
If ``False`` (the default), all LEDs will be off initially. If
``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.
.. _Low Voltage Labs PI-TRAFFIC: http://lowvoltagelabs.com/products/pi-traffic/ .. _Low Voltage Labs PI-TRAFFIC: http://lowvoltagelabs.com/products/pi-traffic/
""" """
def __init__(self, pwm=False, initial_value=False):
def __init__(self): super(PiTraffic, self).__init__(9, 10, 11,
super(PiTraffic, self).__init__(9, 10, 11) pwm=pwm, initial_value=initial_value)
class SnowPi(LEDBoard): class SnowPi(LEDBoard):
@@ -609,24 +634,34 @@ class SnowPi(LEDBoard):
LED. If ``False`` (the default), construct regular :class:`LED` LED. If ``False`` (the default), construct regular :class:`LED`
instances. instances.
:param bool initial_value:
If ``False`` (the default), all LEDs will be off initially. If
``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.
.. _Ryanteck SnowPi: https://ryanteck.uk/raspberry-pi/114-snowpi-the-gpio-snowman-for-raspberry-pi-0635648608303.html .. _Ryanteck SnowPi: https://ryanteck.uk/raspberry-pi/114-snowpi-the-gpio-snowman-for-raspberry-pi-0635648608303.html
""" """
def __init__(self, pwm=False): def __init__(self, pwm=False, initial_value=False):
super(SnowPi, self).__init__( super(SnowPi, self).__init__(
arms=LEDBoard( arms=LEDBoard(
left=LEDBoard( left=LEDBoard(
top=17, middle=18, bottom=22, pwm=pwm, top=17, middle=18, bottom=22,
pwm=pwm, initial_value=initial_value,
_order=('top', 'middle', 'bottom')), _order=('top', 'middle', 'bottom')),
right=LEDBoard( right=LEDBoard(
top=7, middle=8, bottom=9, pwm=pwm, top=7, middle=8, bottom=9,
pwm=pwm, initial_value=initial_value,
_order=('top', 'middle', 'bottom')), _order=('top', 'middle', 'bottom')),
_order=('left', 'right') _order=('left', 'right')
), ),
eyes=LEDBoard( eyes=LEDBoard(
left=23, right=24, pwm=pwm, left=23, right=24,
pwm=pwm, initial_value=initial_value,
_order=('left', 'right') _order=('left', 'right')
), ),
nose=25, pwm=pwm, nose=25,
pwm=pwm, initial_value=initial_value,
_order=('eyes', 'nose', 'arms') _order=('eyes', 'nose', 'arms')
) )

View File

@@ -18,7 +18,7 @@ from gpiozero import *
def setup_function(function): def setup_function(function):
import gpiozero.devices import gpiozero.devices
# dirty, but it does the job # dirty, but it does the job
if function.__name__ in ('test_robot', 'test_ryanteck_robot', 'test_camjam_kit_robot', 'test_led_borg'): if function.__name__ in ('test_robot', 'test_ryanteck_robot', 'test_camjam_kit_robot', 'test_led_borg', 'test_snow_pi_initial_value_pwm'):
gpiozero.devices.pin_factory = MockPWMPin gpiozero.devices.pin_factory = MockPWMPin
else: else:
gpiozero.devices.pin_factory = MockPin gpiozero.devices.pin_factory = MockPin
@@ -566,6 +566,22 @@ def test_snow_pi():
with SnowPi() as board: with SnowPi() as board:
assert [device.pin for device in board.leds] == pins assert [device.pin for device in board.leds] == pins
def test_snow_pi_initial_value():
with SnowPi() as board:
assert all(device.pin.state == False for device in board.leds)
with SnowPi(initial_value=False) as board:
assert all(device.pin.state == False for device in board.leds)
with SnowPi(initial_value=True) as board:
assert all(device.pin.state == True for device in board.leds)
with SnowPi(initial_value=0.5) as board:
assert all(device.pin.state == True for device in board.leds)
def test_snow_pi_initial_value_pwm():
pins = [MockPWMPin(n) for n in (23, 24, 25, 17, 18, 22, 7, 8, 9)]
with SnowPi(pwm=True, initial_value=0.5) as board:
assert [device.pin for device in board.leds] == pins
assert all(device.pin.state == 0.5 for device in board.leds)
def test_traffic_lights_buzzer(): def test_traffic_lights_buzzer():
red_pin = MockPin(2) red_pin = MockPin(2)
amber_pin = MockPin(3) amber_pin = MockPin(3)