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.

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