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.

334 lines
15 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. {
  23. namespace rgbww
  24. {
  25. static const char *TAG = "yeelight_bs2.light";
  26. // Same range as supported by the original Yeelight firmware.
  27. static const int HOME_ASSISTANT_MIRED_MIN = 153;
  28. static const int HOME_ASSISTANT_MIRED_MAX = 588;
  29. // The PWM frequency as used by the original device
  30. // for driving the LED circuitry.
  31. const float PWM_FREQUENCY = 3000.0f;
  32. class YeelightBS2LightOutput : public Component, public light::LightOutput
  33. {
  34. public:
  35. void set_red(ledc::LEDCOutput *red) { red_ = red; red_->set_frequency(PWM_FREQUENCY); }
  36. void set_green(ledc::LEDCOutput *green) { green_ = green; green_->set_frequency(PWM_FREQUENCY); }
  37. void set_blue(ledc::LEDCOutput *blue) { blue_ = blue; blue_->set_frequency(PWM_FREQUENCY); }
  38. void set_white(ledc::LEDCOutput *white) { white_ = white; white_->set_frequency(PWM_FREQUENCY); }
  39. void set_master1(gpio::GPIOBinaryOutput *master1) { master1_ = master1; }
  40. void set_master2(gpio::GPIOBinaryOutput *master2) { master2_ = master2; }
  41. light::LightTraits get_traits() override
  42. {
  43. auto traits = light::LightTraits();
  44. traits.set_supports_rgb(true);
  45. traits.set_supports_color_temperature(true);
  46. traits.set_supports_brightness(true);
  47. traits.set_supports_rgb_white_value(false);
  48. traits.set_supports_color_interlock(true);
  49. traits.set_min_mireds(HOME_ASSISTANT_MIRED_MIN);
  50. traits.set_max_mireds(HOME_ASSISTANT_MIRED_MAX);
  51. return traits;
  52. }
  53. void write_state(light::LightState *state) override
  54. {
  55. auto values = state->current_values;
  56. ESP_LOGD(TAG, "B = State %f, RGB %f %f %f, BRI %f, TEMP %f",
  57. values.get_state(),
  58. values.get_red(), values.get_green(), values.get_blue(),
  59. values.get_brightness(), values.get_color_temperature());
  60. // Power down the light when its state is 'off'.
  61. if (values.get_state() == 0)
  62. {
  63. this->turn_off_();
  64. #ifdef TRANSITION_TO_OFF_BUGFIX
  65. previous_state_ = -1;
  66. previous_brightness_ = 0;
  67. #endif
  68. return;
  69. }
  70. auto brightness = values.get_brightness();
  71. #ifdef TRANSITION_TO_OFF_BUGFIX
  72. // Remember the brightness that is used when the light is fully ON.
  73. if (values.get_state() == 1) {
  74. previous_brightness_ = brightness;
  75. }
  76. // When transitioning towards zero brightness ...
  77. else if (values.get_state() < previous_state_) {
  78. // ... check if the prevous brightness is the same as the current
  79. // brightness. If yes, then the brightness isn't being scaled ...
  80. if (previous_brightness_ == brightness) {
  81. // ... and we need to do that ourselves.
  82. brightness = values.get_state() * brightness;
  83. }
  84. }
  85. previous_state_ = values.get_state();
  86. #endif
  87. // Leave it to the default tooling to figure out the basics.
  88. // Because of the color interlocking, there are two possible outcomes:
  89. // - red, green, blue zero -> the light is in color temperature mode
  90. // - cwhite, wwhite zero -> the light is in RGB mode
  91. float red, green, blue, cwhite, wwhite;
  92. state->current_values_as_rgbww(&red, &green, &blue, &cwhite, &wwhite, true, false);
  93. if (cwhite > 0 || wwhite > 0)
  94. {
  95. this->turn_on_in_color_temperature_mode_(
  96. values.get_color_temperature(), brightness);
  97. }
  98. else
  99. {
  100. // The RGB mode does not use the RGB values as determined by
  101. // current_values_as_rgbww(). The device has LED driving circuitry
  102. // that takes care of the required brightness curve while ramping up
  103. // the brightness. Therefore, the actual RGB values are passed here.
  104. this->turn_on_in_rgb_mode_(
  105. values.get_red(), values.get_green(), values.get_blue(),
  106. brightness, values.get_state());
  107. }
  108. }
  109. protected:
  110. ledc::LEDCOutput *red_;
  111. ledc::LEDCOutput *green_;
  112. ledc::LEDCOutput *blue_;
  113. ledc::LEDCOutput *white_;
  114. esphome::gpio::GPIOBinaryOutput *master1_;
  115. esphome::gpio::GPIOBinaryOutput *master2_;
  116. #ifdef TRANSITION_TO_OFF_BUGFIX
  117. float previous_state_ = 1;
  118. float previous_brightness_ = -1;
  119. #endif
  120. void turn_off_()
  121. {
  122. red_->set_level(1);
  123. green_->set_level(1);
  124. blue_->set_level(1);
  125. white_->turn_off();
  126. master2_->turn_off();
  127. master1_->turn_off();
  128. }
  129. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness, float state)
  130. {
  131. ESP_LOGD(TAG, "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  132. // The brightness must be at least 3/100 to light up the LEDs.
  133. // During transitions (where state is a fraction between 0 and 1,
  134. // indicating the transition progress) we don't apply this to
  135. // get smoother transitioning when turning on the light.
  136. if (state == 1 && brightness < 0.03f)
  137. brightness = 0.03f;
  138. // Apply proper color mixing around the RGB white point.
  139. // Overall, the RGB colors are very usable when simply scaling the
  140. // RGB channels with the brightness, but around the white point,
  141. // the color is a bit on the red side of the spectrum. The following
  142. // scaling was created to fix that.
  143. // RGBW 0.432451, 0.013149, 0.556678
  144. // R 0.57 g 1 b 0.45
  145. auto red_w = (0.07f + brightness*(0.57f - 0.07f)) * red;
  146. auto green_w = (0.13f + brightness*(1.00f - 0.13f)) * green;
  147. auto blue_w = (0.06f + brightness*(0.45f - 0.06f)) * blue;
  148. // For other colors, we can simply scale the RGB channels with the
  149. // requested brightness, resulting in a very usable color. Not 100%
  150. // the same as the original firmware, but sometimes even better IMO.
  151. auto red_c = red * brightness;
  152. auto green_c = green * brightness;
  153. auto blue_c = blue * brightness;
  154. // The actual RGB values are a weighed mix of the above two.
  155. // The closer to the white point, the more the white point
  156. // value applies.
  157. auto level_red = (red_w * ((green+blue)/2)) + (red_c * (1-(green+blue)/2));
  158. auto level_green = (green_w * ((red+blue)/2)) + (green_c * (1-(red+blue)/2));
  159. auto level_blue = (blue_w * ((red+green)/2)) + (blue_c * (1-(red+green)/2));
  160. // Invert the signal. The LEDs in the lamp's circuit are brighter
  161. // when the pwm levels on the GPIO pins are lower.
  162. level_red = 1.0f - level_red;
  163. level_green = 1.0f - level_green;
  164. level_blue = 1.0f - level_blue;
  165. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, off", level_red, level_green, level_blue);
  166. // Drive the LEDs.
  167. master2_->turn_on();
  168. master1_->turn_on();
  169. red_->set_level(level_red);
  170. green_->set_level(level_green);
  171. blue_->set_level(level_blue);
  172. white_->turn_off();
  173. }
  174. void turn_on_in_color_temperature_mode_(float temperature, float brightness)
  175. {
  176. ESP_LOGD(TAG, "Activate TEMPERATURE %f, BRIGHTNESS %f", temperature, brightness);
  177. // Empirically determined during programming the temperature GPIO output
  178. // code from below, by checking how far my outputs were off from the
  179. // original lamp firmeware's outputs. This scaler is used for correcting
  180. // my output towards the original output.
  181. float scaler;
  182. float red = 1.0;
  183. float green = 1.0;
  184. float blue = 1.0;
  185. float white = 1.0;
  186. // Temperature band 370 - 588
  187. if (temperature <= HOME_ASSISTANT_MIRED_MAX && temperature >= 371)
  188. {
  189. scaler = 3.23f;
  190. float start = 371;
  191. float end = 588;
  192. float band = end - start;
  193. float red_volt = 2.86f * (1.0f - brightness);
  194. red = red_volt / scaler;
  195. float green_1 = 2.90f + (temperature - start) * (2.97f - 2.90f) / band;
  196. float green_100 = 0.45f + (temperature - start) * (1.13f - 0.45f) / band;
  197. float green_volt = green_1 + brightness * (green_100 - green_1);
  198. green = green_volt / scaler;
  199. float white_1 = 0.28f - (temperature - start) * (0.28f - 0.19f) / band;
  200. float white_100 = 1.07f - (temperature - start) * (1.07f - 0.22f) / band;
  201. float white_volt = white_1 + brightness * (white_100 - white_1);
  202. white = white_volt / scaler;
  203. }
  204. // Temperature band 334 - 370
  205. else if (temperature >= 334)
  206. {
  207. scaler = 3.23f;
  208. float red_volt = (1.0f - brightness) * 2.86f;
  209. red = red_volt / scaler;
  210. float green_volt = 2.9f - brightness * (2.9f - 0.45f);
  211. green = green_volt / scaler;
  212. float white_volt = 0.28f + brightness * (1.07f - 0.28f);
  213. white = white_volt / scaler;
  214. }
  215. // Temperature band 313 - 333
  216. //
  217. // The light becomes noticably brighter when moving from temperature 334 to
  218. // temperature 333. There's a little jump in the lighting output here.
  219. // Possibly this is a switch from warm to cold lighting as imposed by the
  220. // LED circuitry, making this unavoidable. However, it would be interesting
  221. // to see if we can smoothen this out.
  222. // BTW: This behavior is in sync with the original firmware.
  223. else if (temperature >= 313)
  224. {
  225. scaler = 3.23f;
  226. float red_volt = 2.89f - brightness * (2.89f - 0.32f);
  227. red = red_volt / scaler;
  228. float green_volt = 2.96f - brightness * (2.96f - 1.03f);
  229. green = green_volt / scaler;
  230. float white_volt = 0.42f + brightness * (2.43f - 0.42f);
  231. float scaler_white = 3.45f;
  232. white = white_volt / scaler_white;
  233. }
  234. // Temperature band 251 - 312
  235. else if (temperature >= 251)
  236. {
  237. scaler = 3.48f;
  238. float white_correction = 1.061;
  239. float white_volt = 0.5f + brightness * (3.28f * white_correction - 0.5f);
  240. white = white_volt / scaler;
  241. }
  242. // Temperature band 223 - 250
  243. else if (temperature >= 223)
  244. {
  245. scaler = 3.25f;
  246. float green_volt = 2.94f - brightness * (2.94f - 0.88f);
  247. green = green_volt / scaler;
  248. float blue_volt = 3.02f - brightness * (3.02f - 1.59f);
  249. blue = blue_volt / scaler;
  250. float white_correction = 1.024f;
  251. float white_volt = 0.42f + brightness * (2.51f * white_correction - 0.42f);
  252. float scaler_white = 3.36f;
  253. white = white_volt / scaler_white;
  254. }
  255. // Temperature band 153 - 222
  256. else if (temperature >= HOME_ASSISTANT_MIRED_MIN)
  257. {
  258. float start = 153;
  259. float end = 222;
  260. float band = end - start;
  261. scaler = 3.23f;
  262. float green_volt = 2.86f - brightness * 2.86f;
  263. green = green_volt / scaler;
  264. float blue_1 = 2.92f + (temperature - start) * (2.97f - 2.92f) / band;
  265. float blue_100 = 0.62f + (temperature - start) * (1.17f - 0.62f) / band;
  266. float blue_volt = blue_1 - brightness * (blue_1 - blue_100);
  267. blue = blue_volt / scaler;
  268. float white_1 = 0.28f + (temperature - start) * (0.37f - 0.28f) / band;
  269. float white_100 = 1.1f + (temperature - start) * (2.0f - 1.1f) / band;
  270. float white_volt = white_1 + brightness * (white_100 - white_1);
  271. float scaler_white = 3.27f;
  272. white = white_volt / scaler_white;
  273. }
  274. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f", red, green, blue, white);
  275. master2_->turn_on();
  276. master1_->turn_on();
  277. red_->set_level(red);
  278. green_->set_level(green);
  279. blue_->set_level(blue);
  280. white_->set_level(white);
  281. }
  282. };
  283. } // namespace rgbww
  284. } // namespace esphome