Reading the z-axis register is required.
This commit is contained in:
tronikos 2024-04-27 18:22:41 -07:00 committed by GitHub
parent 9bfb36f58b
commit 76c55992ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -77,9 +77,18 @@ 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;
if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG)
this->read_byte(QMC5883L_REGISTER_STATUS, &status); 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; float mg_per_bit;
switch (this->range_) { switch (this->range_) {
case QMC5883L_RANGE_200_UT: case QMC5883L_RANGE_200_UT:
@ -93,36 +102,11 @@ void QMC5883LComponent::update() {
} }
// in µT // in µT
float x = NAN, y = NAN, z = NAN; const float x = int16_t(raw_x) * mg_per_bit * 0.1f;
if (this->x_sensor_ != nullptr || this->heading_sensor_ != nullptr) { const float y = int16_t(raw_y) * mg_per_bit * 0.1f;
uint16_t raw_x; const float z = int16_t(raw_z) * mg_per_bit * 0.1f;
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; float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
if (this->heading_sensor_ != nullptr) {
heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
}
float temp = NAN; float temp = NAN;
if (this->temperature_sensor_ != nullptr) { if (this->temperature_sensor_ != nullptr) {