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