7static const char *
const TAG =
"xdb401";
9static const uint8_t REG_PRESSURE = 0x06;
10static const uint8_t REG_TEMPERATURE = 0x09;
11static const uint8_t REG_MAKE_MEASURE = 0x30;
12static const uint8_t CMD_MAKE_MEASURE = 0x0A;
13static const uint8_t MASK_MEASURE_READY = 0x08;
14static const float CONVERT_PRESSURE = 8388608.0f;
16static const uint32_t CHECK_DELAY = 5;
17static const uint8_t CHECK_ATTEMPTS = 6;
18static const uint8_t MARK_FAIL_AFTER = 5;
21 ESP_LOGCONFIG(TAG,
"Running setup");
23 uint8_t meas_resp[1] = {};
26 this->
mark_failed(LOG_STR(
"I2C communication failed"));
34 ESP_LOGCONFIG(TAG,
"XDB401:");
36 LOG_UPDATE_INTERVAL(
this);
46 this->
mark_failed(LOG_STR(
"Too many consecutive I2C communication errors"));
57 ESP_LOGE(TAG,
"Error starting measurement, code: %u", err_code);
65 uint8_t meas_resp[1] = {};
68 ESP_LOGE(TAG,
"Error reading measurement status, code: %u", err_code);
73 ESP_LOGV(TAG,
"Config response %02X", meas_resp[0]);
76 if ((meas_resp[0] & MASK_MEASURE_READY) == 0) {
77 ESP_LOGV(TAG,
"Meas mode entered after %u ms", attempt * CHECK_DELAY);
82 if (attempt >= CHECK_ATTEMPTS) {
83 ESP_LOGE(TAG,
"Device not in measurement mode after timeout of %u ms", CHECK_DELAY * CHECK_ATTEMPTS);
123 ESP_LOGE(TAG,
"Error reading pressure register");
128 ESP_LOGV(TAG,
"Got pressure data: %s", pressure_buf);
131 int32_t raw_pressure =
static_cast<int32_t
>(
encode_uint24(p_data[0], p_data[1], p_data[2]) << 8) >> 8;
132 ESP_LOGD(TAG,
"Pressure data raw %i", raw_pressure);
134 pressure = (
static_cast<float>(raw_pressure) / CONVERT_PRESSURE) *
144 ESP_LOGE(TAG,
"Error reading temperature register");
150 ESP_LOGV(TAG,
"Got temperature data: %s", temperature_buf);
153 int16_t raw_temperature =
static_cast<int16_t
>(
encode_uint16(t_data[0], t_data[1]));
154 ESP_LOGD(TAG,
"Temperature data raw %i", raw_temperature);
156 temperature =
static_cast<float>(raw_temperature) / 256.0f;
163 ESP_LOGV(TAG,
"Skipping update, measurement already in progress");
void mark_failed()
Mark this component as failed.
void status_set_warning()
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void status_clear_warning()
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
void publish_state(float state)
Publish a new state to the front-end.
void handle_comm_failure_(const char *message)
sensor::Sensor * temperature_sensor_
i2c::ErrorCode start_measurement_()
i2c::ErrorCode read_temperature_(float &temperature)
static constexpr float full_scale_pressure_pa(uint8_t pressure_range_bar)
void check_measurement_ready_(uint8_t attempt)
void dump_config() override
uint8_t comm_err_counter_
bool measurement_in_progress_
sensor::Sensor * pressure_sensor_
uint8_t pressure_range_bar_
i2c::ErrorCode read_pressure_(float &pressure)
const LogString * message
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.