Make initial run variable available to addressable_lambda (#1035)

* Make initial run variable available to addressable_lambda

* Fix linting

* Remove if clause
This commit is contained in:
Niklas Wagner 2020-04-29 00:24:06 +02:00 committed by GitHub
parent a2a83c5004
commit 39b35b79ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 5 deletions

View file

@ -50,21 +50,25 @@ class AddressableLightEffect : public LightEffect {
class AddressableLambdaLightEffect : public AddressableLightEffect {
public:
AddressableLambdaLightEffect(const std::string &name, const std::function<void(AddressableLight &, ESPColor)> &f,
AddressableLambdaLightEffect(const std::string &name,
const std::function<void(AddressableLight &, ESPColor, bool initial_run)> &f,
uint32_t update_interval)
: AddressableLightEffect(name), f_(f), update_interval_(update_interval) {}
void start() override { this->initial_run_ = true; }
void apply(AddressableLight &it, const ESPColor &current_color) override {
const uint32_t now = millis();
if (now - this->last_run_ >= this->update_interval_) {
this->last_run_ = now;
this->f_(it, current_color);
this->f_(it, current_color, this->initial_run_);
this->initial_run_ = false;
}
}
protected:
std::function<void(AddressableLight &, ESPColor)> f_;
std::function<void(AddressableLight &, ESPColor, bool initial_run)> f_;
uint32_t update_interval_;
uint32_t last_run_{0};
bool initial_run_;
};
class AddressableRainbowLightEffect : public AddressableLightEffect {

View file

@ -162,7 +162,7 @@ def flicker_effect_to_code(config, effect_id):
}
)
def addressable_lambda_effect_to_code(config, effect_id):
args = [(AddressableLightRef, 'it'), (ESPColor, 'current_color')]
args = [(AddressableLightRef, 'it'), (ESPColor, 'current_color'), (bool, 'initial_run')]
lambda_ = yield cg.process_lambda(config[CONF_LAMBDA], args, return_type=cg.void)
var = cg.new_Pvariable(effect_id, config[CONF_NAME], lambda_,
config[CONF_UPDATE_INTERVAL])

View file

@ -1155,7 +1155,9 @@ light:
- addressable_lambda:
name: "Test For Custom Lambda Effect"
lambda: |-
it[0] = current_color;
if (initial_run) {
it[0] = current_color;
}
- automation:
name: Custom Effect