[components] Assemble 24-bit readings with encode_uint24 (#21016)

This commit is contained in:
J. Nick Koston
2026-10-18 09:16:50 -04:00
committed by GitHub
parent ed9616d512
commit eca95348ac
10 changed files with 24 additions and 19 deletions
+1 -1
View File
@@ -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];
}
+2 -1
View File
@@ -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<int32_t>(encode_uint32((uint8_t) input.h, input.m, input.l, 0)) >> 8;
+2 -1
View File
@@ -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();
@@ -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 <cinttypes>
@@ -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);
@@ -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;
+4 -6
View File
@@ -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:
+3 -2
View File
@@ -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);
+1 -1
View File
@@ -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<uint32_t>(ptr[i + 1]) << 8) | (static_cast<uint32_t>(ptr[i + 2]) << 16);
color_value = encode_uint24(ptr[i + 2], ptr[i + 1], ptr[i]);
break;
}
default:
+3 -2
View File
@@ -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);
}
+2 -1
View File
@@ -1,4 +1,5 @@
#include "pzem004t.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include <cinttypes>
@@ -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);