Update docstrings and add initial set of documentation

This commit is contained in:
Ben Nuttall
2015-09-28 11:11:00 +01:00
parent a38ab6cd45
commit 3f960f6da3
12 changed files with 831 additions and 319 deletions

View File

@@ -7,7 +7,7 @@ from time import sleep
class LEDBoard(object):
"""
A Generic LED Board or collecfion of LEDs.
A Generic LED Board or collection of LEDs.
"""
def __init__(self, leds):
self._leds = tuple(LED(led) for led in leds)
@@ -38,38 +38,27 @@ class LEDBoard(object):
for led in self._leds:
led.toggle()
def blink(self, on_time=1, off_time=1):
def blink(self, on_time=1, off_time=1, n=None, background=True):
"""
Make all the LEDs turn on and off repeatedly in the background.
Make all the 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).
"""
for led in self._leds:
led.blink(on_time, off_time)
def flash(self, on_time=1, off_time=1, n=1):
"""
Turn all the LEDs on and off a given number of times.
on_time: 1
Number of seconds on
off_time: 1
Number of seconds off
n: 1
Number of iterations
"""
for i in range(n):
self.on()
sleep(on_time)
self.off()
if i+1 < n: # don't sleep on final iteration
sleep(off_time)
led.blink(on_time, off_time, n, background)
class PiLiter(LEDBoard):
@@ -116,7 +105,7 @@ class PiTraffic(TrafficLights):
class FishDish(TrafficLights):
"""
Pi Supply FishDish: horizontal traffic light LEDs, a button and a buzzer.
Pi Supply FishDish: traffic light LEDs, a button and a buzzer.
"""
def __init__(self):
red, amber, green = (9, 22, 4)
@@ -151,6 +140,28 @@ class FishDish(TrafficLights):
for thing in self._all:
thing.toggle()
def blink(self, on_time=1, off_time=1, n=None, background=True):
"""
Make all the board's components 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).
"""
for thing in self._all:
led.blink(on_time, off_time, n, background)
def lights_on(self):
"""
Turn all the board's LEDs on.
@@ -170,30 +181,31 @@ class FishDish(TrafficLights):
"""
super(FishDish, self).toggle()
def flash_lights(self, on_time=1, off_time=1, n=1):
def blink_lights(self, on_time=1, off_time=1, n=None, background=True):
"""
Turn all the LEDs on and off a given number of times.
Make all the board's LEDs turn on and off repeatedly.
on_time: 1
Number of seconds on
Number of seconds to be on
off_time: 1
Number of seconds off
Number of seconds to be off
n: 1
Number of iterations
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).
"""
for i in range(n):
[led.on() for led in self.leds]
sleep(on_time)
[led.off() for led in self.leds]
if i+1 < n: # don't sleep on final iteration
sleep(off_time)
super(FishDish, self).blink(on_time, off_time, n, background)
class TrafficHat(FishDish):
"""
Ryanteck Traffic HAT: horizontal traffic light LEDs, a button and a buzzer.
Ryanteck Traffic HAT: traffic light LEDs, a button and a buzzer.
"""
def __init__(self):
red, amber, green = (22, 23, 24)

View File

@@ -14,7 +14,8 @@ from .devices import GPIODeviceError, GPIODevice, GPIOQueue
def _alias(key):
return property(
lambda self: getattr(self, key),
lambda self, val: setattr(self, key, val))
lambda self, val: setattr(self, key, val)
)
class InputDeviceError(GPIODeviceError):
@@ -28,12 +29,12 @@ class InputDevice(GPIODevice):
def __init__(self, pin=None, pull_up=False):
super(InputDevice, self).__init__(pin)
self._pull_up = pull_up
self._active_edge = (GPIO.RISING, GPIO.FALLING)[pull_up]
self._inactive_edge = (GPIO.FALLING, GPIO.RISING)[pull_up]
if pull_up:
self._active_state = GPIO.LOW
self._inactive_state = GPIO.HIGH
GPIO.setup(pin, GPIO.IN, (GPIO.PUD_DOWN, GPIO.PUD_UP)[pull_up])
self._active_edge = GPIO.FALLING if pull_up else GPIO.RISING
self._inactive_edge = GPIO.RISING if pull_up else GPIO.FALLING
self._active_state = GPIO.LOW if pull_up else GPIO.HIGH
self._inactive_state = GPIO.HIGH if pull_up else GPIO.LOW
pull = GPIO.PUD_UP if pull_up else GPIO.PUD_DOWN
GPIO.setup(pin, GPIO.IN, pull)
@property
def pull_up(self):
@@ -46,7 +47,7 @@ class InputDevice(GPIODevice):
class WaitableInputDevice(InputDevice):
"""
A time-dependent Generic Input Device.
An action-dependent Generic Input Device.
"""
def __init__(self, pin=None, pull_up=False):
super(WaitableInputDevice, self).__init__(pin, pull_up)
@@ -216,6 +217,8 @@ class Button(DigitalInputDevice):
def __init__(self, pin=None, pull_up=True, bouncetime=None):
super(Button, self).__init__(pin, pull_up, bouncetime)
is_pressed = alias('is_active')
when_pressed = _alias('when_activated')
when_released = _alias('when_deactivated')

View File

@@ -34,7 +34,7 @@ class OutputDevice(GPIODevice):
class DigitalOutputDevice(OutputDevice):
"""
Generic Digital GPIO Output Device (on/off/blink/toggle/flash).
Generic Digital GPIO Output Device (on/off/toggle/blink).
"""
def __init__(self, pin=None):
super(DigitalOutputDevice, self).__init__(pin)
@@ -55,9 +55,20 @@ class DigitalOutputDevice(OutputDevice):
self._stop_blink()
super(DigitalOutputDevice, self).off()
def toggle(self):
"""
Reverse the state of the device.
If it's on, turn it off; if it's off, turn it on.
"""
with self._lock:
if self.is_active:
self.off()
else:
self.on()
def blink(self, on_time=1, off_time=1, n=None, background=True):
"""
Make the device turn on and off repeatedly in the background.
Make the device turn on and off repeatedly.
on_time: 1
Number of seconds on
@@ -83,17 +94,6 @@ class DigitalOutputDevice(OutputDevice):
self._blink_thread.join()
self._blink_thread = None
def toggle(self):
"""
Reverse the state of the device.
If it's on, turn it off; if it's off, turn it on.
"""
with self._lock:
if self.is_active:
self.off()
else:
self.on()
def _stop_blink(self):
if self._blink_thread:
self._blink_thread.stop()
@@ -114,104 +114,21 @@ class LED(DigitalOutputDevice):
"""
An LED (Light Emmitting Diode) component.
"""
def on(self):
"""
Turn the LED on.
"""
super(LED, self).on()
def off(self):
"""
Turn the LED off.
"""
super(LED, self).off()
def blink(self, on_time=1, off_time=1, n=None, background=True):
"""
Make the LED turn on and off repeatedly in the background.
on_time: 1
Number of seconds on
off_time: 1
Number of seconds 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(LED, self).blink(on_time, off_time, n, background)
def toggle(self):
"""
Reverse the state of the LED.
If it's on, turn it off; if it's off, turn it on.
"""
super(LED, self).toggle()
pass
class Buzzer(DigitalOutputDevice):
"""
A Buzzer component.
A digital Buzzer component.
"""
def on(self):
"""
Turn the Buzzer on.
"""
super(Buzzer, self).on()
def off(self):
"""
Turn the Buzzer off.
"""
super(Buzzer, self).off()
def blink(self, on_time=1, off_time=1, n=None, background=True):
"""
Make the Buzzer turn on and off repeatedly in the background.
on_time: 1
Number of seconds on
off_time: 1
Number of seconds 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(Buzzer, self).blink(on_time, off_time, n, background)
def toggle(self):
"""
Reverse the state of the Buzzer.
If it's on, turn it off; if it's off, turn it on.
"""
super(Buzzer, self).toggle()
pass
class Motor(OutputDevice):
def on(self):
"""
Turns the Motor on.
"""
super(Motor, self).toggle()
def off(self):
"""
Turns the Motor off.
"""
super(Motor, self).toggle()
"""
Generic single-direction motor.
"""
pass
class Robot(object):