Merge pull request #89 from waveform80/nice-fish-dish

Fix #81
This commit is contained in:
Dave Jones
2015-10-25 10:51:35 +00:00
2 changed files with 62 additions and 73 deletions

View File

@@ -36,6 +36,7 @@ from .boards import (
PiLiter, PiLiter,
TrafficLights, TrafficLights,
PiTraffic, PiTraffic,
TrafficLightsBuzzer,
FishDish, FishDish,
TrafficHat, TrafficHat,
Robot, Robot,

View File

@@ -25,6 +25,17 @@ class LEDBoard(SourceMixin, CompositeDevice):
super(LEDBoard, self).__init__() super(LEDBoard, self).__init__()
self._leds = tuple(LED(pin) for pin in pins) self._leds = tuple(LED(pin) for pin in pins)
def close(self):
for led in self.leds:
led.close()
@property
def leds(self):
"""
A tuple of all the `LED` objects contained by the instance.
"""
return self._leds
@property @property
def value(self): def value(self):
""" """
@@ -35,28 +46,21 @@ class LEDBoard(SourceMixin, CompositeDevice):
@value.setter @value.setter
def value(self, value): def value(self, value):
for l, v in zip(self._leds, value): for l, v in zip(self.leds, value):
l.value = v l.value = v
@property
def leds(self):
"""
A tuple of all the `LED` objects contained by the instance.
"""
return self._leds
def on(self): def on(self):
""" """
Turn all the LEDs on. Turn all the LEDs on.
""" """
for led in self._leds: for led in self.leds:
led.on() led.on()
def off(self): def off(self):
""" """
Turn all the LEDs off. Turn all the LEDs off.
""" """
for led in self._leds: for led in self.leds:
led.off() led.off()
def toggle(self): def toggle(self):
@@ -64,7 +68,7 @@ class LEDBoard(SourceMixin, CompositeDevice):
Toggle all the LEDs. For each LED, if it's on, turn it off; if it's Toggle all the LEDs. For each LED, if it's on, turn it off; if it's
off, turn it on. off, turn it on.
""" """
for led in self._leds: for led in self.leds:
led.toggle() led.toggle()
def blink(self, on_time=1, off_time=1, n=None, background=True): def blink(self, on_time=1, off_time=1, n=None, background=True):
@@ -86,7 +90,8 @@ class LEDBoard(SourceMixin, CompositeDevice):
finished (warning: the default value of `n` will result in this finished (warning: the default value of `n` will result in this
method never returning). method never returning).
""" """
for led in self._leds: # XXX This isn't going to work for background=False
for led in self.leds:
led.blink(on_time, off_time, n, background) led.blink(on_time, off_time, n, background)
@@ -157,17 +162,24 @@ class PiTraffic(TrafficLights):
super(PiTraffic, self).__init__(9, 10, 11) super(PiTraffic, self).__init__(9, 10, 11)
FishDishTuple = namedtuple('FishDishTuple', ('red', 'amber', 'green', 'buzzer')) TrafficLightsBuzzerTuple = namedtuple('TrafficLightsBuzzerTuple', (
'red', 'amber', 'green', 'buzzer'))
class FishDish(TrafficLights): class TrafficLightsBuzzer(SourceMixin, CompositeDevice):
""" """
Pi Supply FishDish: traffic light LEDs, a button and a buzzer. A generic class for HATs with traffic lights, a button and a buzzer.
""" """
def __init__(self): def __init__(self, lights, buzzer, button):
super(FishDish, self).__init__(9, 22, 4) super(TrafficLightsBuzzer, self).__init__()
self.buzzer = Buzzer(8) self.lights = lights
self.button = Button(7, pull_up=False) self.buzzer = buzzer
self._all = self.leds + (self.buzzer,) self.button = button
self._all = self.lights.leds + (self.buzzer,)
def close(self):
self.lights.close()
self.buzzer.close()
self.button.close()
@property @property
def all(self): def all(self):
@@ -185,28 +197,28 @@ class FishDish(TrafficLights):
to update the state of all the board's components. to update the state of all the board's components.
""" """
return FishDishTuple( return FishDishTuple(
self.red.value, self.lights.red.value,
self.amber.value, self.lights.amber.value,
self.green.value, self.lights.green.value,
self.buzzer.value) self.buzzer.value)
@value.setter @value.setter
def value(self, value): def value(self, value):
for i, v in zip(self._all, value): for i, v in zip(self.all, value):
i.value = v i.value = v
def on(self): def on(self):
""" """
Turn all the board's components on. Turn all the board's components on.
""" """
for thing in self._all: for thing in self.all:
thing.on() thing.on()
def off(self): def off(self):
""" """
Turn all the board's components off. Turn all the board's components off.
""" """
for thing in self._all: for thing in self.all:
thing.off() thing.off()
def toggle(self): def toggle(self):
@@ -214,7 +226,7 @@ class FishDish(TrafficLights):
Toggle all the board's components. For each component, if it's on, turn Toggle all the board's components. For each component, if it's on, turn
it off; if it's off, turn it on. it off; if it's off, turn it on.
""" """
for thing in self._all: for thing in self.all:
thing.toggle() thing.toggle()
def blink(self, on_time=1, off_time=1, n=None, background=True): def blink(self, on_time=1, off_time=1, n=None, background=True):
@@ -236,59 +248,31 @@ class FishDish(TrafficLights):
finished (warning: the default value of `n` will result in this finished (warning: the default value of `n` will result in this
method never returning). method never returning).
""" """
# XXX This isn't going to work for background=False
for thing in self._all: for thing in self._all:
led.blink(on_time, off_time, n, background) thing.blink(on_time, off_time, n, background)
def lights_on(self):
class FishDish(TrafficLightsBuzzer):
""" """
Turn all the board's LEDs on. Pi Supply FishDish: traffic light LEDs, a button and a buzzer.
""" """
super(FishDish, self).on() def __init__(self):
super(FishDish, self).__init__(
def lights_off(self): TrafficLights(9, 22, 4),
""" Buzzer(8),
Turn all the board's LEDs off. Button(7, pull_up=False))
"""
super(FishDish, self).off()
def toggle_lights(self):
"""
Toggle all the board's LEDs. For each LED, if it's on, turn
it off; if it's off, turn it on.
"""
super(FishDish, self).toggle()
def blink_lights(self, on_time=1, off_time=1, n=None, background=True):
"""
Make all the board's LEDs turn on and off repeatedly.
on_time: `1`
Number of seconds to be on
off_time: `1`
Number of seconds to be off
n: `None`
Number of times to blink; None means forever
background: `True`
If `True`, start a background thread to continue blinking and
return immediately. If `False`, only return when the blink is
finished (warning: the default value of `n` will result in this
method never returning).
"""
super(FishDish, self).blink(on_time, off_time, n, background)
class TrafficHat(FishDish): class TrafficHat(TrafficLightsBuzzer):
""" """
Ryanteck Traffic HAT: traffic light LEDs, a button and a buzzer. Ryanteck Traffic HAT: traffic light LEDs, a button and a buzzer.
""" """
def __init__(self): def __init__(self):
super(FishDish, self).__init__(22, 23, 24) super(TrafficHat, self).__init__(
self.buzzer = Buzzer(5) TrafficLights(22, 23, 24),
self.button = Button(25) Buzzer(5),
self._all = self._leds + (self.buzzer,) Button(25))
RobotTuple = namedtuple('RobotTuple', ('left', 'right')) RobotTuple = namedtuple('RobotTuple', ('left', 'right'))
@@ -304,6 +288,10 @@ class Robot(SourceMixin, CompositeDevice):
self._left = Motor(*left) self._left = Motor(*left)
self._right = Motor(*right) self._right = Motor(*right)
def close(self):
self._left.close()
self._right.close()
@property @property
def value(self): def value(self):
""" """