diff mbox series

[libgpiod,3/8] bindings: cxx: examples: consistency cleanup

Message ID 20230623043901.16764-4-warthog618@gmail.com
State New
Headers show
Series replace tool examples with use case examples | expand

Commit Message

Kent Gibson June 23, 2023, 4:38 a.m. UTC
A collection of minor changes to be more consistent with other examples:
 - capitalize comments
 - add line offset to value outputs
 - drop comma from edge event outputs
 - drop trailing return where example loops indefintely
 - sort includes

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---
 .../cxx/examples/async_watch_line_value.cpp   |  8 +++---
 bindings/cxx/examples/get_line_value.cpp      | 28 +++++++++++--------
 bindings/cxx/examples/toggle_line_value.cpp   | 16 +++++------
 bindings/cxx/examples/watch_line_value.cpp    | 12 ++++----
 4 files changed, 33 insertions(+), 31 deletions(-)
diff mbox series

Patch

diff --git a/bindings/cxx/examples/async_watch_line_value.cpp b/bindings/cxx/examples/async_watch_line_value.cpp
index e1d4a1e..d8317a5 100644
--- a/bindings/cxx/examples/async_watch_line_value.cpp
+++ b/bindings/cxx/examples/async_watch_line_value.cpp
@@ -7,6 +7,7 @@ 
 #include <cstring>
 #include <filesystem>
 #include <gpiod.hpp>
+#include <iomanip>
 #include <iostream>
 #include <poll.h>
 
@@ -79,10 +80,9 @@  int main(void)
 
 		for (const auto& event : buffer)
 			::std::cout << "offset: " << event.line_offset()
-				    << ", type: " << edge_event_type_str(event)
-				    << ", event #" << event.line_seqno()
+				    << "  type: " << ::std::setw(7)
+				    << ::std::left << edge_event_type_str(event)
+				    << "  event #" << event.line_seqno()
 				    << ::std::endl;
 	}
-
-	return EXIT_SUCCESS;
 }
diff --git a/bindings/cxx/examples/get_line_value.cpp b/bindings/cxx/examples/get_line_value.cpp
index 8f4e739..a14d7e4 100644
--- a/bindings/cxx/examples/get_line_value.cpp
+++ b/bindings/cxx/examples/get_line_value.cpp
@@ -10,7 +10,7 @@ 
 
 namespace {
 
-/* example configuration - customize to suit your situation */
+/* Example configuration - customize to suit your situation */
 const ::std::filesystem::path chip_path("/dev/gpiochip0");
 const ::gpiod::line::offset line_offset = 5;
 
@@ -18,17 +18,21 @@  const ::gpiod::line::offset line_offset = 5;
 
 int main(void)
 {
-	auto request =
-		::gpiod::chip(chip_path)
-			.prepare_request()
-			.set_consumer("get-line-value")
-			.add_line_settings(
-				line_offset,
-				::gpiod::line_settings().set_direction(
-					::gpiod::line::direction::INPUT))
-			.do_request();
-
-	::std::cout << request.get_value(line_offset) << ::std::endl;
+	auto request = ::gpiod::chip(chip_path)
+			       .prepare_request()
+			       .set_consumer("get-line-value")
+			       .add_line_settings(
+				       line_offset,
+				       ::gpiod::line_settings().set_direction(
+					       ::gpiod::line::direction::INPUT))
+			       .do_request();
+
+	::std::cout << line_offset << "="
+		    << (request.get_value(line_offset) ==
+					::gpiod::line::value::ACTIVE ?
+				"Active" :
+				"Inactive")
+		    << ::std::endl;
 
 	return EXIT_SUCCESS;
 }
diff --git a/bindings/cxx/examples/toggle_line_value.cpp b/bindings/cxx/examples/toggle_line_value.cpp
index a060e8a..a17b43b 100644
--- a/bindings/cxx/examples/toggle_line_value.cpp
+++ b/bindings/cxx/examples/toggle_line_value.cpp
@@ -6,8 +6,8 @@ 
 #include <cstdlib>
 #include <chrono>
 #include <filesystem>
-#include <iostream>
 #include <gpiod.hpp>
+#include <iostream>
 #include <thread>
 
 namespace {
@@ -19,15 +19,15 @@  const ::gpiod::line::offset line_offset = 5;
 ::gpiod::line::value toggle_value(::gpiod::line::value v)
 {
 	return (v == ::gpiod::line::value::ACTIVE) ?
-			::gpiod::line::value::INACTIVE :
-			::gpiod::line::value::ACTIVE;
+		       ::gpiod::line::value::INACTIVE :
+		       ::gpiod::line::value::ACTIVE;
 }
 
 } /* namespace */
 
 int main(void)
 {
-	::gpiod::line::value val = ::gpiod::line::value::ACTIVE;
+	::gpiod::line::value value = ::gpiod::line::value::ACTIVE;
 
 	auto request =
 		::gpiod::chip(chip_path)
@@ -40,12 +40,10 @@  int main(void)
 			.do_request();
 
 	for (;;) {
-		::std::cout << val << ::std::endl;
+		::std::cout << line_offset << "=" << value << ::std::endl;
 
 		std::this_thread::sleep_for(std::chrono::seconds(1));
-		val = toggle_value(val);
-		request.set_value(line_offset, val);
+		value = toggle_value(value);
+		request.set_value(line_offset, value);
 	}
-
-	return EXIT_SUCCESS;
 }
diff --git a/bindings/cxx/examples/watch_line_value.cpp b/bindings/cxx/examples/watch_line_value.cpp
index 5436884..5055789 100644
--- a/bindings/cxx/examples/watch_line_value.cpp
+++ b/bindings/cxx/examples/watch_line_value.cpp
@@ -6,6 +6,7 @@ 
 #include <cstdlib>
 #include <filesystem>
 #include <gpiod.hpp>
+#include <iomanip>
 #include <iostream>
 
 namespace {
@@ -18,7 +19,7 @@  const char *edge_event_type_str(const ::gpiod::edge_event &event)
 {
 	switch (event.type()) {
 	case ::gpiod::edge_event::event_type::RISING_EDGE:
-		return "Rising ";
+		return "Rising";
 	case ::gpiod::edge_event::event_type::FALLING_EDGE:
 		return "Falling";
 	default:
@@ -61,11 +62,10 @@  int main(void)
 		request.read_edge_events(buffer);
 
 		for (const auto &event : buffer)
-			::std::cout << "offset: " << event.line_offset()
-				    << ", type: " << edge_event_type_str(event)
-				    << ", event #" << event.line_seqno()
+			::std::cout << "line: " << event.line_offset()
+				    << "  type: " << ::std::setw(7)
+				    << ::std::left << edge_event_type_str(event)
+				    << "  event #" << event.line_seqno()
 				    << ::std::endl;
 	}
-
-	return EXIT_SUCCESS;
 }