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.

109 lines
2.8 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 : public 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. /**
  41. * Turn on the master switch for the LEDs.
  42. */
  43. void turn_on() {
  44. master1_pin_->turn_on();
  45. master2_pin_->turn_on();
  46. is_on_ = true;
  47. }
  48. /**
  49. * Turn off the master switch for the LEDs.
  50. */
  51. void turn_off() {
  52. master1_pin_->turn_off();
  53. master2_pin_->turn_off();
  54. is_on_ = false;
  55. }
  56. /**
  57. * Check if the light is turned on.
  58. */
  59. bool is_on() {
  60. return is_on_;
  61. }
  62. void set_state(GPIOOutputValues *new_state) {
  63. new_state->copy_to(this);
  64. red_pin_->set_level(this->red);
  65. green_pin_->set_level(this->green);
  66. blue_pin_->set_level(this->blue);
  67. white_pin_->set_level(this->white);
  68. }
  69. void set_rgbw(float r, float g, float b, float w) {
  70. red_pin_->set_level(r);
  71. green_pin_->set_level(g);
  72. blue_pin_->set_level(b);
  73. white_pin_->set_level(w);
  74. this->red = r;
  75. this->green = g;
  76. this->blue = b;
  77. this->white = w;
  78. }
  79. void set_light_mode(std::string light_mode) {
  80. this->light_mode = light_mode;
  81. }
  82. protected:
  83. bool is_on_{false};
  84. ledc::LEDCOutput *red_pin_;
  85. ledc::LEDCOutput *green_pin_;
  86. ledc::LEDCOutput *blue_pin_;
  87. ledc::LEDCOutput *white_pin_;
  88. gpio::GPIOBinaryOutput *master1_pin_;
  89. gpio::GPIOBinaryOutput *master2_pin_;
  90. };
  91. } // namespace bslamp2
  92. } // namespace xiaomi
  93. } // namespace esphome