mirror of
https://github.com/esphome/esphome.git
synced 2026-10-19 20:49:35 +00:00
[components] Decode little-endian I2C values with the new helpers (#20939)
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "apds9930.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include <algorithm>
|
||||
@@ -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<uint16_t>(&raw[1]);
|
||||
const uint16_t ch1 = encode_value_le<uint16_t>(&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<uint16_t>(&raw[5]));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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<uint16_t>(&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++) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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); }
|
||||
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "bme280_spi.h"
|
||||
#include <esphome/components/bme280_base/bme280_base.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<uint16_t>(®s[offset]);
|
||||
}
|
||||
|
||||
// Decodes the trim registers; `regs` holds the bytes of registers 0x5D onwards (at least 21).
|
||||
|
||||
@@ -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); }
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include "bmp280_spi.h"
|
||||
#include <esphome/components/bmp280_base/bmp280_base.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<typename F> 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;
|
||||
|
||||
@@ -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<uint8_t, BQ76972_MAX_TRANSFER / 2> 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<uint8_t *>(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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<uint16_t>(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<uint16_t>(&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<uint16_t>(&buffer[2]);
|
||||
this->project_id_ = encode_value_le<uint16_t>(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"));
|
||||
|
||||
@@ -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<uint16_t>(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<uint32_t>(&buf[i * 4]);
|
||||
is_sop = (token & FUSB_FIFO_RX_TOKEN_BITS) == FUSB_FIFO_RX_SOP;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -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<uint16_t>(data);
|
||||
this->y_raw_max_ = encode_value_le<uint16_t>(&data[2]);
|
||||
if (this->swap_x_y_)
|
||||
std::swap(this->x_raw_max_, this->y_raw_max_);
|
||||
}
|
||||
|
||||
@@ -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<uint16_t>(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<uint16_t>(byte);
|
||||
return (float) humidity * 0.001525879f;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<uint16_t>(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<uint16_t>(&raw_data[2]) * (100.0f / 65536.0f);
|
||||
this->humidity_sensor_->publish_state(humidity);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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<int16_t>(encode_uint16(raw_data[0], raw_data[1])) * accel_factor;
|
||||
data.acceleration[motion::Y_AXIS] = static_cast<int16_t>(encode_uint16(raw_data[2], raw_data[3])) * accel_factor;
|
||||
data.acceleration[motion::Z_AXIS] = static_cast<int16_t>(encode_uint16(raw_data[4], raw_data[5])) * accel_factor;
|
||||
data.acceleration[motion::X_AXIS] = encode_value<int16_t>(raw_data) * accel_factor;
|
||||
data.acceleration[motion::Y_AXIS] = encode_value<int16_t>(&raw_data[2]) * accel_factor;
|
||||
data.acceleration[motion::Z_AXIS] = encode_value<int16_t>(&raw_data[4]) * accel_factor;
|
||||
|
||||
const float gyro_factor = gyro_scale(gyro_range);
|
||||
|
||||
data.angular_rate[motion::X_AXIS] = static_cast<int16_t>(encode_uint16(raw_data[8], raw_data[9])) * gyro_factor;
|
||||
data.angular_rate[motion::Y_AXIS] = static_cast<int16_t>(encode_uint16(raw_data[10], raw_data[11])) * gyro_factor;
|
||||
data.angular_rate[motion::Z_AXIS] = static_cast<int16_t>(encode_uint16(raw_data[12], raw_data[13])) * gyro_factor;
|
||||
data.angular_rate[motion::X_AXIS] = encode_value<int16_t>(&raw_data[8]) * gyro_factor;
|
||||
data.angular_rate[motion::Y_AXIS] = encode_value<int16_t>(&raw_data[10]) * gyro_factor;
|
||||
data.angular_rate[motion::Z_AXIS] = encode_value<int16_t>(&raw_data[12]) * gyro_factor;
|
||||
|
||||
if (!this->temperature_callback_.empty()) {
|
||||
// Datasheet formula: temperature (°C) = raw / 326.8 + 25.
|
||||
const float temperature = static_cast<int16_t>(encode_uint16(raw_data[6], raw_data[7])) / 326.8f + 25.0f;
|
||||
const float temperature = encode_value<int16_t>(&raw_data[6]) / 326.8f + 25.0f;
|
||||
this->temperature_callback_.call(temperature);
|
||||
}
|
||||
|
||||
|
||||
@@ -214,9 +214,9 @@ bool LIS2DW12Component::read_acceleration_data_(float &x, float &y, float &z) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int16_t raw_x = static_cast<int16_t>(encode_uint16(accel_data[1], accel_data[0])) >> this->shift_;
|
||||
int16_t raw_y = static_cast<int16_t>(encode_uint16(accel_data[3], accel_data[2])) >> this->shift_;
|
||||
int16_t raw_z = static_cast<int16_t>(encode_uint16(accel_data[5], accel_data[4])) >> this->shift_;
|
||||
int16_t raw_x = encode_value_le<int16_t>(accel_data) >> this->shift_;
|
||||
int16_t raw_y = encode_value_le<int16_t>(&accel_data[2]) >> this->shift_;
|
||||
int16_t raw_z = encode_value_le<int16_t>(&accel_data[4]) >> this->shift_;
|
||||
|
||||
if (this->swap_xy_) {
|
||||
std::swap(raw_x, raw_y);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<int16_t>(encode_uint16(t_buf[1], t_buf[0]));
|
||||
int16_t encoded = encode_value_le<int16_t>(t_buf);
|
||||
float temp = TEMPERATURE_SCALE * static_cast<float>(encoded);
|
||||
this->temperature_sensor_->publish_state(temp);
|
||||
}
|
||||
|
||||
@@ -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<int16_t>(raw) * gyro_scale;
|
||||
data.angular_rate[motion::Y_AXIS] = encode_value_le<int16_t>(&raw[2]) * gyro_scale;
|
||||
data.angular_rate[motion::Z_AXIS] = encode_value_le<int16_t>(&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<int16_t>(&raw[LSM6DS_ACCEL_OFFSET]) * accel_scale;
|
||||
data.acceleration[motion::Y_AXIS] = encode_value_le<int16_t>(&raw[LSM6DS_ACCEL_OFFSET + 2]) * accel_scale;
|
||||
data.acceleration[motion::Z_AXIS] = encode_value_le<int16_t>(&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<int16_t>(raw_t);
|
||||
float temperature = temp_raw / static_cast<float>(this->temperature_sensitivity_) + 25.0f;
|
||||
this->temperature_callback_.call(temperature);
|
||||
}
|
||||
|
||||
@@ -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<uint16_t>(&buf[3]);
|
||||
return i2c::ERROR_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<int16_t>(&raw_data[ACC_OFFS]);
|
||||
int16_t raw_y = encode_value_le<int16_t>(&raw_data[ACC_OFFS + 2]);
|
||||
int16_t raw_z = encode_value_le<int16_t>(&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<int16_t>(&raw_data[GYR_OFFS]);
|
||||
raw_y = encode_value_le<int16_t>(&raw_data[GYR_OFFS + 2]);
|
||||
raw_z = encode_value_le<int16_t>(&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<int16_t>(&raw_data[TEMP_OFFS]);
|
||||
this->temperature_callback_.call(raw_t / 256.0f);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -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<int16_t>(&a_data_uint8_tr[20]);
|
||||
qmp6988_data_.qmp6988_cali.COE_a2 = encode_value<int16_t>(&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<int16_t>(&a_data_uint8_tr[2]);
|
||||
qmp6988_data_.qmp6988_cali.COE_bt2 = encode_value<int16_t>(&a_data_uint8_tr[4]);
|
||||
qmp6988_data_.qmp6988_cali.COE_bp1 = encode_value<int16_t>(&a_data_uint8_tr[6]);
|
||||
qmp6988_data_.qmp6988_cali.COE_b11 = encode_value<int16_t>(&a_data_uint8_tr[8]);
|
||||
qmp6988_data_.qmp6988_cali.COE_bp2 = encode_value<int16_t>(&a_data_uint8_tr[10]);
|
||||
qmp6988_data_.qmp6988_cali.COE_b12 = encode_value<int16_t>(&a_data_uint8_tr[12]);
|
||||
qmp6988_data_.qmp6988_cali.COE_b21 = encode_value<int16_t>(&a_data_uint8_tr[14]);
|
||||
qmp6988_data_.qmp6988_cali.COE_bp3 = encode_value<int16_t>(&a_data_uint8_tr[16]);
|
||||
|
||||
ESP_LOGV(TAG,
|
||||
"Calibration data:\n"
|
||||
|
||||
@@ -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<uint16_t>(&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<uint16_t>(&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<uint16_t>(&status[2]);
|
||||
|
||||
if (status_high & STATUS_BIOS) {
|
||||
this->status_set_warning(LOG_STR("BIOS status; start CPU"));
|
||||
|
||||
@@ -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<int16_t>(encode_uint16(temp_reg_value[1], temp_reg_value[0]));
|
||||
int16_t temp_raw_value = encode_value_le<int16_t>(temp_reg_value);
|
||||
return temp_raw_value * SENSOR_SCALE; // Apply sensor resolution
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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<uint16_t>(buf);
|
||||
uint16_t signal_strength = encode_value_le<uint16_t>(&buf[2]);
|
||||
int16_t temperature_raw = encode_value_le<int16_t>(&buf[4]);
|
||||
uint16_t timestamp = encode_value_le<uint16_t>(&buf[6]);
|
||||
|
||||
if (timestamp == this->previous_timestamp_) {
|
||||
return false;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<uint16_t>(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
|
||||
|
||||
@@ -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
|
||||
//
|
||||
|
||||
@@ -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<int16_t>(encode_uint16(t_data[0], t_data[1]));
|
||||
int16_t raw_temperature = encode_value<int16_t>(t_data);
|
||||
ESP_LOGD(TAG, "Temperature data raw %i", raw_temperature);
|
||||
|
||||
temperature = static_cast<float>(raw_temperature) / 256.0f;
|
||||
|
||||
Reference in New Issue
Block a user