mirror of
https://github.com/esphome/esphome.git
synced 2025-03-13 22:28:14 +00:00
commit
f2e7bc7a41
@ -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,23 +46,23 @@ void Mcp4461Component::begin_() {
|
||||
// Converts a status to a human readable string
|
||||
static const LogString *mcp4461_get_message_string(int status) {
|
||||
switch (status) {
|
||||
case this->ErrorCode::MCP4461_STATUS_I2C_ERROR:
|
||||
case Mcp4461Component::MCP4461_STATUS_I2C_ERROR:
|
||||
return LOG_STR("I2C error - communication with MCP4461 failed!");
|
||||
case this->ErrorCode::MCP4461_STATUS_REGISTER_ERROR:
|
||||
case Mcp4461Component::MCP4461_STATUS_REGISTER_ERROR:
|
||||
return LOG_STR("Status register could not be read");
|
||||
case MCP4461_STATUS_REGISTER_INVALID:
|
||||
case Mcp4461Component::MCP4461_STATUS_REGISTER_INVALID:
|
||||
return LOG_STR("Invalid status register value - bits 1,7 or 8 are 0");
|
||||
case MCP4461_VALUE_INVALID:
|
||||
case Mcp4461Component::MCP4461_VALUE_INVALID:
|
||||
return LOG_STR("Invalid value for wiper given");
|
||||
case MCP4461_WRITE_PROTECTED:
|
||||
case Mcp4461Component::MCP4461_WRITE_PROTECTED:
|
||||
return LOG_STR("MCP4461 is write protected. Setting nonvolatile wipers/eeprom values is prohibited.");
|
||||
case MCP4461_WIPER_ENABLED:
|
||||
case Mcp4461Component::MCP4461_WIPER_ENABLED:
|
||||
return LOG_STR("MCP4461 Wiper is already enabled, ignoring cmd to enable.");
|
||||
case MCP4461_WIPER_DISABLED:
|
||||
case Mcp4461Component::MCP4461_WIPER_DISABLED:
|
||||
return LOG_STR("MCP4461 Wiper is disabled. All actions on this wiper are prohibited.");
|
||||
case MCP4461_WIPER_LOCKED:
|
||||
case Mcp4461Component::MCP4461_WIPER_LOCKED:
|
||||
return LOG_STR("MCP4461 Wiper is locked using WiperLock-technology. All actions on this wiper are prohibited.");
|
||||
case MCP4461_STATUS_OK:
|
||||
case Mcp4461Component::MCP4461_STATUS_OK:
|
||||
return LOG_STR("Status OK");
|
||||
default:
|
||||
return LOG_STR("Unknown");
|
||||
@ -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<uint8_t>(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<uint8_t>(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<uint8_t>(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<uint8_t>(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<uint8_t>(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<uint8_t>(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<uint8_t>(wiper);
|
||||
@ -379,7 +379,7 @@ bool Mcp4461Component::increase_wiper_(Mcp4461WiperIdx wiper) {
|
||||
reg |= static_cast<uint8_t>(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<uint8_t>(wiper);
|
||||
@ -413,7 +413,7 @@ bool Mcp4461Component::decrease_wiper_(Mcp4461WiperIdx wiper) {
|
||||
reg |= static_cast<uint8_t>(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<uint8_t>(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<uint8_t>(terminal_connector) != 0 && static_cast<uint8_t>(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<uint8_t>(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<uint8_t>(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<uint8_t>(Mcp4461EepromLocation::MCP4461_EEPROM_1) + (static_cast<uint8_t>(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;
|
||||
@ -616,7 +616,7 @@ bool Mcp4461Component::set_eeprom_value(Mcp4461EepromLocation location, uint16_t
|
||||
}
|
||||
|
||||
int Mcp4461Component::get_error_code() {
|
||||
return this->error_code_;
|
||||
return this->error_code;
|
||||
}
|
||||
|
||||
bool Mcp4461Component::is_writing_() {
|
||||
|
@ -100,8 +100,22 @@ class Mcp4461Component : public Component, public i2c::I2CDevice {
|
||||
/// @param[wiper] wiper for which terminal shall be initialized disabled
|
||||
/// @param[terminal] terminal to disable, one of { 'a', 'b', 'w', 'h' }
|
||||
void initialize_terminal_disabled(Mcp4461WiperIdx wiper, char terminal);
|
||||
/// @brief get error code
|
||||
int get_error_code();
|
||||
|
||||
/// @brief available/required status codes
|
||||
enum ErrorCode {
|
||||
MCP4461_STATUS_OK = 0, // CMD completed successfully
|
||||
MCP4461_FAILED, // component failed
|
||||
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_PROHIBITED_FOR_NONVOLATILE, //
|
||||
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;
|
||||
@ -172,22 +186,6 @@ class Mcp4461Component : public Component, public i2c::I2CDevice {
|
||||
/// @return bool - true if write successful, false if not
|
||||
bool set_terminal_register_(Mcp4461TerminalIdx terminal_connector, uint8_t data);
|
||||
|
||||
/// @brief available/required status codes
|
||||
enum ErrorCode {
|
||||
MCP4461_STATUS_OK = 0, // CMD completed successfully
|
||||
MCP4461_FAILED, // component failed
|
||||
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_PROHIBITED_FOR_NONVOLATILE, //
|
||||
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};
|
||||
|
||||
WiperState reg_[8];
|
||||
void begin_();
|
||||
bool last_eeprom_write_timed_out_{false};
|
||||
|
Loading…
x
Reference in New Issue
Block a user