9 static const char *
const TAG =
"qmc5883l";
10 static const uint8_t QMC5883L_ADDRESS = 0x0D;
12 static const uint8_t QMC5883L_REGISTER_DATA_X_LSB = 0x00;
13 static const uint8_t QMC5883L_REGISTER_DATA_X_MSB = 0x01;
14 static const uint8_t QMC5883L_REGISTER_DATA_Y_LSB = 0x02;
15 static const uint8_t QMC5883L_REGISTER_DATA_Y_MSB = 0x03;
16 static const uint8_t QMC5883L_REGISTER_DATA_Z_LSB = 0x04;
17 static const uint8_t QMC5883L_REGISTER_DATA_Z_MSB = 0x05;
18 static const uint8_t QMC5883L_REGISTER_STATUS = 0x06;
19 static const uint8_t QMC5883L_REGISTER_TEMPERATURE_LSB = 0x07;
20 static const uint8_t QMC5883L_REGISTER_TEMPERATURE_MSB = 0x08;
21 static const uint8_t QMC5883L_REGISTER_CONTROL_1 = 0x09;
22 static const uint8_t QMC5883L_REGISTER_CONTROL_2 = 0x0A;
23 static const uint8_t QMC5883L_REGISTER_PERIOD = 0x0B;
26 ESP_LOGCONFIG(TAG,
"Setting up QMC5883L...");
28 if (!this->
write_byte(QMC5883L_REGISTER_CONTROL_2, 1 << 7)) {
35 uint8_t control_1 = 0;
36 control_1 |= 0b01 << 0;
38 control_1 |= this->
range_ << 4;
40 if (!this->
write_byte(QMC5883L_REGISTER_CONTROL_1, control_1)) {
46 uint8_t control_2 = 0;
47 control_2 |= 0b0 << 7;
48 control_2 |= 0b0 << 6;
49 control_2 |= 0b0 << 0;
50 if (!this->
write_byte(QMC5883L_REGISTER_CONTROL_2, control_2)) {
56 uint8_t period = 0x01;
57 if (!this->
write_byte(QMC5883L_REGISTER_PERIOD, period)) {
64 ESP_LOGCONFIG(TAG,
"QMC5883L:");
67 ESP_LOGE(TAG,
"Communication with QMC5883L failed!");
69 LOG_UPDATE_INTERVAL(
this);
71 LOG_SENSOR(
" ",
"X Axis", this->
x_sensor_);
72 LOG_SENSOR(
" ",
"Y Axis", this->
y_sensor_);
73 LOG_SENSOR(
" ",
"Z Axis", this->
z_sensor_);
79 this->
read_byte(QMC5883L_REGISTER_STATUS, &status);
81 uint16_t raw_x, raw_y, raw_z;
82 if (!this->
read_byte_16_(QMC5883L_REGISTER_DATA_X_LSB, &raw_x) ||
102 const float x = int16_t(raw_x) * mg_per_bit * 0.1f;
103 const float y = int16_t(raw_y) * mg_per_bit * 0.1f;
104 const float z = int16_t(raw_z) * mg_per_bit * 0.1f;
106 float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
107 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);
122 *data = (*data & 0x00FF) << 8 | (*data & 0xFF00) >> 8;
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
bool read_byte_16(uint8_t a_register, uint16_t *data)
const float DATA
For components that import data from directly connected sensors like DHT.
sensor::Sensor * heading_sensor_
sensor::Sensor * x_sensor_
enum esphome::qmc5883l::QMC5883LComponent::ErrorCode error_code_
float get_setup_priority() const override
sensor::Sensor * z_sensor_
bool read_byte_16_(uint8_t a_register, uint16_t *data)
void publish_state(float state)
Publish a new state to the front-end.
void status_set_warning()
void dump_config() override
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
virtual void mark_failed()
Mark this component as failed.
QMC5883LDatarate datarate_
QMC5883LOversampling oversampling_
sensor::Sensor * y_sensor_
void IRAM_ATTR HOT delay(uint32_t ms)