server_GPIO.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #ifndef SERVER_GPIO_H
  3. #define SERVER_GPIO_H
  4. #include <esp_log.h>
  5. #include <esp_http_server.h>
  6. #include <map>
  7. #include <string>
  8. #include <mutex>
  9. #include <atomic>
  10. #include "driver/gpio.h"
  11. #include "freertos/queue.h"
  12. #include "freertos/semphr.h"
  13. #include "SmartLeds.h"
  14. typedef enum {
  15. GPIO_PIN_MODE_DISABLED = 0x0,
  16. GPIO_PIN_MODE_INPUT = 0x1,
  17. GPIO_PIN_MODE_INPUT_PULLUP = 0x2,
  18. GPIO_PIN_MODE_INPUT_PULLDOWN = 0x3,
  19. GPIO_PIN_MODE_OUTPUT = 0x4,
  20. GPIO_PIN_MODE_BUILT_IN_FLASH_LED = 0x5,
  21. GPIO_PIN_MODE_OUTPUT_PWM = 0x6,
  22. GPIO_PIN_MODE_EXTERNAL_FLASH_PWM = 0x7,
  23. GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X = 0x8,
  24. } gpio_pin_mode_t;
  25. struct GpioResult {
  26. gpio_num_t gpio;
  27. int value;
  28. bool stopEvent;
  29. };
  30. struct GpioIsrContext {
  31. QueueHandle_t eventQueue;
  32. gpio_num_t gpio;
  33. };
  34. typedef enum {
  35. GPIO_SET_SOURCE_INTERNAL = 0,
  36. GPIO_SET_SOURCE_MQTT = 1,
  37. GPIO_SET_SOURCE_HTTP = 2,
  38. } gpio_set_source;
  39. class GpioPin {
  40. public:
  41. 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);
  42. ~GpioPin();
  43. bool init(QueueHandle_t eventQueue);
  44. bool getValue(std::string* errorText);
  45. void setValue(bool value, gpio_set_source setSource, std::string* errorText, bool publishMqtt = true);
  46. #ifdef ENABLE_MQTT
  47. bool handleMQTT(std::string, char* data, int data_len);
  48. #endif //ENABLE_MQTT
  49. void publishState();
  50. void gpioInterrupt(int value);
  51. gpio_int_type_t getInterruptType() { return _interruptType; }
  52. gpio_pin_mode_t getMode() { return _mode; }
  53. gpio_num_t getGPIO(){return _gpio;};
  54. bool isHttpEnabled() const { return _httpEnable; }
  55. const std::string &getMqttTopic() const { return _mqttTopic; }
  56. private:
  57. gpio_num_t _gpio;
  58. std::string _name;
  59. gpio_pin_mode_t _mode;
  60. gpio_int_type_t _interruptType;
  61. std::string _mqttTopic;
  62. bool _httpEnable = false;
  63. bool _isrRegistered = false;
  64. GpioIsrContext _isrContext = {};
  65. std::atomic<int> currentState{-1};
  66. };
  67. esp_err_t callHandleHttpRequest(httpd_req_t *req);
  68. class GpioHandler {
  69. public:
  70. GpioHandler(std::string configFile, httpd_handle_t httpServer);
  71. ~GpioHandler();
  72. void init();
  73. void deinit();
  74. void registerGpioUri();
  75. esp_err_t handleHttpRequest(httpd_req_t *req);
  76. void taskHandler();
  77. void gpioInterrupt(GpioResult* gpioResult);
  78. void flashLightEnable(bool value);
  79. bool isEnabled() const { return _isEnabled.load(); }
  80. #ifdef ENABLE_MQTT
  81. void handleMQTTconnect();
  82. #endif //ENABLE_MQTT
  83. private:
  84. std::string _configFile;
  85. httpd_handle_t _httpServer;
  86. std::map<gpio_num_t, GpioPin*> *gpioMap = NULL;
  87. std::recursive_mutex _mapMutex;
  88. QueueHandle_t _eventQueue = NULL;
  89. TaskHandle_t _workerTask = NULL;
  90. SemaphoreHandle_t _workerStopped = NULL;
  91. std::atomic<bool> _isEnabled{false};
  92. int LEDNumbers = 2;
  93. Rgb LEDColor = Rgb{ 255, 255, 255 };
  94. LedType LEDType = LED_WS2812;
  95. #ifdef __LEDGLOBAL
  96. SmartLed *leds_global = NULL;
  97. #endif
  98. bool readConfig();
  99. void clear();
  100. static void taskEntry(void *arg);
  101. void workerLoop();
  102. gpio_pin_mode_t resolvePinMode(std::string input);
  103. gpio_int_type_t resolveIntType(std::string input);
  104. };
  105. void gpio_handler_create(httpd_handle_t server);
  106. void gpio_handler_init();
  107. void gpio_handler_deinit();
  108. void gpio_handler_destroy();
  109. GpioHandler* gpio_handler_get();
  110. #endif //SERVER_GPIO_H