在if条件中使用gpio的状态
Using the state of a gpio in an if condition
我有一个函数,在这个函数中我使用usleep()。然而,我只想在某个gpio的值为零的条件下使用usleep()。这是我迄今为止的代码:
const char *const amplifierGPIO = "/sys/class/gpio/gpio107/value";
const char *const hardwareID = "/sys/class/gpio/gpiox/value";
bool isWM8750()
{
std::ifstream id(hardwareID);
if (id.is_open())
{
const char *const value;
id >> value;
if (value == "0")
{
return true;
}
}
return false;
}
void amplifierUnmute()
{
std::ofstream amp(amplifierGPIO);
if (amp.is_open())
{
amp << "1";
amp.close();
}
if(isWM8750())
{
usleep(50000);
}
}
我得到了一个错误,我不知道如何解决:
sound_p51.cpp:38: error: no match for 'operator>>' in 'id >> value'
sound_p51.cpp:40: warning: comparison with string literal results in unspecified behaviour
您正试图将数据放入const char*const变量中。const char*const是指向一个字符串的指针,在该字符串中,指针不能更改,并且所指向的字符串数据也不能更改,因此是const。
该警告是因为const char*没有重载==运算符。对于这种类型的比较,通常使用strcmp()
。
然而,由于您使用的是c++,您可能希望使用std::string
,它应该解决两个引用的编译器消息,如:
#include <string>
// ...
bool isWM8750()
{
std::ifstream id(hardwareID);
if (id.is_open())
{
std::string value;
id >> value;
id.close();
if (value == "0")
{
return true;
}
}
return false;
}
这里有更多关于树莓派gpios的例子:http://www.hertaville.com/introduction-to-accessing-the-raspberry-pis-gpio-in-c.html
相关文章:
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- OSX MetalKit CVMetalTextureCacheCreateTextureFromImage失败,状态:
- std::future_error:无关联状态
- 如何避免LED在循环状态变化中闪烁?
- boost 是否有按特殊类型值编码状态"compact optional"?
- 为什么系统函数总是在C++中返回已转移的退出状态?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 当可输入框在窗口中处于活动状态时获得通知的任何方法
- 检查两个节点在子节点上是否具有相同状态的更优雅的方法
- 将有状态的 lambda 传递到 C 样式函数中,而无需上下文参数
- 在 nullptr 上调用无状态类的非静态成员函数是否合法?
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 试图在崇高中奔跑. 错误 已发生: 收集2.exe: 错误: ld 返回 1 退出状态
- 在容器上移动分配:以前包含的对象的状态
- collect2:错误:ld 返回 1 个退出状态未定义的引用
- 具有动态大小的特征矩阵的默认初始状态
- 如何从Clojure调用C++程序,以使程序保持打开状态?
- 在if条件中使用gpio的状态