c++libgpiodgpiod

Using libgpiod version 2.1.x, how to convert gpiod::timestamp to std::chrono::nanoseconds?


I'm using libgpiod C++ version 2.1.x to get multiple line changes:

Code from github example files:

  auto request =
        ::gpiod::chip(chip_path)
            .prepare_request()
            .set_consumer("qedged_io")
            .add_line_settings(
                line_offsets,
                gpiod::line_settings()
                    .set_direction(
                        gpiod::line::direction::INPUT)
                    .set_edge_detection(
                        gpiod::line::edge::BOTH))
            .do_request();

    gpiod::edge_event_buffer buffer;

    while (true)
    {
        /* Blocks until at leat one event available */
        request.read_edge_events(buffer);

        for (const auto &event : buffer)
        {
            std::cout << "offset: " << event.line_offset()
                      << "  event #" << event.global_seqno()
                      << "  line event #" << event.line_seqno()
                      << ::std::endl;
           
             auto ts = event.timestamp_ns();

             std::chrono::nanoseconds ns = ts.?????; <<== How to convert gpiod::timestamp to std::chrono::nanoseconds, considering the real time clock?

        }
    }

After getting the event I need to get the timestamp in std::chrono::nanoseconds to be used in another function...

How to convert gpiod::timestamp received from event timestamp_ns() to std::chrono::nanoseconds ?


Solution

  • According to the documentation, you can get a nanosecond count since Unix epoch with the member function ns()

    std::chrono::nanoseconds ns( ts.ns() );