ESPHome  2024.11.1
sensor_mlx90393.cpp
Go to the documentation of this file.
1 #include "sensor_mlx90393.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace mlx90393 {
6 
7 static const char *const TAG = "mlx90393";
8 
9 bool MLX90393Cls::transceive(const uint8_t *request, size_t request_size, uint8_t *response, size_t response_size) {
10  i2c::ErrorCode e = this->write(request, request_size);
11  if (e != i2c::ErrorCode::ERROR_OK) {
12  return false;
13  }
14  e = this->read(response, response_size);
15  return e == i2c::ErrorCode::ERROR_OK;
16 }
17 
18 bool MLX90393Cls::has_drdy_pin() { return this->drdy_pin_ != nullptr; }
19 
21  if (this->drdy_pin_ == nullptr) {
22  return false;
23  } else {
24  return this->drdy_pin_->digital_read();
25  }
26 }
27 void MLX90393Cls::sleep_millis(uint32_t millis) { delay(millis); }
29 
31  ESP_LOGCONFIG(TAG, "Setting up MLX90393...");
32  // note the two arguments A0 and A1 which are used to construct an i2c address
33  // we can hard-code these because we never actually use the constructed address
34  // see the transceive function above, which uses the address from I2CComponent
35  this->mlx_.begin_with_hal(this, 0, 0);
36 
37  this->mlx_.setGainSel(this->gain_);
38 
39  this->mlx_.setResolution(this->resolutions_[0], this->resolutions_[1], this->resolutions_[2]);
40 
41  this->mlx_.setOverSampling(this->oversampling_);
42 
43  this->mlx_.setDigitalFiltering(this->filter_);
44 
45  this->mlx_.setTemperatureOverSampling(this->temperature_oversampling_);
46 }
47 
49  ESP_LOGCONFIG(TAG, "MLX90393:");
50  LOG_I2C_DEVICE(this);
51 
52  if (this->is_failed()) {
53  ESP_LOGE(TAG, "Communication with MLX90393 failed!");
54  return;
55  }
56  LOG_UPDATE_INTERVAL(this);
57 
58  LOG_SENSOR(" ", "X Axis", this->x_sensor_);
59  LOG_SENSOR(" ", "Y Axis", this->y_sensor_);
60  LOG_SENSOR(" ", "Z Axis", this->z_sensor_);
61  LOG_SENSOR(" ", "Temperature", this->t_sensor_);
62 }
63 
65 
67  MLX90393::txyz data;
68 
69  if (this->mlx_.readData(data) == MLX90393::STATUS_OK) {
70  ESP_LOGD(TAG, "received %f %f %f", data.x, data.y, data.z);
71  if (this->x_sensor_ != nullptr) {
72  this->x_sensor_->publish_state(data.x);
73  }
74  if (this->y_sensor_ != nullptr) {
75  this->y_sensor_->publish_state(data.y);
76  }
77  if (this->z_sensor_ != nullptr) {
78  this->z_sensor_->publish_state(data.z);
79  }
80  if (this->t_sensor_ != nullptr) {
81  this->t_sensor_->publish_state(data.t);
82  }
83  this->status_clear_warning();
84  } else {
85  ESP_LOGE(TAG, "failed to read data");
86  this->status_set_warning();
87  }
88 }
89 
90 } // namespace mlx90393
91 } // namespace esphome
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool is_failed() const
Definition: component.cpp:143
ErrorCode read(uint8_t *data, size_t len)
reads an array of bytes from the device using an I2CBus
Definition: i2c.h:160
uint32_t IRAM_ATTR HOT micros()
Definition: core.cpp:27
void sleep_millis(uint32_t millis) override
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
ErrorCode write(const uint8_t *data, size_t len, bool stop=true)
writes an array of bytes to a device using an I2CBus
Definition: i2c.h:186
float get_setup_priority() const override
void sleep_micros(uint32_t micros) override
bool transceive(const uint8_t *request, size_t request_size, uint8_t *response, size_t response_size) override
void status_clear_warning()
Definition: component.cpp:166
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
virtual bool digital_read()=0
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26