mirror of
https://github.com/esphome/esphome.git
synced 2025-10-03 18:42:23 +01:00
[hdc1080] remove delays and fix no check for sensor nullptr (#10947)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
@@ -7,24 +7,20 @@ namespace hdc1080 {
|
||||
|
||||
static const char *const TAG = "hdc1080";
|
||||
|
||||
static const uint8_t HDC1080_ADDRESS = 0x40; // 0b1000000 from datasheet
|
||||
static const uint8_t HDC1080_CMD_CONFIGURATION = 0x02;
|
||||
static const uint8_t HDC1080_CMD_TEMPERATURE = 0x00;
|
||||
static const uint8_t HDC1080_CMD_HUMIDITY = 0x01;
|
||||
|
||||
void HDC1080Component::setup() {
|
||||
const uint8_t data[2] = {
|
||||
0b00000000, // resolution 14bit for both humidity and temperature
|
||||
0b00000000 // reserved
|
||||
};
|
||||
const uint8_t config[2] = {0x00, 0x00}; // resolution 14bit for both humidity and temperature
|
||||
|
||||
if (!this->write_bytes(HDC1080_CMD_CONFIGURATION, data, 2)) {
|
||||
// as instruction is same as powerup defaults (for now), interpret as warning if this fails
|
||||
ESP_LOGW(TAG, "HDC1080 initial config instruction error");
|
||||
this->status_set_warning();
|
||||
// if configuration fails - there is a problem
|
||||
if (this->write_register(HDC1080_CMD_CONFIGURATION, config, 2) != i2c::ERROR_OK) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void HDC1080Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "HDC1080:");
|
||||
LOG_I2C_DEVICE(this);
|
||||
@@ -35,39 +31,51 @@ void HDC1080Component::dump_config() {
|
||||
LOG_SENSOR(" ", "Temperature", this->temperature_);
|
||||
LOG_SENSOR(" ", "Humidity", this->humidity_);
|
||||
}
|
||||
|
||||
void HDC1080Component::update() {
|
||||
uint16_t raw_temp;
|
||||
// regardless of what sensor/s are defined in yaml configuration
|
||||
// the hdc1080 setup configuration used, requires both temperature and humidity to be read
|
||||
|
||||
this->status_clear_warning();
|
||||
|
||||
if (this->write(&HDC1080_CMD_TEMPERATURE, 1) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
delay(20);
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_temp), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
raw_temp = i2c::i2ctohs(raw_temp);
|
||||
float temp = raw_temp * 0.0025177f - 40.0f; // raw * 2^-16 * 165 - 40
|
||||
this->temperature_->publish_state(temp);
|
||||
|
||||
uint16_t raw_humidity;
|
||||
if (this->write(&HDC1080_CMD_HUMIDITY, 1) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
delay(20);
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
raw_humidity = i2c::i2ctohs(raw_humidity);
|
||||
float humidity = raw_humidity * 0.001525879f; // raw * 2^-16 * 100
|
||||
this->humidity_->publish_state(humidity);
|
||||
this->set_timeout(20, [this]() {
|
||||
uint16_t raw_temperature;
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Got temperature=%.1f°C humidity=%.1f%%", temp, humidity);
|
||||
this->status_clear_warning();
|
||||
if (this->temperature_ != nullptr) {
|
||||
raw_temperature = i2c::i2ctohs(raw_temperature);
|
||||
float temperature = raw_temperature * 0.0025177f - 40.0f; // raw * 2^-16 * 165 - 40
|
||||
this->temperature_->publish_state(temperature);
|
||||
}
|
||||
|
||||
if (this->write(&HDC1080_CMD_HUMIDITY, 1) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
this->set_timeout(20, [this]() {
|
||||
uint16_t raw_humidity;
|
||||
if (this->read(reinterpret_cast<uint8_t *>(&raw_humidity), 2) != i2c::ERROR_OK) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->humidity_ != nullptr) {
|
||||
raw_humidity = i2c::i2ctohs(raw_humidity);
|
||||
float humidity = raw_humidity * 0.001525879f; // raw * 2^-16 * 100
|
||||
this->humidity_->publish_state(humidity);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
float HDC1080Component::get_setup_priority() const { return setup_priority::DATA; }
|
||||
|
||||
} // namespace hdc1080
|
||||
} // namespace esphome
|
||||
|
@@ -12,13 +12,11 @@ class HDC1080Component : public PollingComponent, public i2c::I2CDevice {
|
||||
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
|
||||
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
|
||||
|
||||
/// Setup the sensor and check for connection.
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
/// Retrieve the latest sensor values. This operation takes approximately 16ms.
|
||||
void update() override;
|
||||
|
||||
float get_setup_priority() const override;
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
|
||||
protected:
|
||||
sensor::Sensor *temperature_{nullptr};
|
||||
|
Reference in New Issue
Block a user