From 22a36de4aed7ef86840ed0a9e707a4d5c01f0308 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 18 Oct 2026 03:20:58 -1000 Subject: [PATCH] [components] Decode little-endian I2C values with the new helpers (#20939) --- esphome/components/apds9930/apds9930.cpp | 7 +++--- esphome/components/as7341/as7341.cpp | 10 ++++---- esphome/components/as7341/as7341.h | 1 - esphome/components/as734x/as734x.cpp | 4 ++-- esphome/components/bh1745/bh1745.cpp | 13 +++++----- .../components/bme280_base/bme280_base.cpp | 4 ++-- esphome/components/bme280_base/bme280_base.h | 2 +- esphome/components/bme280_i2c/bme280_i2c.cpp | 4 ++-- esphome/components/bme280_i2c/bme280_i2c.h | 2 +- esphome/components/bme280_spi/bme280_spi.cpp | 7 +++--- esphome/components/bme280_spi/bme280_spi.h | 2 +- esphome/components/bmi160/bmi160.cpp | 17 ++----------- esphome/components/bmi160/bmi160.h | 3 --- esphome/components/bmi270/bmm150_aux.h | 2 +- .../components/bmp280_base/bmp280_base.cpp | 4 ++-- esphome/components/bmp280_base/bmp280_base.h | 2 +- esphome/components/bmp280_i2c/bmp280_i2c.h | 2 +- esphome/components/bmp280_spi/bmp280_spi.cpp | 7 +++--- esphome/components/bmp280_spi/bmp280_spi.h | 2 +- esphome/components/bq27220/bq27220.cpp | 11 +-------- esphome/components/bq27220/bq27220.h | 3 +-- esphome/components/bq76972/bq76972.cpp | 5 ++-- .../touchscreen/cst9220_touchscreen.cpp | 8 +++---- esphome/components/fusb302b/fusb302b.cpp | 8 +++---- .../gt911/touchscreen/gt911_touchscreen.cpp | 4 ++-- esphome/components/hdc2010/hdc2010.cpp | 5 ++-- esphome/components/hdc2080/hdc2080.cpp | 5 ++-- esphome/components/icm20689/icm20689.cpp | 14 +++++------ esphome/components/lis2dw12/lis2dw12.cpp | 6 ++--- esphome/components/lis3dh/lis3dh.cpp | 16 ++++++------- esphome/components/lps22/lps22.cpp | 3 ++- esphome/components/lsm6ds/lsm6ds.cpp | 18 +++++++------- esphome/components/mlx90614/mlx90614.cpp | 2 +- esphome/components/mpr121/mpr121.cpp | 7 +----- esphome/components/qmi8658/qmi8658.cpp | 14 +++++------ esphome/components/qmp6988/qmp6988.cpp | 20 ++++++++-------- .../spd2010/spd2010_touchscreen.cpp | 6 ++--- esphome/components/stts22h/stts22h.cpp | 3 ++- esphome/components/tca9555/tca9555.cpp | 24 +++++++------------ esphome/components/tcs34725/tcs34725.cpp | 12 +++++----- esphome/components/tfluna/tfluna.cpp | 8 +++---- esphome/components/tsl2561/tsl2561.cpp | 8 +------ esphome/components/veml3235/veml3235.cpp | 6 ++--- esphome/components/veml3235/veml3235.h | 10 +++----- esphome/components/xdb401/xdb401.cpp | 2 +- 45 files changed, 136 insertions(+), 187 deletions(-) diff --git a/esphome/components/apds9930/apds9930.cpp b/esphome/components/apds9930/apds9930.cpp index a4b836beaa1..eec741d5938 100644 --- a/esphome/components/apds9930/apds9930.cpp +++ b/esphome/components/apds9930/apds9930.cpp @@ -1,5 +1,6 @@ #include "apds9930.h" #include "esphome/core/hal.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" #include @@ -111,8 +112,8 @@ void APDS9930Component::update() { const uint8_t status = raw[0]; if (this->illuminance_sensor_ != nullptr && (status & APDS9930_AVALID) != 0) { - const uint16_t ch0 = encode_uint16(raw[2], raw[1]); - const uint16_t ch1 = encode_uint16(raw[4], raw[3]); + const uint16_t ch0 = encode_value_le(&raw[1]); + const uint16_t ch1 = encode_value_le(&raw[3]); if (ch0 >= ALS_SATURATION || ch1 >= ALS_SATURATION) { // A saturated channel would give a lux value that is too low, not too high ESP_LOGW(TAG, "Light channel saturated, lower ambient_light_gain"); @@ -125,7 +126,7 @@ void APDS9930Component::update() { } if (this->proximity_sensor_ != nullptr && (status & APDS9930_PVALID) != 0) { - this->proximity_sensor_->publish_state(encode_uint16(raw[6], raw[5])); + this->proximity_sensor_->publish_state(encode_value_le(&raw[5])); } } diff --git a/esphome/components/as7341/as7341.cpp b/esphome/components/as7341/as7341.cpp index cf02884c5e4..a40b8b507ce 100644 --- a/esphome/components/as7341/as7341.cpp +++ b/esphome/components/as7341/as7341.cpp @@ -95,16 +95,16 @@ uint8_t AS7341Component::get_atime() { } uint16_t AS7341Component::get_astep() { - uint16_t data; - this->read_byte_16(AS7341_ASTEP, &data); - return this->swap_bytes(data); + uint16_t data = 0; + this->read_byte_16_le(AS7341_ASTEP, &data); + return data; } bool AS7341Component::setup_gain(AS7341Gain gain) { return this->write_byte(AS7341_CFG1, gain); } bool AS7341Component::setup_atime(uint8_t atime) { return this->write_byte(AS7341_ATIME, atime); } -bool AS7341Component::setup_astep(uint16_t astep) { return this->write_byte_16(AS7341_ASTEP, swap_bytes(astep)); } +bool AS7341Component::setup_astep(uint16_t astep) { return this->write_byte_16_le(AS7341_ASTEP, astep); } bool AS7341Component::read_channels(uint16_t *data) { this->set_smux_low_channels(true); @@ -253,6 +253,4 @@ bool AS7341Component::clear_register_bit(uint8_t address, uint8_t bit_position) return this->write_byte(address, data); } -uint16_t AS7341Component::swap_bytes(uint16_t data) { return (data >> 8) | (data << 8); } - } // namespace esphome::as7341 diff --git a/esphome/components/as7341/as7341.h b/esphome/components/as7341/as7341.h index 2d72987f1cd..0ecc5dca419 100644 --- a/esphome/components/as7341/as7341.h +++ b/esphome/components/as7341/as7341.h @@ -118,7 +118,6 @@ class AS7341Component final : public PollingComponent, public i2c::I2CDevice { bool write_register_bit(uint8_t address, bool value, uint8_t bit_position); bool set_register_bit(uint8_t address, uint8_t bit_position); bool clear_register_bit(uint8_t address, uint8_t bit_position); - uint16_t swap_bytes(uint16_t data); protected: sensor::Sensor *f1_{nullptr}; diff --git a/esphome/components/as734x/as734x.cpp b/esphome/components/as734x/as734x.cpp index 30412be70f5..5be4f4808ac 100644 --- a/esphome/components/as734x/as734x.cpp +++ b/esphome/components/as734x/as734x.cpp @@ -277,7 +277,7 @@ bool AS734XComponent::update_bits_(uint8_t reg, uint8_t mask, bool value) { bool AS734XComponent::write_exposure_() { const ChipInfo *chip = this->chip_; if (!this->select_bank_(REG_ATIME) || !this->write_byte(REG_ATIME, this->atime_) || - !this->write_byte_16(pgm(chip->astep_reg), byteswap(this->astep_)) || + !this->write_byte_16_le(pgm(chip->astep_reg), this->astep_) || !this->write_byte(pgm(chip->cfg1_reg), this->gain_)) { return false; } @@ -459,7 +459,7 @@ void AS734XComponent::read_step_() { if (frame[0] & ASTATUS_ASAT) { ESP_LOGV(TAG, "Step %u saturated", this->step_); } - auto slot_value = [&frame](uint8_t slot) { return encode_uint16(frame[2 + 2 * slot], frame[1 + 2 * slot]); }; + auto slot_value = [&frame](uint8_t slot) { return encode_value_le(&frame[1 + 2 * slot]); }; const uint8_t *readout = pgm(this->chip_->readout) + this->step_ * NUM_BANDS; const uint8_t extra_clear = pgm(this->chip_->extra_clear_slot); for (uint8_t band = 0; band < NUM_BANDS; band++) { diff --git a/esphome/components/bh1745/bh1745.cpp b/esphome/components/bh1745/bh1745.cpp index ee7b9935f4c..c34c2493310 100644 --- a/esphome/components/bh1745/bh1745.cpp +++ b/esphome/components/bh1745/bh1745.cpp @@ -133,18 +133,17 @@ void BH1745Component::update() { if ((mode_control2 & MODE_CONTROL2_VALID) == 0) return; - uint8_t buffer[BH1745_CHANNELS * 2]; - if (!this->read_bytes(REG_RED_DATA_LSB, buffer, sizeof(buffer))) { + uint16_t buffer[BH1745_CHANNELS]; + if (!this->read_bytes_16_le(REG_RED_DATA_LSB, buffer, BH1745_CHANNELS)) { this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL)); return; } this->status_clear_warning(); - // Each channel is sent low byte first. - const uint16_t red = encode_uint16(buffer[1], buffer[0]); - const uint16_t green = encode_uint16(buffer[3], buffer[2]); - const uint16_t blue = encode_uint16(buffer[5], buffer[4]); - const uint16_t clear = encode_uint16(buffer[7], buffer[6]); + const uint16_t red = buffer[0]; + const uint16_t green = buffer[1]; + const uint16_t blue = buffer[2]; + const uint16_t clear = buffer[3]; ESP_LOGV(TAG, "Red: %u, Green: %u, Blue: %u, Clear: %u", red, green, blue, clear); if (this->red_counts_sensor_ != nullptr) diff --git a/esphome/components/bme280_base/bme280_base.cpp b/esphome/components/bme280_base/bme280_base.cpp index cf61160f77e..f922b1942b4 100644 --- a/esphome/components/bme280_base/bme280_base.cpp +++ b/esphome/components/bme280_base/bme280_base.cpp @@ -335,8 +335,8 @@ uint8_t BME280Component::read_u8_(uint8_t a_register) { } uint16_t BME280Component::read_u16_le_(uint8_t a_register) { uint16_t data = 0; - this->read_byte_16(a_register, &data); - return (data >> 8) | (data << 8); + this->read_byte_16_le(a_register, &data); + return data; } int16_t BME280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); } diff --git a/esphome/components/bme280_base/bme280_base.h b/esphome/components/bme280_base/bme280_base.h index d1a441ccebb..5ddb3cfd456 100644 --- a/esphome/components/bme280_base/bme280_base.h +++ b/esphome/components/bme280_base/bme280_base.h @@ -91,7 +91,7 @@ class BME280Component : public PollingComponent { virtual bool read_byte(uint8_t a_register, uint8_t *data) = 0; virtual bool write_byte(uint8_t a_register, uint8_t data) = 0; virtual bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) = 0; - virtual bool read_byte_16(uint8_t a_register, uint16_t *data) = 0; + virtual bool read_byte_16_le(uint8_t a_register, uint16_t *data) = 0; BME280CalibrationData calibration_; BME280Oversampling temperature_oversampling_{BME280_OVERSAMPLING_16X}; diff --git a/esphome/components/bme280_i2c/bme280_i2c.cpp b/esphome/components/bme280_i2c/bme280_i2c.cpp index f4380b0d343..40f2384852b 100644 --- a/esphome/components/bme280_i2c/bme280_i2c.cpp +++ b/esphome/components/bme280_i2c/bme280_i2c.cpp @@ -16,8 +16,8 @@ bool BME280I2CComponent::write_byte(uint8_t a_register, uint8_t data) { bool BME280I2CComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t len) { return I2CDevice::read_bytes(a_register, data, len); }; -bool BME280I2CComponent::read_byte_16(uint8_t a_register, uint16_t *data) { - return I2CDevice::read_byte_16(a_register, data); +bool BME280I2CComponent::read_byte_16_le(uint8_t a_register, uint16_t *data) { + return I2CDevice::read_byte_16_le(a_register, data); }; void BME280I2CComponent::dump_config() { diff --git a/esphome/components/bme280_i2c/bme280_i2c.h b/esphome/components/bme280_i2c/bme280_i2c.h index 45c3e5b260f..a4817faece0 100644 --- a/esphome/components/bme280_i2c/bme280_i2c.h +++ b/esphome/components/bme280_i2c/bme280_i2c.h @@ -12,7 +12,7 @@ class BME280I2CComponent final : public esphome::bme280_base::BME280Component, p bool read_byte(uint8_t a_register, uint8_t *data) override; bool write_byte(uint8_t a_register, uint8_t data) override; bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override; - bool read_byte_16(uint8_t a_register, uint16_t *data) override; + bool read_byte_16_le(uint8_t a_register, uint16_t *data) override; void dump_config() override; }; diff --git a/esphome/components/bme280_spi/bme280_spi.cpp b/esphome/components/bme280_spi/bme280_spi.cpp index a4e7b7d95c3..b9473fb6985 100644 --- a/esphome/components/bme280_spi/bme280_spi.cpp +++ b/esphome/components/bme280_spi/bme280_spi.cpp @@ -3,6 +3,7 @@ #include "bme280_spi.h" #include +#include "esphome/core/helpers.h" namespace esphome::bme280_spi { @@ -51,11 +52,11 @@ bool BME280SPIComponent::read_bytes(uint8_t a_register, uint8_t *data, size_t le return true; } -bool BME280SPIComponent::read_byte_16(uint8_t a_register, uint16_t *data) { +bool BME280SPIComponent::read_byte_16_le(uint8_t a_register, uint16_t *data) { this->enable(); this->transfer_byte(set_bit(a_register, 7)); - ((uint8_t *) data)[1] = this->transfer_byte(0); - ((uint8_t *) data)[0] = this->transfer_byte(0); + const uint8_t lsb = this->transfer_byte(0); + *data = encode_uint16(this->transfer_byte(0), lsb); this->disable(); return true; } diff --git a/esphome/components/bme280_spi/bme280_spi.h b/esphome/components/bme280_spi/bme280_spi.h index 3879151ea13..98f39f44b38 100644 --- a/esphome/components/bme280_spi/bme280_spi.h +++ b/esphome/components/bme280_spi/bme280_spi.h @@ -12,7 +12,7 @@ class BME280SPIComponent final : public esphome::bme280_base::BME280Component, bool read_byte(uint8_t a_register, uint8_t *data) override; bool write_byte(uint8_t a_register, uint8_t data) override; bool read_bytes(uint8_t a_register, uint8_t *data, size_t len) override; - bool read_byte_16(uint8_t a_register, uint16_t *data) override; + bool read_byte_16_le(uint8_t a_register, uint16_t *data) override; }; } // namespace esphome::bme280_spi diff --git a/esphome/components/bmi160/bmi160.cpp b/esphome/components/bmi160/bmi160.cpp index b97baae06ae..2a522c7a8ce 100644 --- a/esphome/components/bmi160/bmi160.cpp +++ b/esphome/components/bmi160/bmi160.cpp @@ -196,19 +196,6 @@ void BMI160Component::dump_config() { LOG_SENSOR(" ", "Temperature", this->temperature_sensor_); } -i2c::ErrorCode BMI160Component::read_le_int16_(uint8_t reg, int16_t *value, uint8_t len) { - uint8_t raw_data[len * 2]; - // read using read_register because we have little-endian data, and read_bytes_16 will swap it - i2c::ErrorCode err = this->read_register(reg, raw_data, len * 2); - if (err != i2c::ERROR_OK) { - return err; - } - for (int i = 0; i < len; i++) { - value[i] = (int16_t) ((uint16_t) raw_data[i * 2] | ((uint16_t) raw_data[i * 2 + 1] << 8)); - } - return err; -} - void BMI160Component::update() { if (!this->setup_complete_) { return; @@ -216,7 +203,7 @@ void BMI160Component::update() { ESP_LOGV(TAG, " Updating BMI160"); int16_t data[6]; - if (this->read_le_int16_(BMI160_REGISTER_DATA_GYRO_X_LSB, data, 6) != i2c::ERROR_OK) { + if (!this->read_bytes_16_le(BMI160_REGISTER_DATA_GYRO_X_LSB, data, 6)) { this->status_set_warning(); return; } @@ -229,7 +216,7 @@ void BMI160Component::update() { float accel_z = (float) data[5] / (float) INT16_MAX * 16 * GRAVITY_EARTH; int16_t raw_temperature; - if (this->read_le_int16_(BMI160_REGISTER_DATA_TEMP_LSB, &raw_temperature, 1) != i2c::ERROR_OK) { + if (!this->read_bytes_16_le(BMI160_REGISTER_DATA_TEMP_LSB, &raw_temperature, 1)) { this->status_set_warning(); return; } diff --git a/esphome/components/bmi160/bmi160.h b/esphome/components/bmi160/bmi160.h index 53704965cd9..eb0b553cc24 100644 --- a/esphome/components/bmi160/bmi160.h +++ b/esphome/components/bmi160/bmi160.h @@ -34,9 +34,6 @@ class BMI160Component final : public PollingComponent, public i2c::I2CDevice { /// Run one setup stage; false on an I2C failure. bool setup_stage_(int stage); bool setup_complete_{false}; - - /** reads `len` 16-bit little-endian integers from the given i2c register */ - i2c::ErrorCode read_le_int16_(uint8_t reg, int16_t *value, uint8_t len); }; } // namespace esphome::bmi160 diff --git a/esphome/components/bmi270/bmm150_aux.h b/esphome/components/bmi270/bmm150_aux.h index 747a0898fa8..3e1c288b77a 100644 --- a/esphome/components/bmi270/bmm150_aux.h +++ b/esphome/components/bmi270/bmm150_aux.h @@ -61,7 +61,7 @@ struct BMM150Trim { // A little-endian 16-bit trim word, addressed by its register inline uint16_t bmm150_trim_word(const uint8_t *regs, uint8_t reg) { const uint8_t offset = reg - BMM150_REG_TRIM_START; - return encode_uint16(regs[offset + 1], regs[offset]); + return encode_value_le(®s[offset]); } // Decodes the trim registers; `regs` holds the bytes of registers 0x5D onwards (at least 21). diff --git a/esphome/components/bmp280_base/bmp280_base.cpp b/esphome/components/bmp280_base/bmp280_base.cpp index d82f80ccd43..10be0585210 100644 --- a/esphome/components/bmp280_base/bmp280_base.cpp +++ b/esphome/components/bmp280_base/bmp280_base.cpp @@ -247,8 +247,8 @@ uint8_t BMP280Component::read_u8_(uint8_t a_register) { } uint16_t BMP280Component::read_u16_le_(uint8_t a_register) { uint16_t data = 0; - this->bmp_read_byte_16(a_register, &data); - return (data >> 8) | (data << 8); + this->bmp_read_byte_16_le(a_register, &data); + return data; } int16_t BMP280Component::read_s16_le_(uint8_t a_register) { return this->read_u16_le_(a_register); } diff --git a/esphome/components/bmp280_base/bmp280_base.h b/esphome/components/bmp280_base/bmp280_base.h index 1c3fc9cf6cf..cee8f43c7c2 100644 --- a/esphome/components/bmp280_base/bmp280_base.h +++ b/esphome/components/bmp280_base/bmp280_base.h @@ -69,7 +69,7 @@ class BMP280Component : public PollingComponent { virtual bool bmp_read_byte(uint8_t a_register, uint8_t *data) = 0; virtual bool bmp_write_byte(uint8_t a_register, uint8_t data) = 0; virtual bool bmp_read_bytes(uint8_t a_register, uint8_t *data, size_t len) = 0; - virtual bool bmp_read_byte_16(uint8_t a_register, uint16_t *data) = 0; + virtual bool bmp_read_byte_16_le(uint8_t a_register, uint16_t *data) = 0; /// Read the temperature value and store the calculated ambient temperature in t_fine. float read_temperature_(int32_t *t_fine); diff --git a/esphome/components/bmp280_i2c/bmp280_i2c.h b/esphome/components/bmp280_i2c/bmp280_i2c.h index f2ec93176ef..441d1a4565f 100644 --- a/esphome/components/bmp280_i2c/bmp280_i2c.h +++ b/esphome/components/bmp280_i2c/bmp280_i2c.h @@ -16,7 +16,7 @@ class BMP280I2CComponent final : public esphome::bmp280_base::BMP280Component, p bool bmp_read_bytes(uint8_t a_register, uint8_t *data, size_t len) override { return read_bytes(a_register, data, len); } - bool bmp_read_byte_16(uint8_t a_register, uint16_t *data) override { return read_byte_16(a_register, data); } + bool bmp_read_byte_16_le(uint8_t a_register, uint16_t *data) override { return read_byte_16_le(a_register, data); } void dump_config() override; }; diff --git a/esphome/components/bmp280_spi/bmp280_spi.cpp b/esphome/components/bmp280_spi/bmp280_spi.cpp index 04f92f9b899..5008d191392 100644 --- a/esphome/components/bmp280_spi/bmp280_spi.cpp +++ b/esphome/components/bmp280_spi/bmp280_spi.cpp @@ -3,6 +3,7 @@ #include "bmp280_spi.h" #include +#include "esphome/core/helpers.h" namespace esphome::bmp280_spi { @@ -51,11 +52,11 @@ bool BMP280SPIComponent::bmp_read_bytes(uint8_t a_register, uint8_t *data, size_ return true; } -bool BMP280SPIComponent::bmp_read_byte_16(uint8_t a_register, uint16_t *data) { +bool BMP280SPIComponent::bmp_read_byte_16_le(uint8_t a_register, uint16_t *data) { this->enable(); this->transfer_byte(set_bit(a_register, 7)); - ((uint8_t *) data)[1] = this->transfer_byte(0); - ((uint8_t *) data)[0] = this->transfer_byte(0); + const uint8_t lsb = this->transfer_byte(0); + *data = encode_uint16(this->transfer_byte(0), lsb); this->disable(); return true; } diff --git a/esphome/components/bmp280_spi/bmp280_spi.h b/esphome/components/bmp280_spi/bmp280_spi.h index 449167811d3..fafc722c6cc 100644 --- a/esphome/components/bmp280_spi/bmp280_spi.h +++ b/esphome/components/bmp280_spi/bmp280_spi.h @@ -12,7 +12,7 @@ class BMP280SPIComponent final : public esphome::bmp280_base::BMP280Component, bool bmp_read_byte(uint8_t a_register, uint8_t *data) override; bool bmp_write_byte(uint8_t a_register, uint8_t data) override; bool bmp_read_bytes(uint8_t a_register, uint8_t *data, size_t len) override; - bool bmp_read_byte_16(uint8_t a_register, uint16_t *data) override; + bool bmp_read_byte_16_le(uint8_t a_register, uint16_t *data) override; }; } // namespace esphome::bmp280_spi diff --git a/esphome/components/bq27220/bq27220.cpp b/esphome/components/bq27220/bq27220.cpp index b7912b930e6..3c78893437d 100644 --- a/esphome/components/bq27220/bq27220.cpp +++ b/esphome/components/bq27220/bq27220.cpp @@ -25,15 +25,6 @@ static constexpr uint8_t REG_MAC_DATA = 0x40; static constexpr uint16_t DEVICE_NUMBER = 0x0220; static constexpr uint16_t TIME_TO_EMPTY_NOT_APPLICABLE = 0xFFFF; -bool BQ27220Component::read_word_(uint8_t reg, uint16_t &value) { - uint8_t data[2]; - if (this->read_register(reg, data, 2) != i2c::ERROR_OK) { - return false; - } - value = encode_uint16(data[1], data[0]); - return true; -} - void BQ27220Component::setup() { const uint8_t device_number_cmd[2] = {0x01, 0x00}; // DEVICE_NUMBER subcommand, little-endian if (this->write_register(REG_CONTROL, device_number_cmd, 2) != i2c::ERROR_OK) { @@ -42,7 +33,7 @@ void BQ27220Component::setup() { } delay(2); // SLUUBD4A §5.3: the subcommand result needs a moment before it can be read uint16_t device_number = 0; - if (!this->read_word_(REG_MAC_DATA, device_number)) { + if (!this->read_byte_16_le(REG_MAC_DATA, &device_number)) { this->mark_failed(LOG_STR("Failed to read device number from BQ27220")); return; } diff --git a/esphome/components/bq27220/bq27220.h b/esphome/components/bq27220/bq27220.h index d88b76a20f0..62cc4b659d0 100644 --- a/esphome/components/bq27220/bq27220.h +++ b/esphome/components/bq27220/bq27220.h @@ -25,14 +25,13 @@ class BQ27220Component final : public PollingComponent, public i2c::I2CDevice { void dump_config() override; protected: - bool read_word_(uint8_t reg, uint16_t &value); /// Read a standard command register and publish it through the sensor, if one is configured. /// A failed read publishes NAN and clears `ok`. template void publish_(sensor::Sensor *sensor, uint8_t reg, bool &ok, F convert) { if (sensor == nullptr) return; uint16_t raw; - if (!this->read_word_(reg, raw)) { + if (!this->read_byte_16_le(reg, &raw)) { ok = false; sensor->publish_state(NAN); return; diff --git a/esphome/components/bq76972/bq76972.cpp b/esphome/components/bq76972/bq76972.cpp index 9426fbb2526..59d18878370 100644 --- a/esphome/components/bq76972/bq76972.cpp +++ b/esphome/components/bq76972/bq76972.cpp @@ -80,11 +80,10 @@ bool BQ76972Component::write_block_(uint8_t start_register, const uint8_t *data, } bool BQ76972Component::read_words_(uint8_t start_register, uint16_t *data, size_t num_words) { - std::array bytes; - if (num_words * 2 > bytes.size() || !this->read_block_(start_register, bytes.data(), num_words * 2)) + if (!this->read_block_(start_register, reinterpret_cast(data), num_words * 2)) return false; for (size_t i = 0; i < num_words; i++) - data[i] = encode_uint16(bytes[2 * i + 1], bytes[2 * i]); + data[i] = convert_little_endian(data[i]); return true; } diff --git a/esphome/components/cst9220/touchscreen/cst9220_touchscreen.cpp b/esphome/components/cst9220/touchscreen/cst9220_touchscreen.cpp index 366b1846d73..72803804335 100644 --- a/esphome/components/cst9220/touchscreen/cst9220_touchscreen.cpp +++ b/esphome/components/cst9220/touchscreen/cst9220_touchscreen.cpp @@ -51,9 +51,9 @@ void CST9220Touchscreen::continue_setup_() { // Read the panel resolution unless the user supplied calibration values. if (this->read_register16(REG_RESOLUTION, buffer, 4) == i2c::ERROR_OK) { if (this->x_raw_max_ == this->x_raw_min_) - this->x_raw_max_ = encode_uint16(buffer[1], buffer[0]); + this->x_raw_max_ = encode_value_le(buffer); if (this->y_raw_max_ == this->y_raw_min_) - this->y_raw_max_ = encode_uint16(buffer[3], buffer[2]); + this->y_raw_max_ = encode_value_le(&buffer[2]); } // Read the chip type and project id and validate the controller. @@ -62,8 +62,8 @@ void CST9220Touchscreen::continue_setup_() { this->mark_failed(); return; } - this->chip_id_ = encode_uint16(buffer[3], buffer[2]); - this->project_id_ = encode_uint16(buffer[1], buffer[0]); + this->chip_id_ = encode_value_le(&buffer[2]); + this->project_id_ = encode_value_le(buffer); if (this->chip_id_ != CST9220_CHIP_ID && this->chip_id_ != CST9217_CHIP_ID) { ESP_LOGE(TAG, "Unknown chip ID: 0x%04X", this->chip_id_); this->status_set_error(LOG_STR("Unknown chip ID")); diff --git a/esphome/components/fusb302b/fusb302b.cpp b/esphome/components/fusb302b/fusb302b.cpp index 479981f8cac..608b356bbac 100644 --- a/esphome/components/fusb302b/fusb302b.cpp +++ b/esphome/components/fusb302b/fusb302b.cpp @@ -343,16 +343,14 @@ bool FUSB302B::read_message_(PdMsg &msg, bool &is_sop) { uint8_t header[2]; if (!this->read_byte(FUSB_FIFOS, &token) || this->read_register(FUSB_FIFOS, header, 2) != i2c::ERROR_OK) return false; - msg.set_header(encode_uint16(header[1], header[0])); + msg.set_header(encode_value_le(header)); // Data objects, followed by the CRC32 that the chip already checked uint8_t buf[PD_MAX_NUM_DATA_OBJECTS * 4 + 4]; if (this->read_register(FUSB_FIFOS, buf, msg.num_of_obj * 4 + 4) != i2c::ERROR_OK) return false; - for (uint8_t i = 0; i < msg.num_of_obj; i++) { - const uint8_t *obj = &buf[i * 4]; - msg.data_objects[i] = encode_uint32(obj[3], obj[2], obj[1], obj[0]); - } + for (uint8_t i = 0; i < msg.num_of_obj; i++) + msg.data_objects[i] = encode_value_le(&buf[i * 4]); is_sop = (token & FUSB_FIFO_RX_TOKEN_BITS) == FUSB_FIFO_RX_SOP; return true; } diff --git a/esphome/components/gt911/touchscreen/gt911_touchscreen.cpp b/esphome/components/gt911/touchscreen/gt911_touchscreen.cpp index 388b8165fd7..fe251508e27 100644 --- a/esphome/components/gt911/touchscreen/gt911_touchscreen.cpp +++ b/esphome/components/gt911/touchscreen/gt911_touchscreen.cpp @@ -90,8 +90,8 @@ void GT911Touchscreen::setup_internal_() { if (err == i2c::ERROR_OK) { err = this->read(data, sizeof(data)); if (err == i2c::ERROR_OK) { - this->x_raw_max_ = encode_uint16(data[1], data[0]); - this->y_raw_max_ = encode_uint16(data[3], data[2]); + this->x_raw_max_ = encode_value_le(data); + this->y_raw_max_ = encode_value_le(&data[2]); if (this->swap_x_y_) std::swap(this->x_raw_max_, this->y_raw_max_); } diff --git a/esphome/components/hdc2010/hdc2010.cpp b/esphome/components/hdc2010/hdc2010.cpp index 471afe515a1..ecae9909390 100644 --- a/esphome/components/hdc2010/hdc2010.cpp +++ b/esphome/components/hdc2010/hdc2010.cpp @@ -1,4 +1,5 @@ #include "esphome/core/hal.h" +#include "esphome/core/helpers.h" #include "hdc2010.h" // https://github.com/vigsterkr/homebridge-hdc2010/blob/main/src/hdc2010.js // https://github.com/lime-labs/HDC2080-Arduino/blob/master/src/HDC2080.cpp @@ -76,7 +77,7 @@ float HDC2010Component::read_temp() { this->read_register(REG_TEMPERATURE_LOW, &byte[0], 1); this->read_register(REG_TEMPERATURE_HIGH, &byte[1], 1); - uint16_t temp = encode_uint16(byte[1], byte[0]); + uint16_t temp = encode_value_le(byte); return (float) temp * 0.0025177f - 40.0f; } @@ -86,7 +87,7 @@ float HDC2010Component::read_humidity() { this->read_register(REG_HUMIDITY_LOW, &byte[0], 1); this->read_register(REG_HUMIDITY_HIGH, &byte[1], 1); - uint16_t humidity = encode_uint16(byte[1], byte[0]); + uint16_t humidity = encode_value_le(byte); return (float) humidity * 0.001525879f; } diff --git a/esphome/components/hdc2080/hdc2080.cpp b/esphome/components/hdc2080/hdc2080.cpp index beb84f30746..8672dc497f5 100644 --- a/esphome/components/hdc2080/hdc2080.cpp +++ b/esphome/components/hdc2080/hdc2080.cpp @@ -1,5 +1,6 @@ #include "hdc2080.h" #include "esphome/core/hal.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" namespace esphome::hdc2080 { @@ -55,11 +56,11 @@ void HDC2080Component::update() { } this->status_clear_warning(); if (this->temperature_sensor_ != nullptr) { - float temp = encode_uint16(raw_data[1], raw_data[0]) * (165.0f / 65536.0f) - 40.5f; + float temp = encode_value_le(raw_data) * (165.0f / 65536.0f) - 40.5f; this->temperature_sensor_->publish_state(temp); } if (this->humidity_sensor_ != nullptr) { - float humidity = encode_uint16(raw_data[3], raw_data[2]) * (100.0f / 65536.0f); + float humidity = encode_value_le(&raw_data[2]) * (100.0f / 65536.0f); this->humidity_sensor_->publish_state(humidity); } }); diff --git a/esphome/components/icm20689/icm20689.cpp b/esphome/components/icm20689/icm20689.cpp index 3bca35dc254..69cb69c1f25 100644 --- a/esphome/components/icm20689/icm20689.cpp +++ b/esphome/components/icm20689/icm20689.cpp @@ -101,19 +101,19 @@ bool ICM20689Component::update_data(motion::MotionData &data) { const float accel_factor = accel_scale(accel_range); - data.acceleration[motion::X_AXIS] = static_cast(encode_uint16(raw_data[0], raw_data[1])) * accel_factor; - data.acceleration[motion::Y_AXIS] = static_cast(encode_uint16(raw_data[2], raw_data[3])) * accel_factor; - data.acceleration[motion::Z_AXIS] = static_cast(encode_uint16(raw_data[4], raw_data[5])) * accel_factor; + data.acceleration[motion::X_AXIS] = encode_value(raw_data) * accel_factor; + data.acceleration[motion::Y_AXIS] = encode_value(&raw_data[2]) * accel_factor; + data.acceleration[motion::Z_AXIS] = encode_value(&raw_data[4]) * accel_factor; const float gyro_factor = gyro_scale(gyro_range); - data.angular_rate[motion::X_AXIS] = static_cast(encode_uint16(raw_data[8], raw_data[9])) * gyro_factor; - data.angular_rate[motion::Y_AXIS] = static_cast(encode_uint16(raw_data[10], raw_data[11])) * gyro_factor; - data.angular_rate[motion::Z_AXIS] = static_cast(encode_uint16(raw_data[12], raw_data[13])) * gyro_factor; + data.angular_rate[motion::X_AXIS] = encode_value(&raw_data[8]) * gyro_factor; + data.angular_rate[motion::Y_AXIS] = encode_value(&raw_data[10]) * gyro_factor; + data.angular_rate[motion::Z_AXIS] = encode_value(&raw_data[12]) * gyro_factor; if (!this->temperature_callback_.empty()) { // Datasheet formula: temperature (°C) = raw / 326.8 + 25. - const float temperature = static_cast(encode_uint16(raw_data[6], raw_data[7])) / 326.8f + 25.0f; + const float temperature = encode_value(&raw_data[6]) / 326.8f + 25.0f; this->temperature_callback_.call(temperature); } diff --git a/esphome/components/lis2dw12/lis2dw12.cpp b/esphome/components/lis2dw12/lis2dw12.cpp index 21e1a07676f..7af60be87a4 100644 --- a/esphome/components/lis2dw12/lis2dw12.cpp +++ b/esphome/components/lis2dw12/lis2dw12.cpp @@ -214,9 +214,9 @@ bool LIS2DW12Component::read_acceleration_data_(float &x, float &y, float &z) { return false; } - int16_t raw_x = static_cast(encode_uint16(accel_data[1], accel_data[0])) >> this->shift_; - int16_t raw_y = static_cast(encode_uint16(accel_data[3], accel_data[2])) >> this->shift_; - int16_t raw_z = static_cast(encode_uint16(accel_data[5], accel_data[4])) >> this->shift_; + int16_t raw_x = encode_value_le(accel_data) >> this->shift_; + int16_t raw_y = encode_value_le(&accel_data[2]) >> this->shift_; + int16_t raw_z = encode_value_le(&accel_data[4]) >> this->shift_; if (this->swap_xy_) { std::swap(raw_x, raw_y); diff --git a/esphome/components/lis3dh/lis3dh.cpp b/esphome/components/lis3dh/lis3dh.cpp index 64e6da4ef95..f5ade6ab0b3 100644 --- a/esphome/components/lis3dh/lis3dh.cpp +++ b/esphome/components/lis3dh/lis3dh.cpp @@ -144,8 +144,8 @@ bool LIS3DHComponent::update_data(motion::MotionData &data) { if (this->is_failed()) return false; - uint8_t raw[6]; - if (!this->read_bytes(LIS3DH_REG_OUT_X_L | LIS3DH_AUTO_INCREMENT, raw, 6)) { + int16_t raw[3]; + if (!this->read_bytes_16_le(LIS3DH_REG_OUT_X_L | LIS3DH_AUTO_INCREMENT, raw, 3)) { this->status_set_warning(LOG_STR("Failed to read acceleration data")); return false; } @@ -154,9 +154,9 @@ bool LIS3DHComponent::update_data(motion::MotionData &data) { progmem_memcpy(&scale, &SCALE_G[this->range_], sizeof(scale)); // Data is little-endian and left-justified in a signed 16-bit register. - int16_t raw_x = (int16_t) encode_uint16(raw[1], raw[0]); - int16_t raw_y = (int16_t) encode_uint16(raw[3], raw[2]); - int16_t raw_z = (int16_t) encode_uint16(raw[5], raw[4]); + int16_t raw_x = raw[0]; + int16_t raw_y = raw[1]; + int16_t raw_z = raw[2]; ESP_LOGV(TAG, "Read raw accel data: %d, %d, %d", raw_x, raw_y, raw_z); data.acceleration[motion::X_AXIS] = raw_x * scale; data.acceleration[motion::Y_AXIS] = raw_y * scale; @@ -168,14 +168,14 @@ bool LIS3DHComponent::update_data(motion::MotionData &data) { } bool LIS3DHComponent::publish_temperature_() { - uint8_t raw[2]; - if (!this->read_bytes(LIS3DH_REG_OUT_ADC3_L | LIS3DH_AUTO_INCREMENT, raw, 2)) { + int16_t raw; + if (!this->read_bytes_16_le(LIS3DH_REG_OUT_ADC3_L | LIS3DH_AUTO_INCREMENT, &raw, 1)) { this->status_set_warning(LOG_STR("Failed to read temperature data")); return false; } // Signed, left-justified, relative value with 1 °C per count of the high byte; the // low byte carries the fractional bits in the modes that have them. - float temperature = (int16_t) encode_uint16(raw[1], raw[0]) / 256.0f + LIS3DH_TEMP_REFERENCE_C; + float temperature = raw / 256.0f + LIS3DH_TEMP_REFERENCE_C; this->temperature_callback_.call(temperature); return true; } diff --git a/esphome/components/lps22/lps22.cpp b/esphome/components/lps22/lps22.cpp index 57f801c332b..47b00057c69 100644 --- a/esphome/components/lps22/lps22.cpp +++ b/esphome/components/lps22/lps22.cpp @@ -1,4 +1,5 @@ #include "lps22.h" +#include "esphome/core/helpers.h" namespace esphome::lps22 { @@ -65,7 +66,7 @@ void LPS22Component::try_read_() { if (this->temperature_sensor_ != nullptr) { uint8_t t_buf[2]{0}; this->read_register(TEMP_L, t_buf, 2); - int16_t encoded = static_cast(encode_uint16(t_buf[1], t_buf[0])); + int16_t encoded = encode_value_le(t_buf); float temp = TEMPERATURE_SCALE * static_cast(encoded); this->temperature_sensor_->publish_state(temp); } diff --git a/esphome/components/lsm6ds/lsm6ds.cpp b/esphome/components/lsm6ds/lsm6ds.cpp index e34735a4160..1d284d54ef0 100644 --- a/esphome/components/lsm6ds/lsm6ds.cpp +++ b/esphome/components/lsm6ds/lsm6ds.cpp @@ -1,6 +1,7 @@ #include "lsm6ds.h" #include "esphome/core/log.h" #include "esphome/core/hal.h" +#include "esphome/core/helpers.h" #include "esphome/core/progmem.h" namespace esphome::lsm6ds { @@ -166,9 +167,9 @@ bool LSM6DSComponent::update_data(motion::MotionData &data) { }; float gyro_scale = GYRO_SCALE[this->gyro_range_]; - data.angular_rate[motion::X_AXIS] = (int16_t) ((raw[1] << 8) | raw[0]) * gyro_scale; - data.angular_rate[motion::Y_AXIS] = (int16_t) ((raw[3] << 8) | raw[2]) * gyro_scale; - data.angular_rate[motion::Z_AXIS] = (int16_t) ((raw[5] << 8) | raw[4]) * gyro_scale; + data.angular_rate[motion::X_AXIS] = encode_value_le(raw) * gyro_scale; + data.angular_rate[motion::Y_AXIS] = encode_value_le(&raw[2]) * gyro_scale; + data.angular_rate[motion::Z_AXIS] = encode_value_le(&raw[4]) * gyro_scale; // Accelerometer // Sensitivity (mg/LSB) from datasheet Table 3. @@ -182,12 +183,9 @@ bool LSM6DSComponent::update_data(motion::MotionData &data) { }; float accel_scale = ACCEL_SCALE[this->accel_range_]; - data.acceleration[motion::X_AXIS] = - (int16_t) ((raw[LSM6DS_ACCEL_OFFSET + 1] << 8) | raw[LSM6DS_ACCEL_OFFSET + 0]) * accel_scale; - data.acceleration[motion::Y_AXIS] = - (int16_t) ((raw[LSM6DS_ACCEL_OFFSET + 3] << 8) | raw[LSM6DS_ACCEL_OFFSET + 2]) * accel_scale; - data.acceleration[motion::Z_AXIS] = - (int16_t) ((raw[LSM6DS_ACCEL_OFFSET + 5] << 8) | raw[LSM6DS_ACCEL_OFFSET + 4]) * accel_scale; + data.acceleration[motion::X_AXIS] = encode_value_le(&raw[LSM6DS_ACCEL_OFFSET]) * accel_scale; + data.acceleration[motion::Y_AXIS] = encode_value_le(&raw[LSM6DS_ACCEL_OFFSET + 2]) * accel_scale; + data.acceleration[motion::Z_AXIS] = encode_value_le(&raw[LSM6DS_ACCEL_OFFSET + 4]) * accel_scale; // Temperature (lazy — only read if a listener is registered) // Kept as a separate 2-byte read to avoid extending the burst to 14 bytes when @@ -196,7 +194,7 @@ bool LSM6DSComponent::update_data(motion::MotionData &data) { if (!this->temperature_callback_.empty()) { uint8_t raw_t[2]; if (this->read_bytes(LSM6DS_REG_OUT_TEMP_L, raw_t, 2)) { - int16_t temp_raw = (int16_t) ((raw_t[1] << 8) | raw_t[0]); + int16_t temp_raw = encode_value_le(raw_t); float temperature = temp_raw / static_cast(this->temperature_sensitivity_) + 25.0f; this->temperature_callback_.call(temperature); } diff --git a/esphome/components/mlx90614/mlx90614.cpp b/esphome/components/mlx90614/mlx90614.cpp index a67d770cc3d..353057dc34d 100644 --- a/esphome/components/mlx90614/mlx90614.cpp +++ b/esphome/components/mlx90614/mlx90614.cpp @@ -140,7 +140,7 @@ i2c::ErrorCode MLX90614Component::read_register_(uint8_t reg, uint16_t &data) { return i2c::ERROR_CRC; } - data = encode_uint16(buf[4], buf[3]); + data = encode_value_le(&buf[3]); return i2c::ERROR_OK; } diff --git a/esphome/components/mpr121/mpr121.cpp b/esphome/components/mpr121/mpr121.cpp index 3d9cded59ff..bbcee223e8c 100644 --- a/esphome/components/mpr121/mpr121.cpp +++ b/esphome/components/mpr121/mpr121.cpp @@ -72,12 +72,7 @@ void MPR121Component::dump_config() { } void MPR121Component::loop() { uint16_t val = 0; - this->read_byte_16(MPR121_TOUCHSTATUS_L, &val); - - // Flip order - uint8_t lsb = val >> 8; - uint8_t msb = val; - val = (uint16_t(msb) << 8) | lsb; + this->read_byte_16_le(MPR121_TOUCHSTATUS_L, &val); for (auto *channel : this->channels_) channel->process(val); diff --git a/esphome/components/qmi8658/qmi8658.cpp b/esphome/components/qmi8658/qmi8658.cpp index f03aac9a80c..f6690e0af6b 100644 --- a/esphome/components/qmi8658/qmi8658.cpp +++ b/esphome/components/qmi8658/qmi8658.cpp @@ -99,18 +99,18 @@ bool QMI8658Component::update_data(motion::MotionData &data) { // Data is little-endian (low byte first). float scale = ACCEL_SCALE[this->accel_range_ >> 4]; - int16_t raw_x = encode_uint16(raw_data[ACC_OFFS + 1], raw_data[ACC_OFFS + 0]); - int16_t raw_y = encode_uint16(raw_data[ACC_OFFS + 3], raw_data[ACC_OFFS + 2]); - int16_t raw_z = encode_uint16(raw_data[ACC_OFFS + 5], raw_data[ACC_OFFS + 4]); + int16_t raw_x = encode_value_le(&raw_data[ACC_OFFS]); + int16_t raw_y = encode_value_le(&raw_data[ACC_OFFS + 2]); + int16_t raw_z = encode_value_le(&raw_data[ACC_OFFS + 4]); ESP_LOGV(TAG, "Read raw accel data: %d, %d, %d", raw_x, raw_y, raw_z); data.acceleration[motion::X_AXIS] = raw_x * scale; data.acceleration[motion::Y_AXIS] = raw_y * scale; data.acceleration[motion::Z_AXIS] = raw_z * scale; scale = GYRO_SCALE[this->gyro_range_ >> 4]; - raw_x = encode_uint16(raw_data[GYR_OFFS + 1], raw_data[GYR_OFFS + 0]); - raw_y = encode_uint16(raw_data[GYR_OFFS + 3], raw_data[GYR_OFFS + 2]); - raw_z = encode_uint16(raw_data[GYR_OFFS + 5], raw_data[GYR_OFFS + 4]); + raw_x = encode_value_le(&raw_data[GYR_OFFS]); + raw_y = encode_value_le(&raw_data[GYR_OFFS + 2]); + raw_z = encode_value_le(&raw_data[GYR_OFFS + 4]); ESP_LOGV(TAG, "Read raw gyro data: %d, %d, %d", raw_x, raw_y, raw_z); data.angular_rate[motion::X_AXIS] = raw_x * scale; data.angular_rate[motion::Y_AXIS] = raw_y * scale; @@ -119,7 +119,7 @@ bool QMI8658Component::update_data(motion::MotionData &data) { if (this->temperature_callback_.empty()) return true; // Temperature: signed 16-bit, °C = raw / 256 - int16_t raw_t = (int16_t) ((raw_data[TEMP_OFFS + 1] << 8) | raw_data[TEMP_OFFS + 0]); + int16_t raw_t = encode_value_le(&raw_data[TEMP_OFFS]); this->temperature_callback_.call(raw_t / 256.0f); return true; } diff --git a/esphome/components/qmp6988/qmp6988.cpp b/esphome/components/qmp6988/qmp6988.cpp index ea3bf19eb4c..9dce402fe02 100644 --- a/esphome/components/qmp6988/qmp6988.cpp +++ b/esphome/components/qmp6988/qmp6988.cpp @@ -112,21 +112,21 @@ bool QMP6988Component::get_calibration_data_() { (int32_t) encode_uint32(a_data_uint8_tr[18], a_data_uint8_tr[19], (a_data_uint8_tr[24] & 0x0f) << 4, 0); qmp6988_data_.qmp6988_cali.COE_a0 = qmp6988_data_.qmp6988_cali.COE_a0 >> 12; - qmp6988_data_.qmp6988_cali.COE_a1 = (int16_t) encode_uint16(a_data_uint8_tr[20], a_data_uint8_tr[21]); - qmp6988_data_.qmp6988_cali.COE_a2 = (int16_t) encode_uint16(a_data_uint8_tr[22], a_data_uint8_tr[23]); + qmp6988_data_.qmp6988_cali.COE_a1 = encode_value(&a_data_uint8_tr[20]); + qmp6988_data_.qmp6988_cali.COE_a2 = encode_value(&a_data_uint8_tr[22]); qmp6988_data_.qmp6988_cali.COE_b00 = (int32_t) encode_uint32(a_data_uint8_tr[0], a_data_uint8_tr[1], a_data_uint8_tr[24] & 0xf0, 0); qmp6988_data_.qmp6988_cali.COE_b00 = qmp6988_data_.qmp6988_cali.COE_b00 >> 12; - qmp6988_data_.qmp6988_cali.COE_bt1 = (int16_t) encode_uint16(a_data_uint8_tr[2], a_data_uint8_tr[3]); - qmp6988_data_.qmp6988_cali.COE_bt2 = (int16_t) encode_uint16(a_data_uint8_tr[4], a_data_uint8_tr[5]); - qmp6988_data_.qmp6988_cali.COE_bp1 = (int16_t) encode_uint16(a_data_uint8_tr[6], a_data_uint8_tr[7]); - qmp6988_data_.qmp6988_cali.COE_b11 = (int16_t) encode_uint16(a_data_uint8_tr[8], a_data_uint8_tr[9]); - qmp6988_data_.qmp6988_cali.COE_bp2 = (int16_t) encode_uint16(a_data_uint8_tr[10], a_data_uint8_tr[11]); - qmp6988_data_.qmp6988_cali.COE_b12 = (int16_t) encode_uint16(a_data_uint8_tr[12], a_data_uint8_tr[13]); - qmp6988_data_.qmp6988_cali.COE_b21 = (int16_t) encode_uint16(a_data_uint8_tr[14], a_data_uint8_tr[15]); - qmp6988_data_.qmp6988_cali.COE_bp3 = (int16_t) encode_uint16(a_data_uint8_tr[16], a_data_uint8_tr[17]); + qmp6988_data_.qmp6988_cali.COE_bt1 = encode_value(&a_data_uint8_tr[2]); + qmp6988_data_.qmp6988_cali.COE_bt2 = encode_value(&a_data_uint8_tr[4]); + qmp6988_data_.qmp6988_cali.COE_bp1 = encode_value(&a_data_uint8_tr[6]); + qmp6988_data_.qmp6988_cali.COE_b11 = encode_value(&a_data_uint8_tr[8]); + qmp6988_data_.qmp6988_cali.COE_bp2 = encode_value(&a_data_uint8_tr[10]); + qmp6988_data_.qmp6988_cali.COE_b12 = encode_value(&a_data_uint8_tr[12]); + qmp6988_data_.qmp6988_cali.COE_b21 = encode_value(&a_data_uint8_tr[14]); + qmp6988_data_.qmp6988_cali.COE_bp3 = encode_value(&a_data_uint8_tr[16]); ESP_LOGV(TAG, "Calibration data:\n" diff --git a/esphome/components/spd2010/spd2010_touchscreen.cpp b/esphome/components/spd2010/spd2010_touchscreen.cpp index 94fad87d7d4..55fa177d18e 100644 --- a/esphome/components/spd2010/spd2010_touchscreen.cpp +++ b/esphome/components/spd2010/spd2010_touchscreen.cpp @@ -88,7 +88,7 @@ void SPD2010Touchscreen::initialize_() { this->mark_failed(LOG_STR("Failed to read firmware version")); return; } - ESP_LOGD(TAG, "Firmware version: 0x%04X", encode_uint16(version[5], version[4])); + ESP_LOGD(TAG, "Firmware version: 0x%04X", encode_value_le(&version[4])); if (this->interrupt_pin_ != nullptr) { this->interrupt_pin_->setup(); this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE); @@ -104,7 +104,7 @@ bool SPD2010Touchscreen::finish_report_() { return false; if (status[5] == HDP_STATUS_DONE) return this->clear_interrupt_(); - const uint16_t length = encode_uint16(status[3], status[2]); + const uint16_t length = encode_value_le(&status[2]); if (status[5] != HDP_STATUS_MORE || length == 0 || length > MAX_REPORT_LENGTH) break; uint8_t remaining[MAX_REPORT_LENGTH]; @@ -125,7 +125,7 @@ bool SPD2010Touchscreen::read_data_() { } const uint8_t status_low = status[0]; const uint8_t status_high = status[1]; - const uint16_t length = encode_uint16(status[3], status[2]); + const uint16_t length = encode_value_le(&status[2]); if (status_high & STATUS_BIOS) { this->status_set_warning(LOG_STR("BIOS status; start CPU")); diff --git a/esphome/components/stts22h/stts22h.cpp b/esphome/components/stts22h/stts22h.cpp index fa96c6a0227..aee4fc9c204 100644 --- a/esphome/components/stts22h/stts22h.cpp +++ b/esphome/components/stts22h/stts22h.cpp @@ -1,3 +1,4 @@ +#include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "stts22h.h" @@ -48,7 +49,7 @@ float STTS22HComponent::read_temperature_() { // Combine the two bytes into a single 16-bit signed integer // The STTS22H temperature data is in two's complement format - int16_t temp_raw_value = static_cast(encode_uint16(temp_reg_value[1], temp_reg_value[0])); + int16_t temp_raw_value = encode_value_le(temp_reg_value); return temp_raw_value * SENSOR_SCALE; // Apply sensor resolution } diff --git a/esphome/components/tca9555/tca9555.cpp b/esphome/components/tca9555/tca9555.cpp index 2b234983a20..15272e614fb 100644 --- a/esphome/components/tca9555/tca9555.cpp +++ b/esphome/components/tca9555/tca9555.cpp @@ -64,12 +64,12 @@ void TCA9555Component::loop() { bool TCA9555Component::read_gpio_outputs_() { if (this->is_failed()) return false; - uint8_t data[2]; - if (!this->read_bytes(TCA9555_OUTPUT_PORT_REGISTER_0, data, 2)) { + uint16_t value; + if (!this->read_byte_16_le(TCA9555_OUTPUT_PORT_REGISTER_0, &value)) { this->status_set_warning(LOG_STR("Failed to read output register")); return false; } - this->output_mask_ = (uint16_t(data[1]) << 8) | (uint16_t(data[0]) << 0); + this->output_mask_ = value; this->status_clear_warning(); return true; } @@ -77,13 +77,12 @@ bool TCA9555Component::read_gpio_outputs_() { bool TCA9555Component::read_gpio_modes_() { if (this->is_failed()) return false; - uint8_t data[2]; - bool success = this->read_bytes(TCA9555_CONFIGURATION_PORT_0, data, 2); - if (!success) { + uint16_t value; + if (!this->read_byte_16_le(TCA9555_CONFIGURATION_PORT_0, &value)) { this->status_set_warning(LOG_STR("Failed to read mode register")); return false; } - this->mode_mask_ = (uint16_t(data[1]) << 8) | (uint16_t(data[0]) << 0); + this->mode_mask_ = value; this->status_clear_warning(); return true; @@ -120,10 +119,7 @@ void TCA9555Component::digital_write_hw(uint8_t pin, bool value) { this->output_mask_ &= ~(1 << pin); } - uint8_t data[2]; - data[0] = this->output_mask_; - data[1] = this->output_mask_ >> 8; - if (!this->write_bytes(TCA9555_OUTPUT_PORT_REGISTER_0, data, 2)) { + if (!this->write_byte_16_le(TCA9555_OUTPUT_PORT_REGISTER_0, this->output_mask_)) { this->status_set_warning(LOG_STR("Failed to write output register")); return; } @@ -134,11 +130,7 @@ void TCA9555Component::digital_write_hw(uint8_t pin, bool value) { bool TCA9555Component::write_gpio_modes_() { if (this->is_failed()) return false; - uint8_t data[2]; - - data[0] = this->mode_mask_; - data[1] = this->mode_mask_ >> 8; - if (!this->write_bytes(TCA9555_CONFIGURATION_PORT_0, data, 2)) { + if (!this->write_byte_16_le(TCA9555_CONFIGURATION_PORT_0, this->mode_mask_)) { this->status_set_warning(LOG_STR("Failed to write mode register")); return false; } diff --git a/esphome/components/tcs34725/tcs34725.cpp b/esphome/components/tcs34725/tcs34725.cpp index 979137a9729..a8497e7a446 100644 --- a/esphome/components/tcs34725/tcs34725.cpp +++ b/esphome/components/tcs34725/tcs34725.cpp @@ -177,20 +177,20 @@ void TCS34725Component::calculate_temperature_and_lux_(uint16_t r, uint16_t g, u } void TCS34725Component::update() { - uint8_t data[8]; // Buffer to hold the 8 bytes (2 bytes for each of the 4 channels) + uint16_t data[4]; // Clear, red, green and blue channels // Perform burst - if (this->read_register(TCS34725_REGISTER_CRGBDATAL, data, 8) != i2c::ERROR_OK) { + if (!this->read_bytes_16_le(TCS34725_REGISTER_CRGBDATAL, data, 4)) { this->status_set_warning(); ESP_LOGW(TAG, "Error reading TCS34725 sensor data"); return; } // Extract the data - uint16_t raw_c = encode_uint16(data[1], data[0]); // Clear channel - uint16_t raw_r = encode_uint16(data[3], data[2]); // Red channel - uint16_t raw_g = encode_uint16(data[5], data[4]); // Green channel - uint16_t raw_b = encode_uint16(data[7], data[6]); // Blue channel + uint16_t raw_c = data[0]; // Clear channel + uint16_t raw_r = data[1]; // Red channel + uint16_t raw_g = data[2]; // Green channel + uint16_t raw_b = data[3]; // Blue channel ESP_LOGV(TAG, "Raw values clear=%d red=%d green=%d blue=%d", raw_c, raw_r, raw_g, raw_b); diff --git a/esphome/components/tfluna/tfluna.cpp b/esphome/components/tfluna/tfluna.cpp index 706213024d6..ac168327e09 100644 --- a/esphome/components/tfluna/tfluna.cpp +++ b/esphome/components/tfluna/tfluna.cpp @@ -94,10 +94,10 @@ bool TFLuna::read_data_() { // Layout: // buf[0..1] = distance (LE), buf[2..3] = signal (LE), // buf[4..5] = temperature (LE), buf[6..7] = timestamp (LE) - uint16_t distance = encode_uint16(buf[1], buf[0]); - uint16_t signal_strength = encode_uint16(buf[3], buf[2]); - int16_t temperature_raw = encode_uint16(buf[5], buf[4]); - uint16_t timestamp = encode_uint16(buf[7], buf[6]); + uint16_t distance = encode_value_le(buf); + uint16_t signal_strength = encode_value_le(&buf[2]); + int16_t temperature_raw = encode_value_le(&buf[4]); + uint16_t timestamp = encode_value_le(&buf[6]); if (timestamp == this->previous_timestamp_) { return false; diff --git a/esphome/components/tsl2561/tsl2561.cpp b/esphome/components/tsl2561/tsl2561.cpp index f887b7d32ca..0ae96c9fd1c 100644 --- a/esphome/components/tsl2561/tsl2561.cpp +++ b/esphome/components/tsl2561/tsl2561.cpp @@ -148,13 +148,7 @@ bool TSL2561Sensor::tsl2561_write_byte(uint8_t a_register, uint8_t value) { return this->write_byte(a_register | TSL2561_COMMAND_BIT, value); } bool TSL2561Sensor::tsl2561_read_uint(uint8_t a_register, uint16_t *value) { - uint8_t data[2]; - if (!this->read_bytes(a_register | TSL2561_COMMAND_BIT, data, 2)) - return false; - const uint16_t hi = data[1]; - const uint16_t lo = data[0]; - *value = (hi << 8) | lo; - return true; + return this->read_byte_16_le(a_register | TSL2561_COMMAND_BIT, value); } bool TSL2561Sensor::tsl2561_read_byte(uint8_t a_register, uint8_t *value) { return this->read_byte(a_register | TSL2561_COMMAND_BIT, value); diff --git a/esphome/components/veml3235/veml3235.cpp b/esphome/components/veml3235/veml3235.cpp index 09340f3bdee..ffaaf9eb2a3 100644 --- a/esphome/components/veml3235/veml3235.cpp +++ b/esphome/components/veml3235/veml3235.cpp @@ -33,14 +33,14 @@ void VEML3235Sensor::setup() { } bool VEML3235Sensor::refresh_config_reg() { - uint16_t data = 0x1; // mandatory 1 per RM; shutdown bits cleared (device powered on) + uint16_t data = 0x100; // mandatory 1 per RM; shutdown bits cleared (device powered on) data |= (uint16_t(this->integration_time_) << CONFIG_REG_IT_BIT); data |= (uint16_t(this->digital_gain_) << CONFIG_REG_DG_BIT); data |= (uint16_t(this->gain_) << CONFIG_REG_G_BIT); ESP_LOGVV(TAG, "Writing 0x%.4x to register 0x%.2x", data, CONFIG_REG); - return this->write_byte_16(CONFIG_REG, data); + return this->write_byte_16_le(CONFIG_REG, data); } void VEML3235Sensor::update() { @@ -63,7 +63,7 @@ void VEML3235Sensor::read_and_publish_(uint8_t adjustments_left) { this->status_clear_warning(); - uint16_t als_counts = encode_uint16(als_regs[1], als_regs[0]); + uint16_t als_counts = encode_value_le(als_regs); if (this->auto_gain_ && adjustments_left > 0) { // A sample integrated with the previous settings may still be in the data register after the diff --git a/esphome/components/veml3235/veml3235.h b/esphome/components/veml3235/veml3235.h index c19fc17b65f..fb00a30326d 100644 --- a/esphome/components/veml3235/veml3235.h +++ b/esphome/components/veml3235/veml3235.h @@ -15,13 +15,9 @@ static const uint8_t ID_REG = 0x09; // Bit offsets within CONFIG_REG // -// The device expects the low data byte first while write_byte_16() sends the high byte first, so the 16-bit -// configuration word used here is byte-swapped relative to the datasheet: datasheet low-byte bits are word -// bits 15:8 here and datasheet high-byte bits are word bits 7:0. -// -static const uint8_t CONFIG_REG_IT_BIT = 12; -static const uint8_t CONFIG_REG_DG_BIT = 5; -static const uint8_t CONFIG_REG_G_BIT = 3; +static const uint8_t CONFIG_REG_IT_BIT = 4; +static const uint8_t CONFIG_REG_DG_BIT = 13; +static const uint8_t CONFIG_REG_G_BIT = 11; // Other important constants // diff --git a/esphome/components/xdb401/xdb401.cpp b/esphome/components/xdb401/xdb401.cpp index 11a1a569f3f..721300bae74 100644 --- a/esphome/components/xdb401/xdb401.cpp +++ b/esphome/components/xdb401/xdb401.cpp @@ -149,7 +149,7 @@ i2c::ErrorCode XDB401Component::read_temperature_(float &temperature) { ESP_LOGV(TAG, "Got temperature data: %s", temperature_buf); // Temperature is a signed 16-bit big-endian value in 1/256 °C (Q8.8 fixed point). - int16_t raw_temperature = static_cast(encode_uint16(t_data[0], t_data[1])); + int16_t raw_temperature = encode_value(t_data); ESP_LOGD(TAG, "Temperature data raw %i", raw_temperature); temperature = static_cast(raw_temperature) / 256.0f;