|
@ -10,15 +10,19 @@ class Preset { |
|
|
public: |
|
|
public: |
|
|
std::string name; |
|
|
std::string name; |
|
|
Preset *next_preset = nullptr; |
|
|
Preset *next_preset = nullptr; |
|
|
|
|
|
std::string group_name; |
|
|
light::LightCall *light_call; |
|
|
light::LightCall *light_call; |
|
|
|
|
|
|
|
|
explicit Preset(std::string name) : name(name) {} |
|
|
|
|
|
|
|
|
explicit Preset(std::string name, std::string group_name) : |
|
|
|
|
|
name(name), group_name(group_name) {} |
|
|
|
|
|
|
|
|
void set_light_call(light::LightCall *light_call) { |
|
|
void set_light_call(light::LightCall *light_call) { |
|
|
this->light_call = light_call; |
|
|
this->light_call = light_call; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void apply() { |
|
|
void apply() { |
|
|
|
|
|
ESP_LOGI(TAG, "Activating light preset: %s/%s", |
|
|
|
|
|
group_name.c_str(), name.c_str()); |
|
|
light_call->perform(); |
|
|
light_call->perform(); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
@ -36,7 +40,7 @@ public: |
|
|
Preset *make_preset(std::string p_name) { |
|
|
Preset *make_preset(std::string p_name) { |
|
|
auto p = get_preset(p_name); |
|
|
auto p = get_preset(p_name); |
|
|
if (p == nullptr) { |
|
|
if (p == nullptr) { |
|
|
p = new Preset(p_name); |
|
|
|
|
|
|
|
|
p = new Preset(p_name, this->name); |
|
|
if (first_preset == nullptr) { |
|
|
if (first_preset == nullptr) { |
|
|
first_preset = last_preset = active_preset = p; |
|
|
first_preset = last_preset = active_preset = p; |
|
|
} else { |
|
|
} else { |
|
@ -59,14 +63,18 @@ public: |
|
|
|
|
|
|
|
|
class PresetsContainer : public Component { |
|
|
class PresetsContainer : public Component { |
|
|
public: |
|
|
public: |
|
|
|
|
|
PresetGroup *first_group = nullptr; |
|
|
|
|
|
PresetGroup *last_group = nullptr; |
|
|
|
|
|
PresetGroup *active_group = nullptr; |
|
|
|
|
|
|
|
|
explicit PresetsContainer(light::LightState *light) : _light(light) { } |
|
|
explicit PresetsContainer(light::LightState *light) : _light(light) { } |
|
|
|
|
|
|
|
|
void dump_config() { |
|
|
void dump_config() { |
|
|
if (first_group_ == nullptr) { |
|
|
|
|
|
|
|
|
if (first_group == nullptr) { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
ESP_LOGCONFIG(TAG, "Light Presets:"); |
|
|
ESP_LOGCONFIG(TAG, "Light Presets:"); |
|
|
for (auto g = first_group_; g != nullptr; g = g->next_group) { |
|
|
|
|
|
|
|
|
for (auto g = first_group; g != nullptr; g = g->next_group) { |
|
|
ESP_LOGCONFIG(TAG, " Preset group: %s", g->name.c_str()); |
|
|
ESP_LOGCONFIG(TAG, " Preset group: %s", g->name.c_str()); |
|
|
for (auto p = g->first_preset; p != nullptr; p = p->next_preset) { |
|
|
for (auto p = g->first_preset; p != nullptr; p = p->next_preset) { |
|
|
ESP_LOGCONFIG(TAG, " Preset: %s", p->name.c_str()); |
|
|
ESP_LOGCONFIG(TAG, " Preset: %s", p->name.c_str()); |
|
@ -76,9 +84,7 @@ public: |
|
|
|
|
|
|
|
|
void add(std::string g_name, std::string p_name, float r, float g, float b) { |
|
|
void add(std::string g_name, std::string p_name, float r, float g, float b) { |
|
|
auto call = make_preset_call_(g_name, p_name); |
|
|
auto call = make_preset_call_(g_name, p_name); |
|
|
call->set_red(r); |
|
|
|
|
|
call->set_green(g); |
|
|
|
|
|
call->set_blue(b); |
|
|
|
|
|
|
|
|
call->set_rgb(r, g, b); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void add(std::string g_name, std::string p_name, float t) { |
|
|
void add(std::string g_name, std::string p_name, float t) { |
|
@ -86,81 +92,95 @@ public: |
|
|
call->set_color_temperature(t); |
|
|
call->set_color_temperature(t); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
PresetGroup *get_group(std::string g_name) { |
|
|
|
|
|
for (auto g = first_group; g != nullptr; g = g->next_group) { |
|
|
|
|
|
if (g->name == g_name) { |
|
|
|
|
|
return g; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return nullptr; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void activate_next_group() { |
|
|
void activate_next_group() { |
|
|
if (active_group_ == nullptr) { |
|
|
|
|
|
|
|
|
if (active_group == nullptr) { |
|
|
ESP_LOGW(TAG, "activate_next_group(): no preset groups defined"); |
|
|
ESP_LOGW(TAG, "activate_next_group(): no preset groups defined"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
active_group_ = active_group_->next_group == nullptr |
|
|
|
|
|
? first_group_ : active_group_->next_group; |
|
|
|
|
|
if (active_group_->active_preset == nullptr) { |
|
|
|
|
|
|
|
|
active_group = active_group->next_group == nullptr |
|
|
|
|
|
? first_group : active_group->next_group; |
|
|
|
|
|
if (active_group->active_preset == nullptr) { |
|
|
ESP_LOGW(TAG, "activate_next_group(): no presets defined for group %s", |
|
|
ESP_LOGW(TAG, "activate_next_group(): no presets defined for group %s", |
|
|
active_group_->name.c_str()); |
|
|
|
|
|
|
|
|
active_group->name.c_str()); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
ESP_LOGW(TAG, "activate_next_group(): activating %s/%s", |
|
|
|
|
|
active_group_->name.c_str(), |
|
|
|
|
|
active_group_->active_preset->name.c_str()); |
|
|
|
|
|
active_group_->active_preset->apply(); |
|
|
|
|
|
|
|
|
active_group->active_preset->apply(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void activate_next_preset() { |
|
|
void activate_next_preset() { |
|
|
if (active_group_ == nullptr) { |
|
|
|
|
|
|
|
|
if (active_group == nullptr) { |
|
|
ESP_LOGW(TAG, "activate_next_preset(): no preset groups defined"); |
|
|
ESP_LOGW(TAG, "activate_next_preset(): no preset groups defined"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
auto p = active_group_->active_preset; |
|
|
|
|
|
|
|
|
auto p = active_group->active_preset; |
|
|
if (p == nullptr) { |
|
|
if (p == nullptr) { |
|
|
ESP_LOGW(TAG, "activate_next_preset(): no presets defined for group %s", |
|
|
ESP_LOGW(TAG, "activate_next_preset(): no presets defined for group %s", |
|
|
active_group_->name.c_str()); |
|
|
|
|
|
|
|
|
active_group->name.c_str()); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
active_group_->active_preset = p->next_preset == nullptr |
|
|
|
|
|
? active_group_->first_preset : p->next_preset; |
|
|
|
|
|
ESP_LOGW(TAG, "activate_next_preset(): activating %s/%s", |
|
|
|
|
|
active_group_->name.c_str(), |
|
|
|
|
|
active_group_->active_preset->name.c_str()); |
|
|
|
|
|
active_group_->active_preset->apply(); |
|
|
|
|
|
|
|
|
active_group->active_preset = p->next_preset == nullptr |
|
|
|
|
|
? active_group->first_preset : p->next_preset; |
|
|
|
|
|
active_group->active_preset->apply(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void activate_group(std::string g_name) { |
|
|
void activate_group(std::string g_name) { |
|
|
ESP_LOGI(TAG, "Activate group %s", g_name.c_str()); |
|
|
|
|
|
|
|
|
auto g = get_group(g_name); |
|
|
|
|
|
if (g == nullptr) { |
|
|
|
|
|
ESP_LOGE(TAG, "activate_group(%s): preset group does not exist", |
|
|
|
|
|
g_name.c_str()); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
auto p = g->active_preset; |
|
|
|
|
|
if (p == nullptr) { |
|
|
|
|
|
ESP_LOGW(TAG, "activate_group(%s): no presets defined for group", |
|
|
|
|
|
g_name.c_str()); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
p->apply(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void activate_preset(std::string g_name, std::string p_name) { |
|
|
void activate_preset(std::string g_name, std::string p_name) { |
|
|
ESP_LOGI(TAG, "Activate preset %s/%s", g_name.c_str(), p_name.c_str()); |
|
|
|
|
|
|
|
|
auto g = get_group(g_name); |
|
|
|
|
|
if (g == nullptr) { |
|
|
|
|
|
ESP_LOGE(TAG, "activate_preset(%s, %s): preset group %s does not exist", |
|
|
|
|
|
g_name.c_str(), p_name.c_str(), g_name.c_str()); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
auto p = g->get_preset(p_name); |
|
|
|
|
|
if (p == nullptr) { |
|
|
|
|
|
ESP_LOGE(TAG, "activate_preset(%s, %s): preset %s does not exist in group %s", |
|
|
|
|
|
g_name.c_str(), p_name.c_str(), p_name.c_str(), g_name.c_str()); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
p->apply(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected: |
|
|
protected: |
|
|
light::LightState *_light; |
|
|
light::LightState *_light; |
|
|
PresetGroup *first_group_ = nullptr; |
|
|
|
|
|
PresetGroup *last_group_ = nullptr; |
|
|
|
|
|
PresetGroup *active_group_ = nullptr; |
|
|
|
|
|
|
|
|
|
|
|
PresetGroup *make_preset_group_(std::string g_name) { |
|
|
PresetGroup *make_preset_group_(std::string g_name) { |
|
|
auto g = get_group_(g_name); |
|
|
|
|
|
|
|
|
auto g = get_group(g_name); |
|
|
if (g == nullptr) { |
|
|
if (g == nullptr) { |
|
|
g = new PresetGroup(g_name); |
|
|
g = new PresetGroup(g_name); |
|
|
if (first_group_ == nullptr) { |
|
|
|
|
|
first_group_ = last_group_ = active_group_ = g; |
|
|
|
|
|
|
|
|
if (first_group == nullptr) { |
|
|
|
|
|
first_group = last_group = active_group = g; |
|
|
} else { |
|
|
} else { |
|
|
last_group_->next_group = g; |
|
|
|
|
|
last_group_ = g; |
|
|
|
|
|
|
|
|
last_group->next_group = g; |
|
|
|
|
|
last_group = g; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return g; |
|
|
return g; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
PresetGroup *get_group_(std::string g_name) { |
|
|
|
|
|
for (auto g = first_group_; g != nullptr; g = g->next_group) { |
|
|
|
|
|
if (g->name == g_name) { |
|
|
|
|
|
return g; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return nullptr; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
light::LightCall *make_preset_call_(std::string g_name, std::string p_name) { |
|
|
light::LightCall *make_preset_call_(std::string g_name, std::string p_name) { |
|
|
auto g = make_preset_group_(g_name); |
|
|
auto g = make_preset_group_(g_name); |
|
|
auto p = g->make_preset(p_name); |
|
|
auto p = g->make_preset(p_name); |
|
|