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.

99 lines
3.4 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #pragma once
  2. #include "../common.h"
  3. #include "../light_hal.h"
  4. #include "color_handler_chain.h"
  5. #include "esphome/components/light/light_transition.h"
  6. #include "esphome/components/light/transitions.h"
  7. #include "esphome/components/light/light_color_values.h"
  8. namespace esphome {
  9. namespace xiaomi {
  10. namespace bslamp2 {
  11. /**
  12. * A LightTransition class for the Xiaomi Mijia Bedside Lamp 2.
  13. */
  14. class XiaomiBslamp2LightTransition : public light::LightTransition {
  15. public:
  16. explicit XiaomiBslamp2LightTransition(std::string name, LightHAL *light) :
  17. light::LightTransition(name),
  18. light_(light) { }
  19. bool is_completed() override {
  20. return force_completed_ || get_progress_() >= 1.0f;
  21. }
  22. void start() override {
  23. // Determine the GPIO outputs to use for the start and end point.
  24. // This light transition transformer will then transition linearly between them.
  25. light_->copy_to(start_);
  26. end_->set_light_color_values(target_values_);
  27. // Update the light mode of the light HAL to the target state, unless
  28. // this is night mode. For night mode, the update is done after the
  29. // state has been reached. This makes sure that forcing instant
  30. // transitions for night light to night light is only done when the
  31. // night light status has actually been reached. E.g. when in RGB mode
  32. // and transitioning to night light in 10 seconds, interrupting this
  33. // after 5 seconds with a new night light setting should not make the
  34. // transition instant.
  35. if (end_->light_mode != LIGHT_MODE_NIGHT) {
  36. light_->set_light_mode(end_->light_mode);
  37. }
  38. // Run callbacks. These are normally called from the LightOutput, but
  39. // since I don't call LightOutput::write_state() from this transformer's
  40. // code, these callbacks must be called from this transformer as well.
  41. light_->do_light_mode_callback(end_->light_mode);
  42. light_->do_state_callback(target_values_);
  43. }
  44. optional<light::LightColorValues> apply() override {
  45. // When transitioning between night mode light colors, then do this immediately.
  46. // The LED driver circuitry is not capable of doing clean color or brightness
  47. // transitions at the low levels as used for the night light.
  48. if (end_->light_mode == LIGHT_MODE_NIGHT && start_->light_mode == LIGHT_MODE_NIGHT) {
  49. light_->set_state(end_);
  50. force_completed_ = true;
  51. }
  52. // Otherwise perform a standard transition.
  53. else {
  54. auto smoothed = smoothed_progress_(get_progress_());
  55. light_->set_rgbw(
  56. esphome::lerp(smoothed, start_->red, end_->red),
  57. esphome::lerp(smoothed, start_->green, end_->green),
  58. esphome::lerp(smoothed, start_->blue, end_->blue),
  59. esphome::lerp(smoothed, start_->white, end_->white));
  60. if (end_->light_mode != LIGHT_MODE_OFF) {
  61. light_->turn_on();
  62. }
  63. }
  64. if (is_completed()) {
  65. light_->set_light_mode(end_->light_mode);
  66. if (end_->light_mode == LIGHT_MODE_OFF) {
  67. light_->turn_off();
  68. }
  69. return target_values_;
  70. } else {
  71. return {};
  72. }
  73. }
  74. protected:
  75. LightHAL *light_;
  76. bool force_completed_{false};
  77. GPIOOutputValues *start_ = new GPIOOutputValues();
  78. ColorHandler *end_ = new ColorHandlerChain();
  79. protected:
  80. static float smoothed_progress_(float x) {
  81. return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f);
  82. }
  83. };
  84. } // namespace bslamp2
  85. } // namespace xiaomi
  86. } // namespace esphome