ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
hdc2080.cpp
Go to the documentation of this file.
1#include "hdc2080.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::hdc2080 {
7
8ESPHOME_LOG_TAG(TAG, "hdc2080");
9
10// Register map (Table 8-6)
11static constexpr uint8_t REG_TEMPERATURE_LOW = 0x00; // Temperature [7:0]
12static constexpr uint8_t REG_TEMPERATURE_HIGH = 0x01; // Temperature [15:8]
13static constexpr uint8_t REG_HUMIDITY_LOW = 0x02; // Humidity [7:0]
14static constexpr uint8_t REG_HUMIDITY_HIGH = 0x03; // Humidity [15:8]
15static constexpr uint8_t REG_RESET_DRDY_INT_CONF = 0x0E; // Soft Reset and Interrupt Configuration
16static constexpr uint8_t REG_MEASUREMENT_CONFIGURATION = 0x0F;
17
18// Measurement register (0x0F) bit fields
19static constexpr uint8_t MEAS_TRIG = 0x01; // Bit 0: start measurement
20static constexpr uint8_t MEAS_CONF_TEMP = 0x02; // Bits 2:1 = 01: temperature only
21static constexpr uint8_t MEAS_CONF_HUM = 0x04; // Bits 2:1 = 10: humidity only
22
24 const uint8_t data = 0x00; // automatic measurement mode disabled, heater off
25 if (this->write_register(REG_RESET_DRDY_INT_CONF, &data, 1) != i2c::ERROR_OK) {
26 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
27 return;
28 }
29}
30
31void HDC2080Component::dump_config() {
32 ESP_LOGCONFIG(TAG, "HDC2080:");
33 LOG_I2C_DEVICE(this);
34 LOG_UPDATE_INTERVAL(this);
35 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
36 LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
37}
38
39void HDC2080Component::update() {
40 uint8_t data = MEAS_TRIG; // 14-bit resolution, measure both, start
41 if (this->temperature_sensor_ != nullptr && this->humidity_sensor_ == nullptr) {
42 data = MEAS_TRIG | MEAS_CONF_TEMP;
43 } else if (this->temperature_sensor_ == nullptr && this->humidity_sensor_ != nullptr) {
44 data = MEAS_TRIG | MEAS_CONF_HUM;
45 }
46 if (this->write_register(REG_MEASUREMENT_CONFIGURATION, &data, 1) != i2c::ERROR_OK) {
47 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
48 return;
49 }
50 // wait for conversion to complete 2ms should be enough, more is fine
51 this->set_timeout(5, [this]() {
52 uint8_t raw_data[4];
53 if (!this->status_comm_result(this->read_register(REG_TEMPERATURE_LOW, raw_data, 4) == i2c::ERROR_OK))
54 return;
55 PUBLISH_STATE(this->temperature_sensor_, encode_value_le<uint16_t>(raw_data) * (165.0f / 65536.0f) - 40.5f);
56 PUBLISH_STATE(this->humidity_sensor_, encode_value_le<uint16_t>(&raw_data[2]) * (100.0f / 65536.0f));
57 });
58}
59
60} // namespace esphome::hdc2080
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
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:54
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:45
ESPHOME_LOG_TAG(TAG, "hdc2080")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
std::span< const uint8_t > data
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084