From d62708950c2f97bd67c6b7c2b9e95ae0f9161def Mon Sep 17 00:00:00 2001 From: Bartosz Golaszewski Date: Fri, 10 Mar 2023 10:32:47 +0100 Subject: [PATCH] bindings: cxx: examples: implement gpionotify using C++ bindings Add a new C++ code example - gpionotifycxx - which is a simplified reimplementation of the gpionotify tool. Signed-off-by: Bartosz Golaszewski --- bindings/cxx/examples/.gitignore | 1 + bindings/cxx/examples/Makefile.am | 3 ++ bindings/cxx/examples/gpionotifycxx.cpp | 55 +++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 bindings/cxx/examples/gpionotifycxx.cpp diff --git a/bindings/cxx/examples/.gitignore b/bindings/cxx/examples/.gitignore index 54bda46..2209497 100644 --- a/bindings/cxx/examples/.gitignore +++ b/bindings/cxx/examples/.gitignore @@ -6,4 +6,5 @@ gpiofindcxx gpiogetcxx gpioinfocxx gpiomoncxx +gpionotifycxx gpiosetcxx diff --git a/bindings/cxx/examples/Makefile.am b/bindings/cxx/examples/Makefile.am index e4136f5..36977ef 100644 --- a/bindings/cxx/examples/Makefile.am +++ b/bindings/cxx/examples/Makefile.am @@ -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 index 0000000..668734c --- /dev/null +++ b/bindings/cxx/examples/gpionotifycxx.cpp @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-FileCopyrightText: 2023 Bartosz Golaszewski + +/* Simplified C++ reimplementation of the gpionotify tool. */ + +#include +#include +#include + +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] << " ..." << ::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; +} -- 2.30.2