Skip to content

Commit 17d70f5

Browse files
Merge pull request #62 from tiagocoutinho/gpio-doc
GPIO doc
2 parents 92753bd + 5c100e1 commit 17d70f5

File tree

3 files changed

+118
-9
lines changed

3 files changed

+118
-9
lines changed

docs/user_guide/gpio.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ Here are the line configuration options with their restrictions and defaults:
167167
* possible values: `input`, `output`
168168
* default: **input**
169169
* *active*:
170-
* possible values: `active`, `low`
171-
* default: **active**
170+
* possible values: `high`, `low`
171+
* default: **high**
172172
* *bias*:
173173
* possbile values: `pull-up`, `pull-down`, `none`
174174
* default: **none**
@@ -294,6 +294,8 @@ event = next(iter(lines))
294294
295295
```
296296
297+
### asyncio
298+
297299
Async API is also supported:
298300
299301
```python
@@ -309,3 +311,62 @@ async def main():
309311
310312
asyncio.run(main())
311313
```
314+
315+
## Configuration events
316+
317+
Linuxpy GPIO API supports watching for line configuration events:
318+
319+
```python
320+
321+
with find() as gpio:
322+
for event in gpio.info_stream([5, 10]):
323+
print(event)
324+
325+
```
326+
327+
The example above will listen for configuration change, line request and line
328+
released events on lines 5 and 10.
329+
330+
If you need fine control you can manually register for line watch and then
331+
listen for events. So the above example can also be written as:
332+
333+
```python
334+
335+
with find() as gpio:
336+
with gpio.watching([5, 10]):
337+
for event in gpio:
338+
print(event)
339+
```
340+
341+
Or even:
342+
343+
```python
344+
345+
with find() as gpio:
346+
gpio.watch_lines([5, 10])
347+
try:
348+
for event in gpio:
349+
print(event)
350+
finally:
351+
gpio.unwatch_lines([5, 10])
352+
353+
```
354+
355+
### asyncio
356+
357+
Async API is also supported on configuration events:
358+
359+
```python
360+
import asyncio
361+
import contextlib
362+
363+
364+
async def main():
365+
with find() as gpio:
366+
async with contextlib.aclosing(gpio.async_info_stream([5, 10])) as stream:
367+
async for event in stream:
368+
print(event)
369+
370+
371+
asyncio.run(main())
372+
```

linuxpy/gpio/device.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,10 +541,10 @@ def __getitem__(self, key: Union[int, tuple, slice]) -> Request:
541541
return self.request(lines)
542542

543543
def __iter__(self) -> Iterable[LineInfoEvent]:
544-
"""Infinite stream of line info events
544+
"""Infinite stream of line config events
545545
546546
Returns:
547-
Iterable[LineInfoEvent]: the stream of line info events
547+
Iterable[LineInfoEvent]: the stream of line config events
548548
"""
549549
return event_info_stream((self,))
550550

@@ -557,10 +557,28 @@ def __aiter__(self) -> AsyncIterator[LineInfoEvent]:
557557
return async_event_info_stream((self,))
558558

559559
def info_stream(self, lines: Collection[int]) -> Iterable[LineInfoEvent]:
560+
"""Register for watching line config events on the given lines
561+
and stream them
562+
563+
Args:
564+
lines (Collection[int]): line numbers to watch for config events
565+
566+
Returns:
567+
Iterable[LineInfoEvent]: the stream of line config events
568+
"""
560569
with self.watching(lines):
561570
yield from iter(self)
562571

563572
async def async_info_stream(self, lines: Collection[int]) -> AsyncIterator[LineInfoEvent]:
573+
"""Register for watching line config events on the given lines
574+
and async stream them
575+
576+
Args:
577+
lines (Collection[int]): line numbers to watch for config events
578+
579+
Returns:
580+
AsyncIterator[LineInfoEvent]: the asynchronous stream of line info events
581+
"""
564582
with self.watching(lines):
565583
async with aclosing(self.__aiter__()) as stream:
566584
async for event in stream:
@@ -577,21 +595,47 @@ def get_info(self) -> Info:
577595
return get_info(self)
578596

579597
def watch_line(self, line: int):
598+
"""Register the given line for config events
599+
600+
Args:
601+
line (int): the line number to register
602+
"""
580603
watch_line_info(self, line)
581604

582605
def unwatch_line(self, line: int):
606+
"""Unregister the given line from config events
607+
608+
Args:
609+
line (int): the line number to unregister
610+
"""
583611
unwatch_line_info(self, line)
584612

585613
def watch_lines(self, lines: Collection[int]):
614+
"""Register the given lines for config events
615+
616+
Args:
617+
lines (Collection[int]): the line numbers to register
618+
"""
586619
for line in lines:
587620
self.watch_line(line)
588621

589622
def unwatch_lines(self, lines: Collection[int]):
623+
"""Unregister the given lines from config events
624+
625+
Args:
626+
lines (Collection[int]): the lines number to unregister
627+
"""
590628
for line in lines:
591629
self.unwatch_line(line)
592630

593631
@contextlib.contextmanager
594632
def watching(self, lines):
633+
"""A context manager during which the given lines are registered
634+
for line config events
635+
636+
Args:
637+
lines (_type_): the line numbers to listen for config events
638+
"""
595639
self.watch_lines(lines)
596640
try:
597641
yield

scripts/setup-gpio-sim.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,19 @@ def mkdir(path):
4545
path.mkdir()
4646

4747

48+
def cleanup(path):
49+
# clean up first
50+
if path.exists():
51+
live.write_text("0")
52+
for directory, _, _ in path.walk(top_down=False):
53+
directory.rmdir()
54+
55+
4856
path = CONFIGFS_PATH / cfg["name"]
4957
live = path / "live"
5058

51-
# clean up first
52-
if path.exists():
53-
live.write_text("0")
54-
for directory, _, _ in path.walk(top_down=False):
55-
directory.rmdir()
5659

60+
cleanup(path)
5761

5862
mkdir(path)
5963
for bank_id, bank in enumerate(cfg["banks"]):

0 commit comments

Comments
 (0)