9static const uint8_t MLX90614_RAW_IR_1 = 0x04;
10static const uint8_t MLX90614_RAW_IR_2 = 0x05;
11static const uint8_t MLX90614_TEMPERATURE_AMBIENT = 0x06;
12static const uint8_t MLX90614_TEMPERATURE_OBJECT_1 = 0x07;
13static const uint8_t MLX90614_TEMPERATURE_OBJECT_2 = 0x08;
15static const uint8_t MLX90614_TOMAX = 0x20;
16static const uint8_t MLX90614_TOMIN = 0x21;
17static const uint8_t MLX90614_PWMCTRL = 0x22;
18static const uint8_t MLX90614_TARANGE = 0x23;
19static const uint8_t MLX90614_EMISSIVITY = 0x24;
20static const uint8_t MLX90614_CONFIG = 0x25;
21static const uint8_t MLX90614_ADDR = 0x2E;
22static const uint8_t MLX90614_ID1 = 0x3C;
23static const uint8_t MLX90614_ID2 = 0x3D;
24static const uint8_t MLX90614_ID3 = 0x3E;
25static const uint8_t MLX90614_ID4 = 0x3F;
30static constexpr uint8_t EMISSIVITY_WRITE_ATTEMPTS = 3;
33static uint8_t crc8_pec(
const uint8_t *data, uint8_t
len) {
return crc8(data,
len, 0x00, 0x07,
true); }
55 ESP_LOGE(TAG,
"Giving up on writing emissivity after %u attempts", EMISSIVITY_WRITE_ATTEMPTS);
62 uint16_t current_emissivity;
67 const auto desired_emissivity =
static_cast<uint16_t
>(this->
emissivity_ * 0xFFFF);
68 if (current_emissivity == desired_emissivity) {
84 buf[4] = crc8_pec(buf, 4);
87 ESP_LOGW(TAG,
"Can't erase register 0x%02X, error %d",
reg, ec);
98 buf[4] = crc8_pec(buf, 4);
101 ESP_LOGW(TAG,
"Can't write register 0x%02X, error %d",
reg, ec);
112 ESP_LOGW(TAG,
"Can't check register 0x%02X value, error %d",
reg, ec);
116 if (read_back != data) {
117 ESP_LOGW(TAG,
"Read back mismatch on register 0x%02X. Expected 0x%04X, got 0x%04X",
reg, data, read_back);
129 buf[2] = (this->
address_ << 1) | 0x01;
133 ESP_LOGW(TAG,
"i2c read error %d", ec);
137 const auto expected_pec = crc8_pec(buf, 5);
138 if (buf[5] != expected_pec) {
139 ESP_LOGW(TAG,
"i2c CRC error. Expected 0x%02X, got 0x%02X", expected_pec, buf[5]);
148 ESP_LOGCONFIG(TAG,
"MLX90614:");
149 LOG_I2C_DEVICE(
this);
151 ESP_LOGW(TAG,
" Emissivity not written yet, will retry");
153 LOG_UPDATE_INTERVAL(
this);
164 if (sensor ==
nullptr) {
182 const auto object_ec = publish_sensor(this->
object_sensor_, MLX90614_TEMPERATURE_OBJECT_1);
183 const auto ambient_ec = publish_sensor(this->
ambient_sensor_, MLX90614_TEMPERATURE_AMBIENT);
void status_set_warning()
void status_clear_warning()
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
uint8_t address_
store the address of the device on the bus
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
bool write_register_(uint8_t reg, uint16_t data)
uint8_t emissivity_write_attempts_
void dump_config() override
bool emissivity_write_failed_
sensor::Sensor * object_sensor_
void try_write_emissivity_()
i2c::ErrorCode read_register_(uint8_t reg, uint16_t &data)
sensor::Sensor * ambient_sensor_
Base-class for all sensors.
void publish_state(float state)
Publish a new state to the front-end.
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
@ ERROR_CRC
bytes received with a CRC error
@ ERROR_OK
No error found during execution of method.
ESPHOME_LOG_TAG(TAG, "mlx90614")
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
void HOT delay(uint32_t ms)