server_GPIO.h 3.4 KB

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