使用libgpiod和C++,如何等待多行更改并获取更改后的值?

问题描述 投票:0回答:1

在 Raspberry Pi 4 上使用 C++,我正在构建一个名为

monitor
的例程来观察我的 GPIO 线路并记录所有线路的更改。我的简单代码使用
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;
    }
}

对于缺失的代码,有没有办法找出哪一行发生了变化并获取其当前值?

如果不是,我应该在所有行之间迭代吗?我怎样才能知道哪一行发生了变化以及它的值?

版本1代码:

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;
    }
}
c++ gpio libgpiod gpiod
1个回答
0
投票

libgpiodcxx
版本 1.x 中,
line_bulk::event_wait()
返回发生事件的大量行。
line_bulk
表现得像一个集合,你可以迭代它。

    auto bulk = chip("gpiochip0", chip::OPEN_BY_NAME).get_lines({ 5, 6 });

    bulk.request({ "monitor",
        line_request::EVENT_BOTH_EDGES,
        line_request::FLAG_BIAS_PULL_UP });

    while (true) {
        // wait for events and get bulk of lines on which events occured
        auto changed = bulk.event_wait(std::chrono::seconds::max());
        // iterate lines in bulk
        for (const auto &line : changed) {
            std::cout << "Change detected on GPIO line " << line.offset() << std::endl;
            // read events occured on a line
            auto events = line.event_read_multiple();
            // iterate events
            for (const auto &event : events) {
                std::cout << "\ttype: " << event.event_type << std::endl;
            }
        }
    }

免责声明:我不确定

event_wait(std::chrono::seconds::max())
是“无限”阻塞等待的好方法,但我还没有找到正确的记录方法。

© www.soinside.com 2019 - 2024. All rights reserved.