mirror of
https://github.com/esphome/esphome.git
synced 2025-10-26 12:43: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_RECEIVE_TIMEOUT = 1000;
|
||||
|
||||
AdalightLightEffect::AdalightLightEffect(const std::string &name) : AddressableLightEffect(name) {}
|
||||
AdalightLightEffect::AdalightLightEffect(const char *name) : AddressableLightEffect(name) {}
|
||||
|
||||
void AdalightLightEffect::start() {
|
||||
AddressableLightEffect::start();
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace adalight {
|
||||
|
||||
class AdalightLightEffect : public light::AddressableLightEffect, public uart::UARTDevice {
|
||||
public:
|
||||
AdalightLightEffect(const std::string &name);
|
||||
AdalightLightEffect(const char *name);
|
||||
|
||||
void start() override;
|
||||
void stop() override;
|
||||
|
||||
@@ -80,8 +80,8 @@ void E131Component::add_effect(E131AddressableLightEffect *light_effect) {
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Registering '%s' for universes %d-%d.", light_effect->get_name().c_str(),
|
||||
light_effect->get_first_universe(), light_effect->get_last_universe());
|
||||
ESP_LOGD(TAG, "Registering '%s' for universes %d-%d.", light_effect->get_name(), light_effect->get_first_universe(),
|
||||
light_effect->get_last_universe());
|
||||
|
||||
light_effects_.insert(light_effect);
|
||||
|
||||
@@ -95,8 +95,8 @@ void E131Component::remove_effect(E131AddressableLightEffect *light_effect) {
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Unregistering '%s' for universes %d-%d.", light_effect->get_name().c_str(),
|
||||
light_effect->get_first_universe(), light_effect->get_last_universe());
|
||||
ESP_LOGD(TAG, "Unregistering '%s' for universes %d-%d.", light_effect->get_name(), light_effect->get_first_universe(),
|
||||
light_effect->get_last_universe());
|
||||
|
||||
light_effects_.erase(light_effect);
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace e131 {
|
||||
static const char *const TAG = "e131_addressable_light_effect";
|
||||
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_; }
|
||||
|
||||
@@ -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));
|
||||
auto *input_data = packet.values + 1;
|
||||
|
||||
ESP_LOGV(TAG, "Applying data for '%s' on %d universe, for %" PRId32 "-%d.", get_name().c_str(), universe,
|
||||
output_offset, output_end);
|
||||
ESP_LOGV(TAG, "Applying data for '%s' on %d universe, for %" PRId32 "-%d.", get_name(), universe, output_offset,
|
||||
output_end);
|
||||
|
||||
switch (channels_) {
|
||||
case E131_MONO:
|
||||
|
||||
@@ -13,7 +13,7 @@ enum E131LightChannels { E131_MONO = 1, E131_RGB = 3, E131_RGBW = 4 };
|
||||
|
||||
class E131AddressableLightEffect : public light::AddressableLightEffect {
|
||||
public:
|
||||
E131AddressableLightEffect(const std::string &name);
|
||||
E131AddressableLightEffect(const char *name);
|
||||
|
||||
void start() 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 {
|
||||
public:
|
||||
explicit AddressableLightEffect(const std::string &name) : LightEffect(name) {}
|
||||
explicit AddressableLightEffect(const char *name) : LightEffect(name) {}
|
||||
void start_internal() override {
|
||||
this->get_addressable_()->set_effect_active(true);
|
||||
this->get_addressable_()->clear_effect_data();
|
||||
@@ -57,8 +57,7 @@ class AddressableLightEffect : public LightEffect {
|
||||
|
||||
class AddressableLambdaLightEffect : public AddressableLightEffect {
|
||||
public:
|
||||
AddressableLambdaLightEffect(const std::string &name,
|
||||
std::function<void(AddressableLight &, Color, bool initial_run)> f,
|
||||
AddressableLambdaLightEffect(const char *name, std::function<void(AddressableLight &, Color, bool initial_run)> f,
|
||||
uint32_t update_interval)
|
||||
: AddressableLightEffect(name), f_(std::move(f)), update_interval_(update_interval) {}
|
||||
void start() override { this->initial_run_ = true; }
|
||||
@@ -81,7 +80,7 @@ class AddressableLambdaLightEffect : public AddressableLightEffect {
|
||||
|
||||
class AddressableRainbowLightEffect : public AddressableLightEffect {
|
||||
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 {
|
||||
ESPHSVColor hsv;
|
||||
hsv.value = 255;
|
||||
@@ -112,7 +111,7 @@ struct AddressableColorWipeEffectColor {
|
||||
|
||||
class AddressableColorWipeEffect : public AddressableLightEffect {
|
||||
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_add_led_interval(uint32_t add_led_interval) { this->add_led_interval_ = add_led_interval; }
|
||||
void set_reverse(bool reverse) { this->reverse_ = reverse; }
|
||||
@@ -165,7 +164,7 @@ class AddressableColorWipeEffect : public AddressableLightEffect {
|
||||
|
||||
class AddressableScanEffect : public AddressableLightEffect {
|
||||
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_scan_width(uint32_t scan_width) { this->scan_width_ = scan_width; }
|
||||
void apply(AddressableLight &it, const Color ¤t_color) override {
|
||||
@@ -202,7 +201,7 @@ class AddressableScanEffect : public AddressableLightEffect {
|
||||
|
||||
class AddressableTwinkleEffect : public AddressableLightEffect {
|
||||
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 {
|
||||
const uint32_t now = millis();
|
||||
uint8_t pos_add = 0;
|
||||
@@ -244,7 +243,7 @@ class AddressableTwinkleEffect : public AddressableLightEffect {
|
||||
|
||||
class AddressableRandomTwinkleEffect : public AddressableLightEffect {
|
||||
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 {
|
||||
const uint32_t now = millis();
|
||||
uint8_t pos_add = 0;
|
||||
@@ -293,7 +292,7 @@ class AddressableRandomTwinkleEffect : public AddressableLightEffect {
|
||||
|
||||
class AddressableFireworksEffect : public AddressableLightEffect {
|
||||
public:
|
||||
explicit AddressableFireworksEffect(const std::string &name) : AddressableLightEffect(name) {}
|
||||
explicit AddressableFireworksEffect(const char *name) : AddressableLightEffect(name) {}
|
||||
void start() override {
|
||||
auto &it = *this->get_addressable_();
|
||||
it.all() = Color::BLACK;
|
||||
@@ -342,7 +341,7 @@ class AddressableFireworksEffect : public AddressableLightEffect {
|
||||
|
||||
class AddressableFlickerEffect : public AddressableLightEffect {
|
||||
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 {
|
||||
const uint32_t now = millis();
|
||||
const uint8_t intensity = this->intensity_;
|
||||
|
||||
@@ -17,7 +17,7 @@ inline static float random_cubic_float() {
|
||||
/// Pulse effect.
|
||||
class PulseLightEffect : public LightEffect {
|
||||
public:
|
||||
explicit PulseLightEffect(const std::string &name) : LightEffect(name) {}
|
||||
explicit PulseLightEffect(const char *name) : LightEffect(name) {}
|
||||
|
||||
void apply() override {
|
||||
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.
|
||||
class RandomLightEffect : public LightEffect {
|
||||
public:
|
||||
explicit RandomLightEffect(const std::string &name) : LightEffect(name) {}
|
||||
explicit RandomLightEffect(const char *name) : LightEffect(name) {}
|
||||
|
||||
void apply() override {
|
||||
const uint32_t now = millis();
|
||||
@@ -112,7 +112,7 @@ class RandomLightEffect : public LightEffect {
|
||||
|
||||
class LambdaLightEffect : public LightEffect {
|
||||
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) {}
|
||||
|
||||
void start() override { this->initial_run_ = true; }
|
||||
@@ -138,7 +138,7 @@ class LambdaLightEffect : public LightEffect {
|
||||
|
||||
class AutomationLightEffect : public LightEffect {
|
||||
public:
|
||||
AutomationLightEffect(const std::string &name) : LightEffect(name) {}
|
||||
AutomationLightEffect(const char *name) : LightEffect(name) {}
|
||||
void stop() override { this->trig_->stop_action(); }
|
||||
void apply() override {
|
||||
if (!this->trig_->is_action_running()) {
|
||||
@@ -163,7 +163,7 @@ struct StrobeLightEffectColor {
|
||||
|
||||
class StrobeLightEffect : public LightEffect {
|
||||
public:
|
||||
explicit StrobeLightEffect(const std::string &name) : LightEffect(name) {}
|
||||
explicit StrobeLightEffect(const char *name) : LightEffect(name) {}
|
||||
void apply() override {
|
||||
const uint32_t now = millis();
|
||||
if (now - this->last_switch_ < this->colors_[this->at_color_].duration)
|
||||
@@ -198,7 +198,7 @@ class StrobeLightEffect : public LightEffect {
|
||||
|
||||
class FlickerLightEffect : public LightEffect {
|
||||
public:
|
||||
explicit FlickerLightEffect(const std::string &name) : LightEffect(name) {}
|
||||
explicit FlickerLightEffect(const char *name) : LightEffect(name) {}
|
||||
|
||||
void apply() override {
|
||||
LightColorValues remote = this->state_->remote_values;
|
||||
|
||||
@@ -156,7 +156,7 @@ void LightCall::perform() {
|
||||
if (this->effect_ == 0u) {
|
||||
effect_s = "None";
|
||||
} 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) {
|
||||
@@ -511,7 +511,7 @@ LightCall &LightCall::set_effect(const std::string &effect) {
|
||||
for (uint32_t i = 0; i < this->parent_->effects_.size(); 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);
|
||||
found = true;
|
||||
break;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
namespace esphome {
|
||||
@@ -11,7 +9,7 @@ class LightState;
|
||||
|
||||
class LightEffect {
|
||||
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.
|
||||
virtual void start() {}
|
||||
@@ -24,7 +22,11 @@ class LightEffect {
|
||||
/// Apply this effect. Use the provided state for starting transitions, ...
|
||||
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.
|
||||
virtual void init() {}
|
||||
@@ -47,7 +49,7 @@ class LightEffect {
|
||||
|
||||
protected:
|
||||
LightState *state_{nullptr};
|
||||
std::string name_;
|
||||
const char *name_;
|
||||
|
||||
/// Internal method to find this effect's index in the parent light's effect list.
|
||||
uint32_t get_index_in_parent_() const;
|
||||
|
||||
@@ -177,7 +177,7 @@ class LightState : public EntityBase, public Component {
|
||||
return 0;
|
||||
}
|
||||
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_
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ const int DEFAULT_BLANK_TIME = 1000;
|
||||
|
||||
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() {
|
||||
AddressableLightEffect::start();
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace wled {
|
||||
|
||||
class WLEDLightEffect : public light::AddressableLightEffect {
|
||||
public:
|
||||
WLEDLightEffect(const std::string &name);
|
||||
WLEDLightEffect(const char *name);
|
||||
|
||||
void start() override;
|
||||
void stop() override;
|
||||
|
||||
Reference in New Issue
Block a user