9static const char *
const TAG =
"qmc5883l";
11static const uint8_t QMC5883L_ADDRESS = 0x0D;
13static const uint8_t QMC5883L_REGISTER_DATA_X_LSB = 0x00;
14static const uint8_t QMC5883L_REGISTER_DATA_X_MSB = 0x01;
15static const uint8_t QMC5883L_REGISTER_DATA_Y_LSB = 0x02;
16static const uint8_t QMC5883L_REGISTER_DATA_Y_MSB = 0x03;
17static const uint8_t QMC5883L_REGISTER_DATA_Z_LSB = 0x04;
18static const uint8_t QMC5883L_REGISTER_DATA_Z_MSB = 0x05;
19static const uint8_t QMC5883L_REGISTER_STATUS = 0x06;
20static const uint8_t QMC5883L_REGISTER_TEMPERATURE_LSB = 0x07;
21static const uint8_t QMC5883L_REGISTER_TEMPERATURE_MSB = 0x08;
22static const uint8_t QMC5883L_REGISTER_CONTROL_1 = 0x09;
23static const uint8_t QMC5883L_REGISTER_CONTROL_2 = 0x0A;
24static const uint8_t QMC5883L_REGISTER_PERIOD = 0x0B;
30 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)) {
58 uint8_t control_2 = 0;
59 control_2 |= 0b0 << 7;
60 control_2 |= 0b0 << 6;
61 control_2 |= 0b0 << 0;
62 if (!this->
write_byte(QMC5883L_REGISTER_CONTROL_2, control_2)) {
68 uint8_t period = 0x01;
69 if (!this->
write_byte(QMC5883L_REGISTER_PERIOD, period)) {
81 ESP_LOGCONFIG(TAG,
"QMC5883L:");
84 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
86 LOG_UPDATE_INTERVAL(
this);
88 LOG_SENSOR(
" ",
"X Axis", this->
x_sensor_);
89 LOG_SENSOR(
" ",
"Y Axis", this->
y_sensor_);
90 LOG_SENSOR(
" ",
"Z Axis", this->
z_sensor_);
95 ESP_LOGCONFIG(TAG,
" DRDY mode: %s",
96 this->
drdy_use_isr_ ? LOG_STR_LITERAL(
"interrupt") : LOG_STR_LITERAL(
"polling"));
126 if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) {
130 snprintf(buf,
sizeof(buf),
"status read failed (%d)", err);
136 uint16_t
raw[3] = {0};
142 start = QMC5883L_REGISTER_DATA_X_LSB;
145 start = QMC5883L_REGISTER_DATA_Y_LSB;
148 start = QMC5883L_REGISTER_DATA_Z_LSB;
154 snprintf(buf,
sizeof(buf),
"mag read failed (%d)", err);
162 mg_per_bit = 0.0833f;
172 const float x = int16_t(
raw[0]) * mg_per_bit * 0.1f;
173 const float y = int16_t(
raw[1]) * mg_per_bit * 0.1f;
174 const float z = int16_t(
raw[2]) * mg_per_bit * 0.1f;
176 float heading = atan2f(0.0f -
x,
y) * 180.0f / M_PI;
184 snprintf(buf,
sizeof(buf),
"temp read failed (%d)", err);
191 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,
210 for (
size_t i = 0; i <
len; i++)
uint32_t get_loop_interval() const
void mark_failed()
Mark this component as failed.
void status_set_warning()
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void disable_loop()
Disable this component's loop.
virtual ESPDEPRECATED("Override dump_summary(char*, size_t) instead. Will be removed in 2026.7.0.", "2026.1.0") virtual std boo is_internal)()
Get a summary of this pin as a string.
virtual bool digital_read()=0
void start()
Start running the loop continuously.
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
bool write_byte(uint8_t a_register, uint8_t data) const
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
sensor::Sensor * temperature_sensor_
enum esphome::qmc5883l::QMC5883LComponent::ErrorCode error_code_
i2c::ErrorCode read_bytes_16_le_(uint8_t a_register, uint16_t *data, uint8_t len=1)
QMC5883LOversampling oversampling_
sensor::Sensor * z_sensor_
sensor::Sensor * heading_sensor_
QMC5883LDatarate datarate_
sensor::Sensor * x_sensor_
static void IRAM_ATTR gpio_intr(QMC5883LComponent *arg)
void dump_config() override
sensor::Sensor * y_sensor_
HighFrequencyLoopRequester high_freq_
void publish_state(float state)
Publish a new state to the front-end.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
constexpr T convert_little_endian(T val)
Convert a value between host byte order and little endian (least significant byte first) order.
void HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.