server_GPIO.h 3.4 KB

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