1
0
mirror of https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git synced 2026-10-24 00:48:53 +03:00
Files
libgpiod/bindings/python/examples/find_line_by_name.py
Vincent Fazio 485b81cd4b bindings: python: examples: apply linter recommendations
* Sort imports
* Use f-strings where possible
* Remove usage of `tuple` for config key values

Signed-off-by: Vincent Fazio <vfazio@gmail.com>
Link: https://patch.msgid.link/20260401001459.19159-7-vfazio@gmail.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
2026-04-03 11:00:36 +02:00

40 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 os
from collections.abc import Generator
import gpiod
def generate_gpio_chips() -> Generator[str]:
for entry in os.scandir("/dev/"):
if gpiod.is_gpiochip_device(entry.path):
yield entry.path
def find_line_by_name(line_name: str) -> None:
# 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(f"{line_name}: {chip.get_info().name} {offset}")
return
except OSError:
# An OSError is raised if the name is not found.
continue
print(f"line '{line_name}' not found")
if __name__ == "__main__":
try:
find_line_by_name("GPIO19")
except OSError as ex:
print(ex, "\nCustomise the example configuration to suit your situation")