python examples and comment corrections
cover python examples and comment corrections
The first patch is the updated 6/8 patch from my recent series. The updates are as per the review comments, except for renaming gpio_chips() to generate_gpio_chips(), rather than get_gpio_chips(), to indicate the paths are being generated not returned as a list.
Changes:
- rename gpio_chips()
- shorten reconfigure_input_to_output file comment
- use format style prints
The other patches are updates to the file comments to make them consistent with the updated python and to fix some cut-and-paste errors I noticed along the way.
Cheers,
Kent.
Kent Gibson (4):
bindings: python: examples: replace tools examples with use case
examples
core: examples: fix file comments
cxx: examples: file comment consistency
rust: examples: file comment consistency
.../examples/reconfigure_input_to_output.cpp | 4 +- bindings/python/examples/Makefile.am | 18 +++---- bindings/python/examples/find_line_by_name.py | 37 +++++++++++++++ bindings/python/examples/get_chip_info.py | 20 ++++++++ bindings/python/examples/get_line_info.py | 29 ++++++++++++ .../examples/get_multiple_line_values.py | 29 ++++++++++++ bindings/python/examples/gpiodetect.py | 15 ------ bindings/python/examples/gpiofind.py | 20 -------- bindings/python/examples/gpioget.py | 29 ------------ bindings/python/examples/gpioinfo.py | 28 ----------- bindings/python/examples/gpiomon.py | 26 ---------- bindings/python/examples/gpionotify.py | 21 --------- bindings/python/examples/gpioset.py | 36 -------------- bindings/python/examples/helpers.py | 15 ------ .../examples/reconfigure_input_to_output.py | 39 +++++++++++++++ .../examples/toggle_multiple_line_values.py | 47 +++++++++++++++++++ bindings/python/examples/watch_line_info.py | 23 +++++++++ bindings/python/examples/watch_line_rising.py | 31 ++++++++++++ .../examples/watch_multiple_line_values.py | 42 +++++++++++++++++ .../examples/reconfigure_input_to_output.rs | 2 +- examples/async_watch_line_value.c | 2 +- examples/get_multiple_line_values.c | 2 +- examples/reconfigure_input_to_output.c | 5 +- examples/watch_multiple_line_values.c | 2 +- 24 files changed, 317 insertions(+), 205 deletions(-) create mode 100755 bindings/python/examples/find_line_by_name.py create mode 100755 bindings/python/examples/get_chip_info.py create mode 100755 bindings/python/examples/get_line_info.py create mode 100755 bindings/python/examples/get_multiple_line_values.py delete mode 100755 bindings/python/examples/gpiodetect.py delete mode 100755 bindings/python/examples/gpiofind.py delete mode 100755 bindings/python/examples/gpioget.py delete mode 100755 bindings/python/examples/gpioinfo.py delete mode 100755 bindings/python/examples/gpiomon.py delete mode 100755 bindings/python/examples/gpionotify.py delete mode 100755 bindings/python/examples/gpioset.py delete mode 100644 bindings/python/examples/helpers.py create mode 100755 bindings/python/examples/reconfigure_input_to_output.py create mode 100755 bindings/python/examples/toggle_multiple_line_values.py create mode 100755 bindings/python/examples/watch_line_info.py create mode 100755 bindings/python/examples/watch_line_rising.py create mode 100755 bindings/python/examples/watch_multiple_line_values.py
Signature · 1 line
2.41.0
PATCH 1/4 bindings: python: examples: replace tools examples with use case examples
Replace tool examples with use case examples drawn from the tools.
--- bindings/python/examples/Makefile.am | 18 +++---- bindings/python/examples/find_line_by_name.py | 37 +++++++++++++++ bindings/python/examples/get_chip_info.py | 20 ++++++++ bindings/python/examples/get_line_info.py | 29 ++++++++++++ .../examples/get_multiple_line_values.py | 29 ++++++++++++ bindings/python/examples/gpiodetect.py | 15 ------ bindings/python/examples/gpiofind.py | 20 -------- bindings/python/examples/gpioget.py | 29 ------------ bindings/python/examples/gpioinfo.py | 28 ----------- bindings/python/examples/gpiomon.py | 26 ---------- bindings/python/examples/gpionotify.py | 21 --------- bindings/python/examples/gpioset.py | 36 -------------- bindings/python/examples/helpers.py | 15 ------ .../examples/reconfigure_input_to_output.py | 39 +++++++++++++++ .../examples/toggle_multiple_line_values.py | 47 +++++++++++++++++++ bindings/python/examples/watch_line_info.py | 23 +++++++++ bindings/python/examples/watch_line_rising.py | 31 ++++++++++++ .../examples/watch_multiple_line_values.py | 42 +++++++++++++++++ 18 files changed, 307 insertions(+), 198 deletions(-) create mode 100755 bindings/python/examples/find_line_by_name.py create mode 100755 bindings/python/examples/get_chip_info.py create mode 100755 bindings/python/examples/get_line_info.py create mode 100755 bindings/python/examples/get_multiple_line_values.py delete mode 100755 bindings/python/examples/gpiodetect.py delete mode 100755 bindings/python/examples/gpiofind.py delete mode 100755 bindings/python/examples/gpioget.py delete mode 100755 bindings/python/examples/gpioinfo.py delete mode 100755 bindings/python/examples/gpiomon.py delete mode 100755 bindings/python/examples/gpionotify.py delete mode 100755 bindings/python/examples/gpioset.py delete mode 100644 bindings/python/examples/helpers.py create mode 100755 bindings/python/examples/reconfigure_input_to_output.py create mode 100755 bindings/python/examples/toggle_multiple_line_values.py create mode 100755 bindings/python/examples/watch_line_info.py create mode 100755 bindings/python/examples/watch_line_rising.py create mode 100755 bindings/python/examples/watch_multiple_line_values.py
bindings/python/examples/Makefile.am +10 -8
@@ -3,13 +3,15 @@ EXTRA_DIST = \ async_watch_line_value.py \ + find_line_by_name.py \ + get_chip_info.py \ + get_line_info.py \ get_line_value.py \ - gpiodetect.py \ - gpiofind.py \ - gpioget.py \ - gpioinfo.py \ - gpiomon.py \ - gpionotify.py \ - gpioset.py \ + get_multiple_line_values.py \ + reconfigure_input_to_output.py \ toggle_line_value.py \ - watch_line_value.py + toggle_multiple_line_values.py \ + watch_line_requests.py \ + watch_line_value.py \ + watch_line_rising.py \ + watch_multiple_line_values.py
bindings/python/examples/find_line_by_name.py new file +37 -0
@@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""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")
bindings/python/examples/get_chip_info.py new file +20 -0
@@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of reading the info for a chip.""" + +import gpiod + + +def get_chip_info(chip_path): + with gpiod.Chip(chip_path) as chip: + info = chip.get_info() + print("{} [{}] ({} lines)".format(info.name, info.label, info.num_lines)) + + +if __name__ == "__main__": + try: + get_chip_info("/dev/gpiochip0") + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")
bindings/python/examples/get_line_info.py new file +29 -0
@@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of reading the info for a line.""" + +import gpiod + + +def get_line_info(chip_path, line_offset): + with gpiod.Chip(chip_path) as chip: + info = chip.get_line_info(line_offset) + is_input = info.direction == gpiod.line.Direction.INPUT + print( + "line {:>3}: {:>12} {:>12} {:>8} {:>10}".format( + info.offset, + info.name or "unnamed", + info.consumer or "unused", + "input" if is_input else "output", + "active-low" if info.active_low else "active-high", + ) + ) + + +if __name__ == "__main__": + try: + get_line_info("/dev/gpiochip0", 3) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")
bindings/python/examples/get_multiple_line_values.py new file +29 -0
@@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of reading multiple lines.""" + +import gpiod + +from gpiod.line import Direction + + +def get_multiple_line_values(chip_path, line_offsets): + with gpiod.request_lines( + chip_path, + consumer="get-multiple-line-values", + config={tuple(line_offsets): gpiod.LineSettings(direction=Direction.INPUT)}, + ) as request: + vals = request.get_values() + + for offset, val in zip(line_offsets, vals): + print("{}={} ".format(offset, val), end="") + print() + + +if __name__ == "__main__": + try: + get_multiple_line_values("/dev/gpiochip0", [5, 3, 7]) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")
bindings/python/examples/gpiodetect.py deleted +0 -15
@@ -1,15 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Reimplementation of the gpiodetect tool in Python.""" - -import gpiod -import os - -from helpers import gpio_chips - -if __name__ == "__main__": - for chip in gpio_chips(): - info = chip.get_info() - print("{} [{}] ({} lines)".format(info.name, info.label, info.num_lines))
bindings/python/examples/gpiofind.py deleted +0 -20
@@ -1,20 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Reimplementation of the gpiofind tool in Python.""" - -import gpiod -import os -import sys - -if __name__ == "__main__": - for entry in os.scandir("/dev/"): - if gpiod.is_gpiochip_device(entry.path): - with gpiod.Chip(entry.path) as chip: - offset = chip.line_offset_from_id(sys.argv[1]) - if offset is not None: - print("{} {}".format(chip.get_info().name, offset)) - sys.exit(0) - - sys.exit(1)
bindings/python/examples/gpioget.py deleted +0 -29
@@ -1,29 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Simplified reimplementation of the gpioget tool in Python.""" - -import gpiod -import sys - -from gpiod.line import Direction - -if __name__ == "__main__": - if len(sys.argv) < 3: - raise TypeError("usage: gpioget.py <gpiochip> <offset1> <offset2> ...") - - path = sys.argv[1] - lines = [int(line) if line.isdigit() else line for line in sys.argv[2:]] - - request = gpiod.request_lines( - path, - consumer="gpioget.py", - config={tuple(lines): gpiod.LineSettings(direction=Direction.INPUT)}, - ) - - vals = request.get_values() - - for val in vals: - print("{} ".format(val.value), end="") - print()
bindings/python/examples/gpioinfo.py deleted +0 -28
@@ -1,28 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Simplified reimplementation of the gpioinfo tool in Python.""" - -import gpiod -import os - -from helpers import gpio_chips - -if __name__ == "__main__": - for chip in gpio_chips(): - cinfo = chip.get_info() - print("{} - {} lines:".format(cinfo.name, cinfo.num_lines)) - - for offset in range(0, cinfo.num_lines): - linfo = chip.get_line_info(offset) - is_input = linfo.direction == gpiod.line.Direction.INPUT - print( - "\tline {:>3}: {:>18} {:>12} {:>8} {:>10}".format( - linfo.offset, - linfo.name or "unnamed", - linfo.consumer or "unused", - "input" if is_input else "output", - "active-low" if linfo.active_low else "active-high", - ) - )
bindings/python/examples/gpiomon.py deleted +0 -26
@@ -1,26 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Simplified reimplementation of the gpiomon tool in Python.""" - -import gpiod -import sys - -from gpiod.line import Edge - -if __name__ == "__main__": - if len(sys.argv) < 3: - raise TypeError("usage: gpiomon.py <gpiochip> <offset1> <offset2> ...") - - path = sys.argv[1] - lines = [int(line) if line.isdigit() else line for line in sys.argv[2:]] - - with gpiod.request_lines( - path, - consumer="gpiomon.py", - config={tuple(lines): gpiod.LineSettings(edge_detection=Edge.BOTH)}, - ) as request: - while True: - for event in request.read_edge_events(): - print(event)
bindings/python/examples/gpionotify.py deleted +0 -21
@@ -1,21 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2023 Bartosz Golaszewski <[email protected]> - -"""Simplified reimplementation of the gpionotify tool in Python.""" - -import gpiod -import sys - -if __name__ == "__main__": - if len(sys.argv) < 3: - raise TypeError("usage: gpionotify.py <gpiochip> <offset1> <offset2> ...") - - path = sys.argv[1] - - with gpiod.Chip(path) as chip: - for line in sys.argv[2:]: - chip.watch_line_info(line) - - while True: - print(chip.read_info_event())
bindings/python/examples/gpioset.py deleted +0 -36
@@ -1,36 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Simplified reimplementation of the gpioset tool in Python.""" - -import gpiod -import sys - -from gpiod.line import Direction, Value - -if __name__ == "__main__": - if len(sys.argv) < 3: - raise TypeError( - "usage: gpioset.py <gpiochip> <offset1>=<value1> <offset2>=<value2> ..." - ) - - path = sys.argv[1] - - def parse_value(arg): - x, y = arg.split("=") - return (x, Value(int(y))) - - def make_settings(val): - return gpiod.LineSettings(direction=Direction.OUTPUT, output_value=val) - - lvs = [parse_value(arg) for arg in sys.argv[2:]] - config = dict((l, make_settings(v)) for (l, v) in lvs) - - request = gpiod.request_lines( - path, - consumer="gpioset.py", - config=config, - ) - - input()
bindings/python/examples/helpers.py deleted +0 -15
@@ -1,15 +0,0 @@ -# SPDX-License-Identifier: LGPL-2.1-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -import gpiod -import os - - -def gpio_chips(): - for path in [ - entry.path - for entry in os.scandir("/dev/") - if gpiod.is_gpiochip_device(entry.path) - ]: - with gpiod.Chip(path) as chip: - yield chip
bindings/python/examples/reconfigure_input_to_output.py new file +39 -0
@@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Example of a bi-directional line requested as input and then switched to output.""" + +import gpiod + +from gpiod.line import Direction, Value + + +def reconfigure_input_to_output(chip_path, line_offset): + # request the line initially as an input + with gpiod.request_lines( + chip_path, + consumer="reconfigure-input-to-output", + config={line_offset: gpiod.LineSettings(direction=Direction.INPUT)}, + ) as request: + # read the current line value + value = request.get_value(line_offset) + print("{}={} (input)".format(line_offset, value)) + # switch the line to an output and drive it low + request.reconfigure_lines( + config={ + line_offset: gpiod.LineSettings( + direction=Direction.OUTPUT, output_value=Value.INACTIVE + ) + } + ) + # report the current driven value + value = request.get_value(line_offset) + print("{}={} (output)".format(line_offset, value)) + + +if __name__ == "__main__": + try: + reconfigure_input_to_output("/dev/gpiochip0", 5) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")
bindings/python/examples/toggle_multiple_line_values.py new file +47 -0
47 lines not on this page · raw message
bindings/python/examples/watch_line_info.py new file +23 -0
@@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of watching for info changes on particular lines.""" + +import gpiod + + +def watch_line_info(chip_path, line_offsets): + with gpiod.Chip(chip_path) as chip: + for offset in line_offsets: + chip.watch_line_info(offset) + + while True: + print(chip.read_info_event()) + + +if __name__ == "__main__": + try: + watch_line_info("/dev/gpiochip0", [5, 3, 7]) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")
bindings/python/examples/watch_line_rising.py new file +31 -0
@@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of watching for rising edges on a single line.""" + +import gpiod + +from gpiod.line import Edge + + +def watch_line_rising(chip_path, line_offset): + with gpiod.request_lines( + chip_path, + consumer="watch-line-rising", + config={line_offset: gpiod.LineSettings(edge_detection=Edge.RISING)}, + ) as request: + while True: + # Blocks until at least one event is available + for event in request.read_edge_events(): + print( + "line: %d type: Rising event #%d" + % (event.line_offset, event.line_seqno) + ) + + +if __name__ == "__main__": + try: + watch_line_rising("/dev/gpiochip0", 5) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")
bindings/python/examples/watch_multiple_line_values.py new file +42 -0
42 lines not on this page · raw message
Signature · 1 line
2.41.0
Re: [libgpiod][PATCH 1/4] bindings: python: examples: replace tools examples with use case examples
Quoted 594 lines · Kent Gibson
Replace tool examples with use case examples drawn from the tools.
Signed-off-by Kent Gibson <[email protected]>--- bindings/python/examples/Makefile.am | 18 +++---- bindings/python/examples/find_line_by_name.py | 37 +++++++++++++++ bindings/python/examples/get_chip_info.py | 20 ++++++++ bindings/python/examples/get_line_info.py | 29 ++++++++++++ .../examples/get_multiple_line_values.py | 29 ++++++++++++ bindings/python/examples/gpiodetect.py | 15 ------ bindings/python/examples/gpiofind.py | 20 -------- bindings/python/examples/gpioget.py | 29 ------------ bindings/python/examples/gpioinfo.py | 28 ----------- bindings/python/examples/gpiomon.py | 26 ---------- bindings/python/examples/gpionotify.py | 21 --------- bindings/python/examples/gpioset.py | 36 -------------- bindings/python/examples/helpers.py | 15 ------ .../examples/reconfigure_input_to_output.py | 39 +++++++++++++++ .../examples/toggle_multiple_line_values.py | 47 +++++++++++++++++++ bindings/python/examples/watch_line_info.py | 23 +++++++++ bindings/python/examples/watch_line_rising.py | 31 ++++++++++++ .../examples/watch_multiple_line_values.py | 42 +++++++++++++++++ 18 files changed, 307 insertions(+), 198 deletions(-) create mode 100755 bindings/python/examples/find_line_by_name.py create mode 100755 bindings/python/examples/get_chip_info.py create mode 100755 bindings/python/examples/get_line_info.py create mode 100755 bindings/python/examples/get_multiple_line_values.py delete mode 100755 bindings/python/examples/gpiodetect.py delete mode 100755 bindings/python/examples/gpiofind.py delete mode 100755 bindings/python/examples/gpioget.py delete mode 100755 bindings/python/examples/gpioinfo.py delete mode 100755 bindings/python/examples/gpiomon.py delete mode 100755 bindings/python/examples/gpionotify.py delete mode 100755 bindings/python/examples/gpioset.py delete mode 100644 bindings/python/examples/helpers.py create mode 100755 bindings/python/examples/reconfigure_input_to_output.py create mode 100755 bindings/python/examples/toggle_multiple_line_values.py create mode 100755 bindings/python/examples/watch_line_info.py create mode 100755 bindings/python/examples/watch_line_rising.py create mode 100755 bindings/python/examples/watch_multiple_line_values.pybindings/python/examples/Makefile.am +10
-8@@ -3,13 +3,15 @@ EXTRA_DIST = \ async_watch_line_value.py \ + find_line_by_name.py \ + get_chip_info.py \ + get_line_info.py \ get_line_value.py \ - gpiodetect.py \ - gpiofind.py \ - gpioget.py \ - gpioinfo.py \ - gpiomon.py \ - gpionotify.py \ - gpioset.py \ + get_multiple_line_values.py \ + reconfigure_input_to_output.py \ toggle_line_value.py \ - watch_line_value.py + toggle_multiple_line_values.py \ + watch_line_requests.py \ + watch_line_value.py \ + watch_line_rising.py \ + watch_multiple_line_values.pybindings/python/examples/find_line_by_name.py new file +37
-0@@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""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")bindings/python/examples/get_chip_info.py new file +20
-0@@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of reading the info for a chip.""" + +import gpiod + + +def get_chip_info(chip_path): + with gpiod.Chip(chip_path) as chip: + info = chip.get_info() + print("{} [{}] ({} lines)".format(info.name, info.label, info.num_lines)) + + +if __name__ == "__main__": + try: + get_chip_info("/dev/gpiochip0") + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")bindings/python/examples/get_line_info.py new file +29
-0@@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of reading the info for a line.""" + +import gpiod + + +def get_line_info(chip_path, line_offset): + with gpiod.Chip(chip_path) as chip: + info = chip.get_line_info(line_offset) + is_input = info.direction == gpiod.line.Direction.INPUT + print( + "line {:>3}: {:>12} {:>12} {:>8} {:>10}".format( + info.offset, + info.name or "unnamed", + info.consumer or "unused", + "input" if is_input else "output", + "active-low" if info.active_low else "active-high", + ) + ) + + +if __name__ == "__main__": + try: + get_line_info("/dev/gpiochip0", 3) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")bindings/python/examples/get_multiple_line_values.py new file +29
-0@@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of reading multiple lines.""" + +import gpiod + +from gpiod.line import Direction + + +def get_multiple_line_values(chip_path, line_offsets): + with gpiod.request_lines( + chip_path, + consumer="get-multiple-line-values", + config={tuple(line_offsets): gpiod.LineSettings(direction=Direction.INPUT)}, + ) as request: + vals = request.get_values() + + for offset, val in zip(line_offsets, vals): + print("{}={} ".format(offset, val), end="") + print() + + +if __name__ == "__main__": + try: + get_multiple_line_values("/dev/gpiochip0", [5, 3, 7]) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")bindings/python/examples/gpiodetect.py deleted +0
-15@@ -1,15 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Reimplementation of the gpiodetect tool in Python.""" - -import gpiod -import os - -from helpers import gpio_chips - -if __name__ == "__main__": - for chip in gpio_chips(): - info = chip.get_info() - print("{} [{}] ({} lines)".format(info.name, info.label, info.num_lines))bindings/python/examples/gpiofind.py deleted +0
-20@@ -1,20 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Reimplementation of the gpiofind tool in Python.""" - -import gpiod -import os -import sys - -if __name__ == "__main__": - for entry in os.scandir("/dev/"): - if gpiod.is_gpiochip_device(entry.path): - with gpiod.Chip(entry.path) as chip: - offset = chip.line_offset_from_id(sys.argv[1]) - if offset is not None: - print("{} {}".format(chip.get_info().name, offset)) - sys.exit(0) - - sys.exit(1)bindings/python/examples/gpioget.py deleted +0
-29@@ -1,29 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Simplified reimplementation of the gpioget tool in Python.""" - -import gpiod -import sys - -from gpiod.line import Direction - -if __name__ == "__main__": - if len(sys.argv) < 3: - raise TypeError("usage: gpioget.py <gpiochip> <offset1> <offset2> ...") - - path = sys.argv[1] - lines = [int(line) if line.isdigit() else line for line in sys.argv[2:]] - - request = gpiod.request_lines( - path, - consumer="gpioget.py", - config={tuple(lines): gpiod.LineSettings(direction=Direction.INPUT)}, - ) - - vals = request.get_values() - - for val in vals: - print("{} ".format(val.value), end="") - print()bindings/python/examples/gpioinfo.py deleted +0
-28@@ -1,28 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Simplified reimplementation of the gpioinfo tool in Python.""" - -import gpiod -import os - -from helpers import gpio_chips - -if __name__ == "__main__": - for chip in gpio_chips(): - cinfo = chip.get_info() - print("{} - {} lines:".format(cinfo.name, cinfo.num_lines)) - - for offset in range(0, cinfo.num_lines): - linfo = chip.get_line_info(offset) - is_input = linfo.direction == gpiod.line.Direction.INPUT - print( - "\tline {:>3}: {:>18} {:>12} {:>8} {:>10}".format( - linfo.offset, - linfo.name or "unnamed", - linfo.consumer or "unused", - "input" if is_input else "output", - "active-low" if linfo.active_low else "active-high", - ) - )bindings/python/examples/gpiomon.py deleted +0
-26@@ -1,26 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Simplified reimplementation of the gpiomon tool in Python.""" - -import gpiod -import sys - -from gpiod.line import Edge - -if __name__ == "__main__": - if len(sys.argv) < 3: - raise TypeError("usage: gpiomon.py <gpiochip> <offset1> <offset2> ...") - - path = sys.argv[1] - lines = [int(line) if line.isdigit() else line for line in sys.argv[2:]] - - with gpiod.request_lines( - path, - consumer="gpiomon.py", - config={tuple(lines): gpiod.LineSettings(edge_detection=Edge.BOTH)}, - ) as request: - while True: - for event in request.read_edge_events(): - print(event)bindings/python/examples/gpionotify.py deleted +0
-21@@ -1,21 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2023 Bartosz Golaszewski <[email protected]> - -"""Simplified reimplementation of the gpionotify tool in Python.""" - -import gpiod -import sys - -if __name__ == "__main__": - if len(sys.argv) < 3: - raise TypeError("usage: gpionotify.py <gpiochip> <offset1> <offset2> ...") - - path = sys.argv[1] - - with gpiod.Chip(path) as chip: - for line in sys.argv[2:]: - chip.watch_line_info(line) - - while True: - print(chip.read_info_event())bindings/python/examples/gpioset.py deleted +0
-36@@ -1,36 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -"""Simplified reimplementation of the gpioset tool in Python.""" - -import gpiod -import sys - -from gpiod.line import Direction, Value - -if __name__ == "__main__": - if len(sys.argv) < 3: - raise TypeError( - "usage: gpioset.py <gpiochip> <offset1>=<value1> <offset2>=<value2> ..." - ) - - path = sys.argv[1] - - def parse_value(arg): - x, y = arg.split("=") - return (x, Value(int(y))) - - def make_settings(val): - return gpiod.LineSettings(direction=Direction.OUTPUT, output_value=val) - - lvs = [parse_value(arg) for arg in sys.argv[2:]] - config = dict((l, make_settings(v)) for (l, v) in lvs) - - request = gpiod.request_lines( - path, - consumer="gpioset.py", - config=config, - ) - - input()bindings/python/examples/helpers.py deleted +0
-15@@ -1,15 +0,0 @@ -# SPDX-License-Identifier: LGPL-2.1-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <[email protected]> - -import gpiod -import os - - -def gpio_chips(): - for path in [ - entry.path - for entry in os.scandir("/dev/") - if gpiod.is_gpiochip_device(entry.path) - ]: - with gpiod.Chip(path) as chip: - yield chipbindings/python/examples/reconfigure_input_to_output.py new file +39
-0@@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Example of a bi-directional line requested as input and then switched to output.""" + +import gpiod + +from gpiod.line import Direction, Value + + +def reconfigure_input_to_output(chip_path, line_offset): + # request the line initially as an input + with gpiod.request_lines( + chip_path, + consumer="reconfigure-input-to-output", + config={line_offset: gpiod.LineSettings(direction=Direction.INPUT)}, + ) as request: + # read the current line value + value = request.get_value(line_offset) + print("{}={} (input)".format(line_offset, value)) + # switch the line to an output and drive it low + request.reconfigure_lines( + config={ + line_offset: gpiod.LineSettings( + direction=Direction.OUTPUT, output_value=Value.INACTIVE + ) + } + ) + # report the current driven value + value = request.get_value(line_offset) + print("{}={} (output)".format(line_offset, value)) + + +if __name__ == "__main__": + try: + reconfigure_input_to_output("/dev/gpiochip0", 5) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")bindings/python/examples/toggle_multiple_line_values.py new file +47
-0@@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of toggling multiple lines.""" + +import gpiod +import time + +from gpiod.line import Direction, Value + + +def toggle_value(value): + if value == Value.INACTIVE: + return Value.ACTIVE + return Value.INACTIVE + + +def toggle_multiple_line_values(chip_path, line_values): + value_str = {Value.ACTIVE: "Active", Value.INACTIVE: "Inactive"} + + request = gpiod.request_lines( + chip_path, + consumer="toggle-multiple-line-values", + config={ + tuple(line_values.keys()): gpiod.LineSettings(direction=Direction.OUTPUT) + }, + output_values=line_values, + ) + + while True: + print( + " ".join("{}={}".format(l, value_str[v]) for (l, v) in line_values.items()) + ) + time.sleep(1) + for l, v in line_values.items(): + line_values[l] = toggle_value(v) + request.set_values(line_values) + + +if __name__ == "__main__": + try: + toggle_multiple_line_values( + "/dev/gpiochip0", {5: Value.ACTIVE, 3: Value.ACTIVE, 7: Value.INACTIVE} + ) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")bindings/python/examples/watch_line_info.py new file +23
-0@@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of watching for info changes on particular lines.""" + +import gpiod + + +def watch_line_info(chip_path, line_offsets): + with gpiod.Chip(chip_path) as chip: + for offset in line_offsets: + chip.watch_line_info(offset) + + while True: + print(chip.read_info_event()) + + +if __name__ == "__main__": + try: + watch_line_info("/dev/gpiochip0", [5, 3, 7]) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")bindings/python/examples/watch_line_rising.py new file +14
-0@@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of watching for rising edges on a single line.""" + +import gpiod + +from gpiod.line import Edge + + +def watch_line_rising(chip_path, line_offset): + with gpiod.request_lines( + chip_path,
bindings/python/examples/watch_line_rising.py line 24
Quoted 10 lines
bindings/python/examples/watch_line_rising.py new file +10
-0+ consumer="watch-line-rising", + config={line_offset: gpiod.LineSettings(edge_detection=Edge.RISING)}, + ) as request: + while True: + # Blocks until at least one event is available + for event in request.read_edge_events(): + print( + "line: %d type: Rising event #%d" + % (event.line_offset, event.line_seqno) + )
This still uses the old style formatting.
Quoted 21 lines
bindings/python/examples/watch_line_rising.py new file +7
-0+ + +if __name__ == "__main__": + try: + watch_line_rising("/dev/gpiochip0", 5) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")bindings/python/examples/watch_multiple_line_values.py new file +8
-0@@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of watching for edges on multiple lines.""" + +import gpiod +
bindings/python/examples/watch_multiple_line_values.py line 18
Quoted 10 lines
bindings/python/examples/watch_multiple_line_values.py new file +10
-0+from gpiod.line import Edge + + +def edge_type(event): + if event.event_type is event.Type.RISING_EDGE: + return "Rising" + if event.event_type is event.Type.FALLING_EDGE: + return "Falling" + return "Unknown" +
As there'll be another iteration, maybe change this to get_edge_type() or get_edge_event_type_str()? I have a preference for function names to reflect the action in general.
Bart
Quoted 27 lines
bindings/python/examples/watch_multiple_line_values.py new file +24
-1+ +def watch_multiple_line_values(chip_path, line_offsets): + with gpiod.request_lines( + chip_path, + consumer="watch-multiple-line-values", + config={tuple(line_offsets): gpiod.LineSettings(edge_detection=Edge.BOTH)}, + ) as request: + while True: + for event in request.read_edge_events(): + print( + "offset: {} type: {:<7} event #{} line event #{}".format( + event.line_offset, + edge_type(event), + event.global_seqno, + event.line_seqno, + ) + ) + + +if __name__ == "__main__": + try: + watch_multiple_line_values("/dev/gpiochip0", [5, 3, 7]) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation") --2.41.0
Re: [libgpiod][PATCH 1/4] bindings: python: examples: replace tools examples with use case examples
Quoted 11 lines · Bartosz Golaszewski
bindings/python/examples/watch_line_rising.py line 24
Quoted 9 lines · Kent Gibson
Replace tool examples with use case examples drawn from the tools.
bindings/python/examples/watch_line_rising.py new file +7
-0+ while True: + # Blocks until at least one event is available + for event in request.read_edge_events(): + print( + "line: %d type: Rising event #%d" + % (event.line_offset, event.line_seqno) + )
Quoted 2 lines
This still uses the old style formatting.
Well spotted.
I only fixed the one you pointed out - didn't even consider that there could be others. Turns out there are a few - probably all cut-and-paste.
Quoted 27 lines
Quoted 27 lines
bindings/python/examples/watch_line_rising.py new file +7
-0+ + +if __name__ == "__main__": + try: + watch_line_rising("/dev/gpiochip0", 5) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")bindings/python/examples/watch_multiple_line_values.py new file +14
-0@@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of watching for edges on multiple lines.""" + +import gpiod + +from gpiod.line import Edge + + +def edge_type(event): + if event.event_type is event.Type.RISING_EDGE: + return "Rising"
Quoted 9 lines
bindings/python/examples/watch_multiple_line_values.py line 18
Quoted 4 lines
bindings/python/examples/watch_multiple_line_values.py new file +4
-0+ if event.event_type is event.Type.FALLING_EDGE: + return "Falling" + return "Unknown" +As there'll be another iteration, maybe change this to
get_edge_type()orget_edge_event_type_str()? I have a preference for function names to reflect the action in general.
Yeah, I use _str elsewhere, not sure why I didn't here.
What an I supposed to resend the update as given the remainder of the series was merged - v3??
Cheers,
Kent.
Re: [libgpiod][PATCH 1/4] bindings: python: examples: replace tools examples with use case examples
Quoted 58 lines · Kent Gibson
Quoted 11 lines · Bartosz Golaszewski
bindings/python/examples/watch_line_rising.py line 24
Quoted 9 lines · Kent Gibson
Replace tool examples with use case examples drawn from the tools.
bindings/python/examples/watch_line_rising.py new file +7
-0+ while True: + # Blocks until at least one event is available + for event in request.read_edge_events(): + print( + "line: %d type: Rising event #%d" + % (event.line_offset, event.line_seqno) + )Quoted 2 lines
This still uses the old style formatting.
Well spotted.
I only fixed the one you pointed out - didn't even consider that there could be others. Turns out there are a few - probably all cut-and-paste.Quoted 27 lines
Quoted 27 lines
bindings/python/examples/watch_line_rising.py new file +7
-0+ + +if __name__ == "__main__": + try: + watch_line_rising("/dev/gpiochip0", 5) + except OSError as ex: + print(ex, "\nCustomise the example configuration to suit your situation")bindings/python/examples/watch_multiple_line_values.py new file +14
-0@@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> + +"""Minimal example of watching for edges on multiple lines.""" + +import gpiod + +from gpiod.line import Edge + + +def edge_type(event): + if event.event_type is event.Type.RISING_EDGE: + return "Rising"Quoted 9 lines
bindings/python/examples/watch_multiple_line_values.py line 18
Quoted 4 lines
bindings/python/examples/watch_multiple_line_values.py new file +4
-0+ if event.event_type is event.Type.FALLING_EDGE: + return "Falling" + return "Unknown" +As there'll be another iteration, maybe change this to
get_edge_type()orget_edge_event_type_str()? I have a preference for function names to reflect the action in general.
Quoted 5 lines
Yeah, I use
_strelsewhere, not sure why I didn't here.What an I supposed to resend the update as given the remainder of the series was merged - v3??
Just this patch marked as v3 is fine.
Bart
Quoted 2 lines
Cheers,
Kent.
PATCH 2/4 core: examples: fix file comments
In reply to Kent GibsonSome of the file comments are cut-and-paste errors, so replace them with the correct comment.
--- examples/async_watch_line_value.c | 2 +- examples/get_multiple_line_values.c | 2 +- examples/reconfigure_input_to_output.c | 5 ++++- examples/watch_multiple_line_values.c | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-)
examples/async_watch_line_value.c +1 -1
@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> -/* Minimal example of asynchronously watching for edges on a single line */ +/* Minimal example of asynchronously watching for edges on a single line. */ #include <errno.h> #include <gpiod.h>
examples/get_multiple_line_values.c +1 -1
@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> -/* Minimal example of reading a single line. */ +/* Minimal example of reading multiple lines. */ #include <errno.h> #include <gpiod.h>
examples/reconfigure_input_to_output.c +4 -1
@@ -1,7 +1,10 @@// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> -/* Minimal example of reading a single line. */ +/* +* Example of a bi-directional line requested as input and then switched +* to output. +*/ #include <errno.h> #include <gpiod.h>
examples/watch_multiple_line_values.c +1 -1
@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> -/* Minimal example of watching for edges on a single line. */ +/* Minimal example of watching for edges on multiple lines. */ #include <errno.h> #include <gpiod.h>
Signature · 1 line
2.41.0
Re: [libgpiod][PATCH 2/4] core: examples: fix file comments
Quoted 68 lines · Kent Gibson
Some of the file comments are cut-and-paste errors, so replace them with the correct comment.
Signed-off-by Kent Gibson <[email protected]>--- examples/async_watch_line_value.c | 2 +- examples/get_multiple_line_values.c | 2 +- examples/reconfigure_input_to_output.c | 5 ++++- examples/watch_multiple_line_values.c | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-)examples/async_watch_line_value.c +1
-1@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> -/* Minimal example of asynchronously watching for edges on a single line */ +/* Minimal example of asynchronously watching for edges on a single line. */ #include <errno.h> #include <gpiod.h> examples/get_multiple_line_values.c +1
-1@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> -/* Minimal example of reading a single line. */ +/* Minimal example of reading multiple lines. */ #include <errno.h> #include <gpiod.h> examples/reconfigure_input_to_output.c +4
-1@@ -1,7 +1,10 @@// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> -/* Minimal example of reading a single line. */ +/* +* Example of a bi-directional line requested as input and then switched +* to output. +*/ #include <errno.h> #include <gpiod.h> examples/watch_multiple_line_values.c +1
-2@@ -1,7 +1,7 @@// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> -/* Minimal example of watching for edges on a single line. */ +/* Minimal example of watching for edges on multiple lines. */ #include <errno.h> #include <gpiod.h> --
Quoted 2 lines
2.41.0
I applied this and patches 3 and 4 as well. I fixed the whitespace error reported by git in this one.
Bart
PATCH 3/4 cxx: examples: file comment consistency
In reply to Kent GibsonMake the file comment for reconfigure_input_to_output consistent with other language examples.
--- bindings/cxx/examples/reconfigure_input_to_output.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
bindings/cxx/examples/reconfigure_input_to_output.cpp +2 -2
@@ -2,8 +2,8 @@ // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> /* - * Minimal example of requesting a line intended for bi-directional use as - * input and then switching to output. + * Example of a bi-directional line requested as input and then switched + * to output. */ #include <cstdlib>
Signature · 1 line
2.41.0
PATCH 4/4 rust: examples: file comment consistency
In reply to Kent GibsonMake the file comment for reconfigure_input_to_output consistent with other language examples.
--- bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs +1 -1
@@ -1,7 +1,7 @@// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause // SPDX-FileCopyrightText: 2023 Kent Gibson <[email protected]> // -// Minimal example of requesting a line intended for bi-directional use as input and then switching to output. +// Example of a bi-directional line requested as input and then switched to output. use libgpiod::line;
Signature · 1 line
2.41.0