10 static const char *
const TAG =
"qmc5883l";
11 static const uint8_t QMC5883L_ADDRESS = 0x0D;
13 static const uint8_t QMC5883L_REGISTER_DATA_X_LSB = 0x00;
14 static const uint8_t QMC5883L_REGISTER_DATA_X_MSB = 0x01;
15 static const uint8_t QMC5883L_REGISTER_DATA_Y_LSB = 0x02;
16 static const uint8_t QMC5883L_REGISTER_DATA_Y_MSB = 0x03;
17 static const uint8_t QMC5883L_REGISTER_DATA_Z_LSB = 0x04;
18 static const uint8_t QMC5883L_REGISTER_DATA_Z_MSB = 0x05;
19 static const uint8_t QMC5883L_REGISTER_STATUS = 0x06;
20 static const uint8_t QMC5883L_REGISTER_TEMPERATURE_LSB = 0x07;
21 static const uint8_t QMC5883L_REGISTER_TEMPERATURE_MSB = 0x08;
22 static const uint8_t QMC5883L_REGISTER_CONTROL_1 = 0x09;
23 static const uint8_t QMC5883L_REGISTER_CONTROL_2 = 0x0A;
24 static const uint8_t QMC5883L_REGISTER_PERIOD = 0x0B;
27 ESP_LOGCONFIG(TAG,
"Setting up QMC5883L...");
29 if (!this->
write_byte(QMC5883L_REGISTER_CONTROL_2, 1 << 7)) {
36 uint8_t control_1 = 0;
37 control_1 |= 0b01 << 0;
39 control_1 |= this->
range_ << 4;
41 if (!this->
write_byte(QMC5883L_REGISTER_CONTROL_1, control_1)) {
47 uint8_t control_2 = 0;
48 control_2 |= 0b0 << 7;
49 control_2 |= 0b0 << 6;
50 control_2 |= 0b0 << 0;
51 if (!this->
write_byte(QMC5883L_REGISTER_CONTROL_2, control_2)) {
57 uint8_t period = 0x01;
58 if (!this->
write_byte(QMC5883L_REGISTER_PERIOD, period)) {
69 ESP_LOGCONFIG(TAG,
"QMC5883L:");
72 ESP_LOGE(TAG,
"Communication with QMC5883L failed!");
74 LOG_UPDATE_INTERVAL(
this);
76 LOG_SENSOR(
" ",
"X Axis", this->
x_sensor_);
77 LOG_SENSOR(
" ",
"Y Axis", this->
y_sensor_);
78 LOG_SENSOR(
" ",
"Z Axis", this->
z_sensor_);
91 if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG) {
92 err = this->
read_register(QMC5883L_REGISTER_STATUS, &status, 1);
99 uint16_t
raw[3] = {0};
105 start = QMC5883L_REGISTER_DATA_X_LSB;
108 start = QMC5883L_REGISTER_DATA_Y_LSB;
111 start = QMC5883L_REGISTER_DATA_Z_LSB;
123 mg_per_bit = 0.0833f;
133 const float x = int16_t(raw[0]) * mg_per_bit * 0.1f;
134 const float y = int16_t(raw[1]) * mg_per_bit * 0.1f;
135 const float z = int16_t(raw[2]) * mg_per_bit * 0.1f;
137 float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
147 temp = int16_t(raw_temp) * 0.01f;
150 ESP_LOGD(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,
169 for (
size_t i = 0; i <
len; i++)
const float DATA
For components that import data from directly connected sensors like DHT.
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
sensor::Sensor * heading_sensor_
void status_set_warning(const char *message="unspecified")
sensor::Sensor * x_sensor_
HighFrequencyLoopRequester high_freq_
enum esphome::qmc5883l::QMC5883LComponent::ErrorCode error_code_
float get_setup_priority() const override
void start()
Start running the loop continuously.
No error found during execution of method.
std::string str_sprintf(const char *fmt,...)
sensor::Sensor * z_sensor_
void publish_state(float state)
Publish a new state to the front-end.
Application App
Global storage of Application pointer - only one Application can exist.
i2c::ErrorCode read_bytes_16_le_(uint8_t a_register, uint16_t *data, uint8_t len=1)
uint32_t get_loop_interval() const
sensor::Sensor * temperature_sensor_
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
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.
constexpr14 T convert_little_endian(T val)
Convert a value between host byte order and little endian (least significant byte first) order...
Implementation of SPI Controller mode.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
QMC5883LDatarate datarate_
QMC5883LOversampling oversampling_
sensor::Sensor * y_sensor_
void IRAM_ATTR HOT delay(uint32_t ms)