10static constexpr uint32_t VERIFY_SETTINGS_TIMEOUT_ID = 0;
15 return LOG_STR(
"gain");
17 return LOG_STR(
"resolution");
19 return LOG_STR(
"oversampling");
21 return LOG_STR(
"digital filtering");
23 return LOG_STR(
"temperature oversampling");
25 return LOG_STR(
"temperature compensation");
27 return LOG_STR(
"hallconf");
29 return LOG_STR(
"error");
31 return LOG_STR(
"unknown");
35bool MLX90393Cls::transceive(
const uint8_t *request,
size_t request_size, uint8_t *response,
size_t response_size) {
38 ESP_LOGV(TAG,
"i2c failed to write %u", e);
41 e = this->
read(response, response_size);
43 ESP_LOGV(TAG,
"i2c failed to read %u", e);
49bool MLX90393Cls::has_drdy_pin() {
return this->
drdy_pin_ !=
nullptr; }
51bool MLX90393Cls::read_drdy_pin() {
74 ret = this->
mlx_.setDigitalFiltering(this->
filter_);
88 if (ret != MLX90393::STATUS_OK) {
98 this->
mlx_.getGainSel(ignore);
100 uint8_t result = MLX90393::STATUS_OK;
105 return result == MLX90393::STATUS_OK;
112 this->
mlx_.begin_with_hal(
this, 0, 0);
122void MLX90393Cls::dump_config() {
123 ESP_LOGCONFIG(TAG,
"MLX90393:");
124 LOG_I2C_DEVICE(
this);
128 LOG_UPDATE_INTERVAL(
this);
130 LOG_SENSOR(
" ",
"X Axis", this->x_sensor_);
131 LOG_SENSOR(
" ",
"Y Axis", this->y_sensor_);
132 LOG_SENSOR(
" ",
"Z Axis", this->z_sensor_);
133 LOG_SENSOR(
" ",
"Temperature", this->t_sensor_);
136void MLX90393Cls::update() {
139 if (this->
mlx_.readData(data) == MLX90393::STATUS_OK) {
140 ESP_LOGD(TAG,
"received %f %f %f",
data.x,
data.y,
data.z);
141 PUBLISH_STATE(this->x_sensor_,
data.x);
142 PUBLISH_STATE(this->y_sensor_,
data.y);
143 PUBLISH_STATE(this->z_sensor_,
data.z);
144 PUBLISH_STATE(this->t_sensor_,
data.t);
147 ESP_LOGE(TAG,
"failed to read data");
153 uint8_t read_value = 0xFF;
154 uint8_t expected_value = 0xFF;
155 uint8_t read_status = -1;
156 char read_back_str[33] = {0};
160 read_status = this->
mlx_.getGainSel(read_value);
161 expected_value = this->
gain_;
166 uint8_t read_resolutions[3] = {0xFF};
167 read_status = this->
mlx_.getResolution(read_resolutions[0], read_resolutions[1], read_resolutions[2]);
168 snprintf(read_back_str,
sizeof(read_back_str),
"%u %u %u expected %u %u %u", read_resolutions[0],
171 bool is_correct =
true;
172 for (
int i = 0; i < 3; i++) {
173 is_correct &= read_resolutions[i] == this->
resolutions_[i];
187 read_status = this->
mlx_.getOverSampling(read_value);
192 read_status = this->
mlx_.getDigitalFiltering(read_value);
193 expected_value = this->
filter_;
197 read_status = this->
mlx_.getTemperatureOverSampling(read_value);
202 read_status = this->
mlx_.getTemperatureCompensation(read_value);
207 read_status = this->
mlx_.getHallConf(read_value);
215 if (read_status != MLX90393::STATUS_OK) {
216 ESP_LOGE(TAG,
"verify error: failed to read %s", LOG_STR_ARG(
settings_to_string(which)));
219 if (read_back_str[0] == 0x0) {
220 snprintf(read_back_str,
sizeof(read_back_str),
"%u expected %u", read_value, expected_value);
222 bool is_correct = read_value == expected_value;
224 ESP_LOGW(TAG,
"verify failed: read back wrong %s: got %s", LOG_STR_ARG(
settings_to_string(which)), read_back_str);
227 ESP_LOGD(TAG,
"verify succeeded for %s. got %s", LOG_STR_ARG(
settings_to_string(which)), read_back_str);
241 if (!is_setting_ok) {
242 if (this->
mlx_.checkStatus(this->mlx_.reset()) != MLX90393::STATUS_OK) {
243 this->
mark_failed(LOG_STR(
"Failed to reset device"));
248 this->
mark_failed(LOG_STR(
"Failed to re-apply settings"));
250 ESP_LOGI(TAG,
"reset and re-apply settings completed");
259 this->
set_timeout(VERIFY_SETTINGS_TIMEOUT_ID, 3000,
void mark_failed()
Mark this component as failed.
virtual void setup()
Where the component's initialization should happen.
void status_set_warning()
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
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
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
uint8_t temperature_oversampling_
bool apply_all_settings_()
void verify_settings_timeout_(MLX90393Setting stage)
Regularly checks that our settings are still applied.
bool verify_setting_(MLX90393Setting which)
bool temperature_compensation_
uint8_t apply_setting_(MLX90393Setting which)
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_OK
No error found during execution of method.
@ MLX90393_TEMPERATURE_OVER_SAMPLING
@ MLX90393_DIGITAL_FILTERING
@ MLX90393_TEMPERATURE_COMPENSATION
ESPHOME_LOG_TAG(TAG, "mlx90393")
const LogString * settings_to_string(MLX90393Setting setting)
std::span< const uint8_t > data
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
uint32_t IRAM_ATTR HOT micros()
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()