Debian Codegraph @

bindings/python/examples/find_line_by_name.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0-or-later
3 # SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]>
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 # Names are not guaranteed unique, so this finds the first line with
19 # the given name.
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 # An OSError is raised if the name is not found.
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")