mirror of
https://github.com/esphome/esphome.git
synced 2024-12-22 13:34:54 +01:00
Optimize QMC5883L: Read registers only for enabled sensors (#6458)
This commit is contained in:
parent
1be5d14fd9
commit
63db07a156
1 changed files with 32 additions and 13 deletions
|
@ -76,15 +76,8 @@ void QMC5883LComponent::dump_config() {
|
||||||
float QMC5883LComponent::get_setup_priority() const { return setup_priority::DATA; }
|
float QMC5883LComponent::get_setup_priority() const { return setup_priority::DATA; }
|
||||||
void QMC5883LComponent::update() {
|
void QMC5883LComponent::update() {
|
||||||
uint8_t status = false;
|
uint8_t status = false;
|
||||||
this->read_byte(QMC5883L_REGISTER_STATUS, &status);
|
if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG)
|
||||||
|
this->read_byte(QMC5883L_REGISTER_STATUS, &status);
|
||||||
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;
|
float mg_per_bit;
|
||||||
switch (this->range_) {
|
switch (this->range_) {
|
||||||
|
@ -99,11 +92,37 @@ void QMC5883LComponent::update() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// in µT
|
// in µT
|
||||||
const float x = int16_t(raw_x) * mg_per_bit * 0.1f;
|
float x = NAN, y = NAN, z = NAN;
|
||||||
const float y = int16_t(raw_y) * mg_per_bit * 0.1f;
|
if (this->x_sensor_ != nullptr || this->heading_sensor_ != nullptr) {
|
||||||
const float z = int16_t(raw_z) * mg_per_bit * 0.1f;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
|
||||||
ESP_LOGD(TAG, "Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f° status=%u", x, y, z, heading, status);
|
ESP_LOGD(TAG, "Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f° status=%u", x, y, z, heading, status);
|
||||||
|
|
||||||
if (this->x_sensor_ != nullptr)
|
if (this->x_sensor_ != nullptr)
|
||||||
|
|
Loading…
Reference in a new issue