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.

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