diff mbox series

[libgpiod,1/2] bindings: python: examples: fix potential glitch in gpioset.py

Message ID 20230609153607.133379-2-warthog618@gmail.com
State New
Headers show
Series fix potential glitches in bindings example gpiosets | expand

Commit Message

Kent Gibson June 9, 2023, 3:36 p.m. UTC
gpioset.py requests lines without setting their output value, and so
sets them all inactive, and subsequently sets them to their requested
value.  This can result in glitches on lines which were active and
are set active.

As this is example code, it is also important to demonstrate that the
output value can be set by the request itself.

Request the lines with the correct output values set in the request
itself.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
 bindings/python/examples/gpioset.py | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Comments

Bartosz Golaszewski June 12, 2023, 2:27 p.m. UTC | #1
On Fri, Jun 9, 2023 at 5:36 PM Kent Gibson <warthog618@gmail.com> wrote:
>
> gpioset.py requests lines without setting their output value, and so
> sets them all inactive, and subsequently sets them to their requested
> value.  This can result in glitches on lines which were active and
> are set active.
>
> As this is example code, it is also important to demonstrate that the
> output value can be set by the request itself.
>
> Request the lines with the correct output values set in the request
> itself.
>
> Signed-off-by: Kent Gibson <warthog618@gmail.com>
> ---
>  bindings/python/examples/gpioset.py | 10 +++++-----
>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/bindings/python/examples/gpioset.py b/bindings/python/examples/gpioset.py
> index 372a9a8..b36b376 100755
> --- a/bindings/python/examples/gpioset.py
> +++ b/bindings/python/examples/gpioset.py
> @@ -21,16 +21,16 @@ if __name__ == "__main__":
>          x, y = arg.split("=")
>          return (x, Value(int(y)))
>
> +    def settings(v):

I renamed this to make_settings() when applying. Thanks.

Bart

> +        return gpiod.LineSettings(direction=Direction.OUTPUT, output_value=v)
> +
>      lvs = [parse_value(arg) for arg in sys.argv[2:]]
> -    lines = [x[0] for x in lvs]
> -    values = dict(lvs)
> +    config = dict([(l, settings(v)) for (l, v) in lvs])
>
>      request = gpiod.request_lines(
>          path,
>          consumer="gpioset.py",
> -        config={tuple(lines): gpiod.LineSettings(direction=Direction.OUTPUT)},
> +        config=config,
>      )
>
> -    vals = request.set_values(values)
> -
>      input()
> --
> 2.40.1
>
diff mbox series

Patch

diff --git a/bindings/python/examples/gpioset.py b/bindings/python/examples/gpioset.py
index 372a9a8..b36b376 100755
--- a/bindings/python/examples/gpioset.py
+++ b/bindings/python/examples/gpioset.py
@@ -21,16 +21,16 @@  if __name__ == "__main__":
         x, y = arg.split("=")
         return (x, Value(int(y)))
 
+    def settings(v):
+        return gpiod.LineSettings(direction=Direction.OUTPUT, output_value=v)
+
     lvs = [parse_value(arg) for arg in sys.argv[2:]]
-    lines = [x[0] for x in lvs]
-    values = dict(lvs)
+    config = dict([(l, settings(v)) for (l, v) in lvs])
 
     request = gpiod.request_lines(
         path,
         consumer="gpioset.py",
-        config={tuple(lines): gpiod.LineSettings(direction=Direction.OUTPUT)},
+        config=config,
     )
 
-    vals = request.set_values(values)
-
     input()