I have a function and inside that function I am using usleep(). However, I would only like to use usleep() under the condition that a certain gpio has a value of zero. Here is the code I have so far:
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);
}
}
I am getting an error I am not sure how to resolve:
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
You are trying to put data into a const char* const variable. A const char* const is a pointer to a string where the pointer can not change, and the string data that is pointed to can not change, hence the const's.
The warning is because there is no overloaded == operator for const char*. For this type of comparison generally you will use strcmp()
.
However, since you are using c++, you probably want to use a std::string
which should solve both of the referenced compiler messages like so:
#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;
}
Some more examples with raspberry pi gpios here: http://www.hertaville.com/introduction-to-accessing-the-raspberry-pis-gpio-in-c.html