1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-25 12:39:18 +03:00
Files
libgpiod/bindings/python/examples/find_line_by_name.py
Kent Gibson 96c52d3e59 bindings: python: examples: replace tools examples with use case examples
Replace tool examples with use case examples drawn from the tools.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
2023-06-26 12:09:10 +02:00

38 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
# SPDX-FileCopyrightText: 2023 Kent Gibson <warthog618@gmail.com>
"""Minimal example of finding a line with the given name."""
import gpiod
import os
def generate_gpio_chips():
for entry in os.scandir("/dev/"):
if gpiod.is_gpiochip_device(entry.path):
yield entry.path
def find_line_by_name(line_name):
# Names are not guaranteed unique, so this finds the first line with
# the given name.
for path in generate_gpio_chips():
with gpiod.Chip(path) as chip:
try:
offset = chip.line_offset_from_id(line_name)
print("{}: {} {}".format(line_name, chip.get_info().name, offset))
return
except OSError:
# An OSError is raised if the name is not found.
continue
print("line '{}' not found".format(line_name))
if __name__ == "__main__":
try:
find_line_by_name("GPIO19")
except OSError as ex:
print(ex, "\nCustomise the example configuration to suit your situation")