1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-27 15:42:22 +01:00

Add monochromatic effects: Pulse, Random (#1616)

This commit is contained in:
Christian Ferbar
2021-04-28 05:05:04 +02:00
committed by GitHub
parent 6d3ccf4df5
commit cc43e01e34
3 changed files with 65 additions and 5 deletions

View File

@@ -11,6 +11,40 @@ inline static float random_cubic_float() {
return r * r * r;
}
/// Pulse effect.
class PulseLightEffect : public LightEffect {
public:
explicit PulseLightEffect(const std::string &name) : LightEffect(name) {}
void apply() override {
const uint32_t now = millis();
if (now - this->last_color_change_ < this->update_interval_) {
return;
}
auto call = this->state_->turn_on();
float out = this->on_ ? 1.0 : 0.0;
call.set_brightness_if_supported(out);
this->on_ = !this->on_;
call.set_transition_length_if_supported(this->transition_length_);
// don't tell HA every change
call.set_publish(false);
call.set_save(false);
call.perform();
this->last_color_change_ = now;
}
void set_transition_length(uint32_t transition_length) { this->transition_length_ = transition_length; }
void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
protected:
bool on_ = false;
uint32_t last_color_change_{0};
uint32_t transition_length_{};
uint32_t update_interval_{};
};
/// Random effect. Sets random colors every 10 seconds and slowly transitions between them.
class RandomLightEffect : public LightEffect {
public:
@@ -22,10 +56,14 @@ class RandomLightEffect : public LightEffect {
return;
}
auto call = this->state_->turn_on();
call.set_red_if_supported(random_float());
call.set_green_if_supported(random_float());
call.set_blue_if_supported(random_float());
call.set_white_if_supported(random_float());
if (this->state_->get_traits().get_supports_rgb()) {
call.set_red_if_supported(random_float());
call.set_green_if_supported(random_float());
call.set_blue_if_supported(random_float());
call.set_white_if_supported(random_float());
} else {
call.set_brightness_if_supported(random_float());
}
call.set_color_temperature_if_supported(random_float());
call.set_transition_length_if_supported(this->transition_length_);
call.set_publish(true);