ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
tc74.cpp
Go to the documentation of this file.
1// Based on the TC74 datasheet https://ww1.microchip.com/downloads/en/DeviceDoc/21462D.pdf
2
3#include "tc74.h"
4#include "esphome/core/log.h"
5
6namespace esphome::tc74 {
7
8ESPHOME_LOG_TAG(TAG, "tc74");
9
10static const uint8_t TC74_REGISTER_TEMPERATURE = 0x00;
11static const uint8_t TC74_REGISTER_CONFIGURATION = 0x01;
12static const uint8_t TC74_DATA_READY_MASK = 0x40;
13
14// It is possible the "Data Ready" bit will not be set if the TC74 has not been powered on for at least 250ms, so it not
15// being set does not constitute a failure.
17 uint8_t config_reg;
18 if (this->read_register(TC74_REGISTER_CONFIGURATION, &config_reg, 1) != i2c::ERROR_OK) {
19 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
20 return;
21 }
22 this->data_ready_ = config_reg & TC74_DATA_READY_MASK;
23}
24
26
28 LOG_SENSOR("", "TC74", this);
29 LOG_I2C_DEVICE(this);
30 LOG_UPDATE_INTERVAL(this);
31}
32
34 if (!this->data_ready_) {
35 uint8_t config_reg;
36 if (this->read_register(TC74_REGISTER_CONFIGURATION, &config_reg, 1) != i2c::ERROR_OK) {
37 this->status_set_warning();
38 return;
39 }
40
41 if (config_reg & TC74_DATA_READY_MASK) {
42 this->data_ready_ = true;
43 } else {
44 ESP_LOGD(TAG, "TC74 not ready");
45 return;
46 }
47 }
48
49 int8_t temperature_reg;
50 if (this->read_register(TC74_REGISTER_TEMPERATURE, reinterpret_cast<uint8_t *>(&temperature_reg), 1) !=
52 this->status_set_warning();
53 return;
54 }
55
56 ESP_LOGD(TAG, "Got Temperature=%d °C", temperature_reg);
57 this->publish_state(temperature_reg);
59}
60
61} // namespace esphome::tc74
void mark_failed()
Mark this component as failed.
void status_clear_warning()
Definition component.h:296
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
Definition i2c.cpp:28
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
void read_temperature_()
Internal method to read the temperature from the component after it has been scheduled.
Definition tc74.cpp:33
void update() override
Update the sensor value (temperature).
Definition tc74.cpp:25
void setup() override
Setup the sensor and check connection.
Definition tc74.cpp:16
void dump_config() override
Definition tc74.cpp:27
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
ESPHOME_LOG_TAG(TAG, "tc74")