forked from romilly/quick2wire-python-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpio.py
More file actions
297 lines (220 loc) · 7.74 KB
/
gpio.py
File metadata and controls
297 lines (220 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""A convenient API to access the GPIO pins of the Raspberry Pi.
"""
import os
import subprocess
from contextlib import contextmanager
from quick2wire.board_revision import revision
from quick2wire.selector import EDGE
def gpio_admin(subcommand, pin, pull=None):
if pull:
subprocess.check_call(["gpio-admin", subcommand, str(pin), pull])
else:
subprocess.check_call(["gpio-admin", subcommand, str(pin)])
Out = "out"
In = "in"
Rising = "rising"
Falling = "falling"
Both = "both"
PullDown = "pulldown"
PullUp = "pullup"
class PinAPI(object):
def __init__(self, bank, index):
self._bank = bank
self._index = index
@property
def index(self):
return self._index
@property
def bank(self):
return self._bank
def __enter__(self):
self.open()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
value = property(lambda p: p.get(),
lambda p,v: p.set(v),
doc="""The value of the pin: 1 if the pin is high, 0 if the pin is low.""")
class PinBankAPI(object):
def __getitem__(self, n):
if 0 < n < len(self):
raise ValueError("no pin index {n} out of range", n=n)
return self.pin(n)
def write(self):
pass
def read(self):
pass
class Pin(PinAPI):
"""Controls a GPIO pin."""
__trigger__ = EDGE
def __init__(self, bank, index, soc_pin_number, direction=In, interrupt=None, pull=None):
"""Creates a pin
Parameters:
user_pin_number -- the identity of the pin used to create the derived class.
soc_pin_number -- the pin on the header to control, identified by the SoC pin number.
direction -- (optional) the direction of the pin, either In or Out.
interrupt -- (optional)
pull -- (optional)
Raises:
IOError -- could not export the pin (if direction is given)
"""
super(Pin,self).__init__(None, index)
self._soc_pin_number = soc_pin_number
self._file = None
self._direction = direction
self._interrupt = interrupt
self._pull = pull
@property
def soc_pin_number(self):
return self._soc_pin_number
def open(self):
gpio_admin("export", self.soc_pin_number, self._pull)
self._file = open(self._pin_path("value"), "r+")
self._write("direction", self._direction)
if self._direction == In:
self._write("edge", self._interrupt if self._interrupt is not None else "none")
def close(self):
if not self.closed:
if self.direction == Out:
self.value = 0
self._file.close()
self._file = None
self._write("direction", In)
self._write("edge", "none")
gpio_admin("unexport", self.soc_pin_number)
def get(self):
"""The current value of the pin: 1 if the pin is high or 0 if the pin is low.
The value can only be set if the pin's direction is Out.
Raises:
IOError -- could not read or write the pin's value.
"""
self._check_open()
self._file.seek(0)
v = self._file.read()
return int(v) if v else 0
def set(self, new_value):
self._check_open()
if self._direction != Out:
raise ValueError("not an output pin")
self._file.seek(0)
self._file.write(str(int(new_value)))
self._file.flush()
@property
def direction(self):
"""The direction of the pin: either In or Out.
The value of the pin can only be set if its direction is Out.
Raises:
IOError -- could not set the pin's direction.
"""
return self._direction
@direction.setter
def direction(self, new_value):
self._write("direction", new_value)
self._direction = new_value
@property
def interrupt(self):
"""The interrupt property specifies what event (if any) will raise an interrupt.
One of:
Rising -- voltage changing from low to high
Falling -- voltage changing from high to low
Both -- voltage changing in either direction
None -- interrupts are not raised
Raises:
IOError -- could not read or set the pin's interrupt trigger
"""
return self._interrupt
@interrupt.setter
def interrupt(self, new_value):
self._write("edge", new_value)
self._interrupt = new_value
@property
def pull(self):
return self._pull
def fileno(self):
"""Return the underlying file descriptor. Useful for select, epoll, etc."""
return self._file.fileno()
@property
def closed(self):
"""Returns if this pin is closed"""
return self._file is None or self._file.closed
def _check_open(self):
if self.closed:
raise IOError(str(self) + " is closed")
def _write(self, filename, value):
with open(self._pin_path(filename), "w+") as f:
f.write(value)
def _pin_path(self, filename=""):
return "/sys/devices/virtual/gpio/gpio%i/%s" % (self.soc_pin_number, filename)
def __repr__(self):
return self.__module__ + "." + str(self)
def __str__(self):
return "{type}({index})".format(
type=self.__class__.__name__,
index=self.index)
class PinBank(PinBankAPI):
def __init__(self, index_to_soc_fn, count=None):
super(PinBank,self).__init__()
self._index_to_soc = index_to_soc_fn
self._count = count
def pin(self, index, *args, **kwargs):
return Pin(self, index, self._index_to_soc(index), *args, **kwargs)
@property
def has_len(self):
return self._count is not None
def __len__(self):
if self._count is not None:
return self._count
else:
raise TypeError(self.__class__.__name__ + " has no len")
BUTTON = 0
LED = 1
SPI_INTERRUPT = 6
I2C_INTERRUPT = 7
_pi_revision = revision()
if _pi_revision == 0:
# Not running on the Raspberry Pi, so define no-op pin banks
pins = PinBank(lambda p: p)
pi_broadcom_soc = pins
pi_header_1 = pins
else:
def by_revision(d):
return d[_pi_revision]
# Maps header pin numbers to SoC GPIO numbers
# See http://elinux.org/RPi_Low-level_peripherals
#
# Note: - header pins are numbered from 1, SoC GPIO from zero
# - the Pi documentation identifies some header pins as GPIO0,
# GPIO1, etc., but these are not the same as the SoC GPIO
# numbers.
_pi_header_1_pins = {
3: by_revision({1:0, 2:2}),
5: by_revision({1:1, 2:3}),
7: 4,
8: 14,
10: 15,
11: 17,
12: 18,
13: by_revision({1:21, 2:27}),
15: 22,
16: 23,
18: 24,
19: 10,
21: 9,
22: 25,
23: 11,
24: 8,
26: 7
}
_pi_gpio_pins = [_pi_header_1_pins[i] for i in [11, 12, 13, 15, 16, 18, 22, 7]]
def lookup(pin_mapping, i):
try:
if i >= 0:
return pin_mapping[i]
except LookupError:
pass
raise IndexError(str(i) + " is not a valid pin index")
def map_with(pin_mapping):
return lambda i: lookup(pin_mapping,i)
pi_broadcom_soc = PinBank(lambda p: p)
pi_header_1 = PinBank(map_with(_pi_header_1_pins))
pins = PinBank(map_with(_pi_gpio_pins), len(_pi_gpio_pins))