mirror of
https://github.com/esphome/esphome.git
synced 2025-09-10 15:22:24 +01:00
fix: refactor DynamicLamp to improve lamp management and add name handling
This commit is contained in:
@@ -17,6 +17,10 @@ namespace dynamic_lamp {
|
|||||||
static const char *TAG = "dynamic_lamp";
|
static const char *TAG = "dynamic_lamp";
|
||||||
|
|
||||||
void DynamicLamp::setup() {
|
void DynamicLamp::setup() {
|
||||||
|
this->begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void DynamicLamp::begin() {
|
||||||
uint8_t i = 0;
|
uint8_t i = 0;
|
||||||
bool valid = true;
|
bool valid = true;
|
||||||
if(this->save_mode_ == 0) {
|
if(this->save_mode_ == 0) {
|
||||||
@@ -28,13 +32,11 @@ void DynamicLamp::setup() {
|
|||||||
this->restore_lamp_values_(i);
|
this->restore_lamp_values_(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// for testing only
|
this->add_lamp_("First Lamp");
|
||||||
uint8_t first_lamp;
|
this->add_lamp_output_("First Lamp", this->available_outputs_[0]);
|
||||||
first_lamp = this->add_lamp_();
|
this->add_lamp_output_("First Lamp", this->available_outputs_[1]);
|
||||||
this->add_lamp_output_(first_lamp, this->available_outputs_[0]);
|
this->add_lamp_output_("First Lamp", this->available_outputs_[2]);
|
||||||
this->add_lamp_output_(first_lamp, this->available_outputs_[1]);
|
this->add_lamp_output_("First Lamp", this->available_outputs_[3]);
|
||||||
this->add_lamp_output_(first_lamp, this->available_outputs_[2]);
|
|
||||||
this->add_lamp_output_(first_lamp, this->available_outputs_[3]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::loop() {
|
void DynamicLamp::loop() {
|
||||||
@@ -108,23 +110,30 @@ void DynamicLamp::add_available_output(output::FloatOutput * output, std::string
|
|||||||
counter++;
|
counter++;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t DynamicLamp::add_lamp_() {
|
void DynamicLamp::add_lamp_(std::string name) {
|
||||||
uint8_t i = 0;
|
if (this->lamp_count_ < 15) {
|
||||||
while (i < 16) {
|
this->lamp_count_++;
|
||||||
if (!this->active_lamps_[i].active) {
|
this->active_lamps_[this->lamp_count_].active = true;
|
||||||
this->active_lamps_[i].active = true;
|
this->active_lamps_[this->lamp_count_].name = name;
|
||||||
this->lamp_count_++;
|
return;
|
||||||
return i;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
this->mark_failed();
|
ESP_LOGW(TAG, "No more lamps available, max 16 lamps supported!");
|
||||||
|
this->set_warning_state(true);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DynamicLamp::add_lamp_output_(uint8_t lamp_number, LinkedOutput output) {
|
void DynamicLamp::add_lamp_output_(std::string lamp_name, LinkedOutput output) {
|
||||||
this->active_lamps_[lamp_number].used_outputs[output.output_index] = true;
|
while (i < 16) {
|
||||||
//ESP_LOGV(TAG, "Added output %s to lamp %" PRIu8 "", output.output_id.c_str(), lamp_number);
|
if (this->active_lamps_[i].name == lamp_name) {
|
||||||
|
this->add_lamp_output_(i, output);
|
||||||
|
this->active_lamps_[lamp_number].used_outputs[output.output_index] = true;
|
||||||
|
ESP_LOGV(TAG, "Added output %s to lamp %" PRIu8 "", output.output_id.c_str(), lamp_number);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
this->set_warning_state(true);
|
||||||
|
ESP_LOGW(TAG, "No lamp with name %s defined !", lamp_name.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::array<bool, 16> DynamicLamp::get_lamp_outputs_(uint8_t lamp_number) {
|
std::array<bool, 16> DynamicLamp::get_lamp_outputs_(uint8_t lamp_number) {
|
||||||
|
@@ -32,6 +32,7 @@ struct LinkedOutput {
|
|||||||
|
|
||||||
struct CombinedLamp {
|
struct CombinedLamp {
|
||||||
bool active = false;
|
bool active = false;
|
||||||
|
std::string name = "";
|
||||||
bool used_outputs[16];
|
bool used_outputs[16];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -44,7 +45,8 @@ class DynamicLamp : public Component {
|
|||||||
void set_save_mode(uint8_t save_mode);
|
void set_save_mode(uint8_t save_mode);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
uint8_t add_lamp_();
|
void begin();
|
||||||
|
void add_lamp_(std::string name);
|
||||||
std::array<bool, 16> get_lamp_outputs_(uint8_t lamp_number);
|
std::array<bool, 16> get_lamp_outputs_(uint8_t lamp_number);
|
||||||
void add_lamp_output_(uint8_t lamp_number, LinkedOutput output);
|
void add_lamp_output_(uint8_t lamp_number, LinkedOutput output);
|
||||||
void restore_lamp_values_(uint8_t lamp_number);
|
void restore_lamp_values_(uint8_t lamp_number);
|
||||||
|
Reference in New Issue
Block a user