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
2.2 KiB

3 years ago
  1. #pragma once
  2. #include <utility>
  3. #include "esphome/components/light/light_effect.h"
  4. namespace esphome {
  5. namespace light {
  6. inline static float my_cubic_float() {
  7. const float r = random_float() * 2.0f - 1.0f;
  8. return r * r * r;
  9. }
  10. class CandleLightEffect : public LightEffect {
  11. public:
  12. explicit CandleLightEffect(const std::string &name) : LightEffect(name) {}
  13. void apply() override {
  14. LightColorValues remote = this->state_->remote_values;
  15. LightColorValues current = this->state_->current_values;
  16. LightColorValues out;
  17. const float alpha = this->alpha_;
  18. const float beta = 1.0f - alpha;
  19. out.set_state(true);
  20. out.set_brightness(remote.get_brightness() * beta + current.get_brightness() * alpha +
  21. (my_cubic_float() * this->intensity_));
  22. out.set_red(remote.get_red() * beta + current.get_red() * alpha + (my_cubic_float() * this->intensity_));
  23. out.set_green(remote.get_green() * beta + current.get_green() * alpha + (my_cubic_float() * this->intensity_));
  24. out.set_blue(remote.get_blue() * beta + current.get_blue() * alpha + (my_cubic_float() * this->intensity_));
  25. out.set_white(remote.get_white() * beta + current.get_white() * alpha + (my_cubic_float() * this->intensity_));
  26. out.set_cold_white(remote.get_cold_white() * beta + current.get_cold_white() * alpha +
  27. (my_cubic_float() * this->intensity_));
  28. out.set_warm_white(remote.get_warm_white() * beta + current.get_warm_white() * alpha +
  29. (my_cubic_float() * this->intensity_));
  30. auto traits = this->state_->get_traits();
  31. auto call = this->state_->make_call();
  32. call.set_publish(false);
  33. call.set_save(false);
  34. call.set_transition_length_if_supported(0);
  35. call.from_light_color_values(out);
  36. call.set_state(true);
  37. call.perform();
  38. }
  39. void set_alpha(float alpha) { this->alpha_ = alpha; }
  40. void set_intensity(float intensity) { this->intensity_ = intensity; }
  41. protected:
  42. float intensity_{};
  43. float flicker_chance = 0.2f;
  44. float min_flicker_depth = 0.05f;
  45. float max_flicker_depth = 0.10f;
  46. int min_flicker_time = 250;
  47. int max_flicker_time = 400;
  48. int max_flickers_ = 5;
  49. };
  50. } // namespace light
  51. } // namespace esphome