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.

301 lines
12 KiB

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