ESP32 Native version of Blinky, featureful controller code for WS2811/WS2812/NeoPixels
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

63 rindas
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 state_topic;
  25. std::string cmd_topic;
  26. std::string data_topic;
  27. bool ready, should_publish;
  28. SemaphoreHandle_t mutex;
  29. SemaphoreHandle_t sem;
  30. bool power_on;
  31. std::string effect;
  32. int strip_length;
  33. esp_mqtt_client_handle_t client;
  34. static void on_mqtt_connect(esp_mqtt_client_handle_t client, void* data) {
  35. ((Device*)data)->on_mqtt_connect(client);
  36. }
  37. static void on_mqtt_message(esp_mqtt_client_handle_t client, esp_mqtt_event_handle_t event, void* data) {
  38. ((Device*)data)->on_mqtt_message(client, event);
  39. }
  40. void on_mqtt_connect(esp_mqtt_client_handle_t);
  41. void on_mqtt_message(esp_mqtt_client_handle_t, esp_mqtt_event_handle_t);
  42. void publish_state_locked();
  43. };