ESPHome  2024.3.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 namespace esphome {
8 namespace xgzp68xx {
9 
10 static const char *const TAG = "xgzp68xx.sensor";
11 
12 static const uint8_t CMD_ADDRESS = 0x30;
13 static const uint8_t SYSCONFIG_ADDRESS = 0xA5;
14 static const uint8_t PCONFIG_ADDRESS = 0xA6;
15 static const uint8_t READ_COMMAND = 0x0A;
16 
18  // Request temp + pressure acquisition
19  this->write_register(0x30, &READ_COMMAND, 1);
20 
21  // Wait 20mS per datasheet
22  this->set_timeout("measurement", 20, [this]() {
23  uint8_t data[5];
24  uint32_t pressure_raw;
25  uint16_t temperature_raw;
26  float pressure_in_pa, temperature;
27  int success;
28 
29  // Read the sensor data
30  success = this->read_register(0x06, data, 5);
31  if (success != 0) {
32  ESP_LOGE(TAG, "Failed to read sensor data! Error code: %i", success);
33  return;
34  }
35 
36  pressure_raw = encode_uint24(data[0], data[1], data[2]);
37  temperature_raw = encode_uint16(data[3], data[4]);
38 
39  // Convert the pressure data to hPa
40  ESP_LOGV(TAG, "Got raw pressure=%d, raw temperature=%d ", pressure_raw, temperature_raw);
41  ESP_LOGV(TAG, "K value is %d ", this->k_value_);
42 
43  // The most significant bit of both pressure and temperature will be 1 to indicate a negative value.
44  // This is directly from the datasheet, and the calculations below will handle this.
45  if (pressure_raw > pow(2, 23)) {
46  // Negative pressure
47  pressure_in_pa = (pressure_raw - pow(2, 24)) / (float) (this->k_value_);
48  } else {
49  // Positive pressure
50  pressure_in_pa = pressure_raw / (float) (this->k_value_);
51  }
52 
53  if (temperature_raw > pow(2, 15)) {
54  // Negative temperature
55  temperature = (float) (temperature_raw - pow(2, 16)) / 256.0f;
56  } else {
57  // Positive temperature
58  temperature = (float) temperature_raw / 256.0f;
59  }
60 
61  if (this->pressure_sensor_ != nullptr)
62  this->pressure_sensor_->publish_state(pressure_in_pa);
63 
64  if (this->temperature_sensor_ != nullptr)
65  this->temperature_sensor_->publish_state(temperature);
66  }); // end of set_timeout
67 }
68 
70  ESP_LOGD(TAG, "Setting up XGZP68xx...");
71  uint8_t config;
72 
73  // Display some sample bits to confirm we are talking to the sensor
74  this->read_register(SYSCONFIG_ADDRESS, &config, 1);
75  ESP_LOGCONFIG(TAG, "Gain value is %d", (config >> 3) & 0b111);
76  ESP_LOGCONFIG(TAG, "XGZP68xx started!");
77 }
78 
80  ESP_LOGCONFIG(TAG, "XGZP68xx");
81  LOG_SENSOR(" ", "Temperature: ", this->temperature_sensor_);
82  LOG_SENSOR(" ", "Pressure: ", this->pressure_sensor_);
83  LOG_I2C_DEVICE(this);
84  if (this->is_failed()) {
85  ESP_LOGE(TAG, " Connection with XGZP68xx failed!");
86  }
87  LOG_UPDATE_INTERVAL(this);
88 }
89 
90 } // namespace xgzp68xx
91 } // 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
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
float temperature
Definition: qmp6988.h:71
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:191
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:182
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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