ESPHome 2026.6.0-dev
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"
6
7#include <cinttypes>
8
10
11static const char *const TAG = "xgzp68xx.sensor";
12
13static const uint8_t CMD_ADDRESS = 0x30;
14static const uint8_t SYSCONFIG_ADDRESS = 0xA5;
15static const uint8_t PCONFIG_ADDRESS = 0xA6;
16static const uint8_t READ_COMMAND = 0x0A;
17
18[[maybe_unused]] static const char *oversampling_to_str(XGZP68XXOversampling oversampling) {
19 switch (oversampling) {
21 return "256x";
23 return "512x";
25 return "1024x";
27 return "2048x";
29 return "4096x";
31 return "8192x";
33 return "16384x";
35 return "32768x";
36 default:
37 return "UNKNOWN";
38 }
39}
40
42 // Do we need to change oversampling?
44 uint8_t oldconfig = 0;
45 this->read_register(PCONFIG_ADDRESS, &oldconfig, 1);
46 uint8_t newconfig = (oldconfig & 0xf8) | (this->pressure_oversampling_ & 0x7);
47 this->write_register(PCONFIG_ADDRESS, &newconfig, 1);
48 ESP_LOGD(TAG, "oversampling to %s: oldconfig = 0x%x newconfig = 0x%x",
49 oversampling_to_str(this->pressure_oversampling_), oldconfig, newconfig);
51 }
52
53 // Request temp + pressure acquisition
54 this->write_register(0x30, &READ_COMMAND, 1);
55
56 // Wait 20mS per datasheet
57 this->set_timeout("measurement", 20, [this]() {
58 uint8_t data[5] = {};
59 uint32_t pressure_raw = 0;
60 uint16_t temperature_raw = 0;
61 int success;
62
63 // Read the sensor data
64 success = this->read_register(0x06, data, 5);
65 if (success != 0) {
66 ESP_LOGE(TAG, "Failed to read sensor data! Error code: %i", success);
67 return;
68 }
69
70 pressure_raw = encode_uint24(data[0], data[1], data[2]);
71 temperature_raw = encode_uint16(data[3], data[4]);
72
73 // Convert the pressure data to hPa
74 ESP_LOGV(TAG, "Got raw pressure=%" PRIu32 ", raw temperature=%u, K value=%u", pressure_raw, temperature_raw,
75 this->k_value_);
76
77 // Sign extend the pressure
78 float pressure_in_pa = (float) (((int32_t) pressure_raw << 8) >> 8);
79 pressure_in_pa /= (float) (this->k_value_);
80
81 float temperature = ((float) (int16_t) temperature_raw) / 256.0f;
82
83 if (this->pressure_sensor_ != nullptr)
84 this->pressure_sensor_->publish_state(pressure_in_pa);
85
86 if (this->temperature_sensor_ != nullptr)
87 this->temperature_sensor_->publish_state(temperature);
88 }); // end of set_timeout
89}
90
92 uint8_t config1 = 0, config2 = 0;
93
94 // Display some sample bits to confirm we are talking to the sensor
95 if (i2c::ErrorCode::ERROR_OK != this->read_register(SYSCONFIG_ADDRESS, &config1, 1)) {
96 this->mark_failed();
97 return;
98 }
99 if (i2c::ErrorCode::ERROR_OK != this->read_register(PCONFIG_ADDRESS, &config2, 1)) {
100 this->mark_failed();
101 return;
102 }
103 ESP_LOGD(TAG, "sys_config 0x%x, p_config 0x%x", config1, config2);
104}
105
107 ESP_LOGCONFIG(TAG, "XGZP68xx:");
108 LOG_SENSOR(" ", "Temperature: ", this->temperature_sensor_);
109 LOG_SENSOR(" ", "Pressure: ", this->pressure_sensor_);
110 if (this->pressure_sensor_ != nullptr) {
111 ESP_LOGCONFIG(TAG, " Oversampling: %s", oversampling_to_str(this->pressure_oversampling_));
112 }
113 LOG_I2C_DEVICE(this);
114 if (this->is_failed()) {
115 ESP_LOGE(TAG, " Connection failed");
116 }
117 LOG_UPDATE_INTERVAL(this);
118}
119
120} // namespace esphome::xgzp68xx
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
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.
Definition component.h:493
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:34
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:25
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:14
XGZP68XXOversampling
Enum listing all oversampling options for the XGZP68XX.
Definition xgzp68xx.h:10
@ XGZP68XX_OVERSAMPLING_32768X
Definition xgzp68xx.h:18
@ XGZP68XX_OVERSAMPLING_1024X
Definition xgzp68xx.h:13
@ XGZP68XX_OVERSAMPLING_256X
Definition xgzp68xx.h:11
@ XGZP68XX_OVERSAMPLING_8192X
Definition xgzp68xx.h:16
@ XGZP68XX_OVERSAMPLING_2048X
Definition xgzp68xx.h:14
@ XGZP68XX_OVERSAMPLING_4096X
Definition xgzp68xx.h:15
@ XGZP68XX_OVERSAMPLING_16384X
Definition xgzp68xx.h:17
@ XGZP68XX_OVERSAMPLING_512X
Definition xgzp68xx.h:12
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:863
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:859
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12