diff mbox series

[libgpiod,2/2] bindings: cxx: examples: fix potential glitch in gpiosetcxx

Message ID 20230609153607.133379-3-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
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(-)

Comments

Andy Shevchenko June 9, 2023, 8:19 p.m. UTC | #1
Fri, Jun 09, 2023 at 11:36:07PM +0800, Kent Gibson kirjoitti:
> 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.

Same question about commenting in the code.
diff mbox series

Patch

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();