server_GPIO.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #ifndef SERVER_GPIO_H
  3. #define SERVER_GPIO_H
  4. #include <map>
  5. #include <driver/gpio.h>
  6. #include <esp_http_server.h>
  7. #include <esp_log.h>
  8. #include "SmartLeds.h"
  9. typedef enum
  10. {
  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_OUTPUT_PWM = 0x5,
  17. GPIO_PIN_MODE_OUTPUT_WS281X = 0x6,
  18. GPIO_PIN_MODE_BUILTIN_FLASH = 0x7,
  19. GPIO_PIN_MODE_BUILTIN_FLASH_PWM = 0x8,
  20. } gpio_pin_mode_t;
  21. struct GpioResult
  22. {
  23. gpio_num_t gpio;
  24. int value;
  25. };
  26. typedef enum
  27. {
  28. GPIO_SET_SOURCE_INTERNAL = 0,
  29. GPIO_SET_SOURCE_MQTT = 1,
  30. GPIO_SET_SOURCE_HTTP = 2,
  31. } gpio_set_source;
  32. class GpioPin
  33. {
  34. public:
  35. 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);
  36. ~GpioPin();
  37. void init();
  38. bool getValue(std::string *errorText);
  39. void setValue(bool value, gpio_set_source setSource, std::string *errorText);
  40. bool handleMQTT(std::string, char *data, int data_len);
  41. void publishState();
  42. void gpioInterrupt(int value);
  43. gpio_int_type_t getInterruptType() { return _interruptType; }
  44. gpio_pin_mode_t getMode() { return _mode; }
  45. gpio_num_t getGPIO() { return _gpio; };
  46. private:
  47. gpio_num_t _gpio;
  48. const char *_name;
  49. gpio_pin_mode_t _mode;
  50. gpio_int_type_t _interruptType;
  51. std::string _mqttTopic;
  52. int currentState = -1;
  53. };
  54. esp_err_t callHandleHttpRequest(httpd_req_t *req);
  55. void taskGpioHandler(void *pvParameter);
  56. class GpioHandler
  57. {
  58. public:
  59. GpioHandler(std::string configFile, httpd_handle_t httpServer);
  60. ~GpioHandler();
  61. void init();
  62. void deinit();
  63. void registerGpioUri();
  64. esp_err_t handleHttpRequest(httpd_req_t *req);
  65. void taskHandler();
  66. void gpioInterrupt(GpioResult *gpioResult);
  67. void flashLightEnable(bool value);
  68. bool isEnabled() { return _isEnabled; }
  69. void handleMQTTconnect();
  70. private:
  71. httpd_handle_t _httpServer;
  72. std::map<gpio_num_t, GpioPin *> *gpioMap = NULL;
  73. TaskHandle_t xHandleTaskGpio = NULL;
  74. bool _isEnabled = false;
  75. int LEDNumbers = 2;
  76. Rgb LEDColor = Rgb{255, 255, 255};
  77. LedType LEDType = LED_WS2812;
  78. #ifdef __LEDGLOBAL
  79. SmartLed *leds_global = NULL;
  80. #endif
  81. bool readConfig();
  82. void clear();
  83. gpio_num_t resolvePinNr(uint8_t pinNr);
  84. gpio_pin_mode_t resolvePinMode(std::string input);
  85. gpio_int_type_t resolveIntType(std::string input);
  86. };
  87. void gpio_handler_create(httpd_handle_t server);
  88. void gpio_handler_init();
  89. void gpio_handler_deinit();
  90. void gpio_handler_destroy();
  91. GpioHandler *gpio_handler_get();
  92. #endif // SERVER_GPIO_H