// // Created by yingluo907 on 2019/4/11. // #include "Gpio.h" #include "DevUtil.h" #include "stringex.hpp" #include #include #include #include #define IOPATH "%s/gpio%d/%s" const std::string Gpio::falling = "falling"; const std::string Gpio::rising = "rising"; const std::string Gpio::both = "both"; const std::string Gpio::none = "none"; const std::string Gpio::in = "in"; const std::string Gpio::out = "out"; Gpio::Gpio(int port) { this->port = port; path_value = string_format(IOPATH, path_gpiobase.c_str(), port, path_value.c_str()); path_edge = string_format(IOPATH, path_gpiobase.c_str(), port, path_edge.c_str()); path_direction = string_format(IOPATH, path_gpiobase.c_str(), port, path_direction.c_str()); path_active_low = string_format(IOPATH, path_gpiobase.c_str(), port, path_active_low.c_str()); char fpath[128]{}; sprintf(fpath, "/sys/class/gpio/gpio%d/value", port); printf("Gpio::Gpio(int port = %d) \n", port); gpio_fd = open(fpath, O_RDWR); } Gpio::~Gpio() { close(gpio_fd); } int Gpio::getPort() { return port; } void Gpio::setValue(GpioLevel level) { // write_dev(path_value, level); if (port == 153 || port == 150) printf("\n Gpio %d setvalue %d ", port, level == Low ? 0 : 1); if (level == Low) write(gpio_fd, "0\n", 2); else write(gpio_fd, "1\n", 2); } Gpio::GpioLevel Gpio::getValue() { return (Gpio::GpioLevel)read_dev_i(path_value); } std::string Gpio::getDirection() { return read_dev_s(path_direction); } void Gpio::setDirection(std::string direction) { if (port == 153 || port == 150) printf("\n Gpio %d setDirection %s ", port, direction.c_str()); write_dev(path_direction, direction); } void Gpio::setActive(GpioLevel level) { write_dev(path_active_low, level); } Gpio::GpioLevel Gpio::getActive() { return (GpioLevel)read_dev_i(path_active_low); } void Gpio::setEdge(std::string edge) { write_dev(path_edge, edge); } std::string Gpio::getEdge() { return read_dev_s(path_edge); } GpioOut::GpioOut(int port) : Gpio(port) { setDirection(out); }