From: Bas Schouten Date: Tue, 29 Aug 2023 12:56:35 +0000 (+0200) Subject: Completely remove dependencies on external libraries for GPIO interaction. X-Git-Url: https://git.basschouten.com/?a=commitdiff_plain;h=HEAD;p=mqttthermostat.git Completely remove dependencies on external libraries for GPIO interaction. --- diff --git a/MQTTThermostat/CMakeLists.txt b/MQTTThermostat/CMakeLists.txt index 19c0885..3529552 100644 --- a/MQTTThermostat/CMakeLists.txt +++ b/MQTTThermostat/CMakeLists.txt @@ -22,7 +22,6 @@ endif (HAS_AHT_SUPPORT) if (HAS_GPIO_SUPPORT) target_sources (mqttthermostat PRIVATE "GPIOHeatingController.cpp") -target_link_libraries (mqttthermostat PRIVATE pigpio) add_compile_definitions(HAS_GPIO_SUPPORT) endif (HAS_GPIO_SUPPORT) diff --git a/MQTTThermostat/GPIOHeatingController.cpp b/MQTTThermostat/GPIOHeatingController.cpp index ece65f1..e4306bb 100644 --- a/MQTTThermostat/GPIOHeatingController.cpp +++ b/MQTTThermostat/GPIOHeatingController.cpp @@ -1,25 +1,54 @@ #include #include +#include +#include +#include +#include #include "GPIOHeatingController.h" -#include using namespace std; GPIOHeatingController::GPIOHeatingController(uint32_t aPin) : mPin(aPin) { - gpioInitialise(); - gpioSetMode(mPin, PI_OUTPUT); + char buffer[256]; + ssize_t bytes_written; + int fd; + + fd = open("/sys/class/gpio/export", O_WRONLY); + if (-1 == fd) { + fprintf(stderr, "Failed to open export for writing!\n"); + return; + } + + bytes_written = snprintf(buffer, 256, "%d", mPin); + write(fd, buffer, bytes_written); + close(fd); + + snprintf(buffer, 256, "/sys/class/gpio/gpio%d/direction", mPin); + fd = open(buffer, O_WRONLY); + if (-1 == fd) { + fprintf(stderr, "Failed to set gpio direction.\n"); + } + + write(fd, "out", 3); + close(fd); } void GPIOHeatingController::setHeatingActive(bool aActive) { - if (aActive) { - gpioWrite(mPin, 1); - } - else { - gpioWrite(mPin, 0); + char buffer[256]; + int fd; + + snprintf(buffer, 256, "/sys/class/gpio/gpio%d/value", mPin); + fd = open(buffer, O_WRONLY); + if (-1 == fd) { + fprintf(stderr, "Failed to output gpio.\n"); + return; } + + write(fd, aActive ? "1" : "0", 1); + close(fd); }