server_GPIO.h 2.6 KB

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