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