使用c++libgpiod库,如何将gpio行设置为输出,并使用set_value()函数操作单行
Using c++ libgpiod library, how can I set gpio lines to be outputs and manipulate single lines with set_value() function?
我刚开始使用libgpiod库的c++绑定,设置gpios时遇到问题。我知道,我可以创建值的长向量,并一次将其应用于所有值,但我希望能够设置它们的方向,并分别控制它们。我该怎么做?
我尝试的是:
第一:同时应用所有值的工作代码:
#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;
}
第二,我想要的方法:
#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;
}
我也找不到一个简单的C++示例来使用最新的Raspberry PI库切换单个GPIO线。
下面有一个多行示例,但这不是最初询问的内容:https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/cxx
下面是一个示例,它将导致GPIO17先变高,然后变低,以创建单线输出脉冲。
// 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();
}
也不要忘记使用标志-lgpiodcxx
(对于c++(或-lgpiod
(对于c(进行构建
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- 为什么我无法更改"set<set>"循环中的值<int>
- 对于set上的循环-获取next元素迭代器
- 为什么static_assert错误:即使我传递常量"expression must have a constant value"?
- 在声明中合并两个常量"std::set"(不是在运行时)
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将 std::set 与基于键的比较器一起使用
- 如何使用set实现无序数据结构?
- 使用运算符调用 void 函数时出错<set>
- 为什么我会" void value not ignored as it ought to be"?
- 修改"std::set"中用户定义类型的值
- 方法错误"not all control paths return a value"和方法不返回值
- C++/SDL "initial value of reference to a non-const must be an lvalue"
- 如何在 c++ 中理解这样的代码 [request->headers().Method()->value().getStringView())]
- std::<key-value>不同类型的对向量
- 生成提升::hana::set 的常量表达式问题
- 如何在构造函数参数中初始化"std::set"?
- Visual Studio xtree _STL_VERIFY(this->_Getcont(), "cannot increment value-initialized map/set ite
- 同样的'value'对于 std::set 意味着什么?
- std::set<VALUE, std::owner_less>的目的是什么<VALUE>?