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.

119 lines
3.7 KiB

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