mirror of
https://github.com/esphome/esphome.git
synced 2025-10-27 05:03:48 +00:00
[light] Store effect names in flash (const char*) to save RAM (#11487)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -9,7 +9,7 @@ static const char *const TAG = "adalight_light_effect";
|
|||||||
static const uint32_t ADALIGHT_ACK_INTERVAL = 1000;
|
static const uint32_t ADALIGHT_ACK_INTERVAL = 1000;
|
||||||
static const uint32_t ADALIGHT_RECEIVE_TIMEOUT = 1000;
|
static const uint32_t ADALIGHT_RECEIVE_TIMEOUT = 1000;
|
||||||
|
|
||||||
AdalightLightEffect::AdalightLightEffect(const std::string &name) : AddressableLightEffect(name) {}
|
AdalightLightEffect::AdalightLightEffect(const char *name) : AddressableLightEffect(name) {}
|
||||||
|
|
||||||
void AdalightLightEffect::start() {
|
void AdalightLightEffect::start() {
|
||||||
AddressableLightEffect::start();
|
AddressableLightEffect::start();
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace adalight {
|
|||||||
|
|
||||||
class AdalightLightEffect : public light::AddressableLightEffect, public uart::UARTDevice {
|
class AdalightLightEffect : public light::AddressableLightEffect, public uart::UARTDevice {
|
||||||
public:
|
public:
|
||||||
AdalightLightEffect(const std::string &name);
|
AdalightLightEffect(const char *name);
|
||||||
|
|
||||||
void start() override;
|
void start() override;
|
||||||
void stop() override;
|
void stop() override;
|
||||||
|
|||||||
@@ -80,8 +80,8 @@ void E131Component::add_effect(E131AddressableLightEffect *light_effect) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Registering '%s' for universes %d-%d.", light_effect->get_name().c_str(),
|
ESP_LOGD(TAG, "Registering '%s' for universes %d-%d.", light_effect->get_name(), light_effect->get_first_universe(),
|
||||||
light_effect->get_first_universe(), light_effect->get_last_universe());
|
light_effect->get_last_universe());
|
||||||
|
|
||||||
light_effects_.insert(light_effect);
|
light_effects_.insert(light_effect);
|
||||||
|
|
||||||
@@ -95,8 +95,8 @@ void E131Component::remove_effect(E131AddressableLightEffect *light_effect) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Unregistering '%s' for universes %d-%d.", light_effect->get_name().c_str(),
|
ESP_LOGD(TAG, "Unregistering '%s' for universes %d-%d.", light_effect->get_name(), light_effect->get_first_universe(),
|
||||||
light_effect->get_first_universe(), light_effect->get_last_universe());
|
light_effect->get_last_universe());
|
||||||
|
|
||||||
light_effects_.erase(light_effect);
|
light_effects_.erase(light_effect);
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace e131 {
|
|||||||
static const char *const TAG = "e131_addressable_light_effect";
|
static const char *const TAG = "e131_addressable_light_effect";
|
||||||
static const int MAX_DATA_SIZE = (sizeof(E131Packet::values) - 1);
|
static const int MAX_DATA_SIZE = (sizeof(E131Packet::values) - 1);
|
||||||
|
|
||||||
E131AddressableLightEffect::E131AddressableLightEffect(const std::string &name) : AddressableLightEffect(name) {}
|
E131AddressableLightEffect::E131AddressableLightEffect(const char *name) : AddressableLightEffect(name) {}
|
||||||
|
|
||||||
int E131AddressableLightEffect::get_data_per_universe() const { return get_lights_per_universe() * channels_; }
|
int E131AddressableLightEffect::get_data_per_universe() const { return get_lights_per_universe() * channels_; }
|
||||||
|
|
||||||
@@ -58,8 +58,8 @@ bool E131AddressableLightEffect::process_(int universe, const E131Packet &packet
|
|||||||
std::min(it->size(), std::min(output_offset + get_lights_per_universe(), output_offset + packet.count - 1));
|
std::min(it->size(), std::min(output_offset + get_lights_per_universe(), output_offset + packet.count - 1));
|
||||||
auto *input_data = packet.values + 1;
|
auto *input_data = packet.values + 1;
|
||||||
|
|
||||||
ESP_LOGV(TAG, "Applying data for '%s' on %d universe, for %" PRId32 "-%d.", get_name().c_str(), universe,
|
ESP_LOGV(TAG, "Applying data for '%s' on %d universe, for %" PRId32 "-%d.", get_name(), universe, output_offset,
|
||||||
output_offset, output_end);
|
output_end);
|
||||||
|
|
||||||
switch (channels_) {
|
switch (channels_) {
|
||||||
case E131_MONO:
|
case E131_MONO:
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ enum E131LightChannels { E131_MONO = 1, E131_RGB = 3, E131_RGBW = 4 };
|
|||||||
|
|
||||||
class E131AddressableLightEffect : public light::AddressableLightEffect {
|
class E131AddressableLightEffect : public light::AddressableLightEffect {
|
||||||
public:
|
public:
|
||||||
E131AddressableLightEffect(const std::string &name);
|
E131AddressableLightEffect(const char *name);
|
||||||
|
|
||||||
void start() override;
|
void start() override;
|
||||||
void stop() override;
|
void stop() override;
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ inline static uint8_t half_sin8(uint8_t v) { return sin16_c(uint16_t(v) * 128u)
|
|||||||
|
|
||||||
class AddressableLightEffect : public LightEffect {
|
class AddressableLightEffect : public LightEffect {
|
||||||
public:
|
public:
|
||||||
explicit AddressableLightEffect(const std::string &name) : LightEffect(name) {}
|
explicit AddressableLightEffect(const char *name) : LightEffect(name) {}
|
||||||
void start_internal() override {
|
void start_internal() override {
|
||||||
this->get_addressable_()->set_effect_active(true);
|
this->get_addressable_()->set_effect_active(true);
|
||||||
this->get_addressable_()->clear_effect_data();
|
this->get_addressable_()->clear_effect_data();
|
||||||
@@ -57,8 +57,7 @@ class AddressableLightEffect : public LightEffect {
|
|||||||
|
|
||||||
class AddressableLambdaLightEffect : public AddressableLightEffect {
|
class AddressableLambdaLightEffect : public AddressableLightEffect {
|
||||||
public:
|
public:
|
||||||
AddressableLambdaLightEffect(const std::string &name,
|
AddressableLambdaLightEffect(const char *name, std::function<void(AddressableLight &, Color, bool initial_run)> f,
|
||||||
std::function<void(AddressableLight &, Color, bool initial_run)> f,
|
|
||||||
uint32_t update_interval)
|
uint32_t update_interval)
|
||||||
: AddressableLightEffect(name), f_(std::move(f)), update_interval_(update_interval) {}
|
: AddressableLightEffect(name), f_(std::move(f)), update_interval_(update_interval) {}
|
||||||
void start() override { this->initial_run_ = true; }
|
void start() override { this->initial_run_ = true; }
|
||||||
@@ -81,7 +80,7 @@ class AddressableLambdaLightEffect : public AddressableLightEffect {
|
|||||||
|
|
||||||
class AddressableRainbowLightEffect : public AddressableLightEffect {
|
class AddressableRainbowLightEffect : public AddressableLightEffect {
|
||||||
public:
|
public:
|
||||||
explicit AddressableRainbowLightEffect(const std::string &name) : AddressableLightEffect(name) {}
|
explicit AddressableRainbowLightEffect(const char *name) : AddressableLightEffect(name) {}
|
||||||
void apply(AddressableLight &it, const Color ¤t_color) override {
|
void apply(AddressableLight &it, const Color ¤t_color) override {
|
||||||
ESPHSVColor hsv;
|
ESPHSVColor hsv;
|
||||||
hsv.value = 255;
|
hsv.value = 255;
|
||||||
@@ -112,7 +111,7 @@ struct AddressableColorWipeEffectColor {
|
|||||||
|
|
||||||
class AddressableColorWipeEffect : public AddressableLightEffect {
|
class AddressableColorWipeEffect : public AddressableLightEffect {
|
||||||
public:
|
public:
|
||||||
explicit AddressableColorWipeEffect(const std::string &name) : AddressableLightEffect(name) {}
|
explicit AddressableColorWipeEffect(const char *name) : AddressableLightEffect(name) {}
|
||||||
void set_colors(const std::initializer_list<AddressableColorWipeEffectColor> &colors) { this->colors_ = colors; }
|
void set_colors(const std::initializer_list<AddressableColorWipeEffectColor> &colors) { this->colors_ = colors; }
|
||||||
void set_add_led_interval(uint32_t add_led_interval) { this->add_led_interval_ = add_led_interval; }
|
void set_add_led_interval(uint32_t add_led_interval) { this->add_led_interval_ = add_led_interval; }
|
||||||
void set_reverse(bool reverse) { this->reverse_ = reverse; }
|
void set_reverse(bool reverse) { this->reverse_ = reverse; }
|
||||||
@@ -165,7 +164,7 @@ class AddressableColorWipeEffect : public AddressableLightEffect {
|
|||||||
|
|
||||||
class AddressableScanEffect : public AddressableLightEffect {
|
class AddressableScanEffect : public AddressableLightEffect {
|
||||||
public:
|
public:
|
||||||
explicit AddressableScanEffect(const std::string &name) : AddressableLightEffect(name) {}
|
explicit AddressableScanEffect(const char *name) : AddressableLightEffect(name) {}
|
||||||
void set_move_interval(uint32_t move_interval) { this->move_interval_ = move_interval; }
|
void set_move_interval(uint32_t move_interval) { this->move_interval_ = move_interval; }
|
||||||
void set_scan_width(uint32_t scan_width) { this->scan_width_ = scan_width; }
|
void set_scan_width(uint32_t scan_width) { this->scan_width_ = scan_width; }
|
||||||
void apply(AddressableLight &it, const Color ¤t_color) override {
|
void apply(AddressableLight &it, const Color ¤t_color) override {
|
||||||
@@ -202,7 +201,7 @@ class AddressableScanEffect : public AddressableLightEffect {
|
|||||||
|
|
||||||
class AddressableTwinkleEffect : public AddressableLightEffect {
|
class AddressableTwinkleEffect : public AddressableLightEffect {
|
||||||
public:
|
public:
|
||||||
explicit AddressableTwinkleEffect(const std::string &name) : AddressableLightEffect(name) {}
|
explicit AddressableTwinkleEffect(const char *name) : AddressableLightEffect(name) {}
|
||||||
void apply(AddressableLight &addressable, const Color ¤t_color) override {
|
void apply(AddressableLight &addressable, const Color ¤t_color) override {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
uint8_t pos_add = 0;
|
uint8_t pos_add = 0;
|
||||||
@@ -244,7 +243,7 @@ class AddressableTwinkleEffect : public AddressableLightEffect {
|
|||||||
|
|
||||||
class AddressableRandomTwinkleEffect : public AddressableLightEffect {
|
class AddressableRandomTwinkleEffect : public AddressableLightEffect {
|
||||||
public:
|
public:
|
||||||
explicit AddressableRandomTwinkleEffect(const std::string &name) : AddressableLightEffect(name) {}
|
explicit AddressableRandomTwinkleEffect(const char *name) : AddressableLightEffect(name) {}
|
||||||
void apply(AddressableLight &it, const Color ¤t_color) override {
|
void apply(AddressableLight &it, const Color ¤t_color) override {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
uint8_t pos_add = 0;
|
uint8_t pos_add = 0;
|
||||||
@@ -293,7 +292,7 @@ class AddressableRandomTwinkleEffect : public AddressableLightEffect {
|
|||||||
|
|
||||||
class AddressableFireworksEffect : public AddressableLightEffect {
|
class AddressableFireworksEffect : public AddressableLightEffect {
|
||||||
public:
|
public:
|
||||||
explicit AddressableFireworksEffect(const std::string &name) : AddressableLightEffect(name) {}
|
explicit AddressableFireworksEffect(const char *name) : AddressableLightEffect(name) {}
|
||||||
void start() override {
|
void start() override {
|
||||||
auto &it = *this->get_addressable_();
|
auto &it = *this->get_addressable_();
|
||||||
it.all() = Color::BLACK;
|
it.all() = Color::BLACK;
|
||||||
@@ -342,7 +341,7 @@ class AddressableFireworksEffect : public AddressableLightEffect {
|
|||||||
|
|
||||||
class AddressableFlickerEffect : public AddressableLightEffect {
|
class AddressableFlickerEffect : public AddressableLightEffect {
|
||||||
public:
|
public:
|
||||||
explicit AddressableFlickerEffect(const std::string &name) : AddressableLightEffect(name) {}
|
explicit AddressableFlickerEffect(const char *name) : AddressableLightEffect(name) {}
|
||||||
void apply(AddressableLight &it, const Color ¤t_color) override {
|
void apply(AddressableLight &it, const Color ¤t_color) override {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
const uint8_t intensity = this->intensity_;
|
const uint8_t intensity = this->intensity_;
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ inline static float random_cubic_float() {
|
|||||||
/// Pulse effect.
|
/// Pulse effect.
|
||||||
class PulseLightEffect : public LightEffect {
|
class PulseLightEffect : public LightEffect {
|
||||||
public:
|
public:
|
||||||
explicit PulseLightEffect(const std::string &name) : LightEffect(name) {}
|
explicit PulseLightEffect(const char *name) : LightEffect(name) {}
|
||||||
|
|
||||||
void apply() override {
|
void apply() override {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
@@ -60,7 +60,7 @@ class PulseLightEffect : public LightEffect {
|
|||||||
/// Random effect. Sets random colors every 10 seconds and slowly transitions between them.
|
/// Random effect. Sets random colors every 10 seconds and slowly transitions between them.
|
||||||
class RandomLightEffect : public LightEffect {
|
class RandomLightEffect : public LightEffect {
|
||||||
public:
|
public:
|
||||||
explicit RandomLightEffect(const std::string &name) : LightEffect(name) {}
|
explicit RandomLightEffect(const char *name) : LightEffect(name) {}
|
||||||
|
|
||||||
void apply() override {
|
void apply() override {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
@@ -112,7 +112,7 @@ class RandomLightEffect : public LightEffect {
|
|||||||
|
|
||||||
class LambdaLightEffect : public LightEffect {
|
class LambdaLightEffect : public LightEffect {
|
||||||
public:
|
public:
|
||||||
LambdaLightEffect(const std::string &name, std::function<void(bool initial_run)> f, uint32_t update_interval)
|
LambdaLightEffect(const char *name, std::function<void(bool initial_run)> f, uint32_t update_interval)
|
||||||
: LightEffect(name), f_(std::move(f)), update_interval_(update_interval) {}
|
: LightEffect(name), f_(std::move(f)), update_interval_(update_interval) {}
|
||||||
|
|
||||||
void start() override { this->initial_run_ = true; }
|
void start() override { this->initial_run_ = true; }
|
||||||
@@ -138,7 +138,7 @@ class LambdaLightEffect : public LightEffect {
|
|||||||
|
|
||||||
class AutomationLightEffect : public LightEffect {
|
class AutomationLightEffect : public LightEffect {
|
||||||
public:
|
public:
|
||||||
AutomationLightEffect(const std::string &name) : LightEffect(name) {}
|
AutomationLightEffect(const char *name) : LightEffect(name) {}
|
||||||
void stop() override { this->trig_->stop_action(); }
|
void stop() override { this->trig_->stop_action(); }
|
||||||
void apply() override {
|
void apply() override {
|
||||||
if (!this->trig_->is_action_running()) {
|
if (!this->trig_->is_action_running()) {
|
||||||
@@ -163,7 +163,7 @@ struct StrobeLightEffectColor {
|
|||||||
|
|
||||||
class StrobeLightEffect : public LightEffect {
|
class StrobeLightEffect : public LightEffect {
|
||||||
public:
|
public:
|
||||||
explicit StrobeLightEffect(const std::string &name) : LightEffect(name) {}
|
explicit StrobeLightEffect(const char *name) : LightEffect(name) {}
|
||||||
void apply() override {
|
void apply() override {
|
||||||
const uint32_t now = millis();
|
const uint32_t now = millis();
|
||||||
if (now - this->last_switch_ < this->colors_[this->at_color_].duration)
|
if (now - this->last_switch_ < this->colors_[this->at_color_].duration)
|
||||||
@@ -198,7 +198,7 @@ class StrobeLightEffect : public LightEffect {
|
|||||||
|
|
||||||
class FlickerLightEffect : public LightEffect {
|
class FlickerLightEffect : public LightEffect {
|
||||||
public:
|
public:
|
||||||
explicit FlickerLightEffect(const std::string &name) : LightEffect(name) {}
|
explicit FlickerLightEffect(const char *name) : LightEffect(name) {}
|
||||||
|
|
||||||
void apply() override {
|
void apply() override {
|
||||||
LightColorValues remote = this->state_->remote_values;
|
LightColorValues remote = this->state_->remote_values;
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ void LightCall::perform() {
|
|||||||
if (this->effect_ == 0u) {
|
if (this->effect_ == 0u) {
|
||||||
effect_s = "None";
|
effect_s = "None";
|
||||||
} else {
|
} else {
|
||||||
effect_s = this->parent_->effects_[this->effect_ - 1]->get_name().c_str();
|
effect_s = this->parent_->effects_[this->effect_ - 1]->get_name();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (publish) {
|
if (publish) {
|
||||||
@@ -511,7 +511,7 @@ LightCall &LightCall::set_effect(const std::string &effect) {
|
|||||||
for (uint32_t i = 0; i < this->parent_->effects_.size(); i++) {
|
for (uint32_t i = 0; i < this->parent_->effects_.size(); i++) {
|
||||||
LightEffect *e = this->parent_->effects_[i];
|
LightEffect *e = this->parent_->effects_[i];
|
||||||
|
|
||||||
if (strcasecmp(effect.c_str(), e->get_name().c_str()) == 0) {
|
if (strcasecmp(effect.c_str(), e->get_name()) == 0) {
|
||||||
this->set_effect(i + 1);
|
this->set_effect(i + 1);
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
@@ -11,7 +9,7 @@ class LightState;
|
|||||||
|
|
||||||
class LightEffect {
|
class LightEffect {
|
||||||
public:
|
public:
|
||||||
explicit LightEffect(std::string name) : name_(std::move(name)) {}
|
explicit LightEffect(const char *name) : name_(name) {}
|
||||||
|
|
||||||
/// Initialize this LightEffect. Will be called once after creation.
|
/// Initialize this LightEffect. Will be called once after creation.
|
||||||
virtual void start() {}
|
virtual void start() {}
|
||||||
@@ -24,7 +22,11 @@ class LightEffect {
|
|||||||
/// Apply this effect. Use the provided state for starting transitions, ...
|
/// Apply this effect. Use the provided state for starting transitions, ...
|
||||||
virtual void apply() = 0;
|
virtual void apply() = 0;
|
||||||
|
|
||||||
const std::string &get_name() { return this->name_; }
|
/**
|
||||||
|
* Returns the name of this effect.
|
||||||
|
* The returned pointer is valid for the lifetime of the program and must not be freed.
|
||||||
|
*/
|
||||||
|
const char *get_name() const { return this->name_; }
|
||||||
|
|
||||||
/// Internal method called by the LightState when this light effect is registered in it.
|
/// Internal method called by the LightState when this light effect is registered in it.
|
||||||
virtual void init() {}
|
virtual void init() {}
|
||||||
@@ -47,7 +49,7 @@ class LightEffect {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
LightState *state_{nullptr};
|
LightState *state_{nullptr};
|
||||||
std::string name_;
|
const char *name_;
|
||||||
|
|
||||||
/// Internal method to find this effect's index in the parent light's effect list.
|
/// Internal method to find this effect's index in the parent light's effect list.
|
||||||
uint32_t get_index_in_parent_() const;
|
uint32_t get_index_in_parent_() const;
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ class LightState : public EntityBase, public Component {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < this->effects_.size(); i++) {
|
for (size_t i = 0; i < this->effects_.size(); i++) {
|
||||||
if (strcasecmp(effect_name.c_str(), this->effects_[i]->get_name().c_str()) == 0) {
|
if (strcasecmp(effect_name.c_str(), this->effects_[i]->get_name()) == 0) {
|
||||||
return i + 1; // Effects are 1-indexed in active_effect_index_
|
return i + 1; // Effects are 1-indexed in active_effect_index_
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ const int DEFAULT_BLANK_TIME = 1000;
|
|||||||
|
|
||||||
static const char *const TAG = "wled_light_effect";
|
static const char *const TAG = "wled_light_effect";
|
||||||
|
|
||||||
WLEDLightEffect::WLEDLightEffect(const std::string &name) : AddressableLightEffect(name) {}
|
WLEDLightEffect::WLEDLightEffect(const char *name) : AddressableLightEffect(name) {}
|
||||||
|
|
||||||
void WLEDLightEffect::start() {
|
void WLEDLightEffect::start() {
|
||||||
AddressableLightEffect::start();
|
AddressableLightEffect::start();
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace wled {
|
|||||||
|
|
||||||
class WLEDLightEffect : public light::AddressableLightEffect {
|
class WLEDLightEffect : public light::AddressableLightEffect {
|
||||||
public:
|
public:
|
||||||
WLEDLightEffect(const std::string &name);
|
WLEDLightEffect(const char *name);
|
||||||
|
|
||||||
void start() override;
|
void start() override;
|
||||||
void stop() override;
|
void stop() override;
|
||||||
|
|||||||
Reference in New Issue
Block a user