mirror of
https://github.com/esphome/esphome.git
synced 2025-10-04 02:52:22 +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 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_CONFIGURATION = 0x02;
|
||||||
static const uint8_t HDC1080_CMD_TEMPERATURE = 0x00;
|
static const uint8_t HDC1080_CMD_TEMPERATURE = 0x00;
|
||||||
static const uint8_t HDC1080_CMD_HUMIDITY = 0x01;
|
static const uint8_t HDC1080_CMD_HUMIDITY = 0x01;
|
||||||
|
|
||||||
void HDC1080Component::setup() {
|
void HDC1080Component::setup() {
|
||||||
const uint8_t data[2] = {
|
const uint8_t config[2] = {0x00, 0x00}; // resolution 14bit for both humidity and temperature
|
||||||
0b00000000, // resolution 14bit for both humidity and temperature
|
|
||||||
0b00000000 // reserved
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!this->write_bytes(HDC1080_CMD_CONFIGURATION, data, 2)) {
|
// if configuration fails - there is a problem
|
||||||
// as instruction is same as powerup defaults (for now), interpret as warning if this fails
|
if (this->write_register(HDC1080_CMD_CONFIGURATION, config, 2) != i2c::ERROR_OK) {
|
||||||
ESP_LOGW(TAG, "HDC1080 initial config instruction error");
|
this->mark_failed();
|
||||||
this->status_set_warning();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void HDC1080Component::dump_config() {
|
void HDC1080Component::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "HDC1080:");
|
ESP_LOGCONFIG(TAG, "HDC1080:");
|
||||||
LOG_I2C_DEVICE(this);
|
LOG_I2C_DEVICE(this);
|
||||||
@@ -35,39 +31,51 @@ void HDC1080Component::dump_config() {
|
|||||||
LOG_SENSOR(" ", "Temperature", this->temperature_);
|
LOG_SENSOR(" ", "Temperature", this->temperature_);
|
||||||
LOG_SENSOR(" ", "Humidity", this->humidity_);
|
LOG_SENSOR(" ", "Humidity", this->humidity_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void HDC1080Component::update() {
|
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) {
|
if (this->write(&HDC1080_CMD_TEMPERATURE, 1) != i2c::ERROR_OK) {
|
||||||
this->status_set_warning();
|
this->status_set_warning();
|
||||||
return;
|
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;
|
this->set_timeout(20, [this]() {
|
||||||
if (this->write(&HDC1080_CMD_HUMIDITY, 1) != i2c::ERROR_OK) {
|
uint16_t raw_temperature;
|
||||||
this->status_set_warning();
|
if (this->read(reinterpret_cast<uint8_t *>(&raw_temperature), 2) != i2c::ERROR_OK) {
|
||||||
return;
|
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);
|
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Got temperature=%.1f°C humidity=%.1f%%", temp, humidity);
|
if (this->temperature_ != nullptr) {
|
||||||
this->status_clear_warning();
|
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 hdc1080
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
@@ -12,13 +12,11 @@ class HDC1080Component : public PollingComponent, public i2c::I2CDevice {
|
|||||||
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
|
void set_temperature(sensor::Sensor *temperature) { temperature_ = temperature; }
|
||||||
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
|
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
|
||||||
|
|
||||||
/// Setup the sensor and check for connection.
|
|
||||||
void setup() override;
|
void setup() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
/// Retrieve the latest sensor values. This operation takes approximately 16ms.
|
|
||||||
void update() override;
|
void update() override;
|
||||||
|
|
||||||
float get_setup_priority() const override;
|
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
sensor::Sensor *temperature_{nullptr};
|
sensor::Sensor *temperature_{nullptr};
|
||||||
|
Reference in New Issue
Block a user