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.

243 lines
9.5 KiB

3 years ago
3 years ago
3 years ago
  1. #pragma once
  2. #include "esphome/core/component.h"
  3. #include "esphome/components/ledc/ledc_output.h"
  4. #include "esphome/components/light/light_output.h"
  5. #include "esphome/components/gpio/output/gpio_binary_output.h"
  6. // What seems to be a bug in ESPHome transitioning: when turning on
  7. // the device, the brightness is scaled along with the state (which
  8. // runs from 0 to 1), but when turning off the device, the brightness
  9. // is kept the same while the state goes down from 1 to 0. As a result
  10. // when turning off the lamp with a transition time of 1s, the light
  11. // stays on for 1s and then turn itself off abruptly.
  12. //
  13. // Reported the issue + fix at:
  14. // https://github.com/esphome/esphome/pull/1643
  15. //
  16. // A work-around for this issue can be enabled using the following
  17. // define. Note that the code provides a forward-compatible fix, so
  18. // having this define active with a fixed ESPHome version should
  19. // not be a problem.
  20. #define TRANSITION_TO_OFF_BUGFIX
  21. namespace esphome {
  22. namespace rgbww {
  23. static const char *TAG = "yeelight_bs2.light";
  24. // Same range as supported by the original Yeelight firmware.
  25. static const int HOME_ASSISTANT_MIRED_MIN = 153;
  26. static const int HOME_ASSISTANT_MIRED_MAX = 588;
  27. // The PWM frequencies as used by the original device
  28. // for driving the LED circuitry.
  29. const float RGB_PWM_FREQUENCY = 3000.0f;
  30. const float WHITE_PWM_FREQUENCY = 10000.0f;
  31. class YeelightBS2LightOutput : public Component, public light::LightOutput
  32. {
  33. public:
  34. light::LightTraits get_traits() override
  35. {
  36. auto traits = light::LightTraits();
  37. traits.set_supports_rgb(true);
  38. traits.set_supports_color_temperature(true);
  39. traits.set_supports_brightness(true);
  40. traits.set_supports_rgb_white_value(false);
  41. traits.set_supports_color_interlock(true);
  42. traits.set_min_mireds(HOME_ASSISTANT_MIRED_MIN);
  43. traits.set_max_mireds(HOME_ASSISTANT_MIRED_MAX);
  44. return traits;
  45. }
  46. void set_red_output(ledc::LEDCOutput *red) {
  47. red_ = red;
  48. red_->set_frequency(RGB_PWM_FREQUENCY);
  49. }
  50. void set_green_output(ledc::LEDCOutput *green) {
  51. green_ = green;
  52. green_->set_frequency(RGB_PWM_FREQUENCY);
  53. }
  54. void set_blue_output(ledc::LEDCOutput *blue) {
  55. blue_ = blue;
  56. blue_->set_frequency(RGB_PWM_FREQUENCY);
  57. }
  58. void set_white_output(ledc::LEDCOutput *white) {
  59. white_ = white;
  60. white_->set_frequency(WHITE_PWM_FREQUENCY);
  61. }
  62. void set_master1_output(gpio::GPIOBinaryOutput *master1) {
  63. master1_ = master1;
  64. }
  65. void set_master2_output(gpio::GPIOBinaryOutput *master2) {
  66. master2_ = master2;
  67. }
  68. void write_state(light::LightState *state) override
  69. {
  70. auto values = state->current_values;
  71. ESP_LOGD(TAG, "B = State %f, RGB %f %f %f, BRI %f, TEMP %f",
  72. values.get_state(),
  73. values.get_red(), values.get_green(), values.get_blue(),
  74. values.get_brightness(), values.get_color_temperature());
  75. // Power down the light when its state is 'off'.
  76. if (values.get_state() == 0)
  77. {
  78. this->turn_off_();
  79. #ifdef TRANSITION_TO_OFF_BUGFIX
  80. previous_state_ = -1;
  81. previous_brightness_ = 0;
  82. #endif
  83. return;
  84. }
  85. auto brightness = values.get_brightness();
  86. #ifdef TRANSITION_TO_OFF_BUGFIX
  87. // Remember the brightness that is used when the light is fully ON.
  88. if (values.get_state() == 1) {
  89. previous_brightness_ = brightness;
  90. }
  91. // When transitioning towards zero brightness ...
  92. else if (values.get_state() < previous_state_) {
  93. // ... check if the prevous brightness is the same as the current
  94. // brightness. If yes, then the brightness isn't being scaled ...
  95. if (previous_brightness_ == brightness) {
  96. // ... and we need to do that ourselves.
  97. brightness = values.get_state() * brightness;
  98. }
  99. }
  100. previous_state_ = values.get_state();
  101. #endif
  102. // Leave it to the default tooling to figure out the basics.
  103. // Because of the color interlocking, there are two possible outcomes:
  104. // - red, green, blue zero -> the light is in color temperature mode
  105. // - cwhite, wwhite zero -> the light is in RGB mode
  106. float red, green, blue, cwhite, wwhite;
  107. state->current_values_as_rgbww(&red, &green, &blue, &cwhite, &wwhite, true, false);
  108. if (cwhite > 0 || wwhite > 0)
  109. {
  110. turn_on_in_white_mode_(values.get_color_temperature(), brightness);
  111. }
  112. else
  113. {
  114. // The RGB mode does not use the RGB values as determined by
  115. // current_values_as_rgbww(). The device has LED driving circuitry
  116. // that takes care of the required brightness curve while ramping up
  117. // the brightness. Therefore, the actual RGB values are passed here.
  118. turn_on_in_rgb_mode_(
  119. values.get_red(), values.get_green(), values.get_blue(),
  120. brightness, values.get_state());
  121. }
  122. }
  123. protected:
  124. ledc::LEDCOutput *red_;
  125. ledc::LEDCOutput *green_;
  126. ledc::LEDCOutput *blue_;
  127. ledc::LEDCOutput *white_;
  128. esphome::gpio::GPIOBinaryOutput *master1_;
  129. esphome::gpio::GPIOBinaryOutput *master2_;
  130. esphome::rgbww::yeelight_bs2::WhiteLight white_light_;
  131. #ifdef TRANSITION_TO_OFF_BUGFIX
  132. float previous_state_ = 1;
  133. float previous_brightness_ = -1;
  134. #endif
  135. void turn_off_()
  136. {
  137. red_->set_level(1);
  138. green_->set_level(1);
  139. blue_->set_level(1);
  140. white_->turn_off();
  141. master2_->turn_off();
  142. master1_->turn_off();
  143. }
  144. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness, float state)
  145. {
  146. ESP_LOGD(TAG, "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  147. // The brightness must be at least 3/100 to light up the LEDs.
  148. // During transitions (where state is a fraction between 0 and 1,
  149. // indicating the transition progress) we don't apply this to
  150. // get smoother transitioning when turning on the light.
  151. if (state == 1 && brightness < 0.03f)
  152. brightness = 0.03f;
  153. // Apply proper color mixing around the RGB white point.
  154. // Overall, the RGB colors are very usable when simply scaling the
  155. // RGB channels with the brightness, but around the white point,
  156. // the color is a bit on the red side of the spectrum. The following
  157. // scaling was created to fix that.
  158. // RGBW 0.432451, 0.013149, 0.556678
  159. // R 0.57 g 1 b 0.45
  160. auto red_w = (0.07f + brightness*(0.57f - 0.07f)) * red;
  161. auto green_w = (0.13f + brightness*(1.00f - 0.13f)) * green;
  162. auto blue_w = (0.06f + brightness*(0.45f - 0.06f)) * blue;
  163. // For other colors, we can simply scale the RGB channels with the
  164. // requested brightness, resulting in a very usable color. Not 100%
  165. // the same as the original firmware, but sometimes even better IMO.
  166. auto red_c = red * brightness;
  167. auto green_c = green * brightness;
  168. auto blue_c = blue * brightness;
  169. // The actual RGB values are a weighed mix of the above two.
  170. // The closer to the white point, the more the white point
  171. // value applies.
  172. auto level_red = (red_w * ((green+blue)/2)) + (red_c * (1-(green+blue)/2));
  173. auto level_green = (green_w * ((red+blue)/2)) + (green_c * (1-(red+blue)/2));
  174. auto level_blue = (blue_w * ((red+green)/2)) + (blue_c * (1-(red+green)/2));
  175. // Invert the signal. The LEDs in the lamp's circuit are brighter
  176. // when the pwm levels on the GPIO pins are lower.
  177. level_red = 1.0f - level_red;
  178. level_green = 1.0f - level_green;
  179. level_blue = 1.0f - level_blue;
  180. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, off", level_red, level_green, level_blue);
  181. // Drive the LEDs.
  182. master2_->turn_on();
  183. master1_->turn_on();
  184. red_->set_level(level_red);
  185. green_->set_level(level_green);
  186. blue_->set_level(level_blue);
  187. white_->turn_off();
  188. }
  189. void turn_on_in_white_mode_(float temperature, float brightness)
  190. {
  191. ESP_LOGD(TAG, "Activate TEMPERATURE %f, BRIGHTNESS %f",
  192. temperature, brightness);
  193. white_light_.set_color(temperature, brightness);
  194. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f",
  195. white_light_.red, white_light_.green, white_light_.blue,
  196. white_light_.white);
  197. master2_->turn_on();
  198. master1_->turn_on();
  199. red_->set_level(white_light_.red);
  200. green_->set_level(white_light_.green);
  201. blue_->set_level(white_light_.blue);
  202. white_->turn_on();
  203. white_->set_level(white_light_.white);
  204. }
  205. };
  206. } // namespace rgbww
  207. } // namespace esphome