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.

123 lines
3.8 KiB

  1. /**
  2. * This code implements the white light mode (based on temperature +
  3. * brightness) for the Yeelight Bedside Lamp 2.
  4. */
  5. #pragma once
  6. #include <array>
  7. #include <stdexcept>
  8. namespace esphome {
  9. namespace yeelight {
  10. namespace bs2 {
  11. // Same range as supported by the original Yeelight firmware.
  12. static const int MIRED_MAX = 153;
  13. static const int MIRED_MIN = 588;
  14. struct RGBWLevelsByTemperature {
  15. float from_temperature;
  16. float red;
  17. float green;
  18. float blue;
  19. float white;
  20. };
  21. using RGBWLevelsTable = std::array<RGBWLevelsByTemperature, 15>;
  22. static const RGBWLevelsTable rgbw_levels_1_ {{
  23. { 501.0f, 0.873f, 0.907f, 1.000f, 0.063f },
  24. { 455.0f, 0.873f, 0.896f, 1.000f, 0.063f },
  25. { 417.0f, 0.873f, 0.891f, 1.000f, 0.068f },
  26. { 371.0f, 0.873f, 0.880f, 1.000f, 0.070f },
  27. { 334.0f, 0.873f, 0.887f, 1.000f, 0.088f },
  28. { 313.0f, 0.882f, 0.904f, 1.000f, 0.128f },
  29. { 295.0f, 0.947f, 1.000f, 0.968f, 0.145f },
  30. { 251.0f, 0.999f, 1.000f, 1.000f, 0.155f },
  31. { 223.0f, 1.000f, 0.899f, 0.921f, 0.130f },
  32. { 201.0f, 1.000f, 0.873f, 0.908f, 0.115f },
  33. { 182.0f, 1.000f, 0.873f, 0.901f, 0.103f },
  34. { 173.0f, 1.000f, 0.873f, 0.904f, 0.094f },
  35. { 167.0f, 1.000f, 0.873f, 0.891f, 0.098f },
  36. { 154.0f, 1.000f, 0.873f, 0.894f, 0.090f },
  37. { 153.0f, 1.000f, 0.873f, 0.892f, 0.088f }
  38. }};
  39. static const RGBWLevelsTable rgbw_levels_100_ {{
  40. { 501.0f, 0.000f, 0.344f, 1.000f, 0.068f },
  41. { 455.0f, 0.000f, 0.237f, 1.000f, 0.093f },
  42. { 417.0f, 0.000f, 0.186f, 1.000f, 0.120f },
  43. { 371.0f, 0.000f, 0.149f, 1.000f, 0.167f },
  44. { 334.0f, 0.000f, 0.135f, 1.000f, 0.325f },
  45. { 313.0f, 0.097f, 0.314f, 1.000f, 0.740f },
  46. { 295.0f, 0.745f, 1.000f, 0.953f, 0.905f },
  47. { 251.0f, 1.000f, 1.000f, 1.000f, 1.000f },
  48. { 223.0f, 1.000f, 0.267f, 0.485f, 0.765f },
  49. { 201.0f, 1.000f, 0.000f, 0.355f, 0.609f },
  50. { 182.0f, 1.000f, 0.000f, 0.282f, 0.489f },
  51. { 173.0f, 1.000f, 0.000f, 0.313f, 0.392f },
  52. { 167.0f, 1.000f, 0.000f, 0.180f, 0.422f },
  53. { 154.0f, 1.000f, 0.000f, 0.218f, 0.368f },
  54. { 153.0f, 1.000f, 0.000f, 0.187f, 0.335f }
  55. }};
  56. class ColorWhiteLight
  57. {
  58. public:
  59. float red = 0;
  60. float green = 0;
  61. float blue = 0;
  62. float white = 0;
  63. void set_color(float temperature, float brightness)
  64. {
  65. temperature = clamp_temperature_(temperature);
  66. brightness = clamp_brightness_(brightness);
  67. auto levels_1 = lookup_in_table_(rgbw_levels_1_, temperature);
  68. auto levels_100 = lookup_in_table_(rgbw_levels_100_, temperature);
  69. red = interpolate_(levels_1.red, levels_100.red, brightness);
  70. green = interpolate_(levels_1.green, levels_100.green, brightness);
  71. blue = interpolate_(levels_1.blue, levels_100.blue, brightness);
  72. white = interpolate_(levels_1.white, levels_100.white, brightness);
  73. }
  74. protected:
  75. float clamp_temperature_(float temperature)
  76. {
  77. if (temperature < MIRED_MAX)
  78. temperature = MIRED_MAX;
  79. else if (temperature > MIRED_MIN)
  80. temperature = MIRED_MIN;
  81. return temperature;
  82. }
  83. float clamp_brightness_(float brightness)
  84. {
  85. if (brightness < 0.01f)
  86. brightness = 0.01f;
  87. else if (brightness > 1.00f)
  88. brightness = 1.00f;
  89. return brightness;
  90. }
  91. RGBWLevelsByTemperature lookup_in_table_(RGBWLevelsTable table, float temperature)
  92. {
  93. for (RGBWLevelsByTemperature& item : table)
  94. if (temperature >= item.from_temperature)
  95. return item;
  96. throw std::invalid_argument("received too low temperature");
  97. }
  98. float interpolate_(float level_1, float level_100, float brightness)
  99. {
  100. auto coefficient = (level_100 - level_1) / 0.99f;
  101. auto level = level_1 + (brightness - 0.01f) * coefficient;
  102. return level;
  103. }
  104. };
  105. } // namespace bs2
  106. } // namespace yeelight
  107. } // namespace esphome