Skip to content

Commit

Permalink
Merge pull request #798 from gpiozero/pibrella
Browse files Browse the repository at this point in the history
Add Pibrella class
  • Loading branch information
bennuttall committed Sep 23, 2019
2 parents 97d7702 + fd6c433 commit 808820c
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Other contributors:
- `Claire Pollard`_
- `Philippe Muller`_
- `Sofiia Kosovan`_
- `Carl Monk`_


.. _Andrew Scheller: https://github.com/lurch
Expand All @@ -182,3 +183,4 @@ Other contributors:
.. _Claire Pollard: https://github.com/tuftii
.. _Philippe Muller: https://github.com/pmuller
.. _Sofiia Kosovan: https://github.com/SofiiaKosovan
.. _Carl Monk: https://github.com/ForToffee
9 changes: 9 additions & 0 deletions docs/api_boards.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ JamHat

.. autoclass:: JamHat(\*, pwm=False, pin_factory=None)
:members:
:members:


Pibrella
--------

.. autoclass:: Pibrella(\*, pwm=False, pin_factory=None)
:members:
:members:


Robot
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def __getattr__(cls, name):
# -- Intersphinx configuration --------------------------------------------

intersphinx_mapping = {
'python': ('https://docs.python.org/3.5', None),
'python': ('https://docs.python.org/3.7', None),
'picamera': ('https://picamera.readthedocs.io/en/latest', None),
'colorzero': ('https://colorzero.readthedocs.io/en/latest', None),
}
Expand Down
1 change: 1 addition & 0 deletions gpiozero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
Energenie,
PumpkinPi,
JamHat,
Pibrella,
)
from .internal_devices import (
InternalDevice,
Expand Down
105 changes: 103 additions & 2 deletions gpiozero/boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from time import sleep
from itertools import repeat, cycle, chain
from threading import Lock
from collections import OrderedDict, Counter
from collections import OrderedDict, Counter, namedtuple

from .exc import (
DeviceClosed,
Expand Down Expand Up @@ -2182,7 +2182,7 @@ class JamHat(CompositeOutputDevice):
hat.off()
:param bool pwm:
If :data:`True`, construct :class: PWMLED instances to represent each
If :data:`True`, construct :class:`PWMLED` instances to represent each
LED on the board. If :data:`False` (the default), construct regular
:class:`LED` instances.
Expand Down Expand Up @@ -2240,3 +2240,104 @@ def off(self):
"""
self.buzzer.value = None
super(JamHat, self).off()

class Pibrella(CompositeOutputDevice):
"""
Extends :class:`CompositeOutputDevice` for the Cyntech/Pimoroni `Pibrella`_
board.
The Pibrella board comprises 3 LEDs, a button, a tonal buzzer, four general
purpose input channels, and four general purpose output channels (with LEDs).
This class exposes the LEDs, button and buzzer.
Usage::
from gpiozero import Pibrella
pb = Pibrella()
pb.button.wait_for_press()
pb.lights.on()
pb.buzzer.play('A4')
pb.off()
The four input and output channels are not provided by this class. Instead,
you can create GPIO Zero devices using these pins::
from gpiozero import Pibrella, LED, Button
pb = Pibrella()
btn = Button(pb.inputs.a)
led = LED(pb.outputs.e)
btn.when_pressed = led.on
:param bool pwm:
If :data:`True`, construct :class:`PWMLED` instances to represent each
LED on the board, otherwise if :data:`False` (the default), construct
regular :class:`LED` instances.
:type pin_factory: Factory or None
:param pin_factory:
See :doc:`api_pins` for more information (this is an advanced feature
which most users can ignore).
.. _Pibrella: http://www.pibrella.com/
.. attribute:: lights
:class:`TrafficLights` instance representing the three LEDs
.. attribute:: red, amber, green
:class:`LED` or :class:`PWMLED` instances representing the red,
amber, and green LEDs
.. attribute:: button
The red :class:`Button` object on the Pibrella
.. attribute:: buzzer
A :class:`TonalBuzzer` object representing the buzzer
.. attribute:: inputs
A :func:`~collections.namedtuple` of the input pin numbers
.. attribute:: a, b, c, d
.. attribute:: outputs
A :func:`~collections.namedtuple` of the output pin numbers
.. attribute:: e, f, g, h
"""
def __init__(self, pwm=False, pin_factory=None):
super(Pibrella, self).__init__(
lights=TrafficLights(red=27, amber=17, green=4, pwm=pwm,
pin_factory=pin_factory),
button=Button(11, pull_up=False, pin_factory=pin_factory),
buzzer=TonalBuzzer(18, pin_factory=pin_factory),
_order=('lights', 'button', 'buzzer'),
pin_factory=pin_factory
)
InputPins = namedtuple('InputPins', ['a', 'b', 'c', 'd'])
OutputPins = namedtuple('OutputPins', ['e', 'f', 'g', 'h'])
self.inputs = InputPins(a=9, b=7, c=8, d=10)
self.outputs = OutputPins(e=22, f=23, g=24, h=25)

def on(self):
"""
Turns all the LEDs on and makes the buzzer play its mid tone.
"""
self.buzzer.value = 0
super(Pibrella, self).on()

def off(self):
"""
Turns all the LEDs off and stops the buzzer.
"""
self.buzzer.value = None
super(Pibrella, self).off()
68 changes: 68 additions & 0 deletions tests/test_boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,3 +1531,71 @@ def test_jamhat_pwm(mock_factory, pwm):
False, False, # buttons
None # buzzer
)

def test_pibrella(mock_factory, pwm):
with Pibrella() as pb:
assert repr(pb).startswith('<gpiozero.Pibrella object')
assert isinstance(pb.lights, TrafficLights)
assert isinstance(pb.lights.red, LED)
assert isinstance(pb.button, Button)
assert isinstance(pb.buzzer, TonalBuzzer)
assert pb.inputs == (9, 7, 8, 10)
assert pb.outputs == (22, 23, 24, 25)
assert pb.value == (
(0, 0, 0), # lights
0, # button
None # buzzer
)
pb.on()
assert pb.value == (
(1, 1, 1), # lights
0, # button
0 # buzzer
)
pb.buzzer.play('A5')
pb.button.pin.drive_high()
assert pb.value == (
(1, 1, 1), # lights
1, # button
1 # buzzer
)
pb.off()
assert pb.value == (
(0, 0, 0), # lights
1, # button
None # buzzer
)

def test_pibrella_pwm(mock_factory, pwm):
with Pibrella(pwm=True) as pb:
assert isinstance(pb.lights, TrafficLights)
assert isinstance(pb.lights.red, PWMLED)
assert isinstance(pb.button, Button)
assert isinstance(pb.buzzer, TonalBuzzer)
assert pb.inputs == (9, 7, 8, 10)
assert pb.outputs == (22, 23, 24, 25)
assert pb.value == (
(0, 0, 0), # lights
0, # button
None # buzzer
)
pb.on()
assert pb.value == (
(1, 1, 1), # lights
0, # button
0 # buzzer
)
pb.off()
assert pb.value == (
(0, 0, 0), # lights
0, # button
None # buzzer
)

def test_pibrella_pins(mock_factory, pwm):
with Pibrella() as pb:
with LED(pb.outputs.e) as led:
assert isinstance(led, LED)
assert led.pin.number == 22
with Button(pb.inputs.a) as btn:
assert btn.pin.number == 9

0 comments on commit 808820c

Please sign in to comment.