server_GPIO.h 3.0 KB

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