mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
commit
48fa549042
6 changed files with 112 additions and 47 deletions
|
@ -61,28 +61,57 @@ void I2SAudioMicrophone::start_() {
|
|||
.bits_per_chan = I2S_BITS_PER_CHAN_DEFAULT,
|
||||
};
|
||||
|
||||
esp_err_t err;
|
||||
|
||||
#if SOC_I2S_SUPPORTS_ADC
|
||||
if (this->adc_) {
|
||||
config.mode = (i2s_mode_t) (config.mode | I2S_MODE_ADC_BUILT_IN);
|
||||
i2s_driver_install(this->parent_->get_port(), &config, 0, nullptr);
|
||||
err = i2s_driver_install(this->parent_->get_port(), &config, 0, nullptr);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Error installing I2S driver: %s", esp_err_to_name(err));
|
||||
this->status_set_error();
|
||||
return;
|
||||
}
|
||||
|
||||
err = i2s_set_adc_mode(ADC_UNIT_1, this->adc_channel_);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Error setting ADC mode: %s", esp_err_to_name(err));
|
||||
this->status_set_error();
|
||||
return;
|
||||
}
|
||||
err = i2s_adc_enable(this->parent_->get_port());
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Error enabling ADC: %s", esp_err_to_name(err));
|
||||
this->status_set_error();
|
||||
return;
|
||||
}
|
||||
|
||||
i2s_set_adc_mode(ADC_UNIT_1, this->adc_channel_);
|
||||
i2s_adc_enable(this->parent_->get_port());
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
if (this->pdm_)
|
||||
config.mode = (i2s_mode_t) (config.mode | I2S_MODE_PDM);
|
||||
|
||||
i2s_driver_install(this->parent_->get_port(), &config, 0, nullptr);
|
||||
err = i2s_driver_install(this->parent_->get_port(), &config, 0, nullptr);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Error installing I2S driver: %s", esp_err_to_name(err));
|
||||
this->status_set_error();
|
||||
return;
|
||||
}
|
||||
|
||||
i2s_pin_config_t pin_config = this->parent_->get_pin_config();
|
||||
pin_config.data_in_num = this->din_pin_;
|
||||
|
||||
i2s_set_pin(this->parent_->get_port(), &pin_config);
|
||||
err = i2s_set_pin(this->parent_->get_port(), &pin_config);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Error setting I2S pin: %s", esp_err_to_name(err));
|
||||
this->status_set_error();
|
||||
return;
|
||||
}
|
||||
}
|
||||
this->state_ = microphone::STATE_RUNNING;
|
||||
this->high_freq_.start();
|
||||
this->status_clear_error();
|
||||
}
|
||||
|
||||
void I2SAudioMicrophone::stop() {
|
||||
|
@ -96,11 +125,33 @@ void I2SAudioMicrophone::stop() {
|
|||
}
|
||||
|
||||
void I2SAudioMicrophone::stop_() {
|
||||
i2s_stop(this->parent_->get_port());
|
||||
i2s_driver_uninstall(this->parent_->get_port());
|
||||
esp_err_t err;
|
||||
#if SOC_I2S_SUPPORTS_ADC
|
||||
if (this->adc_) {
|
||||
err = i2s_adc_disable(this->parent_->get_port());
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Error disabling ADC: %s", esp_err_to_name(err));
|
||||
this->status_set_error();
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
err = i2s_stop(this->parent_->get_port());
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Error stopping I2S microphone: %s", esp_err_to_name(err));
|
||||
this->status_set_error();
|
||||
return;
|
||||
}
|
||||
err = i2s_driver_uninstall(this->parent_->get_port());
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "Error uninstalling I2S driver: %s", esp_err_to_name(err));
|
||||
this->status_set_error();
|
||||
return;
|
||||
}
|
||||
this->parent_->unlock();
|
||||
this->state_ = microphone::STATE_STOPPED;
|
||||
this->high_freq_.stop();
|
||||
this->status_clear_error();
|
||||
}
|
||||
|
||||
size_t I2SAudioMicrophone::read(int16_t *buf, size_t len) {
|
||||
|
|
|
@ -77,9 +77,18 @@ void QMC5883LComponent::dump_config() {
|
|||
float QMC5883LComponent::get_setup_priority() const { return setup_priority::DATA; }
|
||||
void QMC5883LComponent::update() {
|
||||
uint8_t status = false;
|
||||
if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG)
|
||||
this->read_byte(QMC5883L_REGISTER_STATUS, &status);
|
||||
|
||||
// Always request X,Y,Z regardless if there are sensors for them
|
||||
// to avoid https://github.com/esphome/issues/issues/5731
|
||||
uint16_t raw_x, raw_y, raw_z;
|
||||
if (!this->read_byte_16_(QMC5883L_REGISTER_DATA_X_LSB, &raw_x) ||
|
||||
!this->read_byte_16_(QMC5883L_REGISTER_DATA_Y_LSB, &raw_y) ||
|
||||
!this->read_byte_16_(QMC5883L_REGISTER_DATA_Z_LSB, &raw_z)) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
float mg_per_bit;
|
||||
switch (this->range_) {
|
||||
case QMC5883L_RANGE_200_UT:
|
||||
|
@ -93,36 +102,11 @@ void QMC5883LComponent::update() {
|
|||
}
|
||||
|
||||
// in µT
|
||||
float x = NAN, y = NAN, z = NAN;
|
||||
if (this->x_sensor_ != nullptr || this->heading_sensor_ != nullptr) {
|
||||
uint16_t raw_x;
|
||||
if (!this->read_byte_16_(QMC5883L_REGISTER_DATA_X_LSB, &raw_x)) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
x = int16_t(raw_x) * mg_per_bit * 0.1f;
|
||||
}
|
||||
if (this->y_sensor_ != nullptr || this->heading_sensor_ != nullptr) {
|
||||
uint16_t raw_y;
|
||||
if (!this->read_byte_16_(QMC5883L_REGISTER_DATA_Y_LSB, &raw_y)) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
y = int16_t(raw_y) * mg_per_bit * 0.1f;
|
||||
}
|
||||
if (this->z_sensor_ != nullptr) {
|
||||
uint16_t raw_z;
|
||||
if (!this->read_byte_16_(QMC5883L_REGISTER_DATA_Z_LSB, &raw_z)) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
z = int16_t(raw_z) * mg_per_bit * 0.1f;
|
||||
}
|
||||
const float x = int16_t(raw_x) * mg_per_bit * 0.1f;
|
||||
const float y = int16_t(raw_y) * mg_per_bit * 0.1f;
|
||||
const float z = int16_t(raw_z) * mg_per_bit * 0.1f;
|
||||
|
||||
float heading = NAN;
|
||||
if (this->heading_sensor_ != nullptr) {
|
||||
heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
|
||||
}
|
||||
float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
|
||||
|
||||
float temp = NAN;
|
||||
if (this->temperature_sensor_ != nullptr) {
|
||||
|
|
|
@ -6,9 +6,14 @@ namespace sht3xd {
|
|||
|
||||
static const char *const TAG = "sht3xd";
|
||||
|
||||
// use read serial number register with clock stretching disabled as per other SHT3XD_COMMAND registers
|
||||
// which provides support for SHT85 sensor
|
||||
// SHT85 does not support clock stretching and uses same registers as SHT3xd with clock stretching disabled
|
||||
// https://sensirion.com/media/documents/E5762713/63D103C2/Sensirion_electronic_identification_code_SHT3x.pdf
|
||||
// indicates two possible read serial number registers either with clock stretching enabled or disabled.
|
||||
// Other SHT3XD_COMMAND registers use the clock stretching disabled register.
|
||||
// To ensure compatibility, reading serial number using the register with clock stretching register enabled
|
||||
// (used originally in this component) is tried first and if that fails the alternate register address
|
||||
// with clock stretching disabled is read.
|
||||
|
||||
static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING = 0x3780;
|
||||
static const uint16_t SHT3XD_COMMAND_READ_SERIAL_NUMBER = 0x3682;
|
||||
|
||||
static const uint16_t SHT3XD_COMMAND_READ_STATUS = 0xF32D;
|
||||
|
@ -22,13 +27,19 @@ static const uint16_t SHT3XD_COMMAND_FETCH_DATA = 0xE000;
|
|||
void SHT3XDComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up SHT3xD...");
|
||||
uint16_t raw_serial_number[2];
|
||||
if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER_CLOCK_STRETCHING, raw_serial_number, 2)) {
|
||||
this->error_code_ = READ_SERIAL_STRETCHED_FAILED;
|
||||
if (!this->get_register(SHT3XD_COMMAND_READ_SERIAL_NUMBER, raw_serial_number, 2)) {
|
||||
this->error_code_ = READ_SERIAL_FAILED;
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this->serial_number_ = (uint32_t(raw_serial_number[0]) << 16) | uint32_t(raw_serial_number[1]);
|
||||
|
||||
if (!this->write_command(heater_enabled_ ? SHT3XD_COMMAND_HEATER_ENABLE : SHT3XD_COMMAND_HEATER_DISABLE)) {
|
||||
this->error_code_ = WRITE_HEATER_MODE_FAILED;
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
@ -36,10 +47,21 @@ void SHT3XDComponent::setup() {
|
|||
|
||||
void SHT3XDComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "SHT3xD:");
|
||||
switch (this->error_code_) {
|
||||
case READ_SERIAL_FAILED:
|
||||
ESP_LOGD(TAG, " Error reading serial number");
|
||||
break;
|
||||
case WRITE_HEATER_MODE_FAILED:
|
||||
ESP_LOGD(TAG, " Error writing heater mode");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (this->is_failed()) {
|
||||
ESP_LOGE(TAG, " Communication with SHT3xD failed!");
|
||||
return;
|
||||
}
|
||||
ESP_LOGD(TAG, " Setup successful");
|
||||
ESP_LOGD(TAG, " Serial Number: 0x%08" PRIX32, this->serial_number_);
|
||||
ESP_LOGD(TAG, " Heater Enabled: %s", this->heater_enabled_ ? "true" : "false");
|
||||
|
||||
|
|
|
@ -22,6 +22,13 @@ class SHT3XDComponent : public PollingComponent, public sensirion_common::Sensir
|
|||
void set_heater_enabled(bool heater_enabled) { heater_enabled_ = heater_enabled; }
|
||||
|
||||
protected:
|
||||
enum ErrorCode {
|
||||
NONE = 0,
|
||||
READ_SERIAL_STRETCHED_FAILED,
|
||||
READ_SERIAL_FAILED,
|
||||
WRITE_HEATER_MODE_FAILED,
|
||||
} error_code_{NONE};
|
||||
|
||||
sensor::Sensor *temperature_sensor_{nullptr};
|
||||
sensor::Sensor *humidity_sensor_{nullptr};
|
||||
bool heater_enabled_{true};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Constants used by esphome."""
|
||||
|
||||
__version__ = "2024.4.1"
|
||||
__version__ = "2024.4.2"
|
||||
|
||||
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
|
||||
VALID_SUBSTITUTIONS_CHARACTERS = (
|
||||
|
|
|
@ -321,7 +321,8 @@ class ESPHomeLoaderMixin:
|
|||
file, vars = node.value, None
|
||||
|
||||
result = _load_yaml_internal(self._rel_path(file))
|
||||
if vars:
|
||||
if not vars:
|
||||
vars = {}
|
||||
result = substitute_vars(result, vars)
|
||||
return result
|
||||
|
||||
|
|
Loading…
Reference in a new issue