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.

242 lines
9.2 KiB

3 years ago
  1. #pragma once
  2. namespace esphome {
  3. namespace yeelight {
  4. namespace bs2 {
  5. /// This is an interface definition that is used to extend the
  6. /// YeelightBS2LightOutput class with methods to access properties
  7. /// of an active LightTranformer from the TransitionHandler class.
  8. ///
  9. /// The transformer is protected in the light output class, making
  10. /// it impossible to access these properties directly from the
  11. /// light output class.
  12. class LightStateTransformerInspector {
  13. public:
  14. virtual bool is_active() = 0;
  15. virtual bool is_transition() = 0;
  16. virtual light::LightColorValues get_end_values() = 0;
  17. virtual float get_progress() = 0;
  18. };
  19. /// This class is used to handle color transition requirements.
  20. ///
  21. /// When using the default ESPHome logic, transitioning is done by
  22. /// transitioning all light properties linearly from the original
  23. /// values to the new values, and letting the light output object
  24. /// translate these properties into light outputs on every step of the
  25. /// way. While this does work, it does not work nicely.
  26. ///
  27. /// For example, when transitioning from warm to cold white light,
  28. /// the color temperature would be transitioned from the old value to
  29. /// the new value. While doing so, the transition hits the middle
  30. /// white light setting, which shows up as a bright flash in the
  31. /// middle of the transition. The original firmware however, shows a
  32. /// smooth transition from warm to cold white light, without any flash.
  33. ///
  34. /// This class handles transitions by not varying the light properties
  35. /// over time, but by transitioning the LEDC duty cycle output levels
  36. /// over time. This matches the behavior of the original firmware.
  37. class TransitionHandler : public GPIOOutputs {
  38. public:
  39. TransitionHandler(LightStateTransformerInspector *inspector) : transformer_(inspector) {}
  40. bool set_light_color_values(light::LightColorValues values) {
  41. if (!light_state_has_active_transition_()) {
  42. // Remember the last active light color values. When a transition
  43. // is detected, use these as the starting point. It is not possible
  44. // to use the current values at that point, because the transition
  45. // is already in progress by the time the transition is detected.
  46. start_values = values;
  47. active_ = false;
  48. return false;
  49. }
  50. if (is_fresh_transition_()) {
  51. start_->set_light_color_values(start_values);
  52. end_->set_light_color_values(transformer_->get_end_values());
  53. active_ = true;
  54. }
  55. else if (is_modified_transition_()) {
  56. this->copy_to(start_);
  57. end_->set_light_color_values(transformer_->get_end_values());
  58. }
  59. auto progress = transformer_->get_progress();
  60. auto smoothed = light::LightTransitionTransformer::smoothed_progress(progress);
  61. red = esphome::lerp(smoothed, start_->red, end_->red);
  62. green = esphome::lerp(smoothed, start_->green, end_->green);
  63. blue = esphome::lerp(smoothed, start_->blue, end_->blue);
  64. white = esphome::lerp(smoothed, start_->white, end_->white);
  65. return true;
  66. }
  67. protected:
  68. bool active_ = false;
  69. LightStateTransformerInspector *transformer_;
  70. light::LightColorValues start_values;
  71. GPIOOutputs *start_ = new ColorTranslator();
  72. GPIOOutputs *end_ = new ColorTranslator();
  73. /// Checks if the LightState object currently has an active LightTransformer.
  74. bool light_state_has_active_transition_() {
  75. if (!transformer_->is_active())
  76. return false;
  77. if (!transformer_->is_transition())
  78. return false;
  79. return true;
  80. }
  81. /// Checks if a fresh transitioning is started.
  82. /// A transitioning is fresh when no existing transition is active.
  83. bool is_fresh_transition_() {
  84. return active_ == false;
  85. }
  86. /// Checks if a new end state is set, while an existing transition
  87. /// is active.
  88. bool is_modified_transition_() {
  89. auto new_end_values = transformer_->get_end_values();
  90. return new_end_values != end_->values;
  91. }
  92. };
  93. /// An implementation of the LightOutput interface for the Yeelight
  94. /// Bedside Lamp 2. The function of this class is to translate a
  95. /// required light state into actual physicial GPIO output signals
  96. /// to drive the device's LED circuitry.
  97. class YeelightBS2LightOutput : public Component, public light::LightOutput {
  98. public:
  99. /// Set the LEDC output for the red LED circuitry channel.
  100. void set_red_output(ledc::LEDCOutput *red) {
  101. red_ = red;
  102. }
  103. /// Set the LEDC output for the green LED circuitry channel.
  104. void set_green_output(ledc::LEDCOutput *green) {
  105. green_ = green;
  106. }
  107. /// Set the LEDC output for the blue LED circuitry channel.
  108. void set_blue_output(ledc::LEDCOutput *blue) {
  109. blue_ = blue;
  110. }
  111. /// Set the LEDC output for the white LED circuitry channel.
  112. void set_white_output(ledc::LEDCOutput *white) {
  113. white_ = white;
  114. }
  115. /// Set the first GPIO binary output, used as internal master
  116. /// switch for the LED light circuitry.
  117. void set_master1_output(gpio::GPIOBinaryOutput *master1) {
  118. master1_ = master1;
  119. }
  120. /// Set the second GPIO binary output, used as internal master
  121. /// switch for the LED light circuitry.
  122. void set_master2_output(gpio::GPIOBinaryOutput *master2) {
  123. master2_ = master2;
  124. }
  125. /// Returns a LightTraits object, which is used to explain to the
  126. /// outside world (e.g. Home Assistant) what features are supported
  127. /// by this device.
  128. light::LightTraits get_traits() override
  129. {
  130. auto traits = light::LightTraits();
  131. traits.set_supports_rgb(true);
  132. traits.set_supports_color_temperature(true);
  133. traits.set_supports_brightness(true);
  134. traits.set_supports_rgb_white_value(false);
  135. traits.set_supports_color_interlock(true);
  136. traits.set_min_mireds(MIRED_MIN);
  137. traits.set_max_mireds(MIRED_MAX);
  138. return traits;
  139. }
  140. /// Applies a requested light state to the physicial GPIO outputs.
  141. void write_state(light::LightState *state)
  142. {
  143. auto values = state->current_values;
  144. // Turn off the light when its state is 'off'.
  145. if (values.get_state() == 0)
  146. {
  147. ESP_LOGD(TAG, "Turn off the light");
  148. red_->set_level(1.0f);
  149. green_->set_level(1.0f);
  150. blue_->set_level(1.0f);
  151. white_->set_level(0.0f);
  152. master2_->turn_off();
  153. master1_->turn_off();
  154. return;
  155. }
  156. GPIOOutputs *delegate;
  157. if (transition_handler_->set_light_color_values(values)) {
  158. transition_handler_->log("TRANSITION");
  159. delegate = transition_handler_;
  160. } else {
  161. instant_handler_->set_light_color_values(values);
  162. instant_handler_->log("INSTANT");
  163. delegate = instant_handler_;
  164. }
  165. delegate->set_light_color_values(values);
  166. master2_->turn_on();
  167. master1_->turn_on();
  168. red_->set_level(delegate->red);
  169. green_->set_level(delegate->green);
  170. blue_->set_level(delegate->blue);
  171. white_->set_level(delegate->white);
  172. }
  173. protected:
  174. ledc::LEDCOutput *red_;
  175. ledc::LEDCOutput *green_;
  176. ledc::LEDCOutput *blue_;
  177. ledc::LEDCOutput *white_;
  178. esphome::gpio::GPIOBinaryOutput *master1_;
  179. esphome::gpio::GPIOBinaryOutput *master2_;
  180. GPIOOutputs *transition_handler_;
  181. GPIOOutputs *instant_handler_ = new ColorTranslator();
  182. friend class YeelightBS2LightState;
  183. /// Called by the YeelightBS2LightState class, to set the object that
  184. /// can be used to access protected data from the light state object.
  185. void set_transformer_inspector(LightStateTransformerInspector *exposer) {
  186. transition_handler_ = new TransitionHandler(exposer);
  187. }
  188. };
  189. class YeelightBS2LightState : public light::LightState, public LightStateTransformerInspector
  190. {
  191. public:
  192. YeelightBS2LightState(const std::string &name, YeelightBS2LightOutput *output) : light::LightState(name, output) {
  193. output->set_transformer_inspector(this);
  194. }
  195. bool is_active() {
  196. return this->transformer_ != nullptr;
  197. }
  198. bool is_transition() {
  199. return this->transformer_->is_transition();
  200. }
  201. light::LightColorValues get_end_values() {
  202. return this->transformer_->get_end_values();
  203. }
  204. float get_progress() {
  205. return this->transformer_->get_progress();
  206. }
  207. };
  208. } // namespace bs2
  209. } // namespace yeelight
  210. } // namespace esphome