diff --git a/esphome/components/mcp4461/mcp4461.cpp b/esphome/components/mcp4461/mcp4461.cpp index f96de2f0bc..4ead7216c7 100644 --- a/esphome/components/mcp4461/mcp4461.cpp +++ b/esphome/components/mcp4461/mcp4461.cpp @@ -13,7 +13,7 @@ void Mcp4461Component::setup() { ESP_LOGCONFIG(TAG, "Setting up mcp4461 using address (0x%02" PRIX8 ")...", this->address_); auto err = this->write(nullptr, 0); if (err != i2c::ERROR_OK) { - this->error_code = MCP4461_STATUS_I2C_ERROR; + this->error_code_ = MCP4461_STATUS_I2C_ERROR; this->mark_failed(); return; } @@ -46,9 +46,9 @@ void Mcp4461Component::begin_() { // Converts a status to a human readable string static const LogString *mcp4461_get_message_string(int status) { switch (status) { - case Mcp4461Component::MCP4461_STATUS_I2C_ERROR: + case Mcp4461Component->MCP4461_STATUS_I2C_ERROR: return LOG_STR("I2C error - communication with MCP4461 failed!"); - case Mcp4461Component::MCP4461_STATUS_REGISTER_ERROR: + case Mcp4461Component->MCP4461_STATUS_REGISTER_ERROR: 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"); @@ -100,7 +100,7 @@ void Mcp4461Component::dump_config() { ESP_LOGCONFIG(TAG, "mcp4461:"); LOG_I2C_DEVICE(this); if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); } // log wiper status for (uint8_t i = 0; i < 8; ++i) { @@ -163,7 +163,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))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return 0; } uint8_t reg = 0; @@ -171,7 +171,7 @@ uint8_t Mcp4461Component::get_status_register_() { reg |= static_cast(Mcp4461Commands::READ); uint16_t buf; if (!this->read_byte_16(reg, &buf)) { - this->error_code = MCP4461_STATUS_REGISTER_ERROR; + this->error_code_ = MCP4461_STATUS_REGISTER_ERROR; this->mark_failed(); return 0; } @@ -181,7 +181,7 @@ uint8_t Mcp4461Component::get_status_register_() { if (msb != 1 || ((lsb >> 7) & 0x01) != 1 || ((lsb >> 1) & 0x01) != 1) { // D8, D7 and R1 bits are hardlocked to 1 -> a status msb bit 0 (bit 9 of status register) of 0 or lsb bit 1/7 = 0 // indicate device/communication issues, therefore mark component failed - this->error_code = MCP4461_STATUS_REGISTER_INVALID; + this->error_code_ = MCP4461_STATUS_REGISTER_INVALID; this->mark_failed(); return 0; } @@ -221,7 +221,7 @@ uint8_t Mcp4461Component::get_wiper_address_(uint8_t wiper) { uint16_t Mcp4461Component::get_wiper_level_(Mcp4461WiperIdx wiper) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return 0; } uint8_t wiper_idx = static_cast(wiper); @@ -247,7 +247,7 @@ uint16_t Mcp4461Component::read_wiper_level_(uint8_t wiper) { } } if (!(this->read_byte_16(reg, &buf))) { - this->error_code = MCP4461_STATUS_I2C_ERROR; + this->error_code_ = MCP4461_STATUS_I2C_ERROR; this->status_set_warning(); ESP_LOGW(TAG, "Error fetching %swiper %" PRIu8 " value", (wiper > 3) ? "nonvolatile " : "", wiper); return 0; @@ -257,7 +257,7 @@ uint16_t Mcp4461Component::read_wiper_level_(uint8_t wiper) { bool Mcp4461Component::update_wiper_level_(Mcp4461WiperIdx wiper) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return false; } uint8_t wiper_idx = static_cast(wiper); @@ -274,7 +274,7 @@ bool Mcp4461Component::update_wiper_level_(Mcp4461WiperIdx wiper) { bool Mcp4461Component::set_wiper_level_(Mcp4461WiperIdx wiper, uint16_t value) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return false; } uint8_t wiper_idx = static_cast(wiper); @@ -302,7 +302,7 @@ void Mcp4461Component::write_wiper_level_(uint8_t wiper, uint16_t value) { nonvolatile = true; } if (!(this->mcp4461_write_(this->get_wiper_address_(wiper), value, nonvolatile))) { - this->error_code = MCP4461_STATUS_I2C_ERROR; + this->error_code_ = MCP4461_STATUS_I2C_ERROR; this->status_set_warning(); ESP_LOGW(TAG, "Error writing %swiper %" PRIu8 " level %" PRIu16 "", (wiper > 3) ? "nonvolatile " : "", wiper, value); @@ -311,7 +311,7 @@ void Mcp4461Component::write_wiper_level_(uint8_t wiper, uint16_t value) { void Mcp4461Component::enable_wiper_(Mcp4461WiperIdx wiper) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return; } uint8_t wiper_idx = static_cast(wiper); @@ -333,7 +333,7 @@ void Mcp4461Component::enable_wiper_(Mcp4461WiperIdx wiper) { void Mcp4461Component::disable_wiper_(Mcp4461WiperIdx wiper) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return; } uint8_t wiper_idx = static_cast(wiper); @@ -355,7 +355,7 @@ void Mcp4461Component::disable_wiper_(Mcp4461WiperIdx wiper) { bool Mcp4461Component::increase_wiper_(Mcp4461WiperIdx wiper) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return false; } uint8_t wiper_idx = static_cast(wiper); @@ -379,7 +379,7 @@ bool Mcp4461Component::increase_wiper_(Mcp4461WiperIdx wiper) { reg |= static_cast(Mcp4461Commands::INCREMENT); auto err = this->write(&this->address_, reg, sizeof(reg)); if (err != i2c::ERROR_OK) { - this->error_code = MCP4461_STATUS_I2C_ERROR; + this->error_code_ = MCP4461_STATUS_I2C_ERROR; this->status_set_warning(); return false; } @@ -389,7 +389,7 @@ bool Mcp4461Component::increase_wiper_(Mcp4461WiperIdx wiper) { bool Mcp4461Component::decrease_wiper_(Mcp4461WiperIdx wiper) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return false; } uint8_t wiper_idx = static_cast(wiper); @@ -413,7 +413,7 @@ bool Mcp4461Component::decrease_wiper_(Mcp4461WiperIdx wiper) { reg |= static_cast(Mcp4461Commands::DECREMENT); auto err = this->write(&this->address_, reg, sizeof(reg)); if (err != i2c::ERROR_OK) { - this->error_code = MCP4461_STATUS_I2C_ERROR; + this->error_code_ = MCP4461_STATUS_I2C_ERROR; this->status_set_warning(); return false; } @@ -447,7 +447,7 @@ uint8_t Mcp4461Component::calc_terminal_connector_byte_(Mcp4461TerminalIdx termi uint8_t Mcp4461Component::get_terminal_register_(Mcp4461TerminalIdx terminal_connector) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return 0; } uint8_t reg = 0; @@ -461,7 +461,7 @@ uint8_t Mcp4461Component::get_terminal_register_(Mcp4461TerminalIdx terminal_con if (this->read_byte_16(reg, &buf)) { return static_cast(buf & 0x00ff); } else { - this->error_code = MCP4461_STATUS_I2C_ERROR; + this->error_code_ = MCP4461_STATUS_I2C_ERROR; this->status_set_warning(); ESP_LOGW(TAG, "Error fetching terminal register value"); return 0; @@ -470,7 +470,7 @@ uint8_t Mcp4461Component::get_terminal_register_(Mcp4461TerminalIdx terminal_con void Mcp4461Component::update_terminal_register_(Mcp4461TerminalIdx terminal_connector) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return; } if ((static_cast(terminal_connector) != 0 && static_cast(terminal_connector) != 1)) { @@ -498,7 +498,7 @@ void Mcp4461Component::update_terminal_register_(Mcp4461TerminalIdx terminal_con bool Mcp4461Component::set_terminal_register_(Mcp4461TerminalIdx terminal_connector, uint8_t data) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return false; } uint8_t addr; @@ -511,7 +511,7 @@ bool Mcp4461Component::set_terminal_register_(Mcp4461TerminalIdx terminal_connec return false; } if (!(this->mcp4461_write_(addr, data))) { - this->error_code = MCP4461_STATUS_I2C_ERROR; + this->error_code_ = MCP4461_STATUS_I2C_ERROR; this->status_set_warning(); return false; } @@ -520,7 +520,7 @@ bool Mcp4461Component::set_terminal_register_(Mcp4461TerminalIdx terminal_connec void Mcp4461Component::enable_terminal_(Mcp4461WiperIdx wiper, char terminal) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return; } uint8_t wiper_idx = static_cast(wiper); @@ -547,7 +547,7 @@ void Mcp4461Component::enable_terminal_(Mcp4461WiperIdx wiper, char terminal) { void Mcp4461Component::disable_terminal_(Mcp4461WiperIdx wiper, char terminal) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return; } uint8_t wiper_idx = static_cast(wiper); @@ -574,7 +574,7 @@ void Mcp4461Component::disable_terminal_(Mcp4461WiperIdx wiper, char terminal) { uint16_t Mcp4461Component::get_eeprom_value(Mcp4461EepromLocation location) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return 0; } uint8_t reg = 0; @@ -585,7 +585,7 @@ uint16_t Mcp4461Component::get_eeprom_value(Mcp4461EepromLocation location) { return 0; } if (!this->read_byte_16(reg, &buf)) { - this->error_code = MCP4461_STATUS_I2C_ERROR; + this->error_code_ = MCP4461_STATUS_I2C_ERROR; this->status_set_warning(); ESP_LOGW(TAG, "Error fetching EEPRom location value"); return 0; @@ -595,7 +595,7 @@ uint16_t Mcp4461Component::get_eeprom_value(Mcp4461EepromLocation location) { bool Mcp4461Component::set_eeprom_value(Mcp4461EepromLocation location, uint16_t value) { if (this->is_failed()) { - ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code))); + ESP_LOGE(TAG, "%s", LOG_STR_ARG(mcp4461_get_message_string(this->error_code_))); return false; } uint8_t addr = 0; @@ -607,7 +607,7 @@ bool Mcp4461Component::set_eeprom_value(Mcp4461EepromLocation location, uint16_t } addr |= static_cast(Mcp4461EepromLocation::MCP4461_EEPROM_1) + (static_cast(location) * 0x10); if (!(this->mcp4461_write_(addr, value, true))) { - this->error_code = MCP4461_STATUS_I2C_ERROR; + this->error_code_ = MCP4461_STATUS_I2C_ERROR; this->status_set_warning(); ESP_LOGW(TAG, "Error writing EEPRom value"); return false; diff --git a/esphome/components/mcp4461/mcp4461.h b/esphome/components/mcp4461/mcp4461.h index 2742cd8340..9e54b16d0c 100644 --- a/esphome/components/mcp4461/mcp4461.h +++ b/esphome/components/mcp4461/mcp4461.h @@ -101,6 +101,80 @@ class Mcp4461Component : public Component, public i2c::I2CDevice { /// @param[terminal] terminal to disable, one of { 'a', 'b', 'w', 'h' } void initialize_terminal_disabled(Mcp4461WiperIdx wiper, char terminal); + protected: + friend class Mcp4461Wiper; + /// @brief update write protection status of device + void update_write_protection_status_(); + /// @brief fetch wiper address for given wiper + /// @param[in] wiper - Wiper to fetch address for, int in range 0-7 + /// @return wiper - Address from Mcp4461Addresses + uint8_t get_wiper_address_(uint8_t wiper); + /// @brief internal i2c function to read given wiper value + /// @return uint16_t - Value in range 0-256 representing current wiper state/level + uint16_t read_wiper_level_(uint8_t wiper); + /// @brief fetch device status register values + /// @return uint8_t - Status register value - see datasheet for bit values + uint8_t get_status_register_(); + /// @brief read current level/state of given wiper with validation checks + /// @return uint16_t - Value in range 0-256 representing current wiper state/level + uint16_t get_wiper_level_(Mcp4461WiperIdx wiper); + /// @brief set level/state of given wiper + /// @return bool - True on success, false on error/warning + bool set_wiper_level_(Mcp4461WiperIdx wiper, uint16_t value); + /// @brief update current level/state of given wiper + /// @return bool - True on success, false on error/warning + bool update_wiper_level_(Mcp4461WiperIdx wiper); + /// @brief enable given wiper + /// @param[in] wiper - The wiper to enable + void enable_wiper_(Mcp4461WiperIdx wiper); + /// @brief disable given wiper + /// @param[in] wiper - The wiper to disable + void disable_wiper_(Mcp4461WiperIdx wiper); + /// @brief increase given wiper + /// @param[in] wiper - Wiper to increase + /// @return bool - True on success, false on error/warning + bool increase_wiper_(Mcp4461WiperIdx wiper); + /// @brief increase given wiper + /// @param[in] wiper - Wiper to decrease + /// @return bool - True on success, false on error/warning + bool decrease_wiper_(Mcp4461WiperIdx wiper); + /// @brief enable terminal of wiper + /// @param[in] wiper - The desired wiper for which the terminal shall be enabled + /// @param[in] terminal - The terminal that is to be enabled for given wiper + void enable_terminal_(Mcp4461WiperIdx wiper, char terminal); + /// @brief disable terminal of wiper + /// @param[in] wiper - The desired wiper for which the terminal shall be disabled + /// @param[in] terminal - The terminal that is to be enabled for given wiper + void disable_terminal_(Mcp4461WiperIdx, char terminal); + /// @brief check if device is still busy writing to eeprom + /// @return bool - true if device is currently writing to eeprom + bool is_writing_(); + /// @brief wait until timeout if device is busy + /// @param[in] wait_if_not_ready - Set to true to wait until timeout again, if previous write timed out already + /// @return bool - true if device eeprom still busy, false if rdy for write to nonvolatile wiper/eeprom + bool is_eeprom_ready_for_writing_(bool wait_if_not_ready); + /// @brief set wiper level + /// @param[in] wiper - The wiper for which the new state shall be set + /// @param[in] value - The int value in range 0-256 the wiper shall be set to + void write_wiper_level_(uint8_t wiper, uint16_t value); + /// @brief internal i2c write function + /// @return bool - True write successful, false if not + bool mcp4461_write_(uint8_t addr, uint16_t data, bool nonvolatile = false); + /// @brief calculate correct terminal register values + /// @return uint8_t - Calculated terminal register value for current internal terminal states + uint8_t calc_terminal_connector_byte_(Mcp4461TerminalIdx terminal_connector); + /// @brief internal function to update terminal registers + void update_terminal_register_(Mcp4461TerminalIdx terminal_connector); + /// @brief internal function to get terminal register values + /// @return uint8_t - Get terminal register value of specified terminal + uint8_t get_terminal_register_(Mcp4461TerminalIdx terminal_connector); + /// @brief internal function to set terminal registers + /// @return bool - True if write successful, false if not + bool set_terminal_register_(Mcp4461TerminalIdx terminal_connector, uint8_t data); + /// @brief get error code + /// @param[in] status - The int status code + int get_status_code(int status); + /// @brief available/required status codes enum ErrorCode { MCP4461_STATUS_OK = 0, // CMD completed successfully @@ -115,76 +189,7 @@ class Mcp4461Component : public Component, public i2c::I2CDevice { 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; - /// @brief update write protection status of device - void update_write_protection_status_(); - /// @brief fetch wiper address for given wiper - /// @param[wiper] wiper to fetch address for, int in range 0-7 - /// @return wiper address from Mcp4461Addresses - uint8_t get_wiper_address_(uint8_t wiper); - /// @brief internal i2c function to read given wiper value - /// @return uint16_t value in range 0-256 representing current wiper state/level - uint16_t read_wiper_level_(uint8_t wiper); - /// @brief fetch device status register values - /// @return uint8_t status register value - see datasheet for bit values - uint8_t get_status_register_(); - /// @brief read current level/state of given wiper with validation checks - /// @return uint16_t value in range 0-256 representing current wiper state/level - uint16_t get_wiper_level_(Mcp4461WiperIdx wiper); - /// @brief set level/state of given wiper - /// @return bool - true on success, false on error/warning - bool set_wiper_level_(Mcp4461WiperIdx wiper, uint16_t value); - /// @brief update current level/state of given wiper - /// @return bool - true on success, false on error/warning - bool update_wiper_level_(Mcp4461WiperIdx wiper); - /// @brief enable given wiper - /// @param[in] wiper The wiper to enable - void enable_wiper_(Mcp4461WiperIdx wiper); - /// @brief disable given wiper - /// @param[in] wiper The wiper to disable - void disable_wiper_(Mcp4461WiperIdx wiper); - /// @brief increase given wiper - /// @param[wiper] wiper to increase - /// @return bool - true on success, false on error/warning - bool increase_wiper_(Mcp4461WiperIdx wiper); - /// @brief increase given wiper - /// @param[wiper] wiper to increase - /// @return bool - true on success, false on error/warning - bool decrease_wiper_(Mcp4461WiperIdx wiper); - /// @brief enable terminal of wiper - /// @param[wiper] desired wiper for which the terminal shall be enabled - void enable_terminal_(Mcp4461WiperIdx wiper, char terminal); - /// @brief disable terminal of wiper - /// @param[wiper] desired wiper for which the terminal shall be disabled - void disable_terminal_(Mcp4461WiperIdx, char terminal); - /// @brief check if device is still busy writing to eeprom - /// @return bool - true if device is currently writing to eeprom - bool is_writing_(); - /// @brief wait until timeout if device is busy - /// @param[wait_if_not_ready] set to true to wait until timeout again, if previous write timed out already - /// @return bool - true if device eeprom still busy, false if rdy for write to nonvolatile wiper/eeprom - bool is_eeprom_ready_for_writing_(bool wait_if_not_ready); - /// @brief set wiper level - /// @param[wiper] wiper for which the new state shall be set - /// @param[value] the int value in range 0-256 the wiper shall be set to - void write_wiper_level_(uint8_t wiper, uint16_t value); - /// @brief internal i2c write function - /// @return bool - true write successful, false if not - bool mcp4461_write_(uint8_t addr, uint16_t data, bool nonvolatile = false); - /// @brief calculate correct terminal register values - /// @return uint8_t - calculated terminal register value for current internal terminal states - uint8_t calc_terminal_connector_byte_(Mcp4461TerminalIdx terminal_connector); - /// @brief internal function to update terminal registers - void update_terminal_register_(Mcp4461TerminalIdx terminal_connector); - /// @brief internal function to get terminal register values - /// /// @return uint8_t - get terminal register value of specified terminal - uint8_t get_terminal_register_(Mcp4461TerminalIdx terminal_connector); - /// @brief internal function to set terminal registers - /// @return bool - true if write successful, false if not - bool set_terminal_register_(Mcp4461TerminalIdx terminal_connector, uint8_t data); + } error_code_{MCP4461_STATUS_OK}; WiperState reg_[8]; void begin_();