mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
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:
parent
a2a83c5004
commit
39b35b79ba
3 changed files with 11 additions and 5 deletions
|
@ -50,21 +50,25 @@ class AddressableLightEffect : public LightEffect {
|
||||||
|
|
||||||
class AddressableLambdaLightEffect : public AddressableLightEffect {
|
class AddressableLambdaLightEffect : public AddressableLightEffect {
|
||||||
public:
|
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)
|
uint32_t update_interval)
|
||||||
: AddressableLightEffect(name), f_(f), update_interval_(update_interval) {}
|
: AddressableLightEffect(name), f_(f), update_interval_(update_interval) {}
|
||||||
|
void start() override { this->initial_run_ = true; }
|
||||||
void apply(AddressableLight &it, const ESPColor ¤t_color) override {
|
void apply(AddressableLight &it, const ESPColor ¤t_color) override {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
if (now - this->last_run_ >= this->update_interval_) {
|
if (now - this->last_run_ >= this->update_interval_) {
|
||||||
this->last_run_ = now;
|
this->last_run_ = now;
|
||||||
this->f_(it, current_color);
|
this->f_(it, current_color, this->initial_run_);
|
||||||
|
this->initial_run_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::function<void(AddressableLight &, ESPColor)> f_;
|
std::function<void(AddressableLight &, ESPColor, bool initial_run)> f_;
|
||||||
uint32_t update_interval_;
|
uint32_t update_interval_;
|
||||||
uint32_t last_run_{0};
|
uint32_t last_run_{0};
|
||||||
|
bool initial_run_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class AddressableRainbowLightEffect : public AddressableLightEffect {
|
class AddressableRainbowLightEffect : public AddressableLightEffect {
|
||||||
|
|
|
@ -162,7 +162,7 @@ def flicker_effect_to_code(config, effect_id):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
def addressable_lambda_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)
|
lambda_ = yield cg.process_lambda(config[CONF_LAMBDA], args, return_type=cg.void)
|
||||||
var = cg.new_Pvariable(effect_id, config[CONF_NAME], lambda_,
|
var = cg.new_Pvariable(effect_id, config[CONF_NAME], lambda_,
|
||||||
config[CONF_UPDATE_INTERVAL])
|
config[CONF_UPDATE_INTERVAL])
|
||||||
|
|
|
@ -1155,7 +1155,9 @@ light:
|
||||||
- addressable_lambda:
|
- addressable_lambda:
|
||||||
name: "Test For Custom Lambda Effect"
|
name: "Test For Custom Lambda Effect"
|
||||||
lambda: |-
|
lambda: |-
|
||||||
it[0] = current_color;
|
if (initial_run) {
|
||||||
|
it[0] = current_color;
|
||||||
|
}
|
||||||
|
|
||||||
- automation:
|
- automation:
|
||||||
name: Custom Effect
|
name: Custom Effect
|
||||||
|
|
Loading…
Reference in a new issue