ESPHome  2024.7.2
qmc5883l.cpp
Go to the documentation of this file.
1 #include "qmc5883l.h"
3 #include "esphome/core/log.h"
4 #include "esphome/core/hal.h"
5 #include <cmath>
6 
7 namespace esphome {
8 namespace qmc5883l {
9 
10 static const char *const TAG = "qmc5883l";
11 static const uint8_t QMC5883L_ADDRESS = 0x0D;
12 
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;
25 
27  ESP_LOGCONFIG(TAG, "Setting up QMC5883L...");
28  // Soft Reset
29  if (!this->write_byte(QMC5883L_REGISTER_CONTROL_2, 1 << 7)) {
31  this->mark_failed();
32  return;
33  }
34  delay(10);
35 
36  uint8_t control_1 = 0;
37  control_1 |= 0b01 << 0; // MODE (Mode) -> 0b00=standby, 0b01=continuous
38  control_1 |= this->datarate_ << 2;
39  control_1 |= this->range_ << 4;
40  control_1 |= this->oversampling_ << 6;
41  if (!this->write_byte(QMC5883L_REGISTER_CONTROL_1, control_1)) {
43  this->mark_failed();
44  return;
45  }
46 
47  uint8_t control_2 = 0;
48  control_2 |= 0b0 << 7; // SOFT_RST (Soft Reset) -> 0b00=disabled, 0b01=enabled
49  control_2 |= 0b0 << 6; // ROL_PNT (Pointer Roll Over) -> 0b00=disabled, 0b01=enabled
50  control_2 |= 0b0 << 0; // INT_ENB (Interrupt) -> 0b00=disabled, 0b01=enabled
51  if (!this->write_byte(QMC5883L_REGISTER_CONTROL_2, control_2)) {
53  this->mark_failed();
54  return;
55  }
56 
57  uint8_t period = 0x01; // recommended value
58  if (!this->write_byte(QMC5883L_REGISTER_PERIOD, period)) {
60  this->mark_failed();
61  return;
62  }
63 
64  if (this->get_update_interval() < App.get_loop_interval()) {
65  high_freq_.start();
66  }
67 }
69  ESP_LOGCONFIG(TAG, "QMC5883L:");
70  LOG_I2C_DEVICE(this);
71  if (this->error_code_ == COMMUNICATION_FAILED) {
72  ESP_LOGE(TAG, "Communication with QMC5883L failed!");
73  }
74  LOG_UPDATE_INTERVAL(this);
75 
76  LOG_SENSOR(" ", "X Axis", this->x_sensor_);
77  LOG_SENSOR(" ", "Y Axis", this->y_sensor_);
78  LOG_SENSOR(" ", "Z Axis", this->z_sensor_);
79  LOG_SENSOR(" ", "Heading", this->heading_sensor_);
80  LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
81 }
84  uint8_t status = false;
85  this->read_byte(QMC5883L_REGISTER_STATUS, &status);
86 
87  // Always request X,Y,Z regardless if there are sensors for them
88  // to avoid https://github.com/esphome/issues/issues/5731
89  uint16_t raw_x, raw_y, raw_z;
90  if (!this->read_byte_16_(QMC5883L_REGISTER_DATA_X_LSB, &raw_x) ||
91  !this->read_byte_16_(QMC5883L_REGISTER_DATA_Y_LSB, &raw_y) ||
92  !this->read_byte_16_(QMC5883L_REGISTER_DATA_Z_LSB, &raw_z)) {
93  this->status_set_warning();
94  return;
95  }
96 
97  float mg_per_bit;
98  switch (this->range_) {
100  mg_per_bit = 0.0833f;
101  break;
103  mg_per_bit = 0.333f;
104  break;
105  default:
106  mg_per_bit = NAN;
107  }
108 
109  // in µT
110  const float x = int16_t(raw_x) * mg_per_bit * 0.1f;
111  const float y = int16_t(raw_y) * mg_per_bit * 0.1f;
112  const float z = int16_t(raw_z) * mg_per_bit * 0.1f;
113 
114  float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
115 
116  float temp = NAN;
117  if (this->temperature_sensor_ != nullptr) {
118  uint16_t raw_temp;
119  if (!this->read_byte_16_(QMC5883L_REGISTER_TEMPERATURE_LSB, &raw_temp)) {
120  this->status_set_warning();
121  return;
122  }
123  temp = int16_t(raw_temp) * 0.01f;
124  }
125 
126  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,
127  temp, status);
128 
129  if (this->x_sensor_ != nullptr)
130  this->x_sensor_->publish_state(x);
131  if (this->y_sensor_ != nullptr)
132  this->y_sensor_->publish_state(y);
133  if (this->z_sensor_ != nullptr)
134  this->z_sensor_->publish_state(z);
135  if (this->heading_sensor_ != nullptr)
136  this->heading_sensor_->publish_state(heading);
137  if (this->temperature_sensor_ != nullptr)
138  this->temperature_sensor_->publish_state(temp);
139 }
140 
141 bool QMC5883LComponent::read_byte_16_(uint8_t a_register, uint16_t *data) {
142  if (!this->read_byte_16(a_register, data))
143  return false;
144  *data = (*data & 0x00FF) << 8 | (*data & 0xFF00) >> 8; // Flip Byte order, LSB first;
145  return true;
146 }
147 
148 } // namespace qmc5883l
149 } // namespace esphome
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:246
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
sensor::Sensor * heading_sensor_
Definition: qmc5883l.h:52
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
uint16_t x
Definition: tt21100.cpp:17
uint16_t y
Definition: tt21100.cpp:18
HighFrequencyLoopRequester high_freq_
Definition: qmc5883l.h:59
enum esphome::qmc5883l::QMC5883LComponent::ErrorCode error_code_
float get_setup_priority() const override
Definition: qmc5883l.cpp:82
void start()
Start running the loop continuously.
Definition: helpers.cpp:648
bool read_byte_16_(uint8_t a_register, uint16_t *data)
Definition: qmc5883l.cpp:141
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
Application App
Global storage of Application pointer - only one Application can exist.
uint32_t get_loop_interval() const
Definition: application.h:232
sensor::Sensor * temperature_sensor_
Definition: qmc5883l.h:53
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
Definition: component.cpp:228
u_int8_t raw_temp
uint8_t status
Definition: bl0942.h:23
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
QMC5883LOversampling oversampling_
Definition: qmc5883l.h:48
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26