ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
xgzp68xx.cpp
Go to the documentation of this file.
1#include "xgzp68xx.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
7
8#include <cinttypes>
9
10namespace esphome::xgzp68xx {
11
12ESPHOME_LOG_TAG(TAG, "xgzp68xx.sensor");
13
14static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
15
16static const uint8_t CMD_ADDRESS = 0x30;
17static const uint8_t SYSCONFIG_ADDRESS = 0xA5;
18static const uint8_t PCONFIG_ADDRESS = 0xA6;
19static const uint8_t READ_COMMAND = 0x0A;
20
21// Indexed by the XGZP68XXOversampling register value, which is not in size order
22PROGMEM_STRING_TABLE(OversamplingStrings, "1024x", "2048x", "4096x", "8192x", "256x", "512x", "16384x", "32768x",
23 "UNKNOWN");
24
25[[maybe_unused]] static const LogString *oversampling_to_str(XGZP68XXOversampling value) {
26 return OversamplingStrings::get_log_str(static_cast<uint8_t>(value), OversamplingStrings::LAST_INDEX);
27}
28
30 // Do we need to change oversampling?
32 uint8_t oldconfig = 0;
33 this->read_register(PCONFIG_ADDRESS, &oldconfig, 1);
34 uint8_t newconfig = (oldconfig & 0xf8) | (this->pressure_oversampling_ & 0x7);
35 this->write_register(PCONFIG_ADDRESS, &newconfig, 1);
36 ESP_LOGD(TAG, "oversampling to %s: oldconfig = 0x%x newconfig = 0x%x",
37 LOG_STR_ARG(oversampling_to_str(this->pressure_oversampling_)), oldconfig, newconfig);
39 }
40
41 // Request temp + pressure acquisition
42 this->write_register(0x30, &READ_COMMAND, 1);
43
44 // Wait 20mS per datasheet
45 this->set_timeout(MEASUREMENT_TIMEOUT_ID, 20, [this]() {
46 uint8_t data[5] = {};
47 uint32_t pressure_raw = 0;
48 uint16_t temperature_raw = 0;
49 int success;
50
51 // Read the sensor data
52 success = this->read_register(0x06, data, 5);
53 if (success != 0) {
54 ESP_LOGE(TAG, "Failed to read sensor data! Error code: %i", success);
55 return;
56 }
57
58 pressure_raw = encode_uint24(data[0], data[1], data[2]);
59 temperature_raw = encode_uint16(data[3], data[4]);
60
61 // Convert the pressure data to hPa
62 ESP_LOGV(TAG, "Got raw pressure=%" PRIu32 ", raw temperature=%u, K value=%u", pressure_raw, temperature_raw,
63 this->k_value_);
64
65 const float pressure_in_pa = static_cast<float>(sign_extend<24>(pressure_raw)) / this->k_value_;
66
67 float temperature = ((float) (int16_t) temperature_raw) / 256.0f;
68
69 PUBLISH_STATE(this->pressure_sensor_, pressure_in_pa);
70
71 PUBLISH_STATE(this->temperature_sensor_, temperature);
72 }); // end of set_timeout
73}
74
76 uint8_t config1 = 0, config2 = 0;
77
78 // Display some sample bits to confirm we are talking to the sensor
79 if (i2c::ErrorCode::ERROR_OK != this->read_register(SYSCONFIG_ADDRESS, &config1, 1)) {
80 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
81 return;
82 }
83 if (i2c::ErrorCode::ERROR_OK != this->read_register(PCONFIG_ADDRESS, &config2, 1)) {
84 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
85 return;
86 }
87 ESP_LOGD(TAG, "sys_config 0x%x, p_config 0x%x", config1, config2);
88}
89
91 ESP_LOGCONFIG(TAG, "XGZP68xx:");
92 LOG_SENSOR(" ", "Temperature: ", this->temperature_sensor_);
93 LOG_SENSOR(" ", "Pressure: ", this->pressure_sensor_);
94 if (this->pressure_sensor_ != nullptr) {
95 ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->pressure_oversampling_)));
96 }
97 LOG_I2C_DEVICE(this);
98 LOG_UPDATE_INTERVAL(this);
99}
100
101} // namespace esphome::xgzp68xx
void mark_failed()
Mark this component as failed.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&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:54
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:45
XGZP68XXOversampling last_pressure_oversampling_
Definition xgzp68xx.h:42
XGZP68XXOversampling pressure_oversampling_
Definition xgzp68xx.h:41
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
XGZP68XXOversampling
Enum listing all oversampling options for the XGZP68XX.
Definition xgzp68xx.h:10
PROGMEM_STRING_TABLE(OversamplingStrings, "1024x", "2048x", "4096x", "8192x", "256x", "512x", "16384x", "32768x", "UNKNOWN")
ESPHOME_LOG_TAG(TAG, "xgzp6899dc.sensor")
uint32_t value
Definition hal.cpp:41
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:1047
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:1057
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12