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.

258 lines
8.3 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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. namespace esphome {
  12. namespace rgbww {
  13. class YeelightBedsideLampV2LightOutput : public Component, public LightOutput
  14. {
  15. public:
  16. YeelightBedsideLampV2LightOutput(
  17. FloatOutput *r, FloatOutput *g, FloatOutput *b, FloatOutput *w,
  18. esphome::gpio::GPIOBinaryOutput *m1, esphome::gpio::GPIOBinaryOutput *m2) :
  19. red_(r), green_(g), blue_(b), white_(w), master1_(m1), master2_(m2) {}
  20. LightTraits get_traits() override
  21. {
  22. auto traits = LightTraits();
  23. traits.set_supports_rgb(true);
  24. traits.set_supports_color_temperature(true);
  25. traits.set_supports_brightness(true);
  26. traits.set_supports_rgb_white_value(false);
  27. traits.set_supports_color_interlock(COLOR_INTERLOCK);
  28. traits.set_min_mireds(HOME_ASSISTANT_MIRED_MIN);
  29. traits.set_max_mireds(HOME_ASSISTANT_MIRED_MAX);
  30. return traits;
  31. }
  32. void write_state(LightState *state) override
  33. {
  34. auto values = state->current_values;
  35. ESP_LOGD("custom", "B = State %f, RGB %f %f %f, BRI %f, TEMP %f",
  36. values.get_state(),
  37. values.get_red(), values.get_green(), values.get_blue(),
  38. values.get_brightness(), values.get_color_temperature());
  39. // Power down the light when its state is 'off'.
  40. if (values.get_state() == 0) {
  41. this->turn_off_();
  42. return;
  43. }
  44. // Leave it to the default tooling to figure out the basics.
  45. // Because of the color interlocking, there are two possible outcomes:
  46. // - red, green, blue zero -> the light is in color temperature mode
  47. // - cwhite, wwhite zero -> the light is in RGB mode
  48. float red, green, blue, cwhite, wwhite;
  49. state->current_values_as_rgbww(
  50. &red, &green, &blue, &cwhite, &wwhite,
  51. CONSTANT_BRIGHTNESS, COLOR_INTERLOCK);
  52. if (cwhite > 0 || wwhite > 0) {
  53. this->turn_on_in_color_temperature_mode_(
  54. values.get_color_temperature(), values.get_brightness());
  55. } else {
  56. this->turn_on_in_rgb_mode_(
  57. values.get_red(), values.get_green(), values.get_blue(), values.get_brightness());
  58. }
  59. }
  60. private:
  61. FloatOutput *red_;
  62. FloatOutput *green_;
  63. FloatOutput *blue_;
  64. FloatOutput *white_;
  65. esphome::gpio::GPIOBinaryOutput *master1_;
  66. esphome::gpio::GPIOBinaryOutput *master2_;
  67. void turn_off_()
  68. {
  69. master2_->turn_off();
  70. master1_->turn_off();
  71. red_->turn_off();
  72. green_->turn_off();
  73. blue_->turn_off();
  74. white_->turn_off();
  75. }
  76. void turn_on_in_rgb_mode_(float red, float green, float blue, float brightness)
  77. {
  78. ESP_LOGD("custom", "Activate RGB %f, %f, %f, BRIGHTNESS %f", red, green, blue, brightness);
  79. // This tunes the power for the red channel a bit, to bring
  80. // the red=1, green=1, blue=1 color more towards white.
  81. // (on my lamps, there is a faint hint of red in the color)
  82. red = red * 0.93f;
  83. // Compensate for brightness.
  84. red = red * brightness;
  85. green = green * brightness;
  86. blue = blue * brightness;
  87. // Inverse the signal. The LEDs in the lamp's circuit are brighter
  88. // when the voltages on the GPIO pins are lower.
  89. red = 1.0f - red;
  90. green = 1.0f - green;
  91. blue = 1.0f - blue;
  92. float white = 0.0;
  93. ESP_LOGD("rgb_mode", "LED state : RGBW %f, %f, %f, %f", red, green, blue, white);
  94. // Drive the LEDs.
  95. red_->set_level(red);
  96. green_->set_level(green);
  97. blue_->set_level(blue);
  98. white_->set_level(white);
  99. master1_->turn_on();
  100. master2_->turn_on();
  101. }
  102. void turn_on_in_color_temperature_mode_(float temperature, float brightness)
  103. {
  104. ESP_LOGD("temperature_mode", "Activate TEMPERATURE %f, BRIGHTNESS %f", temperature, brightness);
  105. // Empirically determined during programming the temperature GPIO output
  106. // code from below, by checking how far my outputs were off from the
  107. // original lamp firmeware's outputs. This scaler is used for correcting
  108. // my output towards the original output.
  109. float volt_scaler;
  110. float red = 1.0;
  111. float green = 1.0;
  112. float blue = 1.0;
  113. float white = 1.0;
  114. // Temperature band 370 - 588
  115. if (temperature <= HOME_ASSISTANT_MIRED_MAX && temperature >= 371)
  116. {
  117. volt_scaler = 3.23f;
  118. float start = 371;
  119. float end = 588;
  120. float band = end - start;
  121. float red_volt = 2.86f * (1.0f - brightness);
  122. red = red_volt / volt_scaler;
  123. float green_1 = 2.90f + (temperature - start) * (2.97f - 2.90f) / band;
  124. float green_100 = 0.45f + (temperature - start) * (1.13f - 0.45f) / band;
  125. float green_volt = green_1 + brightness * (green_100 - green_1);
  126. green = green_volt / volt_scaler;
  127. float white_1 = 0.28f - (temperature - start) * (0.28f - 0.19f) / band;
  128. float white_100 = 1.07f - (temperature - start) * (1.07f - 0.22f) / band;
  129. float white_volt = white_1 + brightness * (white_100 - white_1);
  130. white = white_volt / volt_scaler;
  131. }
  132. // Temperature band 334 - 370
  133. else if (temperature >= 334)
  134. {
  135. volt_scaler = 3.23f;
  136. float red_volt = (1.0f - brightness) * 2.86f;
  137. red = red_volt / volt_scaler;
  138. float green_volt = 2.9f - brightness * (2.9f - 0.45f);
  139. green = green_volt / volt_scaler;
  140. float white_volt = 0.28f + brightness * (1.07f - 0.28f);
  141. white = white_volt / volt_scaler;
  142. }
  143. // Temperature band 313 - 333
  144. //
  145. // The light becomes noticably brighter when moving from temperature 334 to
  146. // temperature 333. There's a little jump in the lighting output here.
  147. // Possibly this is a switch from warm to cold lighting as imposed by the
  148. // LED circuitry, making this unavoidable. However, it would be interesting
  149. // to see if we can smoothen this out.
  150. // BTW: This behavior is in sync with the original firmware.
  151. else if (temperature >= 313)
  152. {
  153. volt_scaler = 3.23f;
  154. float red_volt = 2.89f - brightness * (2.89f - 0.32f);
  155. red = red_volt / volt_scaler;
  156. float green_volt = 2.96f - brightness * (2.96f - 1.03f);
  157. green = green_volt / volt_scaler;
  158. float white_volt = 0.42f + brightness * (2.43f - 0.42f);
  159. float volt_scaler_white = 3.45f;
  160. white = white_volt / volt_scaler_white;
  161. }
  162. // Temperature band 251 - 312
  163. else if (temperature >= 251)
  164. {
  165. volt_scaler = 3.48f;
  166. float white_correction = 1.061;
  167. float white_volt = 0.5f + brightness * (3.28f * white_correction - 0.5f);
  168. white = white_volt / volt_scaler;
  169. }
  170. // Temperature band 223 - 250
  171. else if (temperature >= 223)
  172. {
  173. volt_scaler = 3.25f;
  174. float green_volt = 2.94f - brightness * (2.94f - 0.88f);
  175. green = green_volt / volt_scaler;
  176. float blue_volt = 3.02f - brightness * (3.02f - 1.59f);
  177. blue = blue_volt / volt_scaler;
  178. float white_correction = 1.024f;
  179. float white_volt = 0.42f + brightness * (2.51f * white_correction - 0.42f);
  180. float volt_scaler_white = 3.36f;
  181. white = white_volt / volt_scaler_white;
  182. }
  183. // Temperature band 153 - 222
  184. else if (temperature >= HOME_ASSISTANT_MIRED_MIN)
  185. {
  186. float start = 153;
  187. float end = 222;
  188. float band = end - start;
  189. volt_scaler = 3.23f;
  190. float green_volt = 2.86f - brightness * 2.86f;
  191. green = green_volt / volt_scaler;
  192. float blue_1 = 2.92f + (temperature - start) * (2.97f - 2.92f) / band;
  193. float blue_100 = 0.62f + (temperature - start) * (1.17f - 0.62f) / band;
  194. float blue_volt = blue_1 - brightness * (blue_1 - blue_100);
  195. blue = blue_volt / volt_scaler;
  196. float white_1 = 0.28f + (temperature - start) * (0.37f - 0.28f) / band;
  197. float white_100 = 1.1f + (temperature - start) * (2.0f - 1.1f) / band;
  198. float white_volt = white_1 + brightness * (white_100 - white_1);
  199. float volt_scaler_white = 3.27f;
  200. white = white_volt / volt_scaler_white;
  201. }
  202. ESP_LOGD("temperature_mode", "LED state : RGBW %f, %f, %f, %f", red, green, blue, white);
  203. red_->set_level(red);
  204. green_->set_level(green);
  205. blue_->set_level(blue);
  206. white_->set_level(white);
  207. master2_->turn_on();
  208. master1_->turn_on();
  209. }
  210. };
  211. } // namespace rgbww
  212. } // namespace esphome