]> git.basschouten.com Git - mqttthermostat.git/commitdiff
Completely remove dependencies on external libraries for GPIO interaction. master
authorBas Schouten <bas@basschouten.com>
Tue, 29 Aug 2023 12:56:35 +0000 (14:56 +0200)
committerBas Schouten <bas@basschouten.com>
Tue, 29 Aug 2023 12:56:35 +0000 (14:56 +0200)
MQTTThermostat/CMakeLists.txt
MQTTThermostat/GPIOHeatingController.cpp

index 19c0885dc410d0463fc6e5b0b70580e86b4e1353..352955294c60caaad5a34ea58bfcff4da70aac00 100644 (file)
@@ -22,7 +22,6 @@ endif (HAS_AHT_SUPPORT)
 
 if (HAS_GPIO_SUPPORT)
 target_sources (mqttthermostat PRIVATE "GPIOHeatingController.cpp")
 
 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)
 
 add_compile_definitions(HAS_GPIO_SUPPORT)
 endif (HAS_GPIO_SUPPORT)
 
index ece65f11dba2b7f70e7d56a1d0509bb2912e694d..e4306bb6a0c8fc40571aef5ea544ad1c1042c975 100644 (file)
@@ -1,25 +1,54 @@
 #include <stdio.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
 
 #include "GPIOHeatingController.h"
 
 #include "GPIOHeatingController.h"
-#include <pigpio.h>
 
 using namespace std;
 
 GPIOHeatingController::GPIOHeatingController(uint32_t aPin)
   : mPin(aPin)
 {
 
 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)
 {
 }
 
 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);
 }
 }