ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
sensor_mlx90393.cpp
Go to the documentation of this file.
1#include "sensor_mlx90393.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace mlx90393 {
6
7static const char *const TAG = "mlx90393";
8
9const LogString *settings_to_string(MLX90393Setting setting) {
10 switch (setting) {
12 return LOG_STR("gain");
14 return LOG_STR("resolution");
16 return LOG_STR("oversampling");
18 return LOG_STR("digital filtering");
20 return LOG_STR("temperature oversampling");
22 return LOG_STR("temperature compensation");
24 return LOG_STR("hallconf");
25 case MLX90393_LAST:
26 return LOG_STR("error");
27 default:
28 return LOG_STR("unknown");
29 }
30};
31
32bool MLX90393Cls::transceive(const uint8_t *request, size_t request_size, uint8_t *response, size_t response_size) {
33 i2c::ErrorCode e = this->write(request, request_size);
34 if (e != i2c::ErrorCode::ERROR_OK) {
35 ESP_LOGV(TAG, "i2c failed to write %u", e);
36 return false;
37 }
38 e = this->read(response, response_size);
39 if (e != i2c::ErrorCode::ERROR_OK) {
40 ESP_LOGV(TAG, "i2c failed to read %u", e);
41 return false;
42 }
43 return true;
44}
45
46bool MLX90393Cls::has_drdy_pin() { return this->drdy_pin_ != nullptr; }
47
49 if (this->drdy_pin_ == nullptr) {
50 return false;
51 } else {
52 return this->drdy_pin_->digital_read();
53 }
54}
57
59 uint8_t ret = -1;
60 switch (which) {
62 ret = this->mlx_.setGainSel(this->gain_);
63 break;
65 ret = this->mlx_.setResolution(this->resolutions_[0], this->resolutions_[1], this->resolutions_[2]);
66 break;
68 ret = this->mlx_.setOverSampling(this->oversampling_);
69 break;
71 ret = this->mlx_.setDigitalFiltering(this->filter_);
72 break;
74 ret = this->mlx_.setTemperatureOverSampling(this->temperature_oversampling_);
75 break;
77 ret = this->mlx_.setTemperatureCompensation(this->temperature_compensation_);
78 break;
80 ret = this->mlx_.setHallConf(this->hallconf_);
81 break;
82 default:
83 break;
84 }
85 if (ret != MLX90393::STATUS_OK) {
86 ESP_LOGE(TAG, "failed to apply %s", LOG_STR_ARG(settings_to_string(which)));
87 }
88 return ret;
89}
90
92 // perform dummy read after reset
93 // first one always gets NAK even tough everything is fine
94 uint8_t ignore = 0;
95 this->mlx_.getGainSel(ignore);
96
97 uint8_t result = MLX90393::STATUS_OK;
98 for (int i = MLX90393_GAIN_SEL; i != MLX90393_LAST; i++) {
99 MLX90393Setting stage = static_cast<MLX90393Setting>(i);
100 result |= this->apply_setting_(stage);
101 }
102 return result == MLX90393::STATUS_OK;
103}
104
106 // note the two arguments A0 and A1 which are used to construct an i2c address
107 // we can hard-code these because we never actually use the constructed address
108 // see the transceive function above, which uses the address from I2CComponent
109 this->mlx_.begin_with_hal(this, 0, 0);
110
111 if (!this->apply_all_settings_()) {
112 this->mark_failed();
113 }
114
115 // start verify settings process
116 this->set_timeout("verify settings", 3000, [this]() { this->verify_settings_timeout_(MLX90393_GAIN_SEL); });
117}
118
120 ESP_LOGCONFIG(TAG, "MLX90393:");
121 LOG_I2C_DEVICE(this);
122
123 if (this->is_failed()) {
124 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
125 return;
126 }
127 LOG_UPDATE_INTERVAL(this);
128
129 LOG_SENSOR(" ", "X Axis", this->x_sensor_);
130 LOG_SENSOR(" ", "Y Axis", this->y_sensor_);
131 LOG_SENSOR(" ", "Z Axis", this->z_sensor_);
132 LOG_SENSOR(" ", "Temperature", this->t_sensor_);
133}
134
136 MLX90393::txyz data;
137
138 if (this->mlx_.readData(data) == MLX90393::STATUS_OK) {
139 ESP_LOGD(TAG, "received %f %f %f", data.x, data.y, data.z);
140 if (this->x_sensor_ != nullptr) {
141 this->x_sensor_->publish_state(data.x);
142 }
143 if (this->y_sensor_ != nullptr) {
144 this->y_sensor_->publish_state(data.y);
145 }
146 if (this->z_sensor_ != nullptr) {
147 this->z_sensor_->publish_state(data.z);
148 }
149 if (this->t_sensor_ != nullptr) {
150 this->t_sensor_->publish_state(data.t);
151 }
152 this->status_clear_warning();
153 } else {
154 ESP_LOGE(TAG, "failed to read data");
155 this->status_set_warning();
156 }
157}
158
160 uint8_t read_value = 0xFF;
161 uint8_t expected_value = 0xFF;
162 uint8_t read_status = -1;
163 char read_back_str[25] = {0};
164
165 switch (which) {
166 case MLX90393_GAIN_SEL: {
167 read_status = this->mlx_.getGainSel(read_value);
168 expected_value = this->gain_;
169 break;
170 }
171
172 case MLX90393_RESOLUTION: {
173 uint8_t read_resolutions[3] = {0xFF};
174 read_status = this->mlx_.getResolution(read_resolutions[0], read_resolutions[1], read_resolutions[2]);
175 snprintf(read_back_str, sizeof(read_back_str), "%u %u %u expected %u %u %u", read_resolutions[0],
176 read_resolutions[1], read_resolutions[2], this->resolutions_[0], this->resolutions_[1],
177 this->resolutions_[2]);
178 bool is_correct = true;
179 for (int i = 0; i < 3; i++) {
180 is_correct &= read_resolutions[i] == this->resolutions_[i];
181 }
182 if (is_correct) {
183 // set read_value and expected_value to same number, so the code blow recognizes it is correct
184 read_value = 0;
185 expected_value = 0;
186 } else {
187 // set to different numbers, to show incorrect
188 read_value = 1;
189 expected_value = 0;
190 }
191 break;
192 }
194 read_status = this->mlx_.getOverSampling(read_value);
195 expected_value = this->oversampling_;
196 break;
197 }
199 read_status = this->mlx_.getDigitalFiltering(read_value);
200 expected_value = this->filter_;
201 break;
202 }
204 read_status = this->mlx_.getTemperatureOverSampling(read_value);
205 expected_value = this->temperature_oversampling_;
206 break;
207 }
209 read_status = this->mlx_.getTemperatureCompensation(read_value);
210 expected_value = (bool) this->temperature_compensation_;
211 break;
212 }
213 case MLX90393_HALLCONF: {
214 read_status = this->mlx_.getHallConf(read_value);
215 expected_value = this->hallconf_;
216 break;
217 }
218 default: {
219 return false;
220 }
221 }
222 if (read_status != MLX90393::STATUS_OK) {
223 ESP_LOGE(TAG, "verify error: failed to read %s", LOG_STR_ARG(settings_to_string(which)));
224 return false;
225 }
226 if (read_back_str[0] == 0x0) {
227 snprintf(read_back_str, sizeof(read_back_str), "%u expected %u", read_value, expected_value);
228 }
229 bool is_correct = read_value == expected_value;
230 if (!is_correct) {
231 ESP_LOGW(TAG, "verify failed: read back wrong %s: got %s", LOG_STR_ARG(settings_to_string(which)), read_back_str);
232 return false;
233 }
234 ESP_LOGD(TAG, "verify succeeded for %s. got %s", LOG_STR_ARG(settings_to_string(which)), read_back_str);
235 return true;
236}
237
246 bool is_setting_ok = this->verify_setting_(stage);
247
248 if (!is_setting_ok) {
249 if (this->mlx_.checkStatus(this->mlx_.reset()) != MLX90393::STATUS_OK) {
250 ESP_LOGE(TAG, "failed to reset device");
251 this->status_set_error();
252 this->mark_failed();
253 return;
254 }
255
256 if (!this->apply_all_settings_()) {
257 ESP_LOGE(TAG, "failed to re-apply settings");
258 this->status_set_error();
259 this->mark_failed();
260 } else {
261 ESP_LOGI(TAG, "reset and re-apply settings completed");
262 }
263 }
264
265 MLX90393Setting next_stage = static_cast<MLX90393Setting>(static_cast<int>(stage) + 1);
266 if (next_stage == MLX90393_LAST) {
267 next_stage = static_cast<MLX90393Setting>(0);
268 }
269
270 this->set_timeout("verify settings", 3000, [this, next_stage]() { this->verify_settings_timeout_(next_stage); });
271}
272
273} // namespace mlx90393
274} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:443
void status_clear_warning()
virtual bool digital_read()=0
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:163
void sleep_micros(uint32_t micros) override
void sleep_millis(uint32_t millis) override
void verify_settings_timeout_(MLX90393Setting stage)
Regularly checks that our settings are still applied.
bool verify_setting_(MLX90393Setting which)
bool transceive(const uint8_t *request, size_t request_size, uint8_t *response, size_t response_size) override
uint8_t apply_setting_(MLX90393Setting which)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:12
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14
const LogString * settings_to_string(MLX90393Setting setting)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:29
uint32_t IRAM_ATTR HOT micros()
Definition core.cpp:28
void HOT delay(uint32_t ms)
Definition core.cpp:27
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:25