From 55e031d72fe4ad89d28ba19eb07a2f56934aae73 Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Sun, 23 Apr 2017 23:57:58 +0100 Subject: [PATCH] Add implementation for StatusBoard --- gpiozero/__init__.py | 1 + gpiozero/boards.py | 84 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/gpiozero/__init__.py b/gpiozero/__init__.py index f0037b4..d0f2476 100644 --- a/gpiozero/__init__.py +++ b/gpiozero/__init__.py @@ -81,6 +81,7 @@ from .boards import ( PiTraffic, PiStop, StatusZero, + StatusBoard, SnowPi, TrafficLightsBuzzer, FishDish, diff --git a/gpiozero/boards.py b/gpiozero/boards.py index d5643db..d89524a 100644 --- a/gpiozero/boards.py +++ b/gpiozero/boards.py @@ -796,27 +796,99 @@ class PiStop(TrafficLights): class StatusZero(LEDBoard): - def __init__(self, *names, **kwargs): + """ + Extends :class:`LEDBoard` for The Pi Hut's `STATUS Zero`_: a Pi Zero sized + add-on board with three sets of red/green LEDs to provide a status + indicator. + + The following example designates the first strip the label "wifi" and the + second "raining", and turns them green and red respectfully:: + + from gpiozero import StatusZero + + status = StatusZero('wifi', 'raining') + status.wifi.green.on() + status.raining.red.on() + + :param str \*labels: + Specify the names of the labels you wish to designate the strips to. + You can list up to three labels. If no labels are given, three strips + will be initialised with names 'one', 'two', and 'three'. If some, but + not all strips are given labels, any remaining strips will not be + initialised. + + .. _STATUS Zero: https://thepihut.com/statuszero + """ + def __init__(self, *labels, **kwargs): pins = ( (4, 17), (27, 22), (10, 9), ) - if len(names) == 0: - names = ['one', 'two', 'three', 'four', 'five'][:len(pins)] - elif len(names) > len(pins): + if len(labels) == 0: + labels = ['one', 'two', 'three'][:len(pins)] + elif len(labels) > len(pins): raise ValueError strips = OrderedDict() - for index, name in enumerate(names): + for index, label in enumerate(labels): green, red = pins[index] - strips[name] = LEDBoard(green=green, red=red, **kwargs) + strips[label] = LEDBoard(red=red, green=green, **kwargs) super(StatusZero, self).__init__(_order=strips.keys(), **strips) +class StatusBoard(CompositeOutputDevice): + """ + Extends :class:`CompositeOutputDevice` for The Pi Hut's `STATUS`_ board: a + HAT sized add-on board with five sets of red/green LEDs and buttons to + provide a status indicator with additional input. + + The following example designates the first strip the label "wifi" and the + second "raining", turns the wifi green and then activates the button to + toggle its lights when pressed:: + + from gpiozero import StatusBoard + + status = StatusBoard('wifi', 'raining') + status.wifi.lights.green.on() + status.wifi.button.when_pressed = status.wifi.lights.toggle + + :param str \*labels: + Specify the names of the labels you wish to designate the strips to. + You can list up to three labels. If no labels are given, three strips + will be initialised with names 'one', 'two', and 'three'. If some, but + not all strips are given labels, any remaining strips will not be + initialised. + + .. _STATUS: https://thepihut.com/status + """ + def __init__(self, *labels, **kwargs): + pins = ( + (4, 17, 14), + (27, 22, 19), + (10, 9, 15), + (11, 5, 26), + (6, 13, 18), + ) + if len(labels) == 0: + labels = ['one', 'two', 'three', 'four', 'five'][:len(pins)] + elif len(labels) > len(pins): + raise ValueError + strips = OrderedDict() + for index, label in enumerate(labels): + green, red, button = pins[index] + strips[label] = CompositeOutputDevice( + button=Button(button), + lights=LEDBoard( + red=red, green=green, _order=('red', 'green'), **kwargs + ), _order=('button', 'lights'), **kwargs) + super(StatusBoard, self).__init__(_order=strips.keys(), **strips) + + class SnowPi(LEDBoard): """ Extends :class:`LEDBoard` for the `Ryanteck SnowPi`_ board. + The SnowPi pins are fixed and therefore there's no need to specify them when constructing this class. The following example turns on the eyes, sets the nose pulsing, and the arms blinking::