#ifndef GPIO_CONTROL_H #define GPIO_CONTROL_H #include #include #include #include class GPIOControl { public: GPIOControl(int gpioNum) : gpio(gpioNum) {} bool exportGPIO() { return writeToFile("/sys/class/gpio/export", std::to_string(gpio)); } bool unexportGPIO() { return writeToFile("/sys/class/gpio/unexport", std::to_string(gpio)); } bool setDirection(const std::string &dir) { return writeToFile("/sys/class/gpio/gpio" + std::to_string(gpio) + "/direction", dir); } bool setValue(bool high) { return writeToFile("/sys/class/gpio/gpio" + std::to_string(gpio) + "/value", high ? "1" : "0"); } private: int gpio; bool writeToFile(const std::string &path, const std::string &value) { std::ofstream fs(path); if (!fs.is_open()) { std::cerr << "无法访问: " << path << std::endl; return false; } fs << value; fs.close(); return true; } }; #endif // GPIO_CONTROL_H