ESPHome 2026.5.0b1
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
8static const char *const 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();
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 if (this->is_failed()) {
31 ESP_LOGE(TAG, "Connection with TC74 failed!");
32 }
33 LOG_UPDATE_INTERVAL(this);
34}
35
37 if (!this->data_ready_) {
38 uint8_t config_reg;
39 if (this->read_register(TC74_REGISTER_CONFIGURATION, &config_reg, 1) != i2c::ERROR_OK) {
40 this->status_set_warning();
41 return;
42 }
43
44 if (config_reg & TC74_DATA_READY_MASK) {
45 this->data_ready_ = true;
46 } else {
47 ESP_LOGD(TAG, "TC74 not ready");
48 return;
49 }
50 }
51
52 int8_t temperature_reg;
53 if (this->read_register(TC74_REGISTER_TEMPERATURE, reinterpret_cast<uint8_t *>(&temperature_reg), 1) !=
55 this->status_set_warning();
56 return;
57 }
58
59 ESP_LOGD(TAG, "Got Temperature=%d °C", temperature_reg);
60 this->publish_state(temperature_reg);
62}
63
64} // namespace esphome::tc74
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
void status_clear_warning()
Definition component.h:306
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:25
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
void read_temperature_()
Internal method to read the temperature from the component after it has been scheduled.
Definition tc74.cpp:36
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:14