mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-20 04:53:28 +02:00
6682c43dfa
## Description: Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97 Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍 Progress: - Core support (file copy etc): 80% - Base Abstractions (light, switch): ~50% - Integrations: ~10% - Working? Yes, (but only with ported components). Other refactors: - Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`) - Rework coroutine syntax - Move from `component/platform.py` to `domain/component.py` structure as with HA - Move all defaults out of C++ and into config validation. - Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration. - Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit. Future work: - Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block - Enable loading from `custom_components` folder. **Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97 **Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here> ## Checklist: - [ ] The code change is tested and works locally. - [ ] Tests have been added to verify that the new code works (under `tests/` folder). If user exposed functionality or configuration variables are added/changed: - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
122 lines
3.4 KiB
C++
122 lines
3.4 KiB
C++
#include "sht3xd.h"
|
|
#include "esphome/core/log.h"
|
|
|
|
namespace esphome {
|
|
namespace sht3xd {
|
|
|
|
static const char *TAG = "sht3xd";
|
|
|
|
static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER = 0x3780;
|
|
static const uint16_t SHT3XD_COMMAND_READ_STATUS = 0xF32D;
|
|
static const uint16_t SHT3XD_COMMAND_CLEAR_STATUS = 0x3041;
|
|
static const uint16_t SHT3XD_COMMAND_HEATER_ENABLE = 0x306D;
|
|
static const uint16_t SHT3XD_COMMAND_HEATER_DISABLE = 0x3066;
|
|
static const uint16_t SHT3XD_COMMAND_SOFT_RESET = 0x30A2;
|
|
static const uint16_t SHT3XD_COMMAND_POLLING_H = 0x2400;
|
|
static const uint16_t SHT3XD_COMMAND_FETCH_DATA = 0xE000;
|
|
|
|
void SHT3XDComponent::setup() {
|
|
ESP_LOGCONFIG(TAG, "Setting up SHT3xD...");
|
|
if (!this->write_command_(SHT3XD_COMMAND_READ_SERIAL_NUMBER)) {
|
|
this->mark_failed();
|
|
return;
|
|
}
|
|
|
|
uint16_t raw_serial_number[2];
|
|
if (!this->read_data_(raw_serial_number, 2)) {
|
|
this->mark_failed();
|
|
return;
|
|
}
|
|
uint32_t serial_number = (uint32_t(raw_serial_number[0]) << 16) | uint32_t(raw_serial_number[1]);
|
|
ESP_LOGV(TAG, " Serial Number: 0x%08X", serial_number);
|
|
}
|
|
void SHT3XDComponent::dump_config() {
|
|
ESP_LOGCONFIG(TAG, "SHT3xD:");
|
|
LOG_I2C_DEVICE(this);
|
|
if (this->is_failed()) {
|
|
ESP_LOGE(TAG, "Communication with SHT3xD failed!");
|
|
}
|
|
LOG_UPDATE_INTERVAL(this);
|
|
|
|
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
|
LOG_SENSOR(" ", "Humidity", this->humidity_sensor_);
|
|
}
|
|
float SHT3XDComponent::get_setup_priority() const { return setup_priority::DATA; }
|
|
void SHT3XDComponent::update() {
|
|
if (!this->write_command_(SHT3XD_COMMAND_POLLING_H))
|
|
return;
|
|
|
|
this->set_timeout(50, [this]() {
|
|
uint16_t raw_data[2];
|
|
if (!this->read_data_(raw_data, 2)) {
|
|
this->status_set_warning();
|
|
return;
|
|
}
|
|
|
|
float temperature = 175.0f * float(raw_data[0]) / 65535.0f - 45.0f;
|
|
float humidity = 100.0f * float(raw_data[1]) / 65535.0f;
|
|
|
|
ESP_LOGD(TAG, "Got temperature=%.2f°C humidity=%.2f%%", temperature, humidity);
|
|
if (this->temperature_sensor_ != nullptr)
|
|
this->temperature_sensor_->publish_state(temperature);
|
|
if (this->humidity_sensor_ != nullptr)
|
|
this->humidity_sensor_->publish_state(humidity);
|
|
this->status_clear_warning();
|
|
});
|
|
}
|
|
|
|
bool SHT3XDComponent::write_command_(uint16_t command) {
|
|
// Warning ugly, trick the I2Ccomponent base by setting register to the first 8 bit.
|
|
return this->write_byte(command >> 8, command & 0xFF);
|
|
}
|
|
|
|
uint8_t sht_crc(uint8_t data1, uint8_t data2) {
|
|
uint8_t bit;
|
|
uint8_t crc = 0xFF;
|
|
|
|
crc ^= data1;
|
|
for (bit = 8; bit > 0; --bit) {
|
|
if (crc & 0x80)
|
|
crc = (crc << 1) ^ 0x131;
|
|
else
|
|
crc = (crc << 1);
|
|
}
|
|
|
|
crc ^= data2;
|
|
for (bit = 8; bit > 0; --bit) {
|
|
if (crc & 0x80)
|
|
crc = (crc << 1) ^ 0x131;
|
|
else
|
|
crc = (crc << 1);
|
|
}
|
|
|
|
return crc;
|
|
}
|
|
|
|
bool SHT3XDComponent::read_data_(uint16_t *data, uint8_t len) {
|
|
const uint8_t num_bytes = len * 3;
|
|
auto *buf = new uint8_t[num_bytes];
|
|
|
|
if (!this->parent_->raw_receive(this->address_, buf, num_bytes)) {
|
|
delete[](buf);
|
|
return false;
|
|
}
|
|
|
|
for (uint8_t i = 0; i < len; i++) {
|
|
const uint8_t j = 3 * i;
|
|
uint8_t crc = sht_crc(buf[j], buf[j + 1]);
|
|
if (crc != buf[j + 2]) {
|
|
ESP_LOGE(TAG, "CRC8 Checksum invalid! 0x%02X != 0x%02X", buf[j + 2], crc);
|
|
delete[](buf);
|
|
return false;
|
|
}
|
|
data[i] = (buf[j] << 8) | buf[j + 1];
|
|
}
|
|
|
|
delete[](buf);
|
|
return true;
|
|
}
|
|
|
|
} // namespace sht3xd
|
|
} // namespace esphome
|