diff --git a/esphome/components/mcp4461/mcp4461.cpp b/esphome/components/mcp4461/mcp4461.cpp index 2b84997b99..0dded2b625 100644 --- a/esphome/components/mcp4461/mcp4461.cpp +++ b/esphome/components/mcp4461/mcp4461.cpp @@ -52,6 +52,8 @@ static const LogString *mcp4461_get_message_string_(int status) { return LOG_STR("Status register could not be read"); case Mcp4461Component::MCP4461_STATUS_REGISTER_INVALID: return LOG_STR("Invalid status register value - bits 1,7 or 8 are 0"); + case Mcp4461Component::MCP4461_PARENT_FAILED: + return LOG_STR("Parent component failed"); case Mcp4461Component::MCP4461_VALUE_INVALID: return LOG_STR("Invalid value for wiper given"); case Mcp4461Component::MCP4461_WRITE_PROTECTED: @@ -62,7 +64,7 @@ static const LogString *mcp4461_get_message_string_(int status) { return LOG_STR("MCP4461 Wiper is disabled. All actions on this wiper are prohibited."); case Mcp4461Component::MCP4461_WIPER_LOCKED: return LOG_STR("MCP4461 Wiper is locked using WiperLock-technology. All actions on this wiper are prohibited."); - case Mcp4461Component:::MCP4461_STATUS_OK: + case Mcp4461Component::MCP4461_STATUS_OK: return LOG_STR("Status OK"); default: return LOG_STR("Unknown"); @@ -165,7 +167,7 @@ void Mcp4461Component::loop() { uint8_t Mcp4461Component::get_status_register() { if (this->is_failed()) { ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(this->error_code_))); - return; + return 0; } uint8_t reg = 0; reg |= static_cast(Mcp4461Addresses::MCP4461_STATUS); @@ -225,11 +227,11 @@ uint16_t Mcp4461Component::get_wiper_level(Mcp4461WiperIdx wiper) { ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(this->error_code_))); return 0; } + uint8_t wiper_idx = static_cast(wiper); if (!(this->reg_[wiper_idx].enabled)) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_DISABLED))); - return; + return 0; } - uint8_t wiper_idx = static_cast(wiper); if (!(this->reg_[wiper_idx].enabled)) { ESP_LOGW(TAG, "reading from disabled volatile wiper %" PRIu8 ", returning 0", wiper_idx); return static_cast(0); @@ -259,13 +261,13 @@ uint16_t Mcp4461Component::read_wiper_level_(uint8_t wiper) { void Mcp4461Component::update_wiper_level(Mcp4461WiperIdx wiper) { if (this->is_failed()) { ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(this->error_code_))); - return 0; + return; } + uint8_t wiper_idx = static_cast(wiper); if (!(this->reg_[wiper_idx].enabled)) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_DISABLED))); return; } - uint8_t wiper_idx = static_cast(wiper); uint16_t data; data = this->get_wiper_level(wiper); ESP_LOGV(TAG, "Got value %" PRIu16 " from wiper %" PRIu8, data, wiper_idx); @@ -288,7 +290,7 @@ void Mcp4461Component::set_wiper_level(Mcp4461WiperIdx wiper, uint16_t value) { } if (this->reg_[wiper_idx].wiper_lock_active) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_LOCKED))); - return false; + return; } ESP_LOGV(TAG, "Setting MCP4461 wiper %" PRIu8 " to %" PRIu16 "!", wiper_idx, value); this->reg_[wiper_idx].state = value; @@ -313,15 +315,15 @@ void Mcp4461Component::enable_wiper(Mcp4461WiperIdx wiper) { ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(this->error_code_))); return; } + uint8_t wiper_idx = static_cast(wiper); if ((this->reg_[wiper_idx].enabled)) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_ENABLED))); return; } if (this->reg_[wiper_idx].wiper_lock_active) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_LOCKED))); - return false; + return; } - uint8_t wiper_idx = static_cast(wiper); ESP_LOGV(TAG, "Enabling wiper %" PRIu8, wiper_idx); this->reg_[wiper_idx].terminal_hw = true; this->update_ = true; @@ -332,15 +334,15 @@ void Mcp4461Component::disable_wiper(Mcp4461WiperIdx wiper) { ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(this->error_code_))); return; } + uint8_t wiper_idx = static_cast(wiper); if (!(this->reg_[wiper_idx].enabled)) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_DISABLED))); return; } if (this->reg_[wiper_idx].wiper_lock_active) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_LOCKED))); - return false; + return; } - uint8_t wiper_idx = static_cast(wiper); ESP_LOGV(TAG, "Disabling wiper %" PRIu8, wiper_idx); this->reg_[wiper_idx].terminal_hw = false; this->update_ = true; @@ -354,7 +356,7 @@ bool Mcp4461Component::increase_wiper(Mcp4461WiperIdx wiper) { uint8_t wiper_idx = static_cast(wiper); if (!(this->reg_[wiper_idx].enabled)) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_DISABLED))); - return; + return false; } if (this->reg_[wiper_idx].wiper_lock_active) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_LOCKED))); @@ -384,7 +386,7 @@ bool Mcp4461Component::decrease_wiper(Mcp4461WiperIdx wiper) { uint8_t wiper_idx = static_cast(wiper); if (!(this->reg_[wiper_idx].enabled)) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_DISABLED))); - return; + return false; } if (this->reg_[wiper_idx].wiper_lock_active) { ESP_LOGW(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string_(MCP4461_WIPER_LOCKED))); @@ -455,7 +457,7 @@ uint8_t Mcp4461Component::get_terminal_register(Mcp4461TerminalIdx terminal_conn void Mcp4461Component::update_terminal_register(Mcp4461TerminalIdx terminal_connector) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(LOG_PARENT_FAILED_STR)); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(MCP4461_PARENT_FAILED)); return; } if ((static_cast(terminal_connector) != 0 && static_cast(terminal_connector) != 1)) { diff --git a/esphome/components/mcp4461/mcp4461.h b/esphome/components/mcp4461/mcp4461.h index cb9ff0cfaa..e9ac6b66ee 100644 --- a/esphome/components/mcp4461/mcp4461.h +++ b/esphome/components/mcp4461/mcp4461.h @@ -53,16 +53,6 @@ enum class Mcp4461EepromLocation : uint8_t { enum class Mcp4461TerminalIdx : uint8_t { MCP4461_TERMINAL_0 = 0, MCP4461_TERMINAL_1 = 1 }; -enum ErrorCode { - MCP4461_STATUS_OK = 0, // CMD completed successfully - MCP4461_STATUS_I2C_ERROR, // Unable to communicate with device - MCP4461_STATUS_REGISTER_INVALID, // Status register value was invalid - MCP4461_VALUE_INVALID, // Invalid value given for wiper / eeprom - MCP4461_STATUS_WRITE_PROTECTED, // The value was read, but the CRC over the payload (valid and data) does not match - MCP4461_STATUS_WIPER_LOCKED, // The wiper is locked using WiperLock-technology - all actions for this wiper will be aborted/discarded -} error_code_{MCP4461_STATUS_OK}; - - class Mcp4461Wiper; // Mcp4461Component @@ -101,6 +91,19 @@ class Mcp4461Component : public Component, public i2c::I2CDevice { bool set_eeprom_value(Mcp4461EepromLocation location, uint16_t value); void set_initial_value(Mcp4461WiperIdx wiper, float initial_value); + enum ErrorCode { + MCP4461_STATUS_OK = 0, // CMD completed successfully + MCP4461_STATUS_I2C_ERROR, // Unable to communicate with device + MCP4461_STATUS_REGISTER_INVALID, // Status register value was invalid + MCP4461_STATUS_REGISTER_ERROR, // Error fetching status register + MCP4461_PARENT_FAILED, // Parent component failed + MCP4461_VALUE_INVALID, // Invalid value given for wiper / eeprom + MCP4461_WRITE_PROTECTED, // The value was read, but the CRC over the payload (valid and data) does not match + MCP4461_WIPER_ENABLED, // The wiper is enabled, discard additional enabling actions + MCP4461_WIPER_DISABLED, // The wiper is disabled - all actions for this wiper will be aborted/discarded + MCP4461_WIPER_LOCKED, // The wiper is locked using WiperLock-technology - all actions for this wiper will be aborted/discarded + } error_code_{MCP4461_STATUS_OK}; + protected: friend class Mcp4461Wiper; void set_write_protection_status_(); diff --git a/esphome/components/mcp4461/output/mcp4461_output.cpp b/esphome/components/mcp4461/output/mcp4461_output.cpp index aceca5307c..932cf1a5ac 100644 --- a/esphome/components/mcp4461/output/mcp4461_output.cpp +++ b/esphome/components/mcp4461/output/mcp4461_output.cpp @@ -11,7 +11,7 @@ static const char *const TAG = "mcp4461.output"; void Mcp4461Wiper::write_state(float state) { if (this->parent_->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(LOG_PARENT_FAILED_STR)); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(Mcp4461Component::Mcp4461Component::MCP4461_PARENT_FAILED)); return; } uint8_t wiper_idx = static_cast(this->wiper_); @@ -33,7 +33,7 @@ uint16_t Mcp4461Wiper::get_wiper_level() { return this->parent_->get_wiper_level void Mcp4461Wiper::save_level() { if (this->parent_->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(LOG_PARENT_FAILED_STR)); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(Mcp4461Component::MCP4461_PARENT_FAILED)); return; } uint8_t wiper_idx = static_cast(this->wiper_); @@ -49,7 +49,7 @@ void Mcp4461Wiper::save_level() { void Mcp4461Wiper::enable_wiper() { if (this->parent_->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(LOG_PARENT_FAILED_STR)); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(Mcp4461Component::MCP4461_PARENT_FAILED)); return; } uint8_t wiper_idx = static_cast(this->wiper_); @@ -62,7 +62,7 @@ void Mcp4461Wiper::enable_wiper() { void Mcp4461Wiper::disable_wiper() { if (this->parent_->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(LOG_PARENT_FAILED_STR)); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(Mcp4461Component::MCP4461_PARENT_FAILED)); return; } uint8_t wiper_idx = static_cast(this->wiper_); @@ -75,7 +75,7 @@ void Mcp4461Wiper::disable_wiper() { void Mcp4461Wiper::increase_wiper() { if (this->parent_->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(LOG_PARENT_FAILED_STR)); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(Mcp4461Component::MCP4461_PARENT_FAILED)); return; } uint8_t wiper_idx = static_cast(this->wiper_); @@ -90,7 +90,7 @@ void Mcp4461Wiper::increase_wiper() { void Mcp4461Wiper::decrease_wiper() { if (this->parent_->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(LOG_PARENT_FAILED_STR)); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(Mcp4461Component::MCP4461_PARENT_FAILED)); return; } uint8_t wiper_idx = static_cast(this->wiper_); @@ -105,7 +105,7 @@ void Mcp4461Wiper::decrease_wiper() { void Mcp4461Wiper::enable_terminal(char terminal) { if (this->parent_->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(LOG_PARENT_FAILED_STR)); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(Mcp4461Component::MCP4461_PARENT_FAILED)); return; } uint8_t wiper_idx = static_cast(this->wiper_); @@ -118,7 +118,7 @@ void Mcp4461Wiper::enable_terminal(char terminal) { void Mcp4461Wiper::disable_terminal(char terminal) { if (this->parent_->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(LOG_PARENT_FAILED_STR)); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(Mcp4461Component::MCP4461_PARENT_FAILED)); return; } uint8_t wiper_idx = static_cast(this->wiper_);