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.

80 lines
2.4 KiB

  1. #pragma once
  2. #include "../common.h"
  3. #include "../light_hal.h"
  4. #include "color_instant_handler.h"
  5. #include "gpio_outputs.h"
  6. #include "esphome/components/light/light_transformer.h"
  7. #include "esphome/components/light/light_color_values.h"
  8. namespace esphome {
  9. namespace xiaomi {
  10. namespace bslamp2 {
  11. /**
  12. * A LightTransitionTransformer class for the Xiaomi Mijia Bedside Lamp 2.
  13. */
  14. class XiaomiBslamp2LightTransitionTransformer : public light::LightTransitionTransformer {
  15. public:
  16. explicit XiaomiBslamp2LightTransitionTransformer(LightHAL *light) : light_(light) { }
  17. bool is_completed() override {
  18. return is_completed_ || get_progress_() >= 1.0f;
  19. }
  20. void start() override {
  21. // When a fresh transition is started, then compute the GPIO outputs
  22. // to use for both the start and end point. This light transition transformer
  23. // will then transition linearly between these two.
  24. start_->set_light_color_values(start_values_);
  25. target_->set_light_color_values(target_values_);
  26. // When a transition is modified, then use the current GPIO outputs
  27. // as the new starting point.
  28. // ... TODO
  29. //
  30. ESP_LOGE("DEBUG", "Start this thing"); // TODO
  31. }
  32. void finish() override {
  33. ESP_LOGE("DEBUG", "Finish this thing"); // TODO
  34. }
  35. void abort() override {
  36. ESP_LOGE("DEBUG", "Abort this thing"); // TODO
  37. }
  38. optional<light::LightColorValues> apply() override {
  39. if (target_->light_mode == "night") {
  40. ESP_LOGE("DEBUG", "Set night light directly"); // DEBUG
  41. light_->set_rgbw(target_->red, target_->green, target_->blue, target_->white);
  42. is_completed_ = true;
  43. }
  44. else {
  45. auto smoothed = light::LightTransitionTransformer::smoothed_progress(get_progress_());
  46. light_->set_rgbw(
  47. esphome::lerp(smoothed, start_->red, target_->red),
  48. esphome::lerp(smoothed, start_->green, target_->green),
  49. esphome::lerp(smoothed, start_->blue, target_->blue),
  50. esphome::lerp(smoothed, start_->white, target_->white));
  51. }
  52. if (is_completed()) {
  53. ESP_LOGE("DEBUG", "We're done, publish target values"); // TODO
  54. is_completed_ = true;
  55. return target_values_;
  56. } else {
  57. return {};
  58. }
  59. }
  60. protected:
  61. LightHAL *light_;
  62. bool is_completed_ = false;
  63. GPIOOutputs *start_ = new ColorInstantHandler();
  64. GPIOOutputs *target_ = new ColorInstantHandler();
  65. };
  66. } // namespace bslamp2
  67. } // namespace xiaomi
  68. } // namespace esphome