ESPHome  2024.12.2
xgzp68xx.cpp
Go to the documentation of this file.
1 #include "xgzp68xx.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/hal.h"
4 #include "esphome/core/helpers.h"
6 
7 #include <cinttypes>
8 
9 namespace esphome {
10 namespace xgzp68xx {
11 
12 static const char *const TAG = "xgzp68xx.sensor";
13 
14 static const uint8_t CMD_ADDRESS = 0x30;
15 static const uint8_t SYSCONFIG_ADDRESS = 0xA5;
16 static const uint8_t PCONFIG_ADDRESS = 0xA6;
17 static const uint8_t READ_COMMAND = 0x0A;
18 
20  // Request temp + pressure acquisition
21  this->write_register(0x30, &READ_COMMAND, 1);
22 
23  // Wait 20mS per datasheet
24  this->set_timeout("measurement", 20, [this]() {
25  uint8_t data[5];
26  uint32_t pressure_raw;
27  uint16_t temperature_raw;
28  float pressure_in_pa, temperature;
29  int success;
30 
31  // Read the sensor data
32  success = this->read_register(0x06, data, 5);
33  if (success != 0) {
34  ESP_LOGE(TAG, "Failed to read sensor data! Error code: %i", success);
35  return;
36  }
37 
38  pressure_raw = encode_uint24(data[0], data[1], data[2]);
39  temperature_raw = encode_uint16(data[3], data[4]);
40 
41  // Convert the pressure data to hPa
42  ESP_LOGV(TAG, "Got raw pressure=%" PRIu32 ", raw temperature=%u", pressure_raw, temperature_raw);
43  ESP_LOGV(TAG, "K value is %u", this->k_value_);
44 
45  // The most significant bit of both pressure and temperature will be 1 to indicate a negative value.
46  // This is directly from the datasheet, and the calculations below will handle this.
47  if (pressure_raw > pow(2, 23)) {
48  // Negative pressure
49  pressure_in_pa = (pressure_raw - pow(2, 24)) / (float) (this->k_value_);
50  } else {
51  // Positive pressure
52  pressure_in_pa = pressure_raw / (float) (this->k_value_);
53  }
54 
55  if (temperature_raw > pow(2, 15)) {
56  // Negative temperature
57  temperature = (float) (temperature_raw - pow(2, 16)) / 256.0f;
58  } else {
59  // Positive temperature
60  temperature = (float) temperature_raw / 256.0f;
61  }
62 
63  if (this->pressure_sensor_ != nullptr)
64  this->pressure_sensor_->publish_state(pressure_in_pa);
65 
66  if (this->temperature_sensor_ != nullptr)
67  this->temperature_sensor_->publish_state(temperature);
68  }); // end of set_timeout
69 }
70 
72  ESP_LOGD(TAG, "Setting up XGZP68xx...");
73  uint8_t config;
74 
75  // Display some sample bits to confirm we are talking to the sensor
76  this->read_register(SYSCONFIG_ADDRESS, &config, 1);
77  ESP_LOGCONFIG(TAG, "Gain value is %d", (config >> 3) & 0b111);
78  ESP_LOGCONFIG(TAG, "XGZP68xx started!");
79 }
80 
82  ESP_LOGCONFIG(TAG, "XGZP68xx");
83  LOG_SENSOR(" ", "Temperature: ", this->temperature_sensor_);
84  LOG_SENSOR(" ", "Pressure: ", this->pressure_sensor_);
85  LOG_I2C_DEVICE(this);
86  if (this->is_failed()) {
87  ESP_LOGE(TAG, " Connection with XGZP68xx failed!");
88  }
89  LOG_UPDATE_INTERVAL(this);
90 }
91 
92 } // namespace xgzp68xx
93 } // namespace esphome
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition: i2c.cpp:10
bool is_failed() const
Definition: component.cpp:143
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
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:192
uint16_t temperature
Definition: sun_gtil2.cpp:26
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:183
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a specific register in the I²C device
Definition: i2c.cpp:25