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.

196 lines
5.9 KiB

3 years ago
  1. #pragma once
  2. #include "esphome.h"
  3. #define CONSTANT_BRIGHTNESS true
  4. // The lamp circuitry does not support having RGB and white
  5. // channels active at the same time. Therefore, color interlock
  6. // must be enabled.
  7. #define COLOR_INTERLOCK true
  8. // Same range as supported by the original Yeelight firmware.
  9. #define HOME_ASSISTANT_MIRED_MIN 153
  10. #define HOME_ASSISTANT_MIRED_MAX 588
  11. namespace esphome {
  12. namespace rgbww {
  13. class YeelightBedsideLampV2LightOutput : public Component, public LightOutput
  14. {
  15. public:
  16. YeelightBedsideLampV2LightOutput(
  17. FloatOutput *r, FloatOutput *g, FloatOutput *b, FloatOutput *w,
  18. esphome::gpio::GPIOBinaryOutput *m1, esphome::gpio::GPIOBinaryOutput *m2) :
  19. red_(r), green_(g), blue_(b), white_(w), master1_(m1), master2_(m2) {}
  20. LightTraits get_traits() override
  21. {
  22. auto traits = LightTraits();
  23. traits.set_supports_rgb(true);
  24. traits.set_supports_color_temperature(true);
  25. traits.set_supports_brightness(true);
  26. traits.set_supports_rgb_white_value(false);
  27. traits.set_supports_color_interlock(COLOR_INTERLOCK);
  28. traits.set_min_mireds(HOME_ASSISTANT_MIRED_MIN);
  29. traits.set_max_mireds(HOME_ASSISTANT_MIRED_MAX);
  30. return traits;
  31. }
  32. void write_state(LightState *state) override
  33. {
  34. auto values = state->current_values;
  35. ESP_LOGD("custom", "B = State %f, RGB %f %f %f, BRI %f, TEMP %f",
  36. values.get_state(),
  37. values.get_red(), values.get_green(), values.get_blue(),
  38. values.get_brightness(), values.get_color_temperature());
  39. // Power down the light when its state is 'off'.
  40. if (values.get_state() == 0) {
  41. this->turn_off_();
  42. return;
  43. }
  44. // Leave it to the default tooling to figure out the basics.
  45. // Because of the color interlocking, there are two possible outcomes:
  46. // - red, green, blue zero -> the light is in color temperature mode
  47. // - cwhite, wwhite zero -> the light is in RGB mode
  48. float red, green, blue, cwhite, wwhite;
  49. state->current_values_as_rgbww(
  50. &red, &green, &blue, &cwhite, &wwhite,
  51. CONSTANT_BRIGHTNESS, COLOR_INTERLOCK);
  52. if (cwhite > 0 || wwhite > 0) {
  53. this->turn_on_in_color_temperature_mode_(
  54. values.get_color_temperature(), values.get_brightness());
  55. } else {
  56. this->turn_on_in_rgb_mode_(
  57. values.get_red(), values.get_green(), values.get_blue(), values.get_brightness());
  58. }
  59. }
  60. private:
  61. FloatOutput *red_;
  62. FloatOutput *green_;
  63. FloatOutput *blue_;
  64. FloatOutput *white_;
  65. esphome::gpio::GPIOBinaryOutput *master1_;
  66. esphome::gpio::GPIOBinaryOutput *master2_;
  67. void turn_off_()
  68. {
  69. master1_->turn_off();
  70. red_->set_level(0);
  71. green_->set_level(0);
  72. blue_->set_level(0);
  73. white_->set_level(0);
  74. }
  75. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness)
  76. {
  77. ESP_LOGD("custom", "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  78. // This tunes the power for the red channel a bit, to bring
  79. // the red=1, green=1, blue=1 color more towards white.
  80. // (on my lamps, there is a faint hint of red in the color)
  81. red = red * 0.95f;
  82. // Compensate for brightness.
  83. red = red * brightness;
  84. green = green * brightness;
  85. blue = blue * brightness;
  86. // Inverse the signal. The LEDs in the lamp's circuit are brighter
  87. // when the voltages on the GPIO pins are lower.
  88. red = 1.0f - red;
  89. green = 1.0f - green;
  90. blue = 1.0f - blue;
  91. float white = 0.0;
  92. ESP_LOGD("rgb_mode", "LED state : RGBW %f, %f, %f, %f", red, green, blue, white);
  93. // Drive the LEDs.
  94. red_->set_level(red);
  95. green_->set_level(green);
  96. blue_->set_level(blue);
  97. white_->set_level(white);
  98. master1_->turn_on();
  99. }
  100. void turn_on_in_color_temperature_mode_(float temperature, float brightness)
  101. {
  102. ESP_LOGD("temperature_mode", "Activate TEMPERATURE %f, BRIGHTNESS %f", temperature, brightness);
  103. float volt_scaler = 3.23f;
  104. if (temperature <= HOME_ASSISTANT_MIRED_MAX && temperature >= 371)
  105. {
  106. float start = 371;
  107. float end = 588;
  108. float band = end - start;
  109. float red_volt = 2.86f * (1.0f - brightness);
  110. float red = red_volt / volt_scaler;
  111. float green_1 = 2.90f + (temperature - start) * (2.97f - 2.90f) / band;
  112. float green_100 = 0.45f + (temperature - start) * (1.13f - 0.45f) / band;
  113. float green_volt = green_1 + brightness * (green_100 - green_1);
  114. float green = green_volt / volt_scaler;
  115. float blue = 1.0f;
  116. float white_1 = 0.28f - (temperature - start) * (0.28f - 0.19f) / band;
  117. float white_100 = 1.07f - (temperature - start) * (1.07f - 0.22f) / band;
  118. float white_volt = white_1 + brightness * (white_100 - white_1);
  119. float white = white_volt / volt_scaler;
  120. ESP_LOGD("temperature_mode", "LED state : RGBW %f, %f, %f, %f", red, green, blue, white);
  121. red_->set_level(red);
  122. green_->set_level(green);
  123. blue_->set_level(blue);
  124. white_->set_level(white);
  125. master2_->turn_on();
  126. master1_->turn_on();
  127. return;
  128. }
  129. else if (temperature >= 334)
  130. {
  131. float red_volt = (1.0f - brightness) * 2.86f;
  132. float red = red_volt / volt_scaler;
  133. float green_volt = 2.9f - brightness * (2.9f - 0.45f);
  134. float green = green_volt / volt_scaler;
  135. float blue = 1.0f;
  136. float white_volt = 0.28f + brightness * (1.07f - 0.28f);
  137. float white = white_volt / volt_scaler;
  138. ESP_LOGD("temperature_mode", "LED state : RGBW %f, %f, %f, %f", red, green, blue, white);
  139. red_->set_level(red);
  140. green_->set_level(green);
  141. blue_->set_level(blue);
  142. white_->set_level(white);
  143. master2_->turn_on();
  144. master1_->turn_on();
  145. return;
  146. } else if (temperature >= 313) {
  147. } else if (temperature >= 251) {
  148. } else if (temperature >= 223) {
  149. } else if (temperature >= HOME_ASSISTANT_MIRED_MIN) {
  150. }
  151. red_->set_level(0.5);
  152. green_->set_level(0.5);
  153. blue_->set_level(0.5);
  154. master2_->turn_off();
  155. master1_->turn_off();
  156. }
  157. };
  158. } // namespace rgbww
  159. } // namespace esphome