Skip to content

Commit cfed671

Browse files
committed
Add docstrings, close gpiozero#26
1 parent c619aab commit cfed671

File tree

5 files changed

+290
-18
lines changed

5 files changed

+290
-18
lines changed

gpiozero/boards.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77

88
class LEDBoard(object):
9+
"""
10+
A Generic LED Board or collecfion of LEDs.
11+
"""
912
def __init__(self, leds):
1013
self._leds = tuple(LED(led) for led in leds)
1114

@@ -14,22 +17,53 @@ def leds(self):
1417
return self._leds
1518

1619
def on(self):
20+
"""
21+
Turn all the LEDs on.
22+
"""
1723
for led in self._leds:
1824
led.on()
1925

2026
def off(self):
27+
"""
28+
Turn all the LEDs off.
29+
"""
2130
for led in self._leds:
2231
led.off()
2332

2433
def toggle(self):
34+
"""
35+
Toggle all the LEDs. For each LED, if it's on, turn it off; if it's
36+
off, turn it on.
37+
"""
2538
for led in self._leds:
2639
led.toggle()
2740

2841
def blink(self, on_time=1, off_time=1):
42+
"""
43+
Make all the LEDs turn on and off repeatedly in the background.
44+
45+
on_time: 1
46+
Number of seconds to be on
47+
48+
off_time: 1
49+
Number of seconds to be off
50+
"""
2951
for led in self._leds:
3052
led.blink(on_time, off_time)
3153

3254
def flash(self, on_time=1, off_time=1, n=1):
55+
"""
56+
Turn all the LEDs on and off a given number of times.
57+
58+
on_time: 1
59+
Number of seconds on
60+
61+
off_time: 1
62+
Number of seconds off
63+
64+
n: 1
65+
Number of iterations
66+
"""
3367
for i in range(n):
3468
self.on()
3569
sleep(on_time)
@@ -39,12 +73,27 @@ def flash(self, on_time=1, off_time=1, n=1):
3973

4074

4175
class PiLiter(LEDBoard):
76+
"""
77+
Ciseco Pi-LITEr: strip of 8 very bright LEDs.
78+
"""
4279
def __init__(self):
4380
leds = (4, 17, 27, 18, 22, 23, 24, 25)
4481
super(PiLiter, self).__init__(leds)
4582

4683

4784
class TrafficLights(LEDBoard):
85+
"""
86+
Generic Traffic Lights set.
87+
88+
red: None
89+
Red LED pin
90+
91+
amber: None
92+
Amber LED pin
93+
94+
green: None
95+
Green LED pin
96+
"""
4897
def __init__(self, red=None, amber=None, green=None):
4998
if not all([red, amber, green]):
5099
raise GPIODeviceError('Red, Amber and Green pins must be provided')
@@ -56,12 +105,19 @@ def __init__(self, red=None, amber=None, green=None):
56105

57106

58107
class PiTraffic(TrafficLights):
108+
"""
109+
Low Voltage Labs PI-TRAFFIC: vertical traffic lights board on pins 9, 10
110+
and 11.
111+
"""
59112
def __init__(self):
60113
red, amber, green = (9, 10, 11)
61114
super(PiTraffic, self).__init__(red, amber, green)
62115

63116

64117
class FishDish(TrafficLights):
118+
"""
119+
Pi Supply FishDish: horizontal traffic light LEDs, a button and a buzzer.
120+
"""
65121
def __init__(self):
66122
red, amber, green = (9, 22, 4)
67123
super(FishDish, self).__init__(red, amber, green)
@@ -74,27 +130,59 @@ def all(self):
74130
return self._all
75131

76132
def on(self):
133+
"""
134+
Turn all the board's components on.
135+
"""
77136
for thing in self._all:
78137
thing.on()
79138

80139
def off(self):
140+
"""
141+
Turn all the board's components off.
142+
"""
81143
for thing in self._all:
82144
thing.off()
83145

84146
def toggle(self):
147+
"""
148+
Toggle all the board's components. For each component, if it's on, turn
149+
it off; if it's off, turn it on.
150+
"""
85151
for thing in self._all:
86152
thing.toggle()
87153

88154
def lights_on(self):
155+
"""
156+
Turn all the board's LEDs on.
157+
"""
89158
super(FishDish, self).on()
90159

91160
def lights_off(self):
161+
"""
162+
Turn all the board's LEDs off.
163+
"""
92164
super(FishDish, self).off()
93165

94166
def toggle_lights(self):
167+
"""
168+
Toggle all the board's LEDs. For each LED, if it's on, turn
169+
it off; if it's off, turn it on.
170+
"""
95171
super(FishDish, self).toggle()
96172

97173
def flash_lights(self, on_time=1, off_time=1, n=1):
174+
"""
175+
Turn all the LEDs on and off a given number of times.
176+
177+
on_time: 1
178+
Number of seconds on
179+
180+
off_time: 1
181+
Number of seconds off
182+
183+
n: 1
184+
Number of iterations
185+
"""
98186
for i in range(n):
99187
[led.on() for led in self.leds]
100188
sleep(on_time)
@@ -104,6 +192,9 @@ def flash_lights(self, on_time=1, off_time=1, n=1):
104192

105193

106194
class TrafficHat(FishDish):
195+
"""
196+
Ryanteck Traffic HAT: horizontal traffic light LEDs, a button and a buzzer.
197+
"""
107198
def __init__(self):
108199
red, amber, green = (22, 23, 24)
109200
super(FishDish, self).__init__(red, amber, green)

gpiozero/devices.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class GPIODeviceError(Exception):
1010

1111

1212
class GPIODevice(object):
13+
"""
14+
Generic GPIO Device.
15+
"""
1316
def __init__(self, pin=None):
1417
if pin is None:
1518
raise GPIODeviceError('No GPIO pin number given')

gpiozero/input_devices.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class InputDeviceError(GPIODeviceError):
2222

2323

2424
class InputDevice(GPIODevice):
25+
"""
26+
Generic GPIO Input Device.
27+
"""
2528
def __init__(self, pin=None, pull_up=False):
2629
super(InputDevice, self).__init__(pin)
2730
self._pull_up = pull_up
@@ -42,6 +45,9 @@ def __repr__(self):
4245

4346

4447
class WaitableInputDevice(InputDevice):
48+
"""
49+
A time-dependent Generic Input Device.
50+
"""
4551
def __init__(self, pin=None, pull_up=False):
4652
super(WaitableInputDevice, self).__init__(pin, pull_up)
4753
self._active_event = Event()
@@ -51,9 +57,23 @@ def __init__(self, pin=None, pull_up=False):
5157
self._last_state = None
5258

5359
def wait_for_active(self, timeout=None):
60+
"""
61+
Halt the program until the device is activated, or the timeout is
62+
reached.
63+
64+
timeout: None
65+
Number of seconds (?) to wait before proceeding
66+
"""
5467
return self._active_event.wait(timeout)
5568

5669
def wait_for_inactive(self, timeout=None):
70+
"""
71+
Halt the program until the device is inactivated, or the timeout is
72+
reached.
73+
74+
timeout: None
75+
Number of seconds (?) to wait before proceeding
76+
"""
5777
return self._inactive_event.wait(timeout)
5878

5979
def _get_when_activated(self):
@@ -122,6 +142,9 @@ def _fire_events(self):
122142

123143

124144
class DigitalInputDevice(WaitableInputDevice):
145+
"""
146+
A Generic Digital Input Device.
147+
"""
125148
def __init__(self, pin=None, pull_up=False, bouncetime=None):
126149
super(DigitalInputDevice, self).__init__(pin, pull_up)
127150
# Yes, that's really the default bouncetime in RPi.GPIO...
@@ -140,6 +163,9 @@ def _fire_events(self, channel):
140163

141164

142165
class SmoothedInputDevice(WaitableInputDevice):
166+
"""
167+
A Generic Digital Input Device with background polling.
168+
"""
143169
def __init__(
144170
self, pin=None, pull_up=False, threshold=0.5,
145171
queue_len=5, sample_wait=0.0, partial=False):
@@ -184,6 +210,9 @@ def is_active(self):
184210

185211

186212
class Button(DigitalInputDevice):
213+
"""
214+
A physical push button or switch.
215+
"""
187216
def __init__(self, pin=None, pull_up=True, bouncetime=None):
188217
super(Button, self).__init__(pin, pull_up, bouncetime)
189218

@@ -195,6 +224,9 @@ def __init__(self, pin=None, pull_up=True, bouncetime=None):
195224

196225

197226
class MotionSensor(SmoothedInputDevice):
227+
"""
228+
A PIR (Passive Infra-Red) motion sensor.
229+
"""
198230
def __init__(
199231
self, pin=None, queue_len=5, sample_rate=10, threshold=0.5,
200232
partial=False):
@@ -214,6 +246,9 @@ def __init__(
214246

215247

216248
class LightSensor(SmoothedInputDevice):
249+
"""
250+
An LDR (Light Dependent Resistor) Light Sensor.
251+
"""
217252
def __init__(
218253
self, pin=None, queue_len=5, charge_time_limit=0.01,
219254
threshold=0.1, partial=False):
@@ -260,6 +295,9 @@ def _read(self):
260295

261296

262297
class TemperatureSensor(W1ThermSensor):
298+
"""
299+
A Digital Temperature Sensor.
300+
"""
263301
@property
264302
def value(self):
265303
return self.get_temperature()

0 commit comments

Comments
 (0)