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.

306 lines
13 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. #pragma once
  2. #include "esphome/core/component.h"
  3. #include "esphome/components/output/float_output.h"
  4. #include "esphome/components/light/light_output.h"
  5. #include "esphome/components/gpio/output/gpio_binary_output.h"
  6. #define CONSTANT_BRIGHTNESS true
  7. // The lamp circuitry does not support having RGB and white
  8. // channels active at the same time. Therefore, color interlock
  9. // must be enabled.
  10. #define COLOR_INTERLOCK true
  11. // Same range as supported by the original Yeelight firmware.
  12. #define HOME_ASSISTANT_MIRED_MIN 153
  13. #define HOME_ASSISTANT_MIRED_MAX 588
  14. #define TAG "yeelight_bs2"
  15. //#define YEELIGHT_DEBUG_LOG
  16. //#define TRANSITION_TO_OFF_BUGFIX
  17. namespace esphome
  18. {
  19. namespace rgbww
  20. {
  21. class YeelightBS2LightOutput : public Component, public light::LightOutput
  22. {
  23. public:
  24. void set_red(output::FloatOutput *red) { red_ = red; }
  25. void set_green(output::FloatOutput *green) { green_ = green; }
  26. void set_blue(output::FloatOutput *blue) { blue_ = blue; }
  27. void set_white(output::FloatOutput *white) { white_ = white; }
  28. void set_master1(gpio::GPIOBinaryOutput *master1) { master1_ = master1; }
  29. void set_master2(gpio::GPIOBinaryOutput *master2) { master2_ = master2; }
  30. light::LightTraits get_traits() override
  31. {
  32. auto traits = light::LightTraits();
  33. traits.set_supports_rgb(true);
  34. traits.set_supports_color_temperature(true);
  35. traits.set_supports_brightness(true);
  36. traits.set_supports_rgb_white_value(false);
  37. traits.set_supports_color_interlock(COLOR_INTERLOCK);
  38. traits.set_min_mireds(HOME_ASSISTANT_MIRED_MIN);
  39. traits.set_max_mireds(HOME_ASSISTANT_MIRED_MAX);
  40. return traits;
  41. }
  42. void write_state(light::LightState *state) override
  43. {
  44. auto values = state->current_values;
  45. //#ifdef YEELIGHT_DEBUG_LOG
  46. ESP_LOGD(TAG, "B = State %f, RGB %f %f %f, BRI %f, TEMP %f",
  47. values.get_state(),
  48. values.get_red(), values.get_green(), values.get_blue(),
  49. values.get_brightness(), values.get_color_temperature());
  50. //#endif
  51. // Power down the light when its state is 'off'.
  52. if (values.get_state() == 0)
  53. {
  54. this->turn_off_();
  55. return;
  56. }
  57. auto brightness = values.get_brightness();
  58. #ifdef TRANSITION_TO_OFF_BUGFIX
  59. // What seems to be a bug in ESPHome transitioning: when turning on
  60. // the device, the brightness is scaled along with the state (which
  61. // runs from 0 to 1), but when turning off the device, the brightness
  62. // is kept the same while the state goes down from 1 to 0. As a result
  63. // when turning off the lamp with a transition time of 1s, the light
  64. // stays on for 1s and then turn itself off abruptly.
  65. // For turning off, I implemented this hack here to make the
  66. // transition work better.
  67. if (previous_state_ > values.get_state()) {
  68. brightness = values.get_state() * brightness;
  69. }
  70. previous_state_ = values.get_state();
  71. #endif
  72. // Leave it to the default tooling to figure out the basics.
  73. // Because of the color interlocking, there are two possible outcomes:
  74. // - red, green, blue zero -> the light is in color temperature mode
  75. // - cwhite, wwhite zero -> the light is in RGB mode
  76. float red, green, blue, cwhite, wwhite;
  77. state->current_values_as_rgbww(
  78. &red, &green, &blue, &cwhite, &wwhite,
  79. CONSTANT_BRIGHTNESS, COLOR_INTERLOCK);
  80. if (cwhite > 0 || wwhite > 0)
  81. {
  82. this->turn_on_in_color_temperature_mode_(
  83. values.get_color_temperature(), brightness);
  84. }
  85. else
  86. {
  87. // The RGB mode does not use the RGB values as determined by
  88. // current_values_as_rgbww(). The device has LED driving circuitry
  89. // that takes care of the required brightness curve while ramping up
  90. // the brightness. Therefore, the actual RGB values are passed here.
  91. this->turn_on_in_rgb_mode_(
  92. values.get_red(), values.get_green(), values.get_blue(), brightness);
  93. }
  94. }
  95. protected:
  96. output::FloatOutput *red_;
  97. output::FloatOutput *green_;
  98. output::FloatOutput *blue_;
  99. output::FloatOutput *white_;
  100. esphome::gpio::GPIOBinaryOutput *master1_;
  101. esphome::gpio::GPIOBinaryOutput *master2_;
  102. // Used for a bug hack.
  103. float previous_state_ = 1;
  104. void turn_off_()
  105. {
  106. // Using set_level() calls for the RGB GPIOs, and not
  107. // turn_off(), because turn_off() causes some unwanted
  108. // flashing when powering off at low brightness.
  109. red_->set_level(1);
  110. green_->set_level(1);
  111. blue_->set_level(1);
  112. white_->turn_off();
  113. master1_->turn_off();
  114. master2_->turn_off();
  115. }
  116. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness)
  117. {
  118. #ifdef YEELIGHT_DEBUG_LOG
  119. ESP_LOGD(TAG, "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  120. #endif
  121. // The brightness must be at least 3/100 to light up the LEDs.
  122. if (brightness < 0.03f)
  123. brightness = 0.03f;
  124. // Apply brightness.
  125. red = red * brightness;
  126. green = green * brightness;
  127. blue = blue * brightness;
  128. // Inverse the signal. The LEDs in the lamp's circuit are brighter
  129. // when the pwm levels on the GPIO pins are lower.
  130. red = 1.0f - red;
  131. green = 1.0f - green;
  132. blue = 1.0f - blue;
  133. #ifdef YEELIGHT_DEBUG_LOG
  134. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f", red, green, blue);
  135. #endif
  136. // Drive the LEDs.
  137. red_->set_level(red);
  138. green_->set_level(green);
  139. blue_->set_level(blue);
  140. white_->turn_off();
  141. master1_->turn_on();
  142. master2_->turn_on();
  143. }
  144. void turn_on_in_color_temperature_mode_(float temperature, float brightness)
  145. {
  146. #ifdef YEELIGHT_DEBUG_LOG
  147. ESP_LOGD(TAG, "Activate TEMPERATURE %f, BRIGHTNESS %f", temperature, brightness);
  148. #endif
  149. // Empirically determined during programming the temperature GPIO output
  150. // code from below, by checking how far my outputs were off from the
  151. // original lamp firmeware's outputs. This scaler is used for correcting
  152. // my output towards the original output.
  153. float scaler;
  154. float red = 1.0;
  155. float green = 1.0;
  156. float blue = 1.0;
  157. float white = 1.0;
  158. // Temperature band 370 - 588
  159. if (temperature <= HOME_ASSISTANT_MIRED_MAX && temperature >= 371)
  160. {
  161. scaler = 3.23f;
  162. float start = 371;
  163. float end = 588;
  164. float band = end - start;
  165. float red_volt = 2.86f * (1.0f - brightness);
  166. red = red_volt / scaler;
  167. float green_1 = 2.90f + (temperature - start) * (2.97f - 2.90f) / band;
  168. float green_100 = 0.45f + (temperature - start) * (1.13f - 0.45f) / band;
  169. float green_volt = green_1 + brightness * (green_100 - green_1);
  170. green = green_volt / scaler;
  171. float white_1 = 0.28f - (temperature - start) * (0.28f - 0.19f) / band;
  172. float white_100 = 1.07f - (temperature - start) * (1.07f - 0.22f) / band;
  173. float white_volt = white_1 + brightness * (white_100 - white_1);
  174. white = white_volt / scaler;
  175. }
  176. // Temperature band 334 - 370
  177. else if (temperature >= 334)
  178. {
  179. scaler = 3.23f;
  180. float red_volt = (1.0f - brightness) * 2.86f;
  181. red = red_volt / scaler;
  182. float green_volt = 2.9f - brightness * (2.9f - 0.45f);
  183. green = green_volt / scaler;
  184. float white_volt = 0.28f + brightness * (1.07f - 0.28f);
  185. white = white_volt / scaler;
  186. }
  187. // Temperature band 313 - 333
  188. //
  189. // The light becomes noticably brighter when moving from temperature 334 to
  190. // temperature 333. There's a little jump in the lighting output here.
  191. // Possibly this is a switch from warm to cold lighting as imposed by the
  192. // LED circuitry, making this unavoidable. However, it would be interesting
  193. // to see if we can smoothen this out.
  194. // BTW: This behavior is in sync with the original firmware.
  195. else if (temperature >= 313)
  196. {
  197. scaler = 3.23f;
  198. float red_volt = 2.89f - brightness * (2.89f - 0.32f);
  199. red = red_volt / scaler;
  200. float green_volt = 2.96f - brightness * (2.96f - 1.03f);
  201. green = green_volt / scaler;
  202. float white_volt = 0.42f + brightness * (2.43f - 0.42f);
  203. float scaler_white = 3.45f;
  204. white = white_volt / scaler_white;
  205. }
  206. // Temperature band 251 - 312
  207. else if (temperature >= 251)
  208. {
  209. scaler = 3.48f;
  210. float white_correction = 1.061;
  211. float white_volt = 0.5f + brightness * (3.28f * white_correction - 0.5f);
  212. white = white_volt / scaler;
  213. }
  214. // Temperature band 223 - 250
  215. else if (temperature >= 223)
  216. {
  217. scaler = 3.25f;
  218. float green_volt = 2.94f - brightness * (2.94f - 0.88f);
  219. green = green_volt / scaler;
  220. float blue_volt = 3.02f - brightness * (3.02f - 1.59f);
  221. blue = blue_volt / scaler;
  222. float white_correction = 1.024f;
  223. float white_volt = 0.42f + brightness * (2.51f * white_correction - 0.42f);
  224. float scaler_white = 3.36f;
  225. white = white_volt / scaler_white;
  226. }
  227. // Temperature band 153 - 222
  228. else if (temperature >= HOME_ASSISTANT_MIRED_MIN)
  229. {
  230. float start = 153;
  231. float end = 222;
  232. float band = end - start;
  233. scaler = 3.23f;
  234. float green_volt = 2.86f - brightness * 2.86f;
  235. green = green_volt / scaler;
  236. float blue_1 = 2.92f + (temperature - start) * (2.97f - 2.92f) / band;
  237. float blue_100 = 0.62f + (temperature - start) * (1.17f - 0.62f) / band;
  238. float blue_volt = blue_1 - brightness * (blue_1 - blue_100);
  239. blue = blue_volt / scaler;
  240. float white_1 = 0.28f + (temperature - start) * (0.37f - 0.28f) / band;
  241. float white_100 = 1.1f + (temperature - start) * (2.0f - 1.1f) / band;
  242. float white_volt = white_1 + brightness * (white_100 - white_1);
  243. float scaler_white = 3.27f;
  244. white = white_volt / scaler_white;
  245. }
  246. #ifdef YEELIGHT_DEBUG_LOG
  247. ESP_LOGD(TAG, "New LED state : RGBW %f, %f, %f, %f", red, green, blue, white);
  248. #endif
  249. red_->set_level(red);
  250. green_->set_level(green);
  251. blue_->set_level(blue);
  252. white_->set_level(white);
  253. master2_->turn_on();
  254. master1_->turn_on();
  255. }
  256. };
  257. } // namespace rgbww
  258. } // namespace esphome