1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 00:31:58 +00:00

[ch422g][lc709203f][qmc5883l] Avoid heap allocation in status_set_warning calls (#13152)

This commit is contained in:
J. Nick Koston
2026-01-11 17:18:37 -10:00
committed by GitHub
parent ace3ff2170
commit 52132ea3bc
3 changed files with 24 additions and 10 deletions

View File

@@ -93,7 +93,9 @@ bool CH422GComponent::read_inputs_() {
bool CH422GComponent::write_reg_(uint8_t reg, uint8_t value) {
auto err = this->bus_->write_readv(reg, &value, 1, nullptr, 0);
if (err != i2c::ERROR_OK) {
this->status_set_warning(str_sprintf("write failed for register 0x%X, error %d", reg, err).c_str());
char buf[64];
snprintf(buf, sizeof(buf), "write failed for register 0x%X, error %d", reg, err);
this->status_set_warning(buf);
return false;
}
this->status_clear_warning();
@@ -104,7 +106,9 @@ uint8_t CH422GComponent::read_reg_(uint8_t reg) {
uint8_t value;
auto err = this->bus_->write_readv(reg, nullptr, 0, &value, 1);
if (err != i2c::ERROR_OK) {
this->status_set_warning(str_sprintf("read failed for register 0x%X, error %d", reg, err).c_str());
char buf[64];
snprintf(buf, sizeof(buf), "read failed for register 0x%X, error %d", reg, err);
this->status_set_warning(buf);
return 0;
}
this->status_clear_warning();

View File

@@ -183,11 +183,14 @@ uint8_t Lc709203f::get_register_(uint8_t register_to_read, uint16_t *register_va
return_code = this->read_register(register_to_read, &read_buffer[3], 3);
if (return_code != i2c::NO_ERROR) {
// Error on the i2c bus
this->status_set_warning(
str_sprintf("Error code %d when reading from register 0x%02X", return_code, register_to_read).c_str());
char buf[64];
snprintf(buf, sizeof(buf), "Error code %d when reading from register 0x%02X", return_code, register_to_read);
this->status_set_warning(buf);
} else if (crc8(read_buffer, 5, 0x00, 0x07, true) != read_buffer[5]) {
// I2C indicated OK, but the CRC of the data does not matcth.
this->status_set_warning(str_sprintf("CRC error reading from register 0x%02X", register_to_read).c_str());
char buf[64];
snprintf(buf, sizeof(buf), "CRC error reading from register 0x%02X", register_to_read);
this->status_set_warning(buf);
} else {
*register_value = ((uint16_t) read_buffer[4] << 8) | (uint16_t) read_buffer[3];
return i2c::NO_ERROR;
@@ -225,8 +228,9 @@ uint8_t Lc709203f::set_register_(uint8_t register_to_set, uint16_t value_to_set)
if (return_code == i2c::NO_ERROR) {
return return_code;
} else {
this->status_set_warning(
str_sprintf("Error code %d when writing to register 0x%02X", return_code, register_to_set).c_str());
char buf[64];
snprintf(buf, sizeof(buf), "Error code %d when writing to register 0x%02X", return_code, register_to_set);
this->status_set_warning(buf);
}
}

View File

@@ -105,7 +105,9 @@ void QMC5883LComponent::update() {
if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG) {
err = this->read_register(QMC5883L_REGISTER_STATUS, &status, 1);
if (err != i2c::ERROR_OK) {
this->status_set_warning(str_sprintf("status read failed (%d)", err).c_str());
char buf[32];
snprintf(buf, sizeof(buf), "status read failed (%d)", err);
this->status_set_warning(buf);
return;
}
}
@@ -127,7 +129,9 @@ void QMC5883LComponent::update() {
}
err = this->read_bytes_16_le_(start, &raw[dest], 3 - dest);
if (err != i2c::ERROR_OK) {
this->status_set_warning(str_sprintf("mag read failed (%d)", err).c_str());
char buf[32];
snprintf(buf, sizeof(buf), "mag read failed (%d)", err);
this->status_set_warning(buf);
return;
}
@@ -155,7 +159,9 @@ void QMC5883LComponent::update() {
uint16_t raw_temp;
err = this->read_bytes_16_le_(QMC5883L_REGISTER_TEMPERATURE_LSB, &raw_temp);
if (err != i2c::ERROR_OK) {
this->status_set_warning(str_sprintf("temp read failed (%d)", err).c_str());
char buf[32];
snprintf(buf, sizeof(buf), "temp read failed (%d)", err);
this->status_set_warning(buf);
return;
}
temp = int16_t(raw_temp) * 0.01f;