28 using std::regex_search;
46 5, 6, 13, 16, 19, 20, 21, 26
59 template<
typename... Args>
61 auto stringSize = snprintf(NULL, 0, format.c_str(), args...) + 1;
62 std::unique_ptr<
char[]> buffer(
new char[stringSize]);
64 snprintf(buffer.get(), stringSize, format.c_str(), args...);
66 return string(buffer.get(), buffer.get() + stringSize - 1);
73 bool parseArgs(int32_t argC,
char** argV);
79 int main(int32_t argC,
char** argV) {
101 if (stat(dirPath.c_str(), &_stat) == 0 && S_ISDIR(_stat.st_mode)) {
117 const static regex CHANNEL_PATTERN(R"((ch)?[0-8])");
118 const static regex STATE_PATTERN(R"(on|off|true|false)");
120 for (int32_t i = 0; i < argC; i++) {
121 string arg = argV[i];
124 transform(arg.begin(), arg.end(), arg.begin(), [&](
const auto c) {
return tolower(c); });
126 if (regex_search(arg, CHANNEL_PATTERN)) {
127 g_channel = std::stoi(arg.substr(arg.size() - 1, 1));
128 }
else if (regex_search(arg, STATE_PATTERN)) {
129 g_state = (arg ==
"on" || arg ==
"true");
143 static const string GPIO_DIR_PATH =
"/sys/class/gpio/gpio";
145 const auto gpioPath = formatString(
"%s%d", GPIO_DIR_PATH.c_str(),
g_channel);
147 if (directoryExists(gpioPath))
return;
151 std::ofstream fileStream(
"/sys/class/gpio/export", std::ios::trunc);
157 std::ofstream fileStream(formatString(
"%s/direction", gpioPath.c_str()), std::ios::trunc);
158 fileStream <<
"out" << endl;
166 static const string GPIO_DIR_PATH =
"/sys/class/gpio/gpio";
168 const auto gpioPath = formatString(
"%s%d/value", GPIO_DIR_PATH.c_str(),
g_channel);
170 std::ofstream fileStream(gpioPath, std::ios::trunc);
171 fileStream <<
static_cast<int32_t>(
g_state) << endl;
180 channel_select [ch]0-8 on/off/true/false
181 channel_select --help