Add pin_factory param to all devices

And some docs ...
This commit is contained in:
Dave Jones
2017-07-14 10:44:24 +01:00
parent 1ca017fc6d
commit 8958874a77
16 changed files with 619 additions and 196 deletions

View File

@@ -47,6 +47,10 @@ class CompositeOutputDevice(SourceMixin, CompositeDevice):
specific order). All keyword arguments *must* be included in the
collection. If omitted, an alphabetically sorted order will be selected
for keyword arguments.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
"""
def on(self):
@@ -124,6 +128,10 @@ class ButtonBoard(HoldMixin, CompositeDevice):
executed once per hold. This parameter can only be specified as a
keyword parameter.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
:param \*\*named_pins:
Specify GPIO pins that buttons of the board are attached to,
associating each button with a property name. You can designate as
@@ -135,6 +143,7 @@ class ButtonBoard(HoldMixin, CompositeDevice):
bounce_time = kwargs.pop('bounce_time', None)
hold_time = kwargs.pop('hold_time', 1)
hold_repeat = kwargs.pop('hold_repeat', False)
pin_factory = kwargs.pop('pin_factory', None)
order = kwargs.pop('_order', None)
super(ButtonBoard, self).__init__(
*(
@@ -142,6 +151,7 @@ class ButtonBoard(HoldMixin, CompositeDevice):
for pin in args
),
_order=order,
pin_factory=pin_factory,
**{
name: Button(pin, pull_up, bounce_time, hold_time, hold_repeat)
for name, pin in kwargs.items()
@@ -209,20 +219,28 @@ class LEDCollection(CompositeOutputDevice):
pwm = kwargs.pop('pwm', False)
active_high = kwargs.pop('active_high', True)
initial_value = kwargs.pop('initial_value', False)
pin_factory = kwargs.pop('pin_factory', None)
order = kwargs.pop('_order', None)
LEDClass = PWMLED if pwm else LED
super(LEDCollection, self).__init__(
*(
pin_or_collection
if isinstance(pin_or_collection, LEDCollection) else
LEDClass(pin_or_collection, active_high, initial_value)
LEDClass(
pin_or_collection, active_high, initial_value,
pin_factory=pin_factory
)
for pin_or_collection in args
),
_order=order,
pin_factory=pin_factory,
**{
name: pin_or_collection
if isinstance(pin_or_collection, LEDCollection) else
LEDClass(pin_or_collection, active_high, initial_value)
LEDClass(
pin_or_collection, active_high, initial_value,
pin_factory=pin_factory
)
for name, pin_or_collection in kwargs.items()
})
leds = []
@@ -283,6 +301,10 @@ class LEDBoard(LEDCollection):
the device will be switched on initially. This parameter can only be
specified as a keyword parameter.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
:param \*\*named_pins:
Specify GPIO pins that LEDs of the board are attached to, associating
each LED with a property name. You can designate as many pins as
@@ -486,6 +508,10 @@ class LEDBarGraph(LEDCollection):
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 parameter.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
"""
def __init__(self, *pins, **kwargs):
@@ -495,9 +521,12 @@ class LEDBarGraph(LEDCollection):
pwm = kwargs.pop('pwm', False)
active_high = kwargs.pop('active_high', True)
initial_value = kwargs.pop('initial_value', 0.0)
pin_factory = kwargs.pop('pin_factory', None)
if kwargs:
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, pin_factory=pin_factory
)
try:
self.value = initial_value
except:
@@ -572,12 +601,17 @@ class LedBorg(RGBLED):
each component of the LedBorg. If ``False``, construct regular
:class:`LED` instances, which prevents smooth color graduations.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _PiBorg LedBorg: https://www.piborg.org/ledborg
"""
def __init__(self, initial_value=(0, 0, 0), pwm=True):
def __init__(self, initial_value=(0, 0, 0), pwm=True, pin_factory=None):
super(LedBorg, self).__init__(red=17, green=27, blue=22,
pwm=pwm, initial_value=initial_value)
pwm=pwm, initial_value=initial_value,
pin_factory=pin_factory)
class PiLiter(LEDBoard):
@@ -604,12 +638,17 @@ class PiLiter(LEDBoard):
in when configured for output (warning: this can be on). If ``True``,
the device will be switched on initially.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _Ciseco Pi-LITEr: http://shop.ciseco.co.uk/pi-liter-8-led-strip-for-the-raspberry-pi/
"""
def __init__(self, pwm=False, initial_value=False):
def __init__(self, pwm=False, initial_value=False, pin_factory=None):
super(PiLiter, self).__init__(4, 17, 27, 18, 22, 23, 24, 25,
pwm=pwm, initial_value=initial_value)
pwm=pwm, initial_value=initial_value,
pin_factory=pin_factory)
class PiLiterBarGraph(LEDBarGraph):
@@ -634,13 +673,18 @@ class PiLiterBarGraph(LEDBarGraph):
The initial :attr:`value` of the graph given as a float between -1 and
+1. Defaults to ``0.0``.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _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, pin_factory=None):
pins = (4, 17, 27, 18, 22, 23, 24, 25)
super(PiLiterBarGraph, self).__init__(*pins,
pwm=pwm, initial_value=initial_value)
super(PiLiterBarGraph, self).__init__(
*pins, pwm=pwm, initial_value=initial_value, pin_factory=pin_factory
)
class TrafficLights(LEDBoard):
@@ -680,9 +724,14 @@ class TrafficLights(LEDBoard):
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``.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
"""
def __init__(self, red=None, amber=None, green=None,
pwm=False, initial_value=False, yellow=None):
pwm=False, initial_value=False, yellow=None,
pin_factory=None):
if amber is not None and yellow is not None:
raise OutputDeviceBadValue(
'Only one of amber or yellow can be specified'
@@ -700,7 +749,7 @@ class TrafficLights(LEDBoard):
)
super(TrafficLights, self).__init__(
pwm=pwm, initial_value=initial_value,
_order=devices.keys(),
_order=devices.keys(), pin_factory=pin_factory,
**devices)
def __getattr__(self, name):
@@ -739,11 +788,16 @@ class PiTraffic(TrafficLights):
in when configured for output (warning: this can be on). If ``True``,
the device will be switched on initially.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _Low Voltage Labs PI-TRAFFIC: http://lowvoltagelabs.com/products/pi-traffic/
"""
def __init__(self, pwm=False, initial_value=False):
def __init__(self, pwm=False, initial_value=False, pin_factory=None):
super(PiTraffic, self).__init__(9, 10, 11,
pwm=pwm, initial_value=initial_value)
pwm=pwm, initial_value=initial_value,
pin_factory=pin_factory)
class PiStop(TrafficLights):
@@ -774,6 +828,10 @@ class PiStop(TrafficLights):
in when configured for output (warning: this can be on). If ``True``,
the device will be switched on initially.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _PiHardware Pi-Stop: https://pihw.wordpress.com/meltwaters-pi-hardware-kits/pi-stop/
.. _location: https://github.com/PiHw/Pi-Stop/blob/master/markdown_source/markdown/Discover-PiStop.md
"""
@@ -786,13 +844,17 @@ class PiStop(TrafficLights):
'D': (2, 3, 4),
}
def __init__(self, location=None, pwm=False, initial_value=False):
def __init__(
self, location=None, pwm=False, initial_value=False,
pin_factory=None):
gpios = self.LOCATIONS.get(location, None)
if gpios is None:
raise ValueError('location must be one of: %s' %
', '.join(sorted(self.LOCATIONS.keys())))
super(PiStop, self).__init__(*gpios,
pwm=pwm, initial_value=initial_value)
super(PiStop, self).__init__(
*gpios, pwm=pwm, initial_value=initial_value,
pin_factory=pin_factory
)
class StatusZero(LEDBoard):
@@ -817,6 +879,10 @@ class StatusZero(LEDBoard):
not all strips are given labels, any remaining strips will not be
initialised.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _STATUS Zero: https://thepihut.com/statuszero
"""
default_labels = ('one', 'two', 'three')
@@ -827,6 +893,7 @@ class StatusZero(LEDBoard):
(22, 27),
(9, 10),
)
pin_factory = kwargs.pop('pin_factory', None)
if len(labels) == 0:
labels = self.default_labels
elif len(labels) > len(pins):
@@ -834,10 +901,15 @@ class StatusZero(LEDBoard):
dup, count = Counter(labels).most_common(1)[0]
if count > 1:
raise ValueError("Duplicate label %s" % dup)
super(StatusZero, self).__init__(_order=labels, **{
label: LEDBoard(red=red, green=green, _order=('red', 'green'), **kwargs)
for (green, red), label in zip(pins, labels)
})
super(StatusZero, self).__init__(
_order=labels, pin_factory=pin_factory, **{
label: LEDBoard(
red=red, green=green, _order=('red', 'green'),
pin_factory=pin_factory, **kwargs
)
for (green, red), label in zip(pins, labels)
}
)
class StatusBoard(CompositeOutputDevice):
@@ -862,6 +934,10 @@ class StatusBoard(CompositeOutputDevice):
will be initialised with names 'one' to 'five'. If some, but not all
strips are given labels, any remaining strips will not be initialised.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _STATUS: https://thepihut.com/status
"""
default_labels = ('one', 'two', 'three', 'four', 'five')
@@ -874,6 +950,7 @@ class StatusBoard(CompositeOutputDevice):
(5, 11, 26),
(13, 6, 18),
)
pin_factory = kwargs.pop('pin_factory', None)
if len(labels) == 0:
labels = self.default_labels
elif len(labels) > len(pins):
@@ -881,14 +958,18 @@ class StatusBoard(CompositeOutputDevice):
dup, count = Counter(labels).most_common(1)[0]
if count > 1:
raise ValueError("Duplicate label %s" % dup)
super(StatusBoard, self).__init__(_order=labels, **{
label: CompositeOutputDevice(
button=Button(button),
lights=LEDBoard(
red=red, green=green, _order=('red', 'green'), **kwargs
), _order=('button', 'lights'))
for (green, red, button), label in zip(pins, labels)
})
super(StatusBoard, self).__init__(
_order=labels, pin_factory=pin_factory, **{
label: CompositeOutputDevice(
button=Button(button),
lights=LEDBoard(
red=red, green=green, _order=('red', 'green'),
pin_factory=pin_factory, **kwargs
), _order=('button', 'lights'), pin_factory=pin_factory
)
for (green, red, button), label in zip(pins, labels)
}
)
class SnowPi(LEDBoard):
@@ -917,30 +998,39 @@ class SnowPi(LEDBoard):
in when configured for output (warning: this can be on). If ``True``,
the device will be switched on initially.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _Ryanteck SnowPi: https://ryanteck.uk/raspberry-pi/114-snowpi-the-gpio-snowman-for-raspberry-pi-0635648608303.html
"""
def __init__(self, pwm=False, initial_value=False):
def __init__(self, pwm=False, initial_value=False, pin_factory=None):
super(SnowPi, self).__init__(
arms=LEDBoard(
left=LEDBoard(
top=17, middle=18, bottom=22,
pwm=pwm, initial_value=initial_value,
_order=('top', 'middle', 'bottom')),
_order=('top', 'middle', 'bottom'),
pin_factory=pin_factory),
right=LEDBoard(
top=7, middle=8, bottom=9,
pwm=pwm, initial_value=initial_value,
_order=('top', 'middle', 'bottom')),
_order=('left', 'right')
_order=('top', 'middle', 'bottom'),
pin_factory=pin_factory),
_order=('left', 'right'),
pin_factory=pin_factory
),
eyes=LEDBoard(
left=23, right=24,
pwm=pwm, initial_value=initial_value,
_order=('left', 'right')
_order=('left', 'right'),
pin_factory=pin_factory
),
nose=25,
pwm=pwm, initial_value=initial_value,
_order=('eyes', 'nose', 'arms')
)
_order=('eyes', 'nose', 'arms'),
pin_factory=pin_factory
)
class TrafficLightsBuzzer(CompositeOutputDevice):
@@ -957,12 +1047,18 @@ class TrafficLightsBuzzer(CompositeOutputDevice):
:param Button button:
An instance of :class:`Button` representing the button on the HAT.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
"""
def __init__(self, lights, buzzer, button):
def __init__(self, lights, buzzer, button, pin_factory=None):
super(TrafficLightsBuzzer, self).__init__(
lights=lights, buzzer=buzzer, button=button,
_order=('lights', 'buzzer', 'button'))
_order=('lights', 'buzzer', 'button'),
pin_factory=pin_factory
)
class FishDish(TrafficLightsBuzzer):
@@ -985,14 +1081,19 @@ class FishDish(TrafficLightsBuzzer):
LED. If ``False`` (the default), construct regular :class:`LED`
instances.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _Pi Supply FishDish: https://www.pi-supply.com/product/fish-dish-raspberry-pi-led-buzzer-board/
"""
def __init__(self, pwm=False):
def __init__(self, pwm=False, pin_factory=None):
super(FishDish, self).__init__(
TrafficLights(9, 22, 4, pwm=pwm),
Buzzer(8),
Button(7, pull_up=False),
TrafficLights(9, 22, 4, pwm=pwm, pin_factory=pin_factory),
Buzzer(8, pin_factory=pin_factory),
Button(7, pull_up=False, pin_factory=pin_factory),
pin_factory=pin_factory
)
@@ -1016,14 +1117,19 @@ class TrafficHat(TrafficLightsBuzzer):
LED. If ``False`` (the default), construct regular :class:`LED`
instances.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _Ryanteck Traffic HAT: https://ryanteck.uk/hats/1-traffichat-0635648607122.html
"""
def __init__(self, pwm=False):
def __init__(self, pwm=False, pin_factory=None):
super(TrafficHat, self).__init__(
TrafficLights(24, 23, 22, pwm=pwm),
Buzzer(5),
Button(25),
TrafficLights(24, 23, 22, pwm=pwm, pin_factory=pin_factory),
Buzzer(5, pin_factory=pin_factory),
Button(25, pin_factory=pin_factory),
pin_factory=pin_factory
)
@@ -1049,13 +1155,19 @@ class Robot(SourceMixin, CompositeDevice):
:param tuple right:
A tuple of two GPIO pins representing the forward and backward inputs
of the right motor's controller.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
"""
def __init__(self, left=None, right=None):
def __init__(self, left=None, right=None, pin_factory=None):
super(Robot, self).__init__(
left_motor=Motor(*left),
right_motor=Motor(*right),
_order=('left_motor', 'right_motor'))
left_motor=Motor(*left, pin_factory=pin_factory),
right_motor=Motor(*right, pin_factory=pin_factory),
_order=('left_motor', 'right_motor'),
pin_factory=pin_factory
)
@property
def value(self):
@@ -1148,11 +1260,17 @@ class RyanteckRobot(Robot):
robot = RyanteckRobot()
robot.forward()
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _Ryanteck MCB: https://ryanteck.uk/add-ons/6-ryanteck-rpi-motor-controller-board-0635648607160.html
"""
def __init__(self):
super(RyanteckRobot, self).__init__((17, 18), (22, 23))
def __init__(self, pin_factory=None):
super(RyanteckRobot, self).__init__(
(17, 18), (22, 23), pin_factory=pin_factory
)
class CamJamKitRobot(Robot):
@@ -1168,20 +1286,31 @@ class CamJamKitRobot(Robot):
robot = CamJamKitRobot()
robot.forward()
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _CamJam #3 EduKit: http://camjam.me/?page_id=1035
"""
def __init__(self):
super(CamJamKitRobot, self).__init__((9, 10), (7, 8))
def __init__(self, pin_factory=None):
super(CamJamKitRobot, self).__init__(
(9, 10), (7, 8), pin_factory=pin_factory
)
class _EnergenieMaster(SharedMixin, CompositeOutputDevice):
def __init__(self):
def __init__(self, pin_factory=None):
self._lock = Lock()
super(_EnergenieMaster, self).__init__(
*(OutputDevice(pin) for pin in (17, 22, 23, 27)),
mode=OutputDevice(24), enable=OutputDevice(25),
_order=('mode', 'enable'))
*(
OutputDevice(pin, pin_factory=pin_factory)
for pin in (17, 22, 23, 27)
),
mode=OutputDevice(24, pin_factory=pin_factory),
enable=OutputDevice(25, pin_factory=pin_factory),
_order=('mode', 'enable'), pin_factory=pin_factory
)
def close(self):
if self._lock:
@@ -1190,7 +1319,7 @@ class _EnergenieMaster(SharedMixin, CompositeOutputDevice):
self._lock = None
@classmethod
def _shared_key(cls):
def _shared_key(cls, pin_factory):
# There's only one Energenie master
return None
@@ -1231,18 +1360,22 @@ class Energenie(SourceMixin, Device):
the socket, which will be set upon construction. This defaults to
``False`` which will switch the socket off.
:param Factory pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _Energenie socket: https://energenie4u.co.uk/index.php/catalogue/product/ENER002-2PI
"""
def __init__(self, socket=None, initial_value=False):
def __init__(self, socket=None, initial_value=False, pin_factory=None):
if socket is None:
raise EnergenieSocketMissing('socket number must be provided')
if not (1 <= socket <= 4):
raise EnergenieBadSocket('socket number must be between 1 and 4')
self._value = None
super(Energenie, self).__init__()
super(Energenie, self).__init__(pin_factory=pin_factory)
self._socket = socket
self._master = _EnergenieMaster()
self._master = _EnergenieMaster(pin_factory=pin_factory)
if initial_value:
self.on()
else: