linux-gpio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: linux-gpio@vger.kernel.org, brgl@bgdev.pl
Cc: Kent Gibson <warthog618@gmail.com>
Subject: [libgpiod][PATCH 2/2] bindings: cxx: examples: fix potential glitch in gpiosetcxx
Date: Fri,  9 Jun 2023 23:36:07 +0800	[thread overview]
Message-ID: <20230609153607.133379-3-warthog618@gmail.com> (raw)
In-Reply-To: <20230609153607.133379-1-warthog618@gmail.com>

gpiosetcxx 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/cxx/examples/gpiosetcxx.cpp | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/bindings/cxx/examples/gpiosetcxx.cpp b/bindings/cxx/examples/gpiosetcxx.cpp
index dde5379..ae35038 100644
--- a/bindings/cxx/examples/gpiosetcxx.cpp
+++ b/bindings/cxx/examples/gpiosetcxx.cpp
@@ -16,8 +16,8 @@ int main(int argc, char **argv)
 		return EXIT_FAILURE;
 	}
 
-	::gpiod::line::offsets offsets;
-	::gpiod::line::values values;
+	auto builder = ::gpiod::chip(argv[1]).prepare_request();
+	builder.set_consumer("gpiosetcxx");
 
 	for (int i = 2; i < argc; i++) {
 		::std::string arg(argv[i]);
@@ -31,22 +31,17 @@ int main(int argc, char **argv)
 			throw ::std::invalid_argument("invalid offset=value mapping: " +
 						      ::std::string(argv[i]));
 
-		offsets.push_back(::std::stoul(offset));
-		values.push_back(::std::stoul(value) ? ::gpiod::line::value::ACTIVE :
-						       ::gpiod::line::value::INACTIVE);
-	}
-
-	auto request = ::gpiod::chip(argv[1])
-		.prepare_request()
-		.set_consumer("gpiosetcxx")
-		.add_line_settings(
-			offsets,
+		::gpiod::line::value v = ::std::stoul(value) ?
+						 ::gpiod::line::value::ACTIVE :
+						 ::gpiod::line::value::INACTIVE;
+		builder.add_line_settings(
+			::std::stoul(offset),
 			::gpiod::line_settings()
 				.set_direction(::gpiod::line::direction::OUTPUT)
-		)
-		.do_request();
+				.set_output_value(v));
+	}
 
-	request.set_values(values);
+	auto request = builder.do_request();
 
 	::std::cin.get();
 
-- 
2.40.1


  parent reply	other threads:[~2023-06-09 15:36 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-09 15:36 [libgpiod][PATCH 0/2] fix potential glitches in bindings example gpiosets Kent Gibson
2023-06-09 15:36 ` [libgpiod][PATCH 1/2] bindings: python: examples: fix potential glitch in gpioset.py Kent Gibson
2023-06-09 20:18   ` andy.shevchenko
2023-06-12 14:26     ` Bartosz Golaszewski
2023-06-12 14:44       ` Andy Shevchenko
2023-06-12 14:53       ` Kent Gibson
2023-06-12 14:58         ` Bartosz Golaszewski
2023-06-12 14:27   ` Bartosz Golaszewski
2023-06-09 15:36 ` Kent Gibson [this message]
2023-06-09 20:19   ` [libgpiod][PATCH 2/2] bindings: cxx: examples: fix potential glitch in gpiosetcxx andy.shevchenko
  -- strict thread matches above, loose matches on Subject: below --
2023-06-12 14:56 [libgpiod][PATCH 0/2] bindings: cxx: " Bartosz Golaszewski
2023-06-12 14:56 ` [libgpiod][PATCH 2/2] bindings: cxx: examples: " Bartosz Golaszewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230609153607.133379-3-warthog618@gmail.com \
    --to=warthog618@gmail.com \
    --cc=brgl@bgdev.pl \
    --cc=linux-gpio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).