From: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx> Add a new method to gpiod::line class allowing to read up to 16 line events at once. Signed-off-by: Bartosz Golaszewski <bgolaszewski@xxxxxxxxxxxx> --- bindings/cxx/gpiod.hpp | 7 +++++++ bindings/cxx/line.cpp | 45 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/bindings/cxx/gpiod.hpp b/bindings/cxx/gpiod.hpp index 078201b..bd99d28 100644 --- a/bindings/cxx/gpiod.hpp +++ b/bindings/cxx/gpiod.hpp @@ -421,6 +421,12 @@ public: */ GPIOD_API line_event event_read(void) const; + /** + * @brief Read up to 16 line events. + * @return Vector of line event objects. + */ + GPIOD_API ::std::vector<line_event> event_read_multiple(void) const; + /** * @brief Get the event file descriptor associated with this line. * @return File descriptor number. @@ -513,6 +519,7 @@ private: line(::gpiod_line* line, const chip& owner); void throw_if_null(void) const; + line_event make_line_event(const ::gpiod_line_event& event) const noexcept; ::gpiod_line* _m_line; chip _m_chip; diff --git a/bindings/cxx/line.cpp b/bindings/cxx/line.cpp index ed6ef55..11deae6 100644 --- a/bindings/cxx/line.cpp +++ b/bindings/cxx/line.cpp @@ -206,6 +206,23 @@ bool line::event_wait(const ::std::chrono::nanoseconds& timeout) const return !!event_bulk; } +line_event line::make_line_event(const ::gpiod_line_event& event) const noexcept +{ + line_event ret; + + if (event.event_type == GPIOD_LINE_EVENT_RISING_EDGE) + ret.event_type = line_event::RISING_EDGE; + else if (event.event_type == GPIOD_LINE_EVENT_FALLING_EDGE) + ret.event_type = line_event::FALLING_EDGE; + + ret.timestamp = ::std::chrono::nanoseconds( + event.ts.tv_nsec + (event.ts.tv_sec * 1000000000)); + + ret.source = *this; + + return ret; +} + line_event line::event_read(void) const { this->throw_if_null(); @@ -219,17 +236,29 @@ line_event line::event_read(void) const throw ::std::system_error(errno, ::std::system_category(), "error reading line event"); - if (event_buf.event_type == GPIOD_LINE_EVENT_RISING_EDGE) - event.event_type = line_event::RISING_EDGE; - else if (event_buf.event_type == GPIOD_LINE_EVENT_FALLING_EDGE) - event.event_type = line_event::FALLING_EDGE; + return this->make_line_event(event_buf); +} + +::std::vector<line_event> line::event_read_multiple(void) const +{ + this->throw_if_null(); - event.timestamp = ::std::chrono::nanoseconds( - event_buf.ts.tv_nsec + (event_buf.ts.tv_sec * 1000000000)); + /* 16 is the maximum number of events stored in the kernel FIFO. */ + ::std::array<::gpiod_line_event, 16> event_buf; + ::std::vector<line_event> events; + int rv; + + rv = ::gpiod_line_event_read_multiple(this->_m_line, + event_buf.data(), event_buf.size()); + if (rv < 0) + throw ::std::system_error(errno, ::std::system_category(), + "error reading multiple line events"); - event.source = *this; + events.reserve(rv); + for (int i = 0; i < rv; i++) + events.push_back(this->make_line_event(event_buf[i])); - return event; + return events; } int line::event_get_fd(void) const -- 2.23.0