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.

238 lines
9.0 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 LightStateDataExposer {
  13. public:
  14. virtual bool has_active_transformer() = 0;
  15. virtual bool transformer_is_transition() = 0;
  16. virtual light::LightColorValues get_transformer_end_values() = 0;
  17. virtual float get_transformer_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(LightStateDataExposer *exposer) : exposer_(exposer) {}
  40. bool set_light_color_values(light::LightColorValues values) {
  41. if (!has_active_transition_()) {
  42. start_values = values;
  43. active_ = false;
  44. return false;
  45. }
  46. // TODO use new starting point when interrupting a transition
  47. // halfway by switching to a new color.
  48. if (is_fresh_transition_()) {
  49. start_->set_light_color_values(start_values);
  50. end_->set_light_color_values(exposer_->get_transformer_end_values());
  51. active_ = true;
  52. }
  53. auto progress = exposer_->get_transformer_progress();
  54. auto smoothed = light::LightTransitionTransformer::smoothed_progress(progress);
  55. red = esphome::lerp(smoothed, start_->red, end_->red);
  56. green = esphome::lerp(smoothed, start_->green, end_->green);
  57. blue = esphome::lerp(smoothed, start_->blue, end_->blue);
  58. white = esphome::lerp(smoothed, start_->white, end_->white);
  59. return true;
  60. }
  61. protected:
  62. bool active_ = false;
  63. LightStateDataExposer *exposer_;
  64. light::LightColorValues start_values;
  65. GPIOOutputs *start_ = new ColorTranslator();
  66. GPIOOutputs *end_ = new ColorTranslator();
  67. /// Checks if this class will handle the light output logic.
  68. /// This is the case when a transformer is active and this
  69. /// transformer does implement a transitioning effect.
  70. bool has_active_transition_() {
  71. if (!exposer_->has_active_transformer())
  72. return false;
  73. if (!exposer_->transformer_is_transition())
  74. return false;
  75. return true;
  76. }
  77. /// Checks if a fresh transitioning is started.
  78. /// A transitioning is fresh when either no transition is known to
  79. /// be in progress or when a new end state is found during an
  80. /// ongoing transition.
  81. bool is_fresh_transition_() {
  82. if (active_ == false) {
  83. return true;
  84. }
  85. auto new_end_values = exposer_->get_transformer_end_values();
  86. if (new_end_values != end_->values) {
  87. return true;
  88. }
  89. return false;
  90. }
  91. };
  92. /// An implementation of the LightOutput interface for the Yeelight
  93. /// Bedside Lamp 2. The function of this class is to translate a
  94. /// required light state into actual physicial GPIO output signals
  95. /// to drive the device's LED circuitry.
  96. class YeelightBS2LightOutput : public Component, public light::LightOutput {
  97. public:
  98. /// Set the LEDC output for the red LED circuitry channel.
  99. void set_red_output(ledc::LEDCOutput *red) {
  100. red_ = red;
  101. }
  102. /// Set the LEDC output for the green LED circuitry channel.
  103. void set_green_output(ledc::LEDCOutput *green) {
  104. green_ = green;
  105. }
  106. /// Set the LEDC output for the blue LED circuitry channel.
  107. void set_blue_output(ledc::LEDCOutput *blue) {
  108. blue_ = blue;
  109. }
  110. /// Set the LEDC output for the white LED circuitry channel.
  111. void set_white_output(ledc::LEDCOutput *white) {
  112. white_ = white;
  113. }
  114. /// Set the first GPIO binary output, used as internal master
  115. /// switch for the LED light circuitry.
  116. void set_master1_output(gpio::GPIOBinaryOutput *master1) {
  117. master1_ = master1;
  118. }
  119. /// Set the second GPIO binary output, used as internal master
  120. /// switch for the LED light circuitry.
  121. void set_master2_output(gpio::GPIOBinaryOutput *master2) {
  122. master2_ = master2;
  123. }
  124. /// Returns a LightTraits object, which is used to explain to the
  125. /// outside world (e.g. Home Assistant) what features are supported
  126. /// by this device.
  127. light::LightTraits get_traits() override
  128. {
  129. auto traits = light::LightTraits();
  130. traits.set_supports_rgb(true);
  131. traits.set_supports_color_temperature(true);
  132. traits.set_supports_brightness(true);
  133. traits.set_supports_rgb_white_value(false);
  134. traits.set_supports_color_interlock(true);
  135. traits.set_min_mireds(MIRED_MIN);
  136. traits.set_max_mireds(MIRED_MAX);
  137. return traits;
  138. }
  139. /// Applies a requested light state to the physicial GPIO outputs.
  140. void write_state(light::LightState *state)
  141. {
  142. auto values = state->current_values;
  143. // Power down the light when its state is 'off'.
  144. if (values.get_state() == 0)
  145. {
  146. red_->set_level(1.0f);
  147. green_->set_level(1.0f);
  148. blue_->set_level(1.0f);
  149. white_->set_level(0.0f);
  150. master2_->turn_off();
  151. master1_->turn_off();
  152. return;
  153. }
  154. GPIOOutputs *delegate;
  155. if (transition_handler_->set_light_color_values(values)) {
  156. transition_handler_->log("TRANSITION");
  157. delegate = transition_handler_;
  158. } else {
  159. instant_handler_->set_light_color_values(values);
  160. instant_handler_->log("INSTANT");
  161. delegate = instant_handler_;
  162. }
  163. delegate->set_light_color_values(values);
  164. master2_->turn_on();
  165. master1_->turn_on();
  166. red_->set_level(delegate->red);
  167. green_->set_level(delegate->green);
  168. blue_->set_level(delegate->blue);
  169. white_->set_level(delegate->white);
  170. }
  171. protected:
  172. ledc::LEDCOutput *red_;
  173. ledc::LEDCOutput *green_;
  174. ledc::LEDCOutput *blue_;
  175. ledc::LEDCOutput *white_;
  176. esphome::gpio::GPIOBinaryOutput *master1_;
  177. esphome::gpio::GPIOBinaryOutput *master2_;
  178. GPIOOutputs *transition_handler_;
  179. GPIOOutputs *instant_handler_ = new ColorTranslator();
  180. friend class YeelightBS2LightState;
  181. /// Called by the YeelightBS2LightState class, to set the object that
  182. /// can be used to access protected data from the light state object.
  183. void set_light_state_data_exposer(LightStateDataExposer *exposer) {
  184. transition_handler_ = new TransitionHandler(exposer);
  185. }
  186. };
  187. class YeelightBS2LightState : public light::LightState, public LightStateDataExposer
  188. {
  189. public:
  190. YeelightBS2LightState(const std::string &name, YeelightBS2LightOutput *output) : light::LightState(name, output) {
  191. output->set_light_state_data_exposer(this);
  192. }
  193. bool has_active_transformer() {
  194. return this->transformer_ != nullptr;
  195. }
  196. bool transformer_is_transition() {
  197. return this->transformer_->is_transition();
  198. }
  199. light::LightColorValues get_transformer_end_values() {
  200. return this->transformer_->get_end_values();
  201. }
  202. float get_transformer_progress() {
  203. return this->transformer_->get_progress();
  204. }
  205. };
  206. } // namespace bs2
  207. } // namespace yeelight
  208. } // namespace esphome