2

I'm looking at using the gpiod interface on an embedded Linux device with a C++ framework. The issue is that I haven't come across an example of how to asynchronously monitor inputs using gpiod. Ideally it would be something like gpiomon but not blocking.

I've used asio (specifically asio::async_read) with file descriptors (fd) to accomplish this but it looks incompatible with gpiod (related question).

Further it looks like mixing file descriptors with gpiod is messy as the gpiod lines assign there own fd internally which is not exposed as far as I can see.

Any pointers would be appreciated.

Regards,

leo8382
  • 91
  • 8
  • Thanks for the suggestion but I'd like to avoid the boost version of Asio (https://think-async.com/Asio/AsioAndBoostAsio.html) – leo8382 Jan 31 '22 at 11:54
  • As you wish; you know better. :) – CiaPan Jan 31 '22 at 12:04
  • I am still looking for a proper solution to this exact problem. I 've concluded that a separate thread can do the polling for a specific line (pin) and report to the main thread when the needed event type is detected. – Petross404 Jun 21 '22 at 18:48
  • Probably the way to go. If the thread can call a lambda might be better again – leo8382 Nov 11 '22 at 21:01

1 Answers1

0

In addition to blocking reads and timed waits, libgpiod exposes the line request fd so you have the option of using that with whatever async polling mechanism suits your application.

In libgpiod v2 that is via gpiod::line_request::fd() - ref gpiodcxx/line-request.hpp (included by gpiod.hpp).

For libgpiod v1 it is via gpiod::line::event_get_fd() - ref gpiod.hpp.

The related question is referring to GPIO sysfs file descriptors, which is the old deprecated GPIO API. libgpiod uses the newer character device API and the returned fds are suitable for poll()ing.

Kent Gibson
  • 101
  • 1
  • 5