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.

62 lines
1.8 KiB

  1. #pragma once
  2. #include "../common.h"
  3. #include "../light_hal.h"
  4. #include "color_handler_chain.h"
  5. #include "esphome/components/light/light_transformer.h"
  6. #include "esphome/components/light/light_color_values.h"
  7. namespace esphome {
  8. namespace xiaomi {
  9. namespace bslamp2 {
  10. /**
  11. * A LightTransitionTransformer class for the Xiaomi Mijia Bedside Lamp 2.
  12. */
  13. class XiaomiBslamp2LightTransitionTransformer : public light::LightTransitionTransformer {
  14. public:
  15. explicit XiaomiBslamp2LightTransitionTransformer(LightHAL *light) : light_(light) { }
  16. bool is_finished() override {
  17. return force_finish_ || get_progress_() >= 1.0f;
  18. }
  19. void start() override {
  20. // Compute the GPIO outputs to use for the end point.
  21. // This light transition transformer will then transition linearly between
  22. // the current GPIO outputs and the target ones.
  23. light_->copy_to(start_);
  24. end_->set_light_color_values(target_values_);
  25. }
  26. optional<light::LightColorValues> apply() override {
  27. if (end_->light_mode == "night") {
  28. light_->set_state(end_);
  29. force_finish_ = true;
  30. }
  31. else {
  32. auto smoothed = light::LightTransitionTransformer::smoothed_progress(get_progress_());
  33. light_->set_rgbw(
  34. esphome::lerp(smoothed, start_->red, end_->red),
  35. esphome::lerp(smoothed, start_->green, end_->green),
  36. esphome::lerp(smoothed, start_->blue, end_->blue),
  37. esphome::lerp(smoothed, start_->white, end_->white));
  38. }
  39. if (is_finished()) {
  40. return target_values_;
  41. } else {
  42. return {};
  43. }
  44. }
  45. protected:
  46. LightHAL *light_;
  47. bool force_finish_ = false;
  48. GPIOOutputValues *start_ = new GPIOOutputValues();
  49. ColorHandler *end_ = new ColorHandlerChain();
  50. };
  51. } // namespace bslamp2
  52. } // namespace xiaomi
  53. } // namespace esphome