12static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
14static constexpr uint8_t ID_REGISTER = 0x00;
15static constexpr uint8_t CHIP_CONTROL_REGISTER = 0x01;
16static constexpr uint8_t CFG_OSR_REGISTER = 0x02;
17static constexpr uint8_t DATA_REGISTER = 0x04;
18static constexpr uint8_t CAL_OFFSET_REGISTER = 0x20;
19static constexpr uint8_t CAL_SHIFT_REGISTER = 0x21;
21static constexpr uint8_t START_MEASUREMENT = 0x01;
22static constexpr uint8_t CFG_OSR_P_MASK = 0x1C;
23static constexpr uint8_t CFG_OSR_MODE_MASK = 0x03;
24static constexpr uint8_t CFG_OSR_MODE_NORMAL = 0x01;
25static constexpr uint32_t MEASUREMENT_TIME_MS = 20;
27static constexpr float PRESSURE_FULL_SCALE = 2097152.0f;
28static constexpr float TEMPERATURE_REFERENCE = 25.0f;
39 if (!this->
read_byte(ID_REGISTER, &chip_id) || !this->
read_byte(CAL_OFFSET_REGISTER, &cal_offset) ||
40 !this->
read_byte(CAL_SHIFT_REGISTER, &cal_shift)) {
46 ESP_LOGW(TAG,
"ID register reads 0x%02X, expected 0x%02X; check the configured variant", chip_id,
52 const float offset = power_of_two(cal_offset & 0x7F) * ((cal_offset & 0x80) ? -1.0f : 1.0f);
55 ESP_LOGD(TAG,
"Cal_coff 0x%02X 0x%02X (offset %.0f, shift %u)", cal_offset, cal_shift, offset, cal_shift / 10);
58 if (!this->
update_register(CFG_OSR_REGISTER, CFG_OSR_P_MASK | CFG_OSR_MODE_MASK,
75 for (
size_t i = 0; i <
sizeof(data); i++) {
80 const int16_t temperature_raw =
static_cast<int16_t
>(
encode_uint16(data[3], data[4]));
81 ESP_LOGV(TAG,
"Got raw pressure=%" PRIu32
", raw temperature=%d", pressure_raw, temperature_raw);
88 ESP_LOGCONFIG(TAG,
"XGZP6899DC:");
89 LOG_SENSOR(
" ",
"Temperature: ", this->temperature_sensor_);
90 LOG_SENSOR(
" ",
"Pressure: ", this->pressure_sensor_);
91 if (this->pressure_sensor_ !=
nullptr) {
92 ESP_LOGCONFIG(TAG,
" Span: %.0f Pa", this->
pressure_scale_ * PRESSURE_FULL_SCALE);
94 ESP_LOGCONFIG(TAG,
" Oversampling: factory setting");
100 LOG_UPDATE_INTERVAL(
this);
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
uint8_t get_i2c_address() const
Returns the I2C address of the object.
bool write_byte(uint8_t a_register, uint8_t data) const
bool read_byte(uint8_t a_register, uint8_t *data)
bool update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
void dump_config() override
uint8_t pressure_oversampling_
XGZP6899DCComponent(float pressure_span)
ESPHOME_LOG_TAG(TAG, "xgzp6899dc.sensor")
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.
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...
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.