Skip to content

Commit 597767d

Browse files
committed
YAPF'd
1 parent e43b30c commit 597767d

File tree

6 files changed

+60
-43
lines changed

6 files changed

+60
-43
lines changed

examples/line_echo.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import trio
22
import trio_gpio as gpio
3-
43
"""
54
This script oggles a pin and watches another. The two are presumed to be connected (hardware wire).
65
"""
6+
7+
78
async def pling(line):
89
while True:
910
line.value = 1
1011
await trio.sleep(1)
1112
line.value = 0
1213
await trio.sleep(1)
1314

15+
1416
async def main():
1517
async with trio.open_nursery() as n:
1618
with gpio.Chip(0) as c:
1719
with c.line(19).open(direction=gpio.DIRECTION_OUTPUT) as out_:
1820
in_ = c.line(20)
19-
n.start_soon(pling,out_)
21+
n.start_soon(pling, out_)
2022
with in_.monitor(gpio.REQUEST_EVENT_BOTH_EDGES):
2123
async for e in in_:
2224
print(e, "on" if e.value else "off", "at", e.time.strftime("%H:%M:%S"))
2325

26+
2427
if __name__ == "__main__":
2528
trio.run(main)

examples/line_echo_polled.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
import trio
22
import trio_gpio as gpio
3-
43
"""
54
This script oggles a pin and watches another. The two are presumed to be connected (hardware wire).
65
"""
6+
7+
78
async def pling(line):
89
while True:
910
line.value = 1
1011
await trio.sleep(1)
1112
line.value = 0
1213
await trio.sleep(1)
1314

15+
1416
async def main():
1517
async with trio.open_nursery() as n:
1618
with gpio.Chip(0) as c:
1719
with c.line(19).open(direction=gpio.DIRECTION_OUTPUT) as out_, \
1820
c.line(20).open(direction=gpio.DIRECTION_INPUT) as in_:
19-
n.start_soon(pling,out_)
21+
n.start_soon(pling, out_)
2022
while True:
2123
print(in_.value)
2224
await trio.sleep(0.3)
2325

26+
2427
if __name__ == "__main__":
2528
trio.run(main)

examples/line_value.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import trio_gpio as gpio
22
import time
3-
43
"""Flash an output manually.
54
65
On the Pi3, control the LEDs thus:

trio_gpio/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .gpio import Chip
88
from .libgpiod import * # noqa
99

10+
1011
def open_chip(num=0, consumer=sys.argv[0]):
1112
"""Returns an object representing a GPIO chip.
1213

trio_gpio/gpio.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import trio
55
import datetime
66

7+
78
class Chip:
89
"""Represents a GPIO chip.
910
@@ -15,10 +16,11 @@ class Chip:
1516
1617
"""
1718
_chip = None
19+
1820
def __init__(self, num=0, consumer=sys.argv[0]):
1921
self._num = num
2022
self._consumer = consumer
21-
23+
2224
def __repr__(self):
2325
return "%s(%s)" % (self.__class__.__name, self._num)
2426

@@ -27,7 +29,7 @@ def __enter__(self):
2729
if self._chip == gpio.ffi.NULL:
2830
raise OSError("unable to open chip")
2931
return self
30-
32+
3133
def __exit__(self, *tb):
3234
gpio.lib.gpiod_chip_close(self._chip)
3335
self._chip = None
@@ -43,13 +45,15 @@ def line(self, offset, consumer=None):
4345
consumer = self._consumer
4446
return Line(self, offset, consumer=consumer)
4547

48+
4649
_FREE = 0
4750
_PRE_IO = 1
4851
_IN_IO = 2
4952
_PRE_EV = 3
5053
_IN_EV = 4
5154
_IN_USE = {_IN_IO, _IN_EV}
5255

56+
5357
class Line:
5458
"""Represents a single GPIO line.
5559
@@ -66,9 +70,11 @@ def __init__(self, chip, offset, consumer=sys.argv[0][:-3]):
6670
self._offset = offset
6771
self._consumer = consumer.encode("utf-8")
6872
self.__consumer = gpio.ffi.new("char[]", self._consumer)
69-
73+
7074
def __repr__(self):
71-
return "<%s %s:%d %s=%d>" % (self.__class__.__name__,self._chip,self._offset, self._line,self._state)
75+
return "<%s %s:%d %s=%d>" % (
76+
self.__class__.__name__, self._chip, self._offset, self._line, self._state
77+
)
7278

7379
def open(self, direction=gpio.DIRECTION_INPUT, default=False, flags=0):
7480
"""
@@ -107,20 +113,22 @@ def __enter__(self):
107113
elif self._state == _PRE_EV:
108114
self._enter_ev()
109115
else:
110-
raise RuntimeError("wrong state",self)
116+
raise RuntimeError("wrong state", self)
111117
return self
112118

113119
def _enter_io(self):
114120
if self._direction == gpio.DIRECTION_INPUT:
115121
r = gpio.lib.gpiod_line_request_input_flags(self._line, self._consumer, self._flags)
116122
elif self._direction == gpio.DIRECTION_OUTPUT:
117-
r = gpio.lib.gpiod_line_request_output_flags(self._line, self._consumer, self._flags, self._default)
123+
r = gpio.lib.gpiod_line_request_output_flags(
124+
self._line, self._consumer, self._flags, self._default
125+
)
118126
else:
119127
self.__exit__()
120-
raise RuntimeError("Unknown direction",r)
128+
raise RuntimeError("Unknown direction", r)
121129
if r != 0:
122130
self.__exit__()
123-
raise OSError("unable to set direction",r)
131+
raise OSError("unable to set direction", r)
124132
self._state = _IN_IO
125133
return self
126134

@@ -144,7 +152,7 @@ def __exit__(self, *tb):
144152

145153
def _is_open(self):
146154
if self._state not in _IN_USE:
147-
raise RuntimeError("Line is not open",self)
155+
raise RuntimeError("Line is not open", self)
148156

149157
@property
150158
def value(self):
@@ -187,15 +195,15 @@ def offset(self):
187195
if self._line is None:
188196
return self._offset
189197
return gpio.lib.gpiod_line_offset(self._line)
190-
198+
191199
@property
192200
def name(self):
193201
self._is_open()
194202
n = gpio.lib.gpiod_line_name(self._line)
195203
if n == gpio.ffi.NULL:
196204
return None
197205
return n
198-
206+
199207
@property
200208
def consumer(self):
201209
if self._line is None:
@@ -204,7 +212,7 @@ def consumer(self):
204212
if n == gpio.ffi.NULL:
205213
return None
206214
return gpio.ffi.string(n).decode("utf-8")
207-
215+
208216
def monitor(self, type=gpio.REQUEST_EVENT_RISING_EDGE, flags=0):
209217
"""
210218
Monitor events.
@@ -235,16 +243,18 @@ def _update(self):
235243

236244
def __iter__(self):
237245
raise RuntimeError("You need to use 'async for'")
246+
238247
def __aenter__(self):
239248
raise RuntimeError("YOu need to use 'with'")
249+
240250
def __aexit__(self):
241251
raise RuntimeError("YOu need to use 'with'")
242252

243253
def __aiter__(self):
244254
if self._state != _IN_EV:
245255
raise RuntimeError("You need to call 'with LINE.monitor() / async for event in LINE'")
246256
return self
247-
257+
248258
async def __anext__(self):
249259
if self._state != _IN_EV:
250260
raise RuntimeError("wrong state")
@@ -259,14 +269,16 @@ async def __anext__(self):
259269
if r != 0:
260270
raise OSError("unable to read update")
261271
return Event(ev)
262-
272+
263273
async def aclose(self):
264274
"""close the iterator."""
265275
pass
266276

277+
267278
class Event:
268279
"""Store a Pythonic representation of an event
269280
"""
281+
270282
def __init__(self, ev):
271283
if ev.event_type == gpio.EVENT_RISING_EDGE:
272284
self.value = 1
@@ -280,13 +292,12 @@ def __init__(self, ev):
280292
@property
281293
def timestamp(self):
282294
"""Return a (second,nanosecond) tuple for fast timestamping"""
283-
return (self._ts_sec,self._ts_nsec)
295+
return (self._ts_sec, self._ts_nsec)
284296

285297
@property
286298
def time(self):
287299
"""Return the event's proper datetime"""
288-
return datetime.datetime.fromtimestamp(self._ts_sec + self._ts_nsec/1000000000)
300+
return datetime.datetime.fromtimestamp(self._ts_sec + self._ts_nsec / 1000000000)
289301

290302
def __repr__(self):
291303
return "<%s @%s>" % (self.value, self.time)
292-

trio_gpio/libgpiod.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
# Copyright (c) 2018 Matthias Urlichs <[email protected]>
44
# Based on work Copyright (c) 2018 Steven P. Goldsmith
5-
65
"""
76
libgpiod CFFI interface
87
-------------
@@ -14,27 +13,28 @@
1413
from cffi import FFI
1514

1615
__all__ = [
17-
"DIRECTION_INPUT",
18-
"DIRECTION_OUTPUT",
19-
"ACTIVE_STATE_HIGH",
20-
"ACTIVE_STATE_LOW",
21-
"REQUEST_DIRECTION_AS_IS",
22-
"REQUEST_DIRECTION_INPUT",
23-
"REQUEST_DIRECTION_OUTPUT",
24-
"REQUEST_EVENT_FALLING_EDGE",
25-
"REQUEST_EVENT_RISING_EDGE",
26-
"REQUEST_EVENT_BOTH_EDGES",
27-
"REQUEST_FLAG_OPEN_DRAIN",
28-
"REQUEST_FLAG_OPEN_SOURCE",
29-
"REQUEST_FLAG_ACTIVE_LOW",
30-
"EVENT_RISING_EDGE",
31-
"EVENT_FALLING_EDGE",
32-
"ffi",
33-
"lib",
16+
"DIRECTION_INPUT",
17+
"DIRECTION_OUTPUT",
18+
"ACTIVE_STATE_HIGH",
19+
"ACTIVE_STATE_LOW",
20+
"REQUEST_DIRECTION_AS_IS",
21+
"REQUEST_DIRECTION_INPUT",
22+
"REQUEST_DIRECTION_OUTPUT",
23+
"REQUEST_EVENT_FALLING_EDGE",
24+
"REQUEST_EVENT_RISING_EDGE",
25+
"REQUEST_EVENT_BOTH_EDGES",
26+
"REQUEST_FLAG_OPEN_DRAIN",
27+
"REQUEST_FLAG_OPEN_SOURCE",
28+
"REQUEST_FLAG_ACTIVE_LOW",
29+
"EVENT_RISING_EDGE",
30+
"EVENT_FALLING_EDGE",
31+
"ffi",
32+
"lib",
3433
]
3534

3635
ffi = FFI()
37-
ffi.cdef("""
36+
ffi.cdef(
37+
"""
3838
enum {
3939
GPIOD_CTXLESS_EVENT_CB_TIMEOUT,
4040
GPIOD_CTXLESS_EVENT_CB_RISING_EDGE,
@@ -294,7 +294,8 @@
294294
gpiod_line_iter_next(struct gpiod_line_iter *iter);
295295
296296
const char *gpiod_version_string(void);
297-
""")
297+
"""
298+
)
298299

299300
lib = ffi.dlopen("libgpiod.so")
300301

@@ -314,4 +315,3 @@
314315
REQUEST_FLAG_ACTIVE_LOW = lib.GPIOD_LINE_REQUEST_FLAG_ACTIVE_LOW
315316
EVENT_RISING_EDGE = lib.GPIOD_LINE_EVENT_RISING_EDGE
316317
EVENT_FALLING_EDGE = lib.GPIOD_LINE_EVENT_FALLING_EDGE
317-

0 commit comments

Comments
 (0)