Using C++ on a Raspberry Pi 4, I'm building a single routine called monitor to watch my GPIO lines and log changes for all lines. My simple code using libgpiod:
void monitor()
{
const std::string chipname = "gpiochip0";
const std::vector<int> offsets = {4, 5}; // Lines 4 and 5 for testing
try
{
gpiod::chip chip(chipname);
gpiod::line_bulk lines;
for (int offset : offsets)
{
auto line = chip.get_line(offset);
line.request({"monitor", gpiod::line_request::EVENT_BOTH_EDGES, 0});
lines.append(line);
}
while (true)
{
// Infinite waiting on all lines
auto signals = lines.event_wait(std::chrono::nanoseconds(0));
// STUCK HERE - How can I find out which line has changed and get its value and timestamp.
}
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
}
For the missing code, is there a way to find out which line has changed and get its current value?
If not, should I iterate between all lines? How can I find out which line has changed and its value?
Version 1 Code:
void monitor()
{
const std::string chipname = "gpiochip0";
const std::vector<int> offsets = {4, 5}; // Lines 4 and 5 for testing
try
{
gpiod::chip chip(chipname);
gpiod::line_bulk lines;
for (int offset : offsets)
{
auto line = chip.get_line(offset);
line.request({"monitor", gpiod::line_request::EVENT_BOTH_EDGES, 0});
lines.append(line);
}
while (true)
{
// Infinite waiting on all lines
auto signals = lines.event_wait(std::chrono::nanoseconds(0));
for (unsigned int i = 0; i < lines.size(); ++i)
{
if (lines[i].event_wait(std::chrono::seconds(0)))
{
std::cout << "Change detected on GPIO line " << offsets[i] << std::endl;
}
}
}
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
}
In
libgpiodcxxversions 1.xline_bulk::event_wait()returns bulk of lines on which events occurred.line_bulkbehaves like a collection, you can iterate it.Disclaimer: I'm not sure
event_wait(std::chrono::seconds::max())is a good way for "infinite" blocking wait, but i haven't found proper documented method.