From eca95348acb87f129f4fc650aa9078cfe3ba9cb0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 18 Oct 2026 03:16:50 -1000 Subject: [PATCH] [components] Assemble 24-bit readings with encode_uint24 (#21016) --- esphome/components/am2315c/am2315c.cpp | 2 +- esphome/components/bl0906/bl0906.cpp | 3 ++- esphome/components/bmp085/bmp085.cpp | 3 ++- esphome/components/bmp3xx_base/bmp3xx_base.cpp | 7 ++++--- esphome/components/bmp581_base/bmp581_base.cpp | 3 ++- esphome/components/dps310/dps310.cpp | 10 ++++------ esphome/components/ens210/ens210.cpp | 5 +++-- esphome/components/it8951/it8951.cpp | 2 +- esphome/components/ms5611/ms5611.cpp | 5 +++-- esphome/components/pzem004t/pzem004t.cpp | 3 ++- 10 files changed, 24 insertions(+), 19 deletions(-) diff --git a/esphome/components/am2315c/am2315c.cpp b/esphome/components/am2315c/am2315c.cpp index 9b350560ee9..cd754411577 100644 --- a/esphome/components/am2315c/am2315c.cpp +++ b/esphome/components/am2315c/am2315c.cpp @@ -64,7 +64,7 @@ bool AM2315C::convert_(uint8_t *data, float &humidity, float &temperature) { uint32_t raw; raw = (data[1] << 12) | (data[2] << 4) | (data[3] >> 4); humidity = raw * 9.5367431640625e-5; - raw = ((data[3] & 0x0F) << 16) | (data[4] << 8) | data[5]; + raw = encode_uint24(data[3] & 0x0F, data[4], data[5]); temperature = raw * 1.9073486328125e-4 - 50; return crc8(data, 6, 0xFF, 0x31, true) == data[6]; } diff --git a/esphome/components/bl0906/bl0906.cpp b/esphome/components/bl0906/bl0906.cpp index 35f663ffa0c..6f59da77d34 100644 --- a/esphome/components/bl0906/bl0906.cpp +++ b/esphome/components/bl0906/bl0906.cpp @@ -1,13 +1,14 @@ #include "bl0906.h" #include "constants.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" namespace esphome::bl0906 { ESPHOME_LOG_TAG(TAG, "bl0906"); -constexpr uint32_t to_uint32_t(ube24_t input) { return input.h << 16 | input.m << 8 | input.l; } +constexpr uint32_t to_uint32_t(ube24_t input) { return encode_uint24(input.h, input.m, input.l); } constexpr int32_t to_int32_t(sbe24_t input) { return static_cast(encode_uint32((uint8_t) input.h, input.m, input.l, 0)) >> 8; diff --git a/esphome/components/bmp085/bmp085.cpp b/esphome/components/bmp085/bmp085.cpp index f9ec710a9b6..a7607d4dbd3 100644 --- a/esphome/components/bmp085/bmp085.cpp +++ b/esphome/components/bmp085/bmp085.cpp @@ -1,4 +1,5 @@ #include "bmp085.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" namespace esphome::bmp085 { @@ -93,7 +94,7 @@ void BMP085Component::read_pressure_() { return; } - uint32_t value = (uint32_t(buffer[0]) << 16) | (uint32_t(buffer[1]) << 8) | uint32_t(buffer[2]); + uint32_t value = encode_uint24(buffer[0], buffer[1], buffer[2]); if ((value >> 5) == 0) { ESP_LOGW(TAG, "Invalid pressure!"); this->status_set_warning(); diff --git a/esphome/components/bmp3xx_base/bmp3xx_base.cpp b/esphome/components/bmp3xx_base/bmp3xx_base.cpp index f59abc61ec3..f985ebed870 100644 --- a/esphome/components/bmp3xx_base/bmp3xx_base.cpp +++ b/esphome/components/bmp3xx_base/bmp3xx_base.cpp @@ -7,6 +7,7 @@ #include "bmp3xx_base.h" #include "esphome/core/hal.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/progmem.h" #include @@ -222,7 +223,7 @@ bool BMP3XXComponent::get_temperature(float &temperature) { return false; } // Copy the temperature data into the adc variables - int32_t adc_temp = (int32_t) data[2] << 16 | (int32_t) data[1] << 8 | (int32_t) data[0]; + int32_t adc_temp = encode_uint24(data[2], data[1], data[0]); // Temperature compensation (function from BMP388 datasheet) temperature = bmp388_compensate_temperature_((float) adc_temp); return true; @@ -249,8 +250,8 @@ bool BMP3XXComponent::get_measurements(float &temperature, float &pressure) { return false; } // Copy the temperature and pressure data into the adc variables - int32_t adc_pres = (int32_t) data[2] << 16 | (int32_t) data[1] << 8 | (int32_t) data[0]; - int32_t adc_temp = (int32_t) data[5] << 16 | (int32_t) data[4] << 8 | (int32_t) data[3]; + int32_t adc_pres = encode_uint24(data[2], data[1], data[0]); + int32_t adc_temp = encode_uint24(data[5], data[4], data[3]); // Temperature compensation (function from BMP388 datasheet) temperature = bmp388_compensate_temperature_((float) adc_temp); diff --git a/esphome/components/bmp581_base/bmp581_base.cpp b/esphome/components/bmp581_base/bmp581_base.cpp index 7bc743977b2..9f67f734e6e 100644 --- a/esphome/components/bmp581_base/bmp581_base.cpp +++ b/esphome/components/bmp581_base/bmp581_base.cpp @@ -12,6 +12,7 @@ #include "bmp581_base.h" #include "esphome/core/hal.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/progmem.h" @@ -410,7 +411,7 @@ bool BMP581Component::read_temperature_and_pressure_(float &temperature, float & temperature = (float) (raw_temp / 65536.0); // convert measurement to degrees Celsius (page 22 of datasheet) // pressure MSB is in data[5], LSB is in data[4], XLSB in data[3] - int32_t raw_press = (int32_t) data[5] << 16 | (int32_t) data[4] << 8 | (int32_t) data[3]; + int32_t raw_press = encode_uint24(data[5], data[4], data[3]); pressure = (float) (raw_press / 64.0); // Divide by 2^6=64 for Pa (page 22 of datasheet) return true; diff --git a/esphome/components/dps310/dps310.cpp b/esphome/components/dps310/dps310.cpp index 6ab786e7c71..5b3272b1d5e 100644 --- a/esphome/components/dps310/dps310.cpp +++ b/esphome/components/dps310/dps310.cpp @@ -1,4 +1,5 @@ #include "dps310.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/hal.h" @@ -69,8 +70,7 @@ bool DPS310Component::setup_registers_() { (((uint32_t) coef_data_raw[5] >> 4) & 0x0F); this->c00_ = DPS310Component::twos_complement(c00_, 20); - this->c10_ = - (((uint32_t) coef_data_raw[5] & 0x0F) << 16) | ((uint32_t) coef_data_raw[6] << 8) | (uint32_t) coef_data_raw[7]; + this->c10_ = encode_uint24(coef_data_raw[5] & 0x0F, coef_data_raw[6], coef_data_raw[7]); this->c10_ = DPS310Component::twos_complement(c10_, 20); this->c01_ = int16_t(((uint16_t) coef_data_raw[8] << 8) | (uint16_t) coef_data_raw[9]); @@ -133,8 +133,7 @@ void DPS310Component::read_pressure_() { return; } this->got_pres_ = true; - this->raw_pressure_ = DPS310Component::twos_complement( - int32_t((uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]))), 24); + this->raw_pressure_ = DPS310Component::twos_complement(encode_uint24(bytes[0], bytes[1], bytes[2]), 24); } void DPS310Component::read_temperature_() { @@ -144,8 +143,7 @@ void DPS310Component::read_temperature_() { return; } this->got_temp_ = true; - this->raw_temperature_ = DPS310Component::twos_complement( - int32_t((uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2]))), 24); + this->raw_temperature_ = DPS310Component::twos_complement(encode_uint24(bytes[0], bytes[1], bytes[2]), 24); } // Calculations are taken from the datasheet which can be found here: diff --git a/esphome/components/ens210/ens210.cpp b/esphome/components/ens210/ens210.cpp index 2251e0a574b..0d31a398177 100644 --- a/esphome/components/ens210/ens210.cpp +++ b/esphome/components/ens210/ens210.cpp @@ -7,6 +7,7 @@ // https://github.com/sciosense/ENS210_driver #include "ens210.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/hal.h" @@ -160,7 +161,7 @@ void ENS210Component::update() { return; } // Pack bytes for humidity - h_val_data = (uint32_t) ((uint32_t) data[5] << 16 | (uint32_t) data[4] << 8 | (uint32_t) data[3]); + h_val_data = encode_uint24(data[5], data[4], data[3]); // Extract humidity data and update the status extract_measurement_(h_val_data, &humidity_data, &humidity_status); @@ -175,7 +176,7 @@ void ENS210Component::update() { return; } // Pack bytes for temperature - t_val_data = (uint32_t) ((uint32_t) data[2] << 16 | (uint32_t) data[1] << 8 | (uint32_t) data[0]); + t_val_data = encode_uint24(data[2], data[1], data[0]); // Extract temperature data and update the status extract_measurement_(t_val_data, &temperature_data, &temperature_status); diff --git a/esphome/components/it8951/it8951.cpp b/esphome/components/it8951/it8951.cpp index 6b0a4b56094..e0d6f683f0d 100644 --- a/esphome/components/it8951/it8951.cpp +++ b/esphome/components/it8951/it8951.cpp @@ -988,7 +988,7 @@ void HOT IT8951Display::draw_pixels_internal(int x_start, int y_start, int w, in } case COLOR_BITNESS_888: { const size_t i = source_idx * 3; - color_value = ptr[i] | (static_cast(ptr[i + 1]) << 8) | (static_cast(ptr[i + 2]) << 16); + color_value = encode_uint24(ptr[i + 2], ptr[i + 1], ptr[i]); break; } default: diff --git a/esphome/components/ms5611/ms5611.cpp b/esphome/components/ms5611/ms5611.cpp index e3e5194c415..29d483dd228 100644 --- a/esphome/components/ms5611/ms5611.cpp +++ b/esphome/components/ms5611/ms5611.cpp @@ -1,4 +1,5 @@ #include "ms5611.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/hal.h" @@ -52,7 +53,7 @@ void MS5611Component::read_temperature_() { this->status_set_warning(); return; } - const uint32_t raw_temperature = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2])); + const uint32_t raw_temperature = encode_uint24(bytes[0], bytes[1], bytes[2]); // request pressure reading if (!this->write_bytes(MS5611_CMD_CONV_D1 + 0x08, nullptr, 0)) { @@ -68,7 +69,7 @@ void MS5611Component::read_pressure_(uint32_t raw_temperature) { this->status_set_warning(); return; } - const uint32_t raw_pressure = (uint32_t(bytes[0]) << 16) | (uint32_t(bytes[1]) << 8) | (uint32_t(bytes[2])); + const uint32_t raw_pressure = encode_uint24(bytes[0], bytes[1], bytes[2]); this->calculate_values_(raw_temperature, raw_pressure); } diff --git a/esphome/components/pzem004t/pzem004t.cpp b/esphome/components/pzem004t/pzem004t.cpp index 606a03e0c23..2c5e349bd31 100644 --- a/esphome/components/pzem004t/pzem004t.cpp +++ b/esphome/components/pzem004t/pzem004t.cpp @@ -1,4 +1,5 @@ #include "pzem004t.h" +#include "esphome/core/helpers.h" #include "esphome/core/log.h" #include "esphome/core/application.h" #include @@ -74,7 +75,7 @@ void PZEM004T::loop() { } case 0xA3: { // Energy Response - uint32_t energy = (uint32_t(resp[1]) << 16) | (uint32_t(resp[2]) << 8) | (uint32_t(resp[3])); + uint32_t energy = encode_uint24(resp[1], resp[2], resp[3]); if (this->energy_sensor_ != nullptr) this->energy_sensor_->publish_state(energy); ESP_LOGD(TAG, "Got Energy %" PRIu32 " Wh", energy);