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.

252 lines
9.9 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. // When a fresh transition is started, then compute the GPIO outputs
  51. // to use for both the start and end point. This transition handler
  52. // will then transition linearly between these two.
  53. if (is_fresh_transition_()) {
  54. start_->set_light_color_values(start_values);
  55. end_->set_light_color_values(transformer_->get_end_values());
  56. active_ = true;
  57. }
  58. // When a transition is modified, then use the current GPIO outputs
  59. // as the new starting point.
  60. else if (is_modified_transition_()) {
  61. this->copy_to(start_);
  62. end_->set_light_color_values(transformer_->get_end_values());
  63. }
  64. // Determine the required GPIO outputs for the current transition progress.
  65. progress_ = transformer_->get_progress();
  66. auto smoothed = light::LightTransitionTransformer::smoothed_progress(progress_);
  67. red = esphome::lerp(smoothed, start_->red, end_->red);
  68. green = esphome::lerp(smoothed, start_->green, end_->green);
  69. blue = esphome::lerp(smoothed, start_->blue, end_->blue);
  70. white = esphome::lerp(smoothed, start_->white, end_->white);
  71. return true;
  72. }
  73. protected:
  74. bool active_ = false;
  75. float progress_ = 0.0f;
  76. LightStateTransformerInspector *transformer_;
  77. light::LightColorValues start_values;
  78. GPIOOutputs *start_ = new ColorTranslator();
  79. GPIOOutputs *end_ = new ColorTranslator();
  80. /// Checks if the LightState object currently has an active LightTransformer.
  81. bool light_state_has_active_transition_() {
  82. if (!transformer_->is_active())
  83. return false;
  84. if (!transformer_->is_transition())
  85. return false;
  86. return true;
  87. }
  88. /// Checks if a fresh transitioning is started.
  89. /// A transitioning is fresh when no existing transition is active.
  90. bool is_fresh_transition_() {
  91. return active_ == false;
  92. }
  93. /// Checks if a new end state is set, while an existing transition
  94. /// is active. This might be detected in two ways:
  95. /// - the end color has been updated
  96. /// - the progress has been reverted
  97. bool is_modified_transition_() {
  98. auto new_end_values = transformer_->get_end_values();
  99. auto new_progress = transformer_->get_progress();
  100. return new_end_values != end_->values || new_progress < progress_;
  101. }
  102. };
  103. /// An implementation of the LightOutput interface for the Yeelight
  104. /// Bedside Lamp 2. The function of this class is to translate a
  105. /// required light state into actual physicial GPIO output signals
  106. /// to drive the device's LED circuitry.
  107. class YeelightBS2LightOutput : public Component, public light::LightOutput {
  108. public:
  109. /// Set the LEDC output for the red LED circuitry channel.
  110. void set_red_output(ledc::LEDCOutput *red) {
  111. red_ = red;
  112. }
  113. /// Set the LEDC output for the green LED circuitry channel.
  114. void set_green_output(ledc::LEDCOutput *green) {
  115. green_ = green;
  116. }
  117. /// Set the LEDC output for the blue LED circuitry channel.
  118. void set_blue_output(ledc::LEDCOutput *blue) {
  119. blue_ = blue;
  120. }
  121. /// Set the LEDC output for the white LED circuitry channel.
  122. void set_white_output(ledc::LEDCOutput *white) {
  123. white_ = white;
  124. }
  125. /// Set the first GPIO binary output, used as internal master
  126. /// switch for the LED light circuitry.
  127. void set_master1_output(gpio::GPIOBinaryOutput *master1) {
  128. master1_ = master1;
  129. }
  130. /// Set the second GPIO binary output, used as internal master
  131. /// switch for the LED light circuitry.
  132. void set_master2_output(gpio::GPIOBinaryOutput *master2) {
  133. master2_ = master2;
  134. }
  135. /// Returns a LightTraits object, which is used to explain to the
  136. /// outside world (e.g. Home Assistant) what features are supported
  137. /// by this device.
  138. light::LightTraits get_traits() override
  139. {
  140. auto traits = light::LightTraits();
  141. traits.set_supports_rgb(true);
  142. traits.set_supports_color_temperature(true);
  143. traits.set_supports_brightness(true);
  144. traits.set_supports_rgb_white_value(false);
  145. traits.set_supports_color_interlock(true);
  146. traits.set_min_mireds(MIRED_MIN);
  147. traits.set_max_mireds(MIRED_MAX);
  148. return traits;
  149. }
  150. /// Applies a requested light state to the physicial GPIO outputs.
  151. void write_state(light::LightState *state)
  152. {
  153. auto values = state->current_values;
  154. // Turn off the light when its state is 'off'.
  155. if (values.get_state() == 0)
  156. {
  157. ESP_LOGD(TAG, "Turn off the light");
  158. red_->set_level(1.0f);
  159. green_->set_level(1.0f);
  160. blue_->set_level(1.0f);
  161. white_->set_level(0.0f);
  162. master2_->turn_off();
  163. master1_->turn_off();
  164. return;
  165. }
  166. GPIOOutputs *delegate;
  167. if (transition_handler_->set_light_color_values(values)) {
  168. transition_handler_->log("TRANSITION");
  169. delegate = transition_handler_;
  170. } else {
  171. instant_handler_->set_light_color_values(values);
  172. instant_handler_->log("INSTANT");
  173. delegate = instant_handler_;
  174. }
  175. delegate->set_light_color_values(values);
  176. master2_->turn_on();
  177. master1_->turn_on();
  178. red_->set_level(delegate->red);
  179. green_->set_level(delegate->green);
  180. blue_->set_level(delegate->blue);
  181. white_->set_level(delegate->white);
  182. }
  183. protected:
  184. ledc::LEDCOutput *red_;
  185. ledc::LEDCOutput *green_;
  186. ledc::LEDCOutput *blue_;
  187. ledc::LEDCOutput *white_;
  188. esphome::gpio::GPIOBinaryOutput *master1_;
  189. esphome::gpio::GPIOBinaryOutput *master2_;
  190. GPIOOutputs *transition_handler_;
  191. GPIOOutputs *instant_handler_ = new ColorTranslator();
  192. friend class YeelightBS2LightState;
  193. /// Called by the YeelightBS2LightState class, to set the object that
  194. /// can be used to access protected data from the light state object.
  195. void set_transformer_inspector(LightStateTransformerInspector *exposer) {
  196. transition_handler_ = new TransitionHandler(exposer);
  197. }
  198. };
  199. class YeelightBS2LightState : public light::LightState, public LightStateTransformerInspector
  200. {
  201. public:
  202. YeelightBS2LightState(const std::string &name, YeelightBS2LightOutput *output) : light::LightState(name, output) {
  203. output->set_transformer_inspector(this);
  204. }
  205. bool is_active() {
  206. return this->transformer_ != nullptr;
  207. }
  208. bool is_transition() {
  209. return this->transformer_->is_transition();
  210. }
  211. light::LightColorValues get_end_values() {
  212. return this->transformer_->get_end_values();
  213. }
  214. float get_progress() {
  215. return this->transformer_->get_progress();
  216. }
  217. };
  218. } // namespace bs2
  219. } // namespace yeelight
  220. } // namespace esphome