12static const uint8_t QMC5883L_ADDRESS = 0x0D;
14static const uint8_t QMC5883L_REGISTER_DATA_X_LSB = 0x00;
15static const uint8_t QMC5883L_REGISTER_DATA_X_MSB = 0x01;
16static const uint8_t QMC5883L_REGISTER_DATA_Y_LSB = 0x02;
17static const uint8_t QMC5883L_REGISTER_DATA_Y_MSB = 0x03;
18static const uint8_t QMC5883L_REGISTER_DATA_Z_LSB = 0x04;
19static const uint8_t QMC5883L_REGISTER_DATA_Z_MSB = 0x05;
20static const uint8_t QMC5883L_REGISTER_STATUS = 0x06;
21static const uint8_t QMC5883L_REGISTER_TEMPERATURE_LSB = 0x07;
22static const uint8_t QMC5883L_REGISTER_TEMPERATURE_MSB = 0x08;
23static const uint8_t QMC5883L_REGISTER_CONTROL_1 = 0x09;
24static const uint8_t QMC5883L_REGISTER_CONTROL_2 = 0x0A;
25static const uint8_t QMC5883L_REGISTER_PERIOD = 0x0B;
31 if (!this->
write_byte(QMC5883L_REGISTER_CONTROL_2, 1 << 7)) {
47 uint8_t control_1 = 0;
48 control_1 |= 0b01 << 0;
50 control_1 |= this->
range_ << 4;
52 if (!this->
write_byte(QMC5883L_REGISTER_CONTROL_1, control_1)) {
57 uint8_t control_2 = 0;
58 control_2 |= 0b0 << 7;
59 control_2 |= 0b0 << 6;
60 control_2 |= 0b0 << 0;
61 if (!this->
write_byte(QMC5883L_REGISTER_CONTROL_2, control_2)) {
66 uint8_t period = 0x01;
67 if (!this->
write_byte(QMC5883L_REGISTER_PERIOD, period)) {
77void QMC5883LComponent::dump_config() {
78 ESP_LOGCONFIG(TAG,
"QMC5883L:");
80 LOG_UPDATE_INTERVAL(
this);
82 LOG_SENSOR(
" ",
"X Axis", this->x_sensor_);
83 LOG_SENSOR(
" ",
"Y Axis", this->y_sensor_);
84 LOG_SENSOR(
" ",
"Z Axis", this->z_sensor_);
85 LOG_SENSOR(
" ",
"Heading", this->heading_sensor_);
86 LOG_SENSOR(
" ",
"Temperature", this->temperature_sensor_);
89 ESP_LOGCONFIG(TAG,
" DRDY mode: %s",
90 this->
drdy_use_isr_ ? LOG_STR_LITERAL(
"interrupt") : LOG_STR_LITERAL(
"polling"));
94void QMC5883LComponent::update() {
103void QMC5883LComponent::loop() {
120 if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
124 snprintf(buf,
sizeof(buf),
"status read failed (%d)", err);
130 uint16_t
raw[3] = {0};
135 if (this->heading_sensor_ !=
nullptr || this->x_sensor_ !=
nullptr) {
136 start = QMC5883L_REGISTER_DATA_X_LSB;
138 }
else if (this->y_sensor_ !=
nullptr) {
139 start = QMC5883L_REGISTER_DATA_Y_LSB;
142 start = QMC5883L_REGISTER_DATA_Z_LSB;
148 snprintf(buf,
sizeof(buf),
"mag read failed (%d)", err);
156 mg_per_bit = 0.0833f;
166 const float x = int16_t(
raw[0]) * mg_per_bit * 0.1f;
167 const float y = int16_t(
raw[1]) * mg_per_bit * 0.1f;
168 const float z = int16_t(
raw[2]) * mg_per_bit * 0.1f;
170 float heading = atan2f(0.0f -
x,
y) * 180.0f / std::numbers::pi_v<float>;
173 if (this->temperature_sensor_ !=
nullptr) {
178 snprintf(buf,
sizeof(buf),
"temp read failed (%d)", err);
185 ESP_LOGV(TAG,
"Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f° temperature=%0.01f°C status=%u",
x,
y,
z, heading,
188 PUBLISH_STATE(this->x_sensor_,
x);
189 PUBLISH_STATE(this->y_sensor_,
y);
190 PUBLISH_STATE(this->z_sensor_,
z);
191 PUBLISH_STATE(this->heading_sensor_, heading);
192 PUBLISH_STATE(this->temperature_sensor_, temp);
199 for (
size_t i = 0; i <
len; i++)