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);
38 LOG_SENSOR(
" ",
"Pressure", this->pressure_sensor_);
39 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
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 %" PRIu32
" ms", attempt * CHECK_DELAY);
82 if (attempt >= CHECK_ATTEMPTS) {
83 ESP_LOGE(TAG,
"Device not in measurement mode after timeout of %" PRIu32
" ms", CHECK_DELAY * CHECK_ATTEMPTS);
109 PUBLISH_STATE(this->temperature_sensor_,
temperature);
110 PUBLISH_STATE(this->pressure_sensor_,
pressure);
121 ESP_LOGE(TAG,
"Error reading pressure register");
126 ESP_LOGV(TAG,
"Got pressure data: %s", pressure_buf);
129 ESP_LOGD(TAG,
"Pressure data raw %" PRId32, raw_pressure);
131 pressure = (
static_cast<float>(raw_pressure) / CONVERT_PRESSURE) *
141 ESP_LOGE(TAG,
"Error reading temperature register");
147 ESP_LOGV(TAG,
"Got temperature data: %s", temperature_buf);
151 ESP_LOGD(TAG,
"Temperature data raw %i", raw_temperature);
153 temperature =
static_cast<float>(raw_temperature) / 256.0f;
160 ESP_LOGV(TAG,
"Skipping update, measurement already in progress");
void mark_failed()
Mark this component as failed.
void status_set_warning()
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* 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 handle_comm_failure_(const char *message)
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_
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.
ESPHOME_LOG_TAG(TAG, "xdb401")
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 T encode_value(const uint8_t *bytes)
Encode a value from its constituent bytes (from most to least significant) in an array with length si...
constexpr int32_t sign_extend(uint32_t value)
Sign-extend the low BITS bits of value, e.g. sign_extend<24>(0xFFFFFE) == -2. Higher bits are ignored...