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.

259 lines
10 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. // The color must either be set instantly, or the color is
  155. // transitioning to an end color. The transition handler
  156. // will do its own inspection to see if a transition is
  157. // currently active or not. Based on the outcome, use either
  158. // the instant or transition handler.
  159. GPIOOutputs *delegate;
  160. if (transition_handler_->set_light_color_values(values)) {
  161. delegate = transition_handler_;
  162. } else {
  163. instant_handler_->set_light_color_values(values);
  164. delegate = instant_handler_;
  165. }
  166. // Note: one might think that it is more logical to turn on
  167. // the LED circuitry master switch after setting the individual
  168. // channels, but this is the order that was used by the original
  169. // firmware. I tried to stay as close as possible to the original
  170. // behavior, so that's why these GPIOs are turned on at this point.
  171. if (values.get_state() != 0)
  172. {
  173. master2_->turn_on();
  174. master1_->turn_on();
  175. }
  176. // Apply the current GPIO output levels from the selected handler.
  177. red_->set_level(delegate->red);
  178. green_->set_level(delegate->green);
  179. blue_->set_level(delegate->blue);
  180. white_->set_level(delegate->white);
  181. if (values.get_state() == 0)
  182. {
  183. master2_->turn_off();
  184. master1_->turn_off();
  185. }
  186. }
  187. protected:
  188. ledc::LEDCOutput *red_;
  189. ledc::LEDCOutput *green_;
  190. ledc::LEDCOutput *blue_;
  191. ledc::LEDCOutput *white_;
  192. esphome::gpio::GPIOBinaryOutput *master1_;
  193. esphome::gpio::GPIOBinaryOutput *master2_;
  194. GPIOOutputs *transition_handler_;
  195. GPIOOutputs *instant_handler_ = new ColorTranslator();
  196. friend class YeelightBS2LightState;
  197. /// Called by the YeelightBS2LightState class, to set the object that
  198. /// can be used to access protected data from the light state object.
  199. void set_transformer_inspector(LightStateTransformerInspector *exposer) {
  200. transition_handler_ = new TransitionHandler(exposer);
  201. }
  202. };
  203. /// This custom LightState class is used to provide access to the
  204. /// protected LightTranformer information in the LightState class.
  205. class YeelightBS2LightState : public light::LightState, public LightStateTransformerInspector
  206. {
  207. public:
  208. YeelightBS2LightState(const std::string &name, YeelightBS2LightOutput *output) : light::LightState(name, output) {
  209. output->set_transformer_inspector(this);
  210. }
  211. bool is_active() {
  212. return this->transformer_ != nullptr;
  213. }
  214. bool is_transition() {
  215. return this->transformer_->is_transition();
  216. }
  217. light::LightColorValues get_end_values() {
  218. return this->transformer_->get_end_values();
  219. }
  220. float get_progress() {
  221. return this->transformer_->get_progress();
  222. }
  223. };
  224. } // namespace bs2
  225. } // namespace yeelight
  226. } // namespace esphome