From 8c1f2d420f4766a73c5812402f6d6eebb6f00f69 Mon Sep 17 00:00:00 2001 From: Ben Nuttall Date: Sun, 25 Oct 2015 23:33:14 +0000 Subject: [PATCH 1/2] Add PWM option to add-on boards with LEDs, re: #90 --- gpiozero/boards.py | 53 ++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/gpiozero/boards.py b/gpiozero/boards.py index 020e099..323380d 100644 --- a/gpiozero/boards.py +++ b/gpiozero/boards.py @@ -13,7 +13,7 @@ from time import sleep from collections import namedtuple from .input_devices import InputDeviceError, Button -from .output_devices import OutputDeviceError, LED, Buzzer, Motor +from .output_devices import OutputDeviceError, LED, PWMLED, Buzzer, Motor from .devices import CompositeDevice, SourceMixin @@ -21,9 +21,10 @@ class LEDBoard(SourceMixin, CompositeDevice): """ A Generic LED Board or collection of LEDs. """ - def __init__(self, *pins): + def __init__(self, *pins, pwm=False): super(LEDBoard, self).__init__() - self._leds = tuple(LED(pin) for pin in pins) + LEDClass = PWMLED if pwm else LED + self._leds = tuple(LEDClass(pin) for pin in pins) def close(self): for led in self.leds: @@ -99,12 +100,13 @@ class PiLiter(LEDBoard): """ Ciseco Pi-LITEr: strip of 8 very bright LEDs. """ - def __init__(self): - super(PiLiter, self).__init__(4, 17, 27, 18, 22, 23, 24, 25) + def __init__(self, pwm=False): + super(PiLiter, self).__init__(4, 17, 27, 18, 22, 23, 24, 25, pwm=pwm) TrafficLightTuple = namedtuple('TrafficLightTuple', ('red', 'amber', 'green')) + class TrafficLights(LEDBoard): """ Generic Traffic Lights set. @@ -118,10 +120,12 @@ class TrafficLights(LEDBoard): green: `None` Green LED pin """ - def __init__(self, red=None, amber=None, green=None): + def __init__(self, red=None, amber=None, green=None, pwm=False): if not all([red, amber, green]): - raise OutputDeviceError('red, amber and green pins must be provided') - super(TrafficLights, self).__init__(red, amber, green) + raise OutputDeviceError( + 'red, amber and green pins must be provided' + ) + super(TrafficLights, self).__init__(red, amber, green, pwm=pwm) @property def value(self): @@ -165,6 +169,7 @@ class PiTraffic(TrafficLights): TrafficLightsBuzzerTuple = namedtuple('TrafficLightsBuzzerTuple', ( 'red', 'amber', 'green', 'buzzer')) + class TrafficLightsBuzzer(SourceMixin, CompositeDevice): """ A generic class for HATs with traffic lights, a button and a buzzer. @@ -197,10 +202,11 @@ class TrafficLightsBuzzer(SourceMixin, CompositeDevice): to update the state of all the board's components. """ return FishDishTuple( - self.lights.red.value, - self.lights.amber.value, - self.lights.green.value, - self.buzzer.value) + self.lights.red.value, + self.lights.amber.value, + self.lights.green.value, + self.buzzer.value, + ) @value.setter def value(self, value): @@ -257,33 +263,38 @@ class FishDish(TrafficLightsBuzzer): """ Pi Supply FishDish: traffic light LEDs, a button and a buzzer. """ - def __init__(self): + def __init__(self, pwm=False): super(FishDish, self).__init__( - TrafficLights(9, 22, 4), - Buzzer(8), - Button(7, pull_up=False)) + TrafficLights(9, 22, 4, pwm=pwm), + Buzzer(8), + Button(7, pull_up=False), + ) class TrafficHat(TrafficLightsBuzzer): """ Ryanteck Traffic HAT: traffic light LEDs, a button and a buzzer. """ - def __init__(self): + def __init__(self, pwm=False): super(TrafficHat, self).__init__( - TrafficLights(22, 23, 24), - Buzzer(5), - Button(25)) + TrafficLights(22, 23, 24, pwm=pwm), + Buzzer(5), + Button(25), + ) RobotTuple = namedtuple('RobotTuple', ('left', 'right')) + class Robot(SourceMixin, CompositeDevice): """ Generic dual-motor Robot. """ def __init__(self, left=None, right=None): if not all([left, right]): - raise OutputDeviceError('left and right motor pins must be provided') + raise OutputDeviceError( + 'left and right motor pins must be provided' + ) super(Robot, self).__init__() self._left = Motor(*left) self._right = Motor(*right) From 43fbf03dd77a568bae7bfe28fadcd43e375270b4 Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Mon, 26 Oct 2015 21:08:53 +0000 Subject: [PATCH 2/2] Fix Py2 syntax and value super-call --- gpiozero/boards.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gpiozero/boards.py b/gpiozero/boards.py index 323380d..6e8dbcd 100644 --- a/gpiozero/boards.py +++ b/gpiozero/boards.py @@ -21,8 +21,9 @@ class LEDBoard(SourceMixin, CompositeDevice): """ A Generic LED Board or collection of LEDs. """ - def __init__(self, *pins, pwm=False): + def __init__(self, *pins, **kwargs): super(LEDBoard, self).__init__() + pwm = kwargs.get('pwm', False) LEDClass = PWMLED if pwm else LED self._leds = tuple(LEDClass(pin) for pin in pins) @@ -133,7 +134,8 @@ class TrafficLights(LEDBoard): @value.setter def value(self, value): - super(TrafficLights, self).value = value + # Eurgh, this is horrid but necessary (see #90) + super(TrafficLights, self.__class__).value.fset(self, value) @property def red(self):