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.

60 lines
1.9 KiB

  1. #pragma once
  2. #include <array>
  3. #include <stdexcept>
  4. #include "common.h"
  5. #include "color_off.h"
  6. #include "color_night_light.h"
  7. #include "color_white_light.h"
  8. #include "color_rgb_light.h"
  9. namespace esphome {
  10. namespace yeelight {
  11. namespace bs2 {
  12. /// This class translates LightColorValues into GPIO duty cycles
  13. /// for representing a requested light color.
  14. ///
  15. /// The code handles all known light modes for the device:
  16. ///
  17. /// - off: the light is off
  18. /// - night light: activated when brightness is at its lowest
  19. /// - white light: based on color temperature + brightness
  20. /// - RGB light: based on RGB values + brightness
  21. class ColorTranslator : public GPIOOutputs {
  22. public:
  23. bool set_light_color_values(light::LightColorValues v) {
  24. values = v;
  25. GPIOOutputs *delegate;
  26. // The actual implementation of the various light modes is in
  27. // separated targeted classes. These classes are called here
  28. // in a chain of command-like pattern, to let the first one
  29. // that can handle the light settings do the honours.
  30. if (off_light_->set_light_color_values(v))
  31. off_light_->copy_to(this);
  32. else if (night_light_->set_light_color_values(v))
  33. night_light_->copy_to(this);
  34. else if (white_light_->set_light_color_values(v))
  35. white_light_->copy_to(this);
  36. else if (rgb_light_->set_light_color_values(v))
  37. rgb_light_->copy_to(this);
  38. else
  39. throw std::logic_error(
  40. "None of the GPIOOutputs classes handles the requested light state");
  41. return true;
  42. }
  43. protected:
  44. GPIOOutputs *off_light_ = new ColorOff();
  45. GPIOOutputs *rgb_light_ = new ColorRGBLight();
  46. GPIOOutputs *white_light_ = new ColorWhiteLight();
  47. GPIOOutputs *night_light_ = new ColorNightLight();
  48. };
  49. } // namespace yeelight_bs2
  50. } // namespace yeelight
  51. } // namespace bs2