ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
xdb401.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
3#include "xdb401.h"
4
5namespace esphome::xdb401 {
6
7ESPHOME_LOG_TAG(TAG, "xdb401");
8
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; // 0x800000
15
16static const uint32_t CHECK_DELAY = 5;
17static const uint8_t CHECK_ATTEMPTS = 6;
18static const uint8_t MARK_FAIL_AFTER = 5;
19
21 ESP_LOGCONFIG(TAG, "Running setup");
22
23 uint8_t meas_resp[1] = {};
24 i2c::ErrorCode err_code = this->read_register(REG_MAKE_MEASURE, meas_resp, sizeof(meas_resp));
25 if (!this->setup_check(err_code == i2c::ERROR_OK, LOG_STR("I2C communication failed")))
26 return;
27
28 this->comm_err_counter_ = 0;
29}
30
32 ESP_LOGCONFIG(TAG, "XDB401:");
33 LOG_I2C_DEVICE(this);
34 LOG_UPDATE_INTERVAL(this);
35 ESP_LOGCONFIG(TAG, " Pressure Range: %u bar", this->pressure_range_bar_);
36 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
37 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
38}
39
41 this->status_set_warning(message);
42
43 if (this->comm_err_counter_ >= MARK_FAIL_AFTER) {
44 this->mark_failed(LOG_STR("Too many consecutive I2C communication errors"));
45 } else {
46 this->comm_err_counter_++;
47 }
48
49 this->measurement_in_progress_ = false;
50}
51
53 i2c::ErrorCode err_code = this->write_register(REG_MAKE_MEASURE, &CMD_MAKE_MEASURE, sizeof(CMD_MAKE_MEASURE));
54 if (err_code != i2c::ERROR_OK) {
55 ESP_LOGE(TAG, "Error starting measurement, code: %u", err_code);
56 return err_code;
57 }
58
59 return i2c::ERROR_OK;
60}
61
63 uint8_t meas_resp[1] = {};
64 i2c::ErrorCode err_code = this->read_register(REG_MAKE_MEASURE, meas_resp, sizeof(meas_resp));
65 if (err_code != i2c::ERROR_OK) {
66 ESP_LOGE(TAG, "Error reading measurement status, code: %u", err_code);
67 this->handle_comm_failure_("I2C communication failed");
68 return;
69 }
70
71 ESP_LOGV(TAG, "Config response %02X", meas_resp[0]);
72
73 // Bit 3 shall be 0 when measurement is ready
74 if ((meas_resp[0] & MASK_MEASURE_READY) == 0) {
75 ESP_LOGV(TAG, "Meas mode entered after %" PRIu32 " ms", attempt * CHECK_DELAY);
76 this->read_measurement_();
77 return;
78 }
79
80 if (attempt >= CHECK_ATTEMPTS) {
81 ESP_LOGE(TAG, "Device not in measurement mode after timeout of %" PRIu32 " ms", CHECK_DELAY * CHECK_ATTEMPTS);
82 this->handle_comm_failure_("Measurement timeout");
83 return;
84 }
85
86 this->set_timeout(CHECK_DELAY, [this, attempt]() { this->check_measurement_ready_(attempt + 1); });
87}
88
90 float temperature{};
91 float pressure{};
92
93 i2c::ErrorCode err_code = this->read_pressure_(pressure);
94 if (err_code != i2c::ERROR_OK) {
95 this->handle_comm_failure_("Could not read pressure data");
96 return;
97 }
98
99 err_code = this->read_temperature_(temperature);
100 if (err_code != i2c::ERROR_OK) {
101 this->handle_comm_failure_("Could not read temperature data");
102 return;
103 }
104
105 ESP_LOGD(TAG, "Got pressure=%.1f Pa, temperature=%.2f°C", pressure, temperature);
106
107 PUBLISH_STATE(this->temperature_sensor_, temperature);
108 PUBLISH_STATE(this->pressure_sensor_, pressure);
109
110 this->comm_err_counter_ = 0;
111 this->status_clear_warning();
112 this->measurement_in_progress_ = false;
113}
114
116 uint8_t p_data[3]{};
117 i2c::ErrorCode err_code = this->read_register(REG_PRESSURE, p_data, 3);
118 if (err_code != i2c::ERROR_OK) {
119 ESP_LOGE(TAG, "Error reading pressure register");
120 return err_code;
121 }
122 char pressure_buf[format_hex_pretty_size(3)];
123 format_hex_pretty_to(pressure_buf, sizeof(pressure_buf), p_data, 3);
124 ESP_LOGV(TAG, "Got pressure data: %s", pressure_buf);
125
126 int32_t raw_pressure = sign_extend<24>(encode_uint24(p_data[0], p_data[1], p_data[2]));
127 ESP_LOGD(TAG, "Pressure data raw %" PRId32, raw_pressure);
128
129 pressure = (static_cast<float>(raw_pressure) / CONVERT_PRESSURE) *
131
132 return err_code;
133}
134
136 uint8_t t_data[2]{};
137 i2c::ErrorCode err_code = this->read_register(REG_TEMPERATURE, t_data, 2);
138 if (err_code != i2c::ERROR_OK) {
139 ESP_LOGE(TAG, "Error reading temperature register");
140 return err_code;
141 }
142
143 char temperature_buf[format_hex_pretty_size(2)];
144 format_hex_pretty_to(temperature_buf, sizeof(temperature_buf), t_data, 2);
145 ESP_LOGV(TAG, "Got temperature data: %s", temperature_buf);
146
147 // Temperature is a signed 16-bit big-endian value in 1/256 °C (Q8.8 fixed point).
148 int16_t raw_temperature = encode_value<int16_t>(t_data);
149 ESP_LOGD(TAG, "Temperature data raw %i", raw_temperature);
150
151 temperature = static_cast<float>(raw_temperature) / 256.0f;
152
153 return err_code;
154}
155
157 if (this->measurement_in_progress_) {
158 ESP_LOGV(TAG, "Skipping update, measurement already in progress");
159 return;
160 }
161
162 i2c::ErrorCode err_code = this->start_measurement_();
163 if (err_code != i2c::ERROR_OK) {
164 this->handle_comm_failure_("I2C communication failed");
165 return;
166 }
167
168 this->measurement_in_progress_ = true;
169 this->set_timeout(CHECK_DELAY, [this]() { this->check_measurement_ready_(1); });
170}
171
172} // namespace esphome::xdb401
void mark_failed()
Mark this component as failed.
bool ESPHOME_ALWAYS_INLINE setup_check(bool ok)
Marks the component failed with message (default "Communication failed") unless ok; returns ok.
Definition component.h:236
void status_clear_warning()
Definition component.h:318
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
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
Definition i2c.cpp:79
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
Definition i2c.cpp:58
void handle_comm_failure_(const char *message)
Definition xdb401.cpp:40
i2c::ErrorCode start_measurement_()
Definition xdb401.cpp:52
i2c::ErrorCode read_temperature_(float &temperature)
Definition xdb401.cpp:135
static constexpr float full_scale_pressure_pa(uint8_t pressure_range_bar)
Definition xdb401.h:25
void check_measurement_ready_(uint8_t attempt)
Definition xdb401.cpp:62
i2c::ErrorCode read_pressure_(float &pressure)
Definition xdb401.cpp:115
const LogString * message
Definition component.cpp:35
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:11
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:13
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.
Definition helpers.h:1231
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).
Definition helpers.cpp:529
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".
Definition helpers.h:1920
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...
Definition helpers.h:1248
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...
Definition helpers.h:1241
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7