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.

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