server_GPIO.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. bool 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. bool getValue(std::string* errorText);
  33. void setValue(bool value, gpio_set_source setSource, std::string* errorText);
  34. void init();
  35. bool handleMQTT(std::string, char* data, int data_len);
  36. void gpioInterrupt(bool value);
  37. gpio_int_type_t getInterruptType() { return _interruptType; }
  38. gpio_pin_mode_t getMode() { return _mode; }
  39. private:
  40. gpio_num_t _gpio;
  41. const char* _name;
  42. gpio_pin_mode_t _mode;
  43. gpio_int_type_t _interruptType;
  44. std::string _mqttTopic;
  45. };
  46. esp_err_t callHandleHttpRequest(httpd_req_t *req);
  47. void taskGpioHandler(void *pvParameter);
  48. class GpioHandler {
  49. public:
  50. GpioHandler(std::string configFile, httpd_handle_t httpServer);
  51. ~GpioHandler();
  52. void init();
  53. void deinit();
  54. void registerGpioUri();
  55. esp_err_t handleHttpRequest(httpd_req_t *req);
  56. void gpioInterrupt(GpioResult* gpioResult);
  57. void flashLightEnable(bool value);
  58. bool isEnabled() { return _isEnabled; }
  59. private:
  60. std::string _configFile;
  61. httpd_handle_t _httpServer;
  62. std::map<gpio_num_t, GpioPin*> *gpioMap = NULL;
  63. TaskHandle_t xHandletaskGpioHandler = NULL;
  64. bool _isEnabled = false;
  65. bool readConfig();
  66. void clear();
  67. gpio_num_t resolvePinNr(uint8_t pinNr);
  68. gpio_pin_mode_t resolvePinMode(std::string input);
  69. gpio_int_type_t resolveIntType(std::string input);
  70. };
  71. void GpioHandlerStart();
  72. #endif //SERVER_GPIO_H