bindings: cxx: examples: implement gpionotify using C++ bindings
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Fri, 10 Mar 2023 09:32:47 +0000 (10:32 +0100)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Sun, 12 Mar 2023 13:44:27 +0000 (14:44 +0100)
Add a new C++ code example - gpionotifycxx - which is a simplified
reimplementation of the gpionotify tool.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/cxx/examples/.gitignore
bindings/cxx/examples/Makefile.am
bindings/cxx/examples/gpionotifycxx.cpp [new file with mode: 0644]

index 54bda46ebfe2a5b7a2fc1d356616960a0d6b3fdb..22094975c8979f4eebd5f7406ab09decbcc1f413 100644 (file)
@@ -6,4 +6,5 @@ gpiofindcxx
 gpiogetcxx
 gpioinfocxx
 gpiomoncxx
+gpionotifycxx
 gpiosetcxx
index e4136f533c4d26ebd1227909e800ba3b1efb48ca..36977efc9d12c16f753e894bf999135bb53510a1 100644 (file)
@@ -11,6 +11,7 @@ noinst_PROGRAMS = \
        gpiogetcxx \
        gpioinfocxx \
        gpiomoncxx \
+       gpionotifycxx \
        gpiosetcxx
 
 gpiodetectcxx_SOURCES = gpiodetectcxx.cpp
@@ -23,4 +24,6 @@ gpioinfocxx_SOURCES = gpioinfocxx.cpp
 
 gpiomoncxx_SOURCES = gpiomoncxx.cpp
 
+gpionotifycxx_SOURCES = gpionotifycxx.cpp
+
 gpiosetcxx_SOURCES = gpiosetcxx.cpp
diff --git a/bindings/cxx/examples/gpionotifycxx.cpp b/bindings/cxx/examples/gpionotifycxx.cpp
new file mode 100644 (file)
index 0000000..668734c
--- /dev/null
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// SPDX-FileCopyrightText: 2023 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
+
+/* Simplified C++ reimplementation of the gpionotify tool. */
+
+#include <cstdlib>
+#include <gpiod.hpp>
+#include <iostream>
+
+namespace {
+
+void print_event(const ::gpiod::info_event& event)
+{
+       switch (event.type()) {
+       case ::gpiod::info_event::event_type::LINE_REQUESTED:
+               ::std::cout << "LINE REQUESTED";
+               break;
+       case ::gpiod::info_event::event_type::LINE_RELEASED:
+               ::std::cout << "LINE RELEASED";
+               break;
+       case ::gpiod::info_event::event_type::LINE_CONFIG_CHANGED:
+               ::std::cout << "CONFIG CHANGED";
+               break;
+       }
+
+       ::std::cout << " ";
+
+       ::std::cout << event.timestamp_ns() / 1000000000;
+       ::std::cout << ".";
+       ::std::cout << event.timestamp_ns() % 1000000000;
+
+       ::std::cout << " line: " << event.get_line_info().offset();
+
+       ::std::cout << ::std::endl;
+}
+
+} /* namespace */
+
+int main(int argc, char **argv)
+{
+       if (argc < 3) {
+               ::std::cout << "usage: " << argv[0] << " <chip> <offset0> ..." << ::std::endl;
+               return EXIT_FAILURE;
+       }
+
+       ::gpiod::chip chip(argv[1]);
+
+       for (int i = 2; i < argc; i++)
+               chip.watch_line_info(::std::stoul(argv[i]));
+
+       for (;;)
+               print_event(chip.read_info_event());
+
+       return EXIT_SUCCESS;
+}