Просмотр исходного кода

Add per-handler GPIO event and worker lifecycle state

SybexXx 3 дней назад
Родитель
Сommit
815abaa362
1 измененных файлов с 21 добавлено и 5 удалено
  1. 21 5
      code/components/jomjol_controlGPIO/server_GPIO.h

+ 21 - 5
code/components/jomjol_controlGPIO/server_GPIO.h

@@ -8,7 +8,11 @@
 #include <esp_http_server.h>
 #include <map>
 #include <string>
+#include <mutex>
+#include <atomic>
 #include "driver/gpio.h"
+#include "freertos/queue.h"
+#include "freertos/semphr.h"
 
 #include "SmartLeds.h"
 
@@ -27,6 +31,12 @@ typedef enum {
 struct GpioResult {
     gpio_num_t gpio;
     int value;
+    bool stopEvent;
+};
+
+struct GpioIsrContext {
+    QueueHandle_t eventQueue;
+    gpio_num_t gpio;
 };
 
 typedef enum {
@@ -40,7 +50,7 @@ public:
     GpioPin(gpio_num_t gpio, const char* name, gpio_pin_mode_t mode, gpio_int_type_t interruptType, uint8_t dutyResolution, std::string mqttTopic, bool httpEnable);
     ~GpioPin();
 
-    bool init();
+    bool init(QueueHandle_t eventQueue);
     bool getValue(std::string* errorText);
     void setValue(bool value, gpio_set_source setSource, std::string* errorText);
 #ifdef ENABLE_MQTT
@@ -61,11 +71,11 @@ private:
     std::string _mqttTopic;
     bool _httpEnable = false;
     bool _isrRegistered = false;
+    GpioIsrContext _isrContext = {};
     int currentState = -1;
 };
 
 esp_err_t callHandleHttpRequest(httpd_req_t *req);
-void taskGpioHandler(void *pvParameter);
 
 class GpioHandler {
 public:
@@ -79,7 +89,7 @@ public:
     void taskHandler();
     void gpioInterrupt(GpioResult* gpioResult);  
     void flashLightEnable(bool value);
-    bool isEnabled() { return _isEnabled; }
+    bool isEnabled() const { return _isEnabled.load(); }
 #ifdef ENABLE_MQTT
     void handleMQTTconnect();
 #endif //ENABLE_MQTT
@@ -88,8 +98,11 @@ private:
     std::string _configFile;
     httpd_handle_t _httpServer;
     std::map<gpio_num_t, GpioPin*> *gpioMap = NULL;
-    TaskHandle_t xHandleTaskGpio = NULL;
-    bool _isEnabled = false;
+    QueueHandle_t _eventQueue = NULL;
+    TaskHandle_t _workerTask = NULL;
+    SemaphoreHandle_t _workerStoppedSemaphore = NULL;
+    std::mutex _mapMutex;
+    std::atomic<bool> _isEnabled{false};
 
     int LEDNumbers = 2;
     Rgb LEDColor = Rgb{ 255, 255, 255 };
@@ -100,6 +113,9 @@ private:
 
     bool readConfig();
     void clear();
+    void clearUnlocked();
+    static void taskEntry(void *arg);
+    void workerLoop();
     
     gpio_pin_mode_t resolvePinMode(std::string input);
     gpio_int_type_t resolveIntType(std::string input);