简体   繁体   English

使用 c++ libgpiod 库,如何将 gpio 行设置为输出并使用 set_value() function 操作单行?

[英]Using c++ libgpiod library, how can I set gpio lines to be outputs and manipulate single lines with set_value() function?

I just started using c++ bindings of libgpiod library and have problem with settings gpios.我刚开始使用 libgpiod 库的 c++ 绑定,并且设置 gpios 有问题。 I know, that I can create long vector of values, and apply it in all at once, but I would like to be able to set their direction, and control them separately.我知道,我可以创建长向量值,并一次应用它,但我希望能够设置它们的方向,并分别控制它们。 How can I do that?我怎样才能做到这一点?

What I tried is this:我尝试的是这样的:

First: Working code with applying all values at once:第一:一次应用所有值的工作代码:

#include <gpiod.hpp>

int main(int argc, char **argv)
    {

    ::gpiod::chip chip("gpiochip0");
    auto lines = chip.get_all_lines();

    ::gpiod::line_request requestOutputs = {
        argv[0],
        ::gpiod::line_request::DIRECTION_OUTPUT,
        0
    };

    int value_to_be_set = 0xAAAAAAA ; //example value
    ::std::vector<int> values;

    for (int i = 0; i < 32; i++)
    {
        values.push_back((value_to_be_set >> i) & 1UL);
    }

    lines.request(requestOutputs, values);
    lines.release();

    return EXIT_SUCCESS;
}

Second, my approach to do that I want:其次,我想要做的事情的方法:

#include <gpiod.hpp>

int main(int argc, char **argv)
{
    ::gpiod::chip chip("gpiochip0");
    auto lines = chip.get_all_lines();

    ::gpiod::line_request requestOutputs = {
        argv[0],
        ::gpiod::line_request::DIRECTION_OUTPUT,
        0
    };
    lines.request(requestOutputs);

    int value_to_be_set = 0xAAAAAAA; //example value

    for (int i = 0; i < 32; i++)
    {
        // This does not set value :(
        lines.get(i).set_value((value_to_be_set >> i) & 1UL);
    }

    lines.release();

    return EXIT_SUCCESS;
}

也不要忘记使用标志-lgpiodcxx (对于 c++)或-lgpiod (对于 c)进行构建

I also could not find a simple C++ example to toggle a single GPIO line using the latest Raspberry PI libraries.我也找不到一个简单的 C++ 示例来使用最新的 Raspberry PI 库切换单个 GPIO 线。

There is a multi-line example below but this is not what was originally asked: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/cxx下面有一个多行示例,但这不是最初要求的: https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/cxx

Below is an example that will cause GPIO17 to go high then low to create a single line output pulse.下面是一个示例,它将导致 GPIO17 到 go 先高后低,以创建单线 output 脉冲。

If this answer is acceptable please upvote or tag this as the official answer.如果这个答案是可以接受的,请投票或将其标记为官方答案。

// Use gpio drivers to toggle a single GPIO
// line on Raspberry Pi

// Use following commands to install prerequisites and build
// sudo apt install gpiod
// sudo apt install libgpiod-dev
// g++ -Wall -o gpio gpip.cpp -lgpiodcxx

#include <iostream>
#include <gpiod.hpp>
#include <unistd.h>
 
int main(void)
{ 
   ::gpiod::chip chip("gpiochip0");
   
   auto line = chip.get_line(17);  // GPIO17
   line.request({"example", gpiod::line_request::DIRECTION_OUTPUT, 0},1);  
   
   sleep(0.1);
   
   line.set_value(0);
   line.release();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM