| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Minimal example of finding a line with the given name.""" |
| 6 |
|
| 7 |
import gpiod |
| 8 |
import os |
| 9 |
|
| 10 |
|
| 11 |
def generate_gpio_chips(): |
| 12 |
for entry in os.scandir("/dev/"): |
| 13 |
if gpiod.is_gpiochip_device(entry.path): |
| 14 |
yield entry.path |
| 15 |
|
| 16 |
|
| 17 |
def find_line_by_name(line_name): |
| 18 |
|
| 19 |
|
| 20 |
for path in generate_gpio_chips(): |
| 21 |
with gpiod.Chip(path) as chip: |
| 22 |
try: |
| 23 |
offset = chip.line_offset_from_id(line_name) |
| 24 |
print("{}: {} {}".format(line_name, chip.get_info().name, offset)) |
| 25 |
return |
| 26 |
except OSError: |
| 27 |
|
| 28 |
continue |
| 29 |
|
| 30 |
print("line '{}' not found".format(line_name)) |
| 31 |
|
| 32 |
|
| 33 |
if __name__ == "__main__": |
| 34 |
try: |
| 35 |
find_line_by_name("GPIO19") |
| 36 |
except OSError as ex: |
| 37 |
print(ex, "\nCustomise the example configuration to suit your situation") |