ESPHome  2024.11.0
hmc5883l.cpp
Go to the documentation of this file.
1 #include "hmc5883l.h"
2 #include "esphome/core/log.h"
4 
5 namespace esphome {
6 namespace hmc5883l {
7 
8 static const char *const TAG = "hmc5883l";
9 static const uint8_t HMC5883L_ADDRESS = 0x1E;
10 static const uint8_t HMC5883L_REGISTER_CONFIG_A = 0x00;
11 static const uint8_t HMC5883L_REGISTER_CONFIG_B = 0x01;
12 static const uint8_t HMC5883L_REGISTER_MODE = 0x02;
13 static const uint8_t HMC5883L_REGISTER_DATA_X_MSB = 0x03;
14 static const uint8_t HMC5883L_REGISTER_DATA_X_LSB = 0x04;
15 static const uint8_t HMC5883L_REGISTER_DATA_Z_MSB = 0x05;
16 static const uint8_t HMC5883L_REGISTER_DATA_Z_LSB = 0x06;
17 static const uint8_t HMC5883L_REGISTER_DATA_Y_MSB = 0x07;
18 static const uint8_t HMC5883L_REGISTER_DATA_Y_LSB = 0x08;
19 static const uint8_t HMC5883L_REGISTER_STATUS = 0x09;
20 static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_A = 0x0A;
21 static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_B = 0x0B;
22 static const uint8_t HMC5883L_REGISTER_IDENTIFICATION_C = 0x0C;
23 
25  ESP_LOGCONFIG(TAG, "Setting up HMC5583L...");
26  uint8_t id[3];
27  if (!this->read_byte(HMC5883L_REGISTER_IDENTIFICATION_A, &id[0]) ||
28  !this->read_byte(HMC5883L_REGISTER_IDENTIFICATION_B, &id[1]) ||
29  !this->read_byte(HMC5883L_REGISTER_IDENTIFICATION_C, &id[2])) {
31  this->mark_failed();
32  return;
33  }
34 
35  if (this->get_update_interval() < App.get_loop_interval()) {
36  high_freq_.start();
37  }
38 
39  if (id[0] != 0x48 || id[1] != 0x34 || id[2] != 0x33) {
40  this->error_code_ = ID_REGISTERS;
41  this->mark_failed();
42  return;
43  }
44 
45  uint8_t config_a = 0;
46  config_a |= this->oversampling_ << 5;
47  config_a |= this->datarate_ << 2;
48  config_a |= 0b0 << 0; // Measurement Mode: Normal(high impedance on load)
49  if (!this->write_byte(HMC5883L_REGISTER_CONFIG_A, config_a)) {
51  this->mark_failed();
52  return;
53  }
54 
55  uint8_t config_b = 0;
56  config_b |= this->range_ << 5;
57  if (!this->write_byte(HMC5883L_REGISTER_CONFIG_B, config_b)) {
59  this->mark_failed();
60  return;
61  }
62 
63  uint8_t mode = 0;
64  // Continuous Measurement Mode
65  mode |= 0b00;
66  if (!this->write_byte(HMC5883L_REGISTER_MODE, mode)) {
68  this->mark_failed();
69  return;
70  }
71 }
73  ESP_LOGCONFIG(TAG, "HMC5883L:");
74  LOG_I2C_DEVICE(this);
75  if (this->error_code_ == COMMUNICATION_FAILED) {
76  ESP_LOGE(TAG, "Communication with HMC5883L failed!");
77  } else if (this->error_code_ == ID_REGISTERS) {
78  ESP_LOGE(TAG, "The ID registers don't match - Is this really an HMC5883L?");
79  }
80  LOG_UPDATE_INTERVAL(this);
81 
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 }
89  uint16_t raw_x, raw_y, raw_z;
90  if (!this->read_byte_16(HMC5883L_REGISTER_DATA_X_MSB, &raw_x) ||
91  !this->read_byte_16(HMC5883L_REGISTER_DATA_Y_MSB, &raw_y) ||
92  !this->read_byte_16(HMC5883L_REGISTER_DATA_Z_MSB, &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.073f;
101  break;
103  mg_per_bit = 0.92f;
104  break;
106  mg_per_bit = 1.22f;
107  break;
109  mg_per_bit = 1.52f;
110  break;
112  mg_per_bit = 2.27f;
113  break;
115  mg_per_bit = 2.56f;
116  break;
118  mg_per_bit = 3.03f;
119  break;
121  mg_per_bit = 4.35f;
122  break;
123  default:
124  mg_per_bit = NAN;
125  }
126 
127  // in µT
128  const float x = int16_t(raw_x) * mg_per_bit * 0.1f;
129  const float y = int16_t(raw_y) * mg_per_bit * 0.1f;
130  const float z = int16_t(raw_z) * mg_per_bit * 0.1f;
131 
132  float heading = atan2f(0.0f - x, y) * 180.0f / M_PI;
133  ESP_LOGD(TAG, "Got x=%0.02fµT y=%0.02fµT z=%0.02fµT heading=%0.01f°", x, y, z, heading);
134 
135  if (this->x_sensor_ != nullptr)
136  this->x_sensor_->publish_state(x);
137  if (this->y_sensor_ != nullptr)
138  this->y_sensor_->publish_state(y);
139  if (this->z_sensor_ != nullptr)
140  this->z_sensor_->publish_state(z);
141  if (this->heading_sensor_ != nullptr)
142  this->heading_sensor_->publish_state(heading);
143 }
144 
145 } // namespace hmc5883l
146 } // 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: hmc5883l.h:60
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: hmc5883l.h:66
void start()
Start running the loop continuously.
Definition: helpers.cpp:653
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
HMC5883LOversampling oversampling_
Definition: hmc5883l.h:54
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
virtual uint32_t get_update_interval() const
Get the update interval in ms of this sensor.
Definition: component.cpp:228
enum esphome::hmc5883l::HMC5883LComponent::ErrorCode error_code_
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
float get_setup_priority() const override
Definition: hmc5883l.cpp:87