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.

127 lines
3.3 KiB

  1. #pragma once
  2. #include "esphome/components/gpio/output/gpio_binary_output.h"
  3. #include "esphome/components/ledc/ledc_output.h"
  4. #include "esphome/core/component.h"
  5. namespace esphome {
  6. namespace xiaomi {
  7. namespace bslamp2 {
  8. static const std::string LIGHT_MODE_UNKNOWN{"unknown"};
  9. static const std::string LIGHT_MODE_OFF{"off"};
  10. static const std::string LIGHT_MODE_RGB{"rgb"};
  11. static const std::string LIGHT_MODE_WHITE{"white"};
  12. static const std::string LIGHT_MODE_NIGHT{"night"};
  13. class GPIOOutputValues {
  14. public:
  15. float red = 0.0f;
  16. float green = 0.0f;
  17. float blue = 0.0f;
  18. float white = 0.0f;
  19. std::string light_mode = LIGHT_MODE_OFF;
  20. /**
  21. * Copies the current output values to another GPIOOutputValues object.
  22. */
  23. void copy_to(GPIOOutputValues *other) {
  24. other->red = red;
  25. other->green = green;
  26. other->blue = blue;
  27. other->white = white;
  28. other->light_mode = light_mode;
  29. }
  30. void log(const char *prefix) { ESP_LOGD(TAG, "%s: RGB=[%f,%f,%f], white=%f", prefix, red, green, blue, white); }
  31. };
  32. class LightHAL : Component, public GPIOOutputValues {
  33. public:
  34. void set_red_pin(ledc::LEDCOutput *pin) { red_pin_ = pin; }
  35. void set_green_pin(ledc::LEDCOutput *pin) { green_pin_ = pin; }
  36. void set_blue_pin(ledc::LEDCOutput *pin) { blue_pin_ = pin; }
  37. void set_white_pin(ledc::LEDCOutput *pin) { white_pin_ = pin; }
  38. void set_master1_pin(gpio::GPIOBinaryOutput *pin) { master1_pin_ = pin; }
  39. void set_master2_pin(gpio::GPIOBinaryOutput *pin) { master2_pin_ = pin; }
  40. void add_on_light_mode_callback(std::function<void(std::string)> &&callback) {
  41. light_mode_callback_.add(std::move(callback));
  42. }
  43. void add_on_state_callback(std::function<void(light::LightColorValues)> &&callback) {
  44. state_callback_.add(std::move(callback));
  45. }
  46. /**
  47. * Turn on the master switch for the LEDs.
  48. */
  49. void turn_on() {
  50. master1_pin_->turn_on();
  51. master2_pin_->turn_on();
  52. is_on_ = true;
  53. }
  54. /**
  55. * Turn off the master switch for the LEDs.
  56. */
  57. void turn_off() {
  58. master1_pin_->turn_off();
  59. master2_pin_->turn_off();
  60. is_on_ = false;
  61. }
  62. /**
  63. * Check if the light is turned on.
  64. */
  65. bool is_on() {
  66. return is_on_;
  67. }
  68. void set_state(GPIOOutputValues *new_state) {
  69. new_state->copy_to(this);
  70. red_pin_->set_level(this->red);
  71. green_pin_->set_level(this->green);
  72. blue_pin_->set_level(this->blue);
  73. white_pin_->set_level(this->white);
  74. }
  75. void set_rgbw(float r, float g, float b, float w) {
  76. red_pin_->set_level(r);
  77. green_pin_->set_level(g);
  78. blue_pin_->set_level(b);
  79. white_pin_->set_level(w);
  80. this->red = r;
  81. this->green = g;
  82. this->blue = b;
  83. this->white = w;
  84. }
  85. void set_light_mode(std::string light_mode) {
  86. this->light_mode = light_mode;
  87. }
  88. void do_light_mode_callback(std::string light_mode) {
  89. light_mode_callback_.call(light_mode);
  90. }
  91. void do_state_callback(light::LightColorValues values) {
  92. state_callback_.call(values);
  93. }
  94. protected:
  95. bool is_on_{false};
  96. ledc::LEDCOutput *red_pin_;
  97. ledc::LEDCOutput *green_pin_;
  98. ledc::LEDCOutput *blue_pin_;
  99. ledc::LEDCOutput *white_pin_;
  100. gpio::GPIOBinaryOutput *master1_pin_;
  101. gpio::GPIOBinaryOutput *master2_pin_;
  102. CallbackManager<void(std::string)> light_mode_callback_{};
  103. CallbackManager<void(light::LightColorValues)> state_callback_{};
  104. };
  105. } // namespace bslamp2
  106. } // namespace xiaomi
  107. } // namespace esphome