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.

327 lines
14 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 this define:
  17. #define TRANSITION_TO_OFF_BUGFIX
  18. //#define YEELIGHT_DEBUG_LOG
  19. namespace esphome
  20. {
  21. namespace rgbww
  22. {
  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 frequency as used by the original device
  28. // for driving the LED circuitry.
  29. const float PWM_FREQUENCY = 3000.0f;
  30. class YeelightBS2LightOutput : public Component, public light::LightOutput
  31. {
  32. public:
  33. void set_red(ledc::LEDCOutput *red) { red_ = red; red_->set_frequency(PWM_FREQUENCY); }
  34. void set_green(ledc::LEDCOutput *green) { green_ = green; green_->set_frequency(PWM_FREQUENCY); }
  35. void set_blue(ledc::LEDCOutput *blue) { blue_ = blue; blue_->set_frequency(PWM_FREQUENCY); }
  36. void set_white(ledc::LEDCOutput *white) { white_ = white; white_->set_frequency(PWM_FREQUENCY); }
  37. void set_master1(gpio::GPIOBinaryOutput *master1) { master1_ = master1; }
  38. void set_master2(gpio::GPIOBinaryOutput *master2) { master2_ = master2; }
  39. light::LightTraits get_traits() override
  40. {
  41. auto traits = light::LightTraits();
  42. traits.set_supports_rgb(true);
  43. traits.set_supports_color_temperature(true);
  44. traits.set_supports_brightness(true);
  45. traits.set_supports_rgb_white_value(false);
  46. traits.set_supports_color_interlock(true);
  47. traits.set_min_mireds(HOME_ASSISTANT_MIRED_MIN);
  48. traits.set_max_mireds(HOME_ASSISTANT_MIRED_MAX);
  49. return traits;
  50. }
  51. void write_state(light::LightState *state) override
  52. {
  53. auto values = state->current_values;
  54. #ifdef YEELIGHT_DEBUG_LOG
  55. ESP_LOGD(TAG, "B = State %f, RGB %f %f %f, BRI %f, TEMP %f",
  56. values.get_state(),
  57. values.get_red(), values.get_green(), values.get_blue(),
  58. values.get_brightness(), values.get_color_temperature());
  59. #endif
  60. // Power down the light when its state is 'off'.
  61. if (values.get_state() == 0)
  62. {
  63. this->turn_off_();
  64. #ifdef YEELIGHT_DEBUG_LOG
  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. // Using set_level() calls for the RGB GPIOs, and not
  123. // turn_off(), because turn_off() causes some unwanted
  124. // flashing when powering off at low brightness.
  125. red_->set_level(1);
  126. green_->set_level(1);
  127. blue_->set_level(1);
  128. white_->turn_off();
  129. master1_->turn_off();
  130. master2_->turn_off();
  131. }
  132. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness, float state)
  133. {
  134. #ifdef YEELIGHT_DEBUG_LOG
  135. ESP_LOGD(TAG, "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  136. #endif
  137. // The brightness must be at least 3/100 to light up the LEDs.
  138. // During transitions (where state is a fraction between 0 and 1,
  139. // indicating the transition progress) we don't apply this to
  140. // get smoother transitioning when turning on the light.
  141. if (state == 1 && brightness < 0.03f)
  142. brightness = 0.03f;
  143. // Apply brightness.
  144. red = red * brightness;
  145. green = green * brightness;
  146. blue = blue * brightness;
  147. // Inverse the signal. The LEDs in the lamp's circuit are brighter
  148. // when the pwm levels on the GPIO pins are lower.
  149. red = 1.0f - red;
  150. green = 1.0f - green;
  151. blue = 1.0f - blue;
  152. #ifdef YEELIGHT_DEBUG_LOG
  153. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f", red, green, blue);
  154. #endif
  155. // Drive the LEDs.
  156. red_->set_level(red);
  157. green_->set_level(green);
  158. blue_->set_level(blue);
  159. white_->turn_off();
  160. master1_->turn_on();
  161. master2_->turn_on();
  162. }
  163. void turn_on_in_color_temperature_mode_(float temperature, float brightness)
  164. {
  165. #ifdef YEELIGHT_DEBUG_LOG
  166. ESP_LOGD(TAG, "Activate TEMPERATURE %f, BRIGHTNESS %f", temperature, brightness);
  167. #endif
  168. // Empirically determined during programming the temperature GPIO output
  169. // code from below, by checking how far my outputs were off from the
  170. // original lamp firmeware's outputs. This scaler is used for correcting
  171. // my output towards the original output.
  172. float scaler;
  173. float red = 1.0;
  174. float green = 1.0;
  175. float blue = 1.0;
  176. float white = 1.0;
  177. // Temperature band 370 - 588
  178. if (temperature <= HOME_ASSISTANT_MIRED_MAX && temperature >= 371)
  179. {
  180. scaler = 3.23f;
  181. float start = 371;
  182. float end = 588;
  183. float band = end - start;
  184. float red_volt = 2.86f * (1.0f - brightness);
  185. red = red_volt / scaler;
  186. float green_1 = 2.90f + (temperature - start) * (2.97f - 2.90f) / band;
  187. float green_100 = 0.45f + (temperature - start) * (1.13f - 0.45f) / band;
  188. float green_volt = green_1 + brightness * (green_100 - green_1);
  189. green = green_volt / scaler;
  190. float white_1 = 0.28f - (temperature - start) * (0.28f - 0.19f) / band;
  191. float white_100 = 1.07f - (temperature - start) * (1.07f - 0.22f) / band;
  192. float white_volt = white_1 + brightness * (white_100 - white_1);
  193. white = white_volt / scaler;
  194. }
  195. // Temperature band 334 - 370
  196. else if (temperature >= 334)
  197. {
  198. scaler = 3.23f;
  199. float red_volt = (1.0f - brightness) * 2.86f;
  200. red = red_volt / scaler;
  201. float green_volt = 2.9f - brightness * (2.9f - 0.45f);
  202. green = green_volt / scaler;
  203. float white_volt = 0.28f + brightness * (1.07f - 0.28f);
  204. white = white_volt / scaler;
  205. }
  206. // Temperature band 313 - 333
  207. //
  208. // The light becomes noticably brighter when moving from temperature 334 to
  209. // temperature 333. There's a little jump in the lighting output here.
  210. // Possibly this is a switch from warm to cold lighting as imposed by the
  211. // LED circuitry, making this unavoidable. However, it would be interesting
  212. // to see if we can smoothen this out.
  213. // BTW: This behavior is in sync with the original firmware.
  214. else if (temperature >= 313)
  215. {
  216. scaler = 3.23f;
  217. float red_volt = 2.89f - brightness * (2.89f - 0.32f);
  218. red = red_volt / scaler;
  219. float green_volt = 2.96f - brightness * (2.96f - 1.03f);
  220. green = green_volt / scaler;
  221. float white_volt = 0.42f + brightness * (2.43f - 0.42f);
  222. float scaler_white = 3.45f;
  223. white = white_volt / scaler_white;
  224. }
  225. // Temperature band 251 - 312
  226. else if (temperature >= 251)
  227. {
  228. scaler = 3.48f;
  229. float white_correction = 1.061;
  230. float white_volt = 0.5f + brightness * (3.28f * white_correction - 0.5f);
  231. white = white_volt / scaler;
  232. }
  233. // Temperature band 223 - 250
  234. else if (temperature >= 223)
  235. {
  236. scaler = 3.25f;
  237. float green_volt = 2.94f - brightness * (2.94f - 0.88f);
  238. green = green_volt / scaler;
  239. float blue_volt = 3.02f - brightness * (3.02f - 1.59f);
  240. blue = blue_volt / scaler;
  241. float white_correction = 1.024f;
  242. float white_volt = 0.42f + brightness * (2.51f * white_correction - 0.42f);
  243. float scaler_white = 3.36f;
  244. white = white_volt / scaler_white;
  245. }
  246. // Temperature band 153 - 222
  247. else if (temperature >= HOME_ASSISTANT_MIRED_MIN)
  248. {
  249. float start = 153;
  250. float end = 222;
  251. float band = end - start;
  252. scaler = 3.23f;
  253. float green_volt = 2.86f - brightness * 2.86f;
  254. green = green_volt / scaler;
  255. float blue_1 = 2.92f + (temperature - start) * (2.97f - 2.92f) / band;
  256. float blue_100 = 0.62f + (temperature - start) * (1.17f - 0.62f) / band;
  257. float blue_volt = blue_1 - brightness * (blue_1 - blue_100);
  258. blue = blue_volt / scaler;
  259. float white_1 = 0.28f + (temperature - start) * (0.37f - 0.28f) / band;
  260. float white_100 = 1.1f + (temperature - start) * (2.0f - 1.1f) / band;
  261. float white_volt = white_1 + brightness * (white_100 - white_1);
  262. float scaler_white = 3.27f;
  263. white = white_volt / scaler_white;
  264. }
  265. #ifdef YEELIGHT_DEBUG_LOG
  266. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f", red, green, blue, white);
  267. #endif
  268. red_->set_level(red);
  269. green_->set_level(green);
  270. blue_->set_level(blue);
  271. white_->set_level(white);
  272. master2_->turn_on();
  273. master1_->turn_on();
  274. }
  275. };
  276. } // namespace rgbww
  277. } // namespace esphome