ESPHome 2026.11.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
11ESPHOME_LOG_TAG(TAG, "xgzp68xx.sensor");
12
13static constexpr uint32_t MEASUREMENT_TIMEOUT_ID = 0;
14
15static const uint8_t CMD_ADDRESS = 0x30;
16static const uint8_t SYSCONFIG_ADDRESS = 0xA5;
17static const uint8_t PCONFIG_ADDRESS = 0xA6;
18static const uint8_t READ_COMMAND = 0x0A;
19
20[[maybe_unused]] static const char *oversampling_to_str(XGZP68XXOversampling oversampling) {
21 switch (oversampling) {
23 return "256x";
25 return "512x";
27 return "1024x";
29 return "2048x";
31 return "4096x";
33 return "8192x";
35 return "16384x";
37 return "32768x";
38 default:
39 return "UNKNOWN";
40 }
41}
42
44 // Do we need to change oversampling?
46 uint8_t oldconfig = 0;
47 this->read_register(PCONFIG_ADDRESS, &oldconfig, 1);
48 uint8_t newconfig = (oldconfig & 0xf8) | (this->pressure_oversampling_ & 0x7);
49 this->write_register(PCONFIG_ADDRESS, &newconfig, 1);
50 ESP_LOGD(TAG, "oversampling to %s: oldconfig = 0x%x newconfig = 0x%x",
51 oversampling_to_str(this->pressure_oversampling_), oldconfig, newconfig);
53 }
54
55 // Request temp + pressure acquisition
56 this->write_register(0x30, &READ_COMMAND, 1);
57
58 // Wait 20mS per datasheet
59 this->set_timeout(MEASUREMENT_TIMEOUT_ID, 20, [this]() {
60 uint8_t data[5] = {};
61 uint32_t pressure_raw = 0;
62 uint16_t temperature_raw = 0;
63 int success;
64
65 // Read the sensor data
66 success = this->read_register(0x06, data, 5);
67 if (success != 0) {
68 ESP_LOGE(TAG, "Failed to read sensor data! Error code: %i", success);
69 return;
70 }
71
72 pressure_raw = encode_uint24(data[0], data[1], data[2]);
73 temperature_raw = encode_uint16(data[3], data[4]);
74
75 // Convert the pressure data to hPa
76 ESP_LOGV(TAG, "Got raw pressure=%" PRIu32 ", raw temperature=%u, K value=%u", pressure_raw, temperature_raw,
77 this->k_value_);
78
79 const float pressure_in_pa = static_cast<float>(sign_extend<24>(pressure_raw)) / 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(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
97 return;
98 }
99 if (i2c::ErrorCode::ERROR_OK != this->read_register(PCONFIG_ADDRESS, &config2, 1)) {
100 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
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 LOG_UPDATE_INTERVAL(this);
115}
116
117} // 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:37
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:28
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
@ 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
ESPHOME_LOG_TAG(TAG, "xgzp68xx.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.
Definition helpers.h:1002
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:1012
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:998
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12