server_GPIO.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef SERVER_GPIO_H
  2. #define SERVER_GPIO_H
  3. #include <esp_log.h>
  4. #include <esp_http_server.h>
  5. #include <map>
  6. #include "driver/gpio.h"
  7. #include "SmartLeds.h"
  8. //#include "ClassControllCamera.h"
  9. // wenn __LEDGLOBAL definiert ist, wird eine globale Variable für die LED-Ansteuerung verwendet, ansonsten lokal und jedesmal neu
  10. #define __LEDGLOBAL
  11. typedef enum {
  12. GPIO_PIN_MODE_DISABLED = 0x0,
  13. GPIO_PIN_MODE_INPUT = 0x1,
  14. GPIO_PIN_MODE_INPUT_PULLUP = 0x2,
  15. GPIO_PIN_MODE_INPUT_PULLDOWN = 0x3,
  16. GPIO_PIN_MODE_OUTPUT = 0x4,
  17. GPIO_PIN_MODE_BUILT_IN_FLASH_LED = 0x5,
  18. GPIO_PIN_MODE_OUTPUT_PWM = 0x6,
  19. GPIO_PIN_MODE_EXTERNAL_FLASH_PWM = 0x7,
  20. GPIO_PIN_MODE_EXTERNAL_FLASH_WS281X = 0x8,
  21. } gpio_pin_mode_t;
  22. struct GpioResult {
  23. gpio_num_t gpio;
  24. int value;
  25. };
  26. typedef enum {
  27. GPIO_SET_SOURCE_INTERNAL = 0,
  28. GPIO_SET_SOURCE_MQTT = 1,
  29. GPIO_SET_SOURCE_HTTP = 2,
  30. } gpio_set_source;
  31. class GpioPin {
  32. public:
  33. 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);
  34. ~GpioPin();
  35. void init();
  36. bool getValue(std::string* errorText);
  37. void setValue(bool value, gpio_set_source setSource, std::string* errorText);
  38. #ifdef ENABLE_MQTT
  39. bool handleMQTT(std::string, char* data, int data_len);
  40. #endif //ENABLE_MQTT
  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. public:
  58. GpioHandler(std::string configFile, httpd_handle_t httpServer);
  59. ~GpioHandler();
  60. void init();
  61. void deinit();
  62. void registerGpioUri();
  63. esp_err_t handleHttpRequest(httpd_req_t *req);
  64. void taskHandler();
  65. void gpioInterrupt(GpioResult* gpioResult);
  66. void flashLightEnable(bool value);
  67. bool isEnabled() { return _isEnabled; }
  68. #ifdef ENABLE_MQTT
  69. void handleMQTTconnect();
  70. #endif //ENABLE_MQTT
  71. private:
  72. std::string _configFile;
  73. httpd_handle_t _httpServer;
  74. std::map<gpio_num_t, GpioPin*> *gpioMap = NULL;
  75. TaskHandle_t xHandleTaskGpio = NULL;
  76. bool _isEnabled = false;
  77. int LEDNumbers = 2;
  78. Rgb LEDColor = Rgb{ 255, 255, 255 };
  79. LedType LEDType = LED_WS2812;
  80. #ifdef __LEDGLOBAL
  81. SmartLed *leds_global = NULL;
  82. #endif
  83. bool readConfig();
  84. void clear();
  85. gpio_num_t resolvePinNr(uint8_t pinNr);
  86. gpio_pin_mode_t resolvePinMode(std::string input);
  87. gpio_int_type_t resolveIntType(std::string input);
  88. };
  89. void gpio_handler_create(httpd_handle_t server);
  90. void gpio_handler_init();
  91. void gpio_handler_deinit();
  92. void gpio_handler_destroy();
  93. GpioHandler* gpio_handler_get();
  94. #endif //SERVER_GPIO_H