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.

63 lines
1.5KB

  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_json_config(const 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. int get_strip_length() const { return strip_length; }
  21. cJSON* make_json_config_locked() const;
  22. private:
  23. std::string id;
  24. std::string topic_prefix;
  25. bool ready, should_publish;
  26. SemaphoreHandle_t mutex;
  27. SemaphoreHandle_t sem;
  28. bool power_on;
  29. std::string effect;
  30. int strip_length;
  31. esp_mqtt_client_handle_t client;
  32. static void on_mqtt_connect(esp_mqtt_client_handle_t client, void* data) {
  33. ((Device*)data)->on_mqtt_connect(client);
  34. }
  35. static void on_mqtt_message(esp_mqtt_client_handle_t client, esp_mqtt_event_handle_t event, void* data) {
  36. ((Device*)data)->on_mqtt_message(client, event);
  37. }
  38. void on_mqtt_connect(esp_mqtt_client_handle_t);
  39. void on_mqtt_message(esp_mqtt_client_handle_t, esp_mqtt_event_handle_t);
  40. void publish_state_locked();
  41. std::string subtopic(const std::string &topic);
  42. };