server_GPIO.h 2.8 KB

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