From patchwork Sat Jun 24 05:20:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 696067 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EF54BEB64DC for ; Sat, 24 Jun 2023 05:21:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229653AbjFXFVS (ORCPT ); Sat, 24 Jun 2023 01:21:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229964AbjFXFVQ (ORCPT ); Sat, 24 Jun 2023 01:21:16 -0400 Received: from mail-oi1-x22a.google.com (mail-oi1-x22a.google.com [IPv6:2607:f8b0:4864:20::22a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84D372733 for ; Fri, 23 Jun 2023 22:21:14 -0700 (PDT) Received: by mail-oi1-x22a.google.com with SMTP id 5614622812f47-38c35975545so1118417b6e.1 for ; Fri, 23 Jun 2023 22:21:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687584073; x=1690176073; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Q0XxIdKP6YSPnKmbL1Qrle97TLgnfbHKq1CXdrdENFM=; b=G8FXGK0P96oryfpIglC1Bb85bt4TYw+cDXSJ2IZas1Bj5A3Yy64znjuAeaAFhSZ7P2 WZ0Fry/kNWDHgcZO6bu99lAp7zTDJGEpEWaqo9cTMdN4Q2kcgJtJIvfLzHPD1qx36Gy1 2ZaribO9QpsUmLepdYHcvPjFFB85b+ZTI5IElgfIetwi7QLXGdGchC+PXKp5wmWA7UTL c4/b9HNz7m3I4YOjwghwvrWAb3gMvgCoW9c05X7NxlGCdmAeURivsPXdh2jHa0MtLsDc sqsZAtAxIvfavZ+MYdnA7dqOsSC/4hbdYGQXtytIo+OvBQXpSJP3VitYBTij7gUDMxw+ Ui6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687584073; x=1690176073; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Q0XxIdKP6YSPnKmbL1Qrle97TLgnfbHKq1CXdrdENFM=; b=YUspVRxT7Mq3pF6cw4YcycPaNL82saIMCLMMUduAlBpwALn5PsAeeXoxyB+vobib6/ oW00/eGGT3wg7auRCh27M3NcEZDEoRt2jZxcPM3QoEAngpY8rhXG1U3H0DObnhqyGFWM ivJEx0G6h8A8mKGakPRI0kWsqtAyHsAQsdDGUFOOzZosNUGmmsKJv4NeiWFF1lElOS1o PX9sEpi/MGylCj7OW9VpPJ2g/4q4IlmQEmx9L12TJ43PGpGN6+sF5zmp3t1N9uruy6a4 zyqqL3XRQSYc7BSFMWiHTI7CvQQYG7c9dATElCiL2KHTlg6f3wWAU/5K0RK4D3ckTNw2 0bdA== X-Gm-Message-State: AC+VfDwW6tPsVqjHiKheIRbV0rtXjJfwx8tgUnIpaiC7Le6RJ/z3GkCG YsFJoFEKYFSAHMnx9i3wODEB/YePvf4= X-Google-Smtp-Source: ACHHUZ5V2uV3/NPMFLYfMEPIHch4pkG9qSYcvJkHdPTd7CHPRpGw4L5eOPQf4hN/WLDLne9jp6i4OQ== X-Received: by 2002:a05:6808:3af:b0:39a:6c25:d60 with SMTP id n15-20020a05680803af00b0039a6c250d60mr14118100oie.29.1687584073378; Fri, 23 Jun 2023 22:21:13 -0700 (PDT) Received: from sol.home.arpa (194-223-178-180.tpgi.com.au. [194.223.178.180]) by smtp.gmail.com with ESMTPSA id gf4-20020a17090ac7c400b00256dff5f8e3sm487103pjb.49.2023.06.23.22.21.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 22:21:13 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod][PATCH 1/4] bindings: python: examples: replace tools examples with use case examples Date: Sat, 24 Jun 2023 13:20:51 +0800 Message-ID: <20230624052054.13206-2-warthog618@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230624052054.13206-1-warthog618@gmail.com> References: <20230624052054.13206-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Replace tool examples with use case examples drawn from the tools. Signed-off-by: Kent Gibson --- 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 diff --git a/bindings/python/examples/Makefile.am b/bindings/python/examples/Makefile.am index c8c1c98..52abafc 100644 --- a/bindings/python/examples/Makefile.am +++ b/bindings/python/examples/Makefile.am @@ -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 diff --git a/bindings/python/examples/find_line_by_name.py b/bindings/python/examples/find_line_by_name.py new file mode 100755 index 0000000..ac798a9 --- /dev/null +++ b/bindings/python/examples/find_line_by_name.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson + +"""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") diff --git a/bindings/python/examples/get_chip_info.py b/bindings/python/examples/get_chip_info.py new file mode 100755 index 0000000..7dc76fb --- /dev/null +++ b/bindings/python/examples/get_chip_info.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson + +"""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") diff --git a/bindings/python/examples/get_line_info.py b/bindings/python/examples/get_line_info.py new file mode 100755 index 0000000..cd4ebcc --- /dev/null +++ b/bindings/python/examples/get_line_info.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson + +"""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") diff --git a/bindings/python/examples/get_multiple_line_values.py b/bindings/python/examples/get_multiple_line_values.py new file mode 100755 index 0000000..46cf0b2 --- /dev/null +++ b/bindings/python/examples/get_multiple_line_values.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson + +"""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") diff --git a/bindings/python/examples/gpiodetect.py b/bindings/python/examples/gpiodetect.py deleted file mode 100755 index dc98b03..0000000 --- a/bindings/python/examples/gpiodetect.py +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski - -"""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)) diff --git a/bindings/python/examples/gpiofind.py b/bindings/python/examples/gpiofind.py deleted file mode 100755 index d41660d..0000000 --- a/bindings/python/examples/gpiofind.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski - -"""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) diff --git a/bindings/python/examples/gpioget.py b/bindings/python/examples/gpioget.py deleted file mode 100755 index bf7e0a6..0000000 --- a/bindings/python/examples/gpioget.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski - -"""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 ...") - - 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() diff --git a/bindings/python/examples/gpioinfo.py b/bindings/python/examples/gpioinfo.py deleted file mode 100755 index 3996dcf..0000000 --- a/bindings/python/examples/gpioinfo.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski - -"""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", - ) - ) diff --git a/bindings/python/examples/gpiomon.py b/bindings/python/examples/gpiomon.py deleted file mode 100755 index 702d7c8..0000000 --- a/bindings/python/examples/gpiomon.py +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski - -"""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 ...") - - 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) diff --git a/bindings/python/examples/gpionotify.py b/bindings/python/examples/gpionotify.py deleted file mode 100755 index 4e50515..0000000 --- a/bindings/python/examples/gpionotify.py +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2023 Bartosz Golaszewski - -"""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 ...") - - 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()) diff --git a/bindings/python/examples/gpioset.py b/bindings/python/examples/gpioset.py deleted file mode 100755 index e6a516c..0000000 --- a/bindings/python/examples/gpioset.py +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: GPL-2.0-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski - -"""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 = = ..." - ) - - 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() diff --git a/bindings/python/examples/helpers.py b/bindings/python/examples/helpers.py deleted file mode 100644 index 8b91173..0000000 --- a/bindings/python/examples/helpers.py +++ /dev/null @@ -1,15 +0,0 @@ -# SPDX-License-Identifier: LGPL-2.1-or-later -# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski - -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 diff --git a/bindings/python/examples/reconfigure_input_to_output.py b/bindings/python/examples/reconfigure_input_to_output.py new file mode 100755 index 0000000..0f2e358 --- /dev/null +++ b/bindings/python/examples/reconfigure_input_to_output.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson + +"""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") diff --git a/bindings/python/examples/toggle_multiple_line_values.py b/bindings/python/examples/toggle_multiple_line_values.py new file mode 100755 index 0000000..12b968d --- /dev/null +++ b/bindings/python/examples/toggle_multiple_line_values.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson + +"""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") diff --git a/bindings/python/examples/watch_line_info.py b/bindings/python/examples/watch_line_info.py new file mode 100755 index 0000000..e49a669 --- /dev/null +++ b/bindings/python/examples/watch_line_info.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson + +"""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") diff --git a/bindings/python/examples/watch_line_rising.py b/bindings/python/examples/watch_line_rising.py new file mode 100755 index 0000000..2350d76 --- /dev/null +++ b/bindings/python/examples/watch_line_rising.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson + +"""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") diff --git a/bindings/python/examples/watch_multiple_line_values.py b/bindings/python/examples/watch_multiple_line_values.py new file mode 100755 index 0000000..658ecee --- /dev/null +++ b/bindings/python/examples/watch_multiple_line_values.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Kent Gibson + +"""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" + if event.event_type is event.Type.FALLING_EDGE: + return "Falling" + return "Unknown" + + +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") From patchwork Sat Jun 24 05:20:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 696708 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61885EB64DC for ; Sat, 24 Jun 2023 05:21:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229566AbjFXFV0 (ORCPT ); Sat, 24 Jun 2023 01:21:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229964AbjFXFVZ (ORCPT ); Sat, 24 Jun 2023 01:21:25 -0400 Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 594182703 for ; Fri, 23 Jun 2023 22:21:24 -0700 (PDT) Received: by mail-qk1-x72a.google.com with SMTP id af79cd13be357-763e1a22a68so136491885a.0 for ; Fri, 23 Jun 2023 22:21:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687584083; x=1690176083; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xeO/fSjwOcNHte1KOPNT2lkhwp7i8fzWFDqSWAjDNis=; b=BO2dE+se2TCxQvHeNKSl1vxmvShUVK4GmWPT6MtFbP75LPD4z4bKMzrJxKJeYrDCw2 EutbGKszyc1czTzwgTLl4UZZ3xtDqpZk2/UO/z7oSzssyf24jiP7WwglM2DUfTs3HN58 AwL7UKKQ+bwPA32tywI0AbaFwiPbuXEtXNtMi6zxCwXojl1BCORTBz9TWgmjHp5rnrfp nMSRy72WU+X6bKi+q+q3vcrNHw7/1XwH6ylfJVFvmBGvEvgiCCWONz6OoAOYkbHH/odD LpAKVChFokqgJn7MSn73Tr5WoE4rei0bezrl031fKdxmNQZCkTQZt5F7+Nt82HBmEIo2 LNQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687584083; x=1690176083; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xeO/fSjwOcNHte1KOPNT2lkhwp7i8fzWFDqSWAjDNis=; b=JULnFRpHYIiJMdu9f+fExjoKNHaTCxuH6kyzAHy029MkeXoG9vgLfLQy6EsIe23Nf/ bBOlgodwLl5cKRceJ5mMy6zPxQuTGaF5Me9XjZC9C8NNtuWfv8Sr+s9GLvbKX7aTbAoc su+amTwbsoVZTupfo0M+w5vgvb8aOvBRXmqYilWH5Pn8w+r7GCLDFm1kt+bPsB6G8mgk 4EM0cgWwh/C5BVKzaS+o3wNO+zPPdUryz5RPKuBfBI8VBGrNyIJky95LvgGCnLSW+02U GUTAOLRePPHNtFN7yVYGXpuCpSpcTE/DzStD9KwXX984eyjKCA56j+++b7GpdDZHmKKX Y4Rw== X-Gm-Message-State: AC+VfDziLT5PNSlFxZWAToA58uM+OCOKMhBzFJ3YI+G1cAiS0xMkuAES 5UyAl3a/Lt64oy1ZqSdx0uD867WOU9o= X-Google-Smtp-Source: ACHHUZ5+ZAx1qqz/iAJXlf8APvbJ/qilc4my/ve1EqrVpN2+f+T5PZ4h9TN8pBxOsBeBhfIbH0tmLg== X-Received: by 2002:a05:620a:4154:b0:762:27b:894c with SMTP id k20-20020a05620a415400b00762027b894cmr17907629qko.8.1687584083255; Fri, 23 Jun 2023 22:21:23 -0700 (PDT) Received: from sol.home.arpa (194-223-178-180.tpgi.com.au. [194.223.178.180]) by smtp.gmail.com with ESMTPSA id gf4-20020a17090ac7c400b00256dff5f8e3sm487103pjb.49.2023.06.23.22.21.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 22:21:22 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod][PATCH 2/4] core: examples: fix file comments Date: Sat, 24 Jun 2023 13:20:52 +0800 Message-ID: <20230624052054.13206-3-warthog618@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230624052054.13206-1-warthog618@gmail.com> References: <20230624052054.13206-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Some of the file comments are cut-and-paste errors, so replace them with the correct comment. Signed-off-by: Kent Gibson --- 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(-) diff --git a/examples/async_watch_line_value.c b/examples/async_watch_line_value.c index f35fb1a..8b1d643 100644 --- a/examples/async_watch_line_value.c +++ b/examples/async_watch_line_value.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson -/* Minimal example of asynchronously watching for edges on a single line */ +/* Minimal example of asynchronously watching for edges on a single line. */ #include #include diff --git a/examples/get_multiple_line_values.c b/examples/get_multiple_line_values.c index b16c570..c6df3f6 100644 --- a/examples/get_multiple_line_values.c +++ b/examples/get_multiple_line_values.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson -/* Minimal example of reading a single line. */ +/* Minimal example of reading multiple lines. */ #include #include diff --git a/examples/reconfigure_input_to_output.c b/examples/reconfigure_input_to_output.c index e8fbb1c..abfaf79 100644 --- a/examples/reconfigure_input_to_output.c +++ b/examples/reconfigure_input_to_output.c @@ -1,7 +1,10 @@ // SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson -/* Minimal example of reading a single line. */ +/* + * Example of a bi-directional line requested as input and then switched + * to output. + */ #include #include diff --git a/examples/watch_multiple_line_values.c b/examples/watch_multiple_line_values.c index 8015270..e955b2c 100644 --- a/examples/watch_multiple_line_values.c +++ b/examples/watch_multiple_line_values.c @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later // SPDX-FileCopyrightText: 2023 Kent Gibson -/* Minimal example of watching for edges on a single line. */ +/* Minimal example of watching for edges on multiple lines. */ #include #include From patchwork Sat Jun 24 05:20:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 696066 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 001BEEB64DC for ; Sat, 24 Jun 2023 05:21:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229975AbjFXFVg (ORCPT ); Sat, 24 Jun 2023 01:21:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229964AbjFXFVf (ORCPT ); Sat, 24 Jun 2023 01:21:35 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65438272C for ; Fri, 23 Jun 2023 22:21:34 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-25ecc896007so927805a91.3 for ; Fri, 23 Jun 2023 22:21:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687584093; x=1690176093; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0GLKygSoQjw70yBPG5IlT9IoMODEHnEu/LvGJFjSepg=; b=mzNKlqFoXorEQUa6lD6RT+NO+g82GdJ252aQYOzFBYmhFAQH5KdvIueI269AX1j+C0 Rf9M/84BosMf8MPFHU1SbZ2VoGMm8D7+MXS9Yd2ICGkcsReL6lTqzYsys1NrTPcOYQJH vmiQWPy4ITogGcBqrprshUSC5VrSEUOz0GSFfCI+0WY84gZ20Oro7uZvrDFAxaqJgA4X zOkrdrpfKSV3NsRwqqvKKf4uAorV4uwRZbJJjrxdfb4D9Ba6utwn6AXBsEK+ieLTXb6K lSmAtvnyYkmi7UHt0yDXWJz4z4Ya2f861Va3Y3yxxlTAzXJvidyUjRyE/16Wj9qViweC Se+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687584093; x=1690176093; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0GLKygSoQjw70yBPG5IlT9IoMODEHnEu/LvGJFjSepg=; b=YcS9daNqU4ilMI8SRa3co0/L61Jxr20RFf/+25PFqSeQs1ECtDL6ScWUMYaj64a520 4J/Xe8SniiU+PN1z3mrlUHc/CU4fgQ5E42DySBOLBBEkSSYIs/p+zfnMflZxMOFayaE4 yBHKBLeW6dL7b3UGTTHzaoz2/xTNtgjYjClU59XwLPMVM7zjW7Ej5Kd35O6AFARzaDuq +Vn+C57YfBJcr9xwcqKAoUHDkIxS9Cxs/lZZylrlxcDzHtdpHnHxr74hHkFzXkiDcR+6 uOyHoaMT3pV4GkwsDl+ynge5w0cpnMF5PGoq9wqFyK5Lb50zbZdngV6B+olsjUm5th5b beqA== X-Gm-Message-State: AC+VfDzW4huIXqKV3+yfk/ONyu5g8VgqZOwHzocsRSr/sWhqtIWfEPDV nm0qZL4YN61EQRa5ua1gS44rMxbqCeM= X-Google-Smtp-Source: ACHHUZ42rUTUvkucTfIAJMkvvLE/i8pdEsYMLv1S+bCrdTymSQIkU4ffpwRxxYsFGwQN8681JQbKzg== X-Received: by 2002:a17:90a:dac5:b0:25e:9aa6:2dfc with SMTP id g5-20020a17090adac500b0025e9aa62dfcmr22106420pjx.40.1687584093505; Fri, 23 Jun 2023 22:21:33 -0700 (PDT) Received: from sol.home.arpa (194-223-178-180.tpgi.com.au. [194.223.178.180]) by smtp.gmail.com with ESMTPSA id gf4-20020a17090ac7c400b00256dff5f8e3sm487103pjb.49.2023.06.23.22.21.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 22:21:33 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod][PATCH 3/4] cxx: examples: file comment consistency Date: Sat, 24 Jun 2023 13:20:53 +0800 Message-ID: <20230624052054.13206-4-warthog618@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230624052054.13206-1-warthog618@gmail.com> References: <20230624052054.13206-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Make the file comment for reconfigure_input_to_output consistent with other language examples. Signed-off-by: Kent Gibson --- bindings/cxx/examples/reconfigure_input_to_output.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/cxx/examples/reconfigure_input_to_output.cpp b/bindings/cxx/examples/reconfigure_input_to_output.cpp index 7b52dab..896c812 100644 --- a/bindings/cxx/examples/reconfigure_input_to_output.cpp +++ b/bindings/cxx/examples/reconfigure_input_to_output.cpp @@ -2,8 +2,8 @@ // SPDX-FileCopyrightText: 2023 Kent Gibson /* - * 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 From patchwork Sat Jun 24 05:20:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kent Gibson X-Patchwork-Id: 696707 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3991AEB64DA for ; Sat, 24 Jun 2023 05:21:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230009AbjFXFVq (ORCPT ); Sat, 24 Jun 2023 01:21:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229709AbjFXFVp (ORCPT ); Sat, 24 Jun 2023 01:21:45 -0400 Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DBF5E67 for ; Fri, 23 Jun 2023 22:21:44 -0700 (PDT) Received: by mail-qk1-x72e.google.com with SMTP id af79cd13be357-763ddc4259dso124364785a.3 for ; Fri, 23 Jun 2023 22:21:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1687584103; x=1690176103; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=E6XBOnIPfK/D7TyWjfv848szEgbB/yUubSzUaHVR0ZQ=; b=nits95aQgbKC0Uu+jDD3NVxllqc33QN/6aUoq+vnAuXPvb0bFLMr6ofdbWuy+mieK1 sLeATQsYU7w3m8U+HXKzA/i2hkhDu2ieJytAYTDor3dXS81eH6XVT/AQ9VSksCFvX3Wt DbabpkxVOaGv6aFpXO1bVojCvxUmqkHTz/803NCkayoOiezaiEQsh32rwnr0U3UoT5rj uLljxXva+UL0+/5eCT/iWR2E/OJ7tWwbKhqaTewb0MuMZrreqexrif1Nh+n5XzsFwuso N3Yz2j0PuA+CDIuXZVwXLJevMtScliPDBth//wrl4HxCbiCIJb/gAikm7c6WPs5N6CVA cgCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687584103; x=1690176103; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=E6XBOnIPfK/D7TyWjfv848szEgbB/yUubSzUaHVR0ZQ=; b=LzdA08LoAP/vUHyMjOjmnt/t3EnaoQ7wnMGoVgwlY1nBNDsV+zp7CqAx842+1aaL7J Nk1fW7qfpH8EKUuPE2nNXnGNQdbICJ5apVgwNMD1yTfp3xhZgK76CxCL1nLsqWGpOb9Z UtUnnSMja4ddEGvBm57eQQSpgMmFzGDyRB3CXqUlCCoW/5jeHqeXjaShboOFZ3pm3Che 8RMC7PJLY/3/pTU4crRriX8D2Z24xVwtez0RwGFLB/B9y3ZB5eCRvPNawbFxKeH8K90j TZW7TK/PwvkySRdUVcANeCtFfKNhbGy1OUVHgnPAX7rATzZMO5IhPqpXQA2ySbc7llvK sJpA== X-Gm-Message-State: AC+VfDwjVxD4RRbI+7k/kHgHTWFQ3MV3xTy+yMs8V14f8lNGyjdr8gyc eMSt/p0GciacxXm3DE+FCWN1d8CiQcc= X-Google-Smtp-Source: ACHHUZ4vnOiBFx9mEoIILQ4qBqgiKC3yhrgWvID4Nv39m3q8O+Cc2SdhgCLqagTsjxFJka3bvYupFw== X-Received: by 2002:a05:620a:8f07:b0:763:643c:d6e7 with SMTP id rh7-20020a05620a8f0700b00763643cd6e7mr6760106qkn.47.1687584103639; Fri, 23 Jun 2023 22:21:43 -0700 (PDT) Received: from sol.home.arpa (194-223-178-180.tpgi.com.au. [194.223.178.180]) by smtp.gmail.com with ESMTPSA id gf4-20020a17090ac7c400b00256dff5f8e3sm487103pjb.49.2023.06.23.22.21.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Jun 2023 22:21:43 -0700 (PDT) From: Kent Gibson To: linux-gpio@vger.kernel.org, brgl@bgdev.pl Cc: Kent Gibson Subject: [libgpiod][PATCH 4/4] rust: examples: file comment consistency Date: Sat, 24 Jun 2023 13:20:54 +0800 Message-ID: <20230624052054.13206-5-warthog618@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230624052054.13206-1-warthog618@gmail.com> References: <20230624052054.13206-1-warthog618@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Make the file comment for reconfigure_input_to_output consistent with other language examples. Signed-off-by: Kent Gibson --- bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs b/bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs index 3e2f2c7..fb5402b 100644 --- a/bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs +++ b/bindings/rust/libgpiod/examples/reconfigure_input_to_output.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause // SPDX-FileCopyrightText: 2023 Kent Gibson // -// 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;