在if条件下使用gpio的状态

时间:2019-02-12 21:00:42

标签: c++ fstream iostream gpio

我有一个函数,并且在该函数中,我正在使用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

1 个答案:

答案 0 :(得分:1)

您正在尝试将数据放入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;
    }

此处有更多有关树莓pi gpios的示例:http://www.hertaville.com/introduction-to-accessing-the-raspberry-pis-gpio-in-c.html