ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.3KB

  1. #pragma once
  2. #include <string>
  3. #include <cJSON.h>
  4. #include "mqtt.hpp"
  5. class Device {
  6. public:
  7. Device(std::string id);
  8. void set_mqtt_config(cJSON*);
  9. void lock() const { xSemaphoreTake(mutex, portMAX_DELAY); }
  10. void unlock() const { xSemaphoreGive(mutex); }
  11. bool wait(int timeout_ms = -1) const {
  12. return pdTRUE == xSemaphoreTake(
  13. sem,
  14. (timeout_ms < 0 ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms))
  15. );
  16. }
  17. // Only call while locked
  18. const std::string& get_effect() const { return effect; }
  19. bool is_on() const { return power_on; }
  20. private:
  21. std::string id;
  22. std::string state_topic;
  23. std::string cmd_topic;
  24. bool ready, should_publish;
  25. SemaphoreHandle_t mutex;
  26. SemaphoreHandle_t sem;
  27. bool power_on;
  28. std::string effect;
  29. esp_mqtt_client_handle_t client;
  30. static void on_mqtt_connect(esp_mqtt_client_handle_t client, void* data) {
  31. ((Device*)data)->on_mqtt_connect(client);
  32. }
  33. static void on_mqtt_message(esp_mqtt_client_handle_t client, esp_mqtt_event_handle_t event, void* data) {
  34. ((Device*)data)->on_mqtt_message(client, event);
  35. }
  36. void on_mqtt_connect(esp_mqtt_client_handle_t);
  37. void on_mqtt_message(esp_mqtt_client_handle_t, esp_mqtt_event_handle_t);
  38. void publish_state_locked();
  39. };