ESPHome  2024.11.1
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 
6 namespace esphome {
7 namespace tc74 {
8 
9 static const char *const TAG = "tc74";
10 
11 static const uint8_t TC74_REGISTER_TEMPERATURE = 0x00;
12 static const uint8_t TC74_REGISTER_CONFIGURATION = 0x01;
13 static const uint8_t TC74_DATA_READY_MASK = 0x40;
14 
15 // 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
16 // being set does not constitute a failure.
18  ESP_LOGCONFIG(TAG, "Setting up TC74...");
19  uint8_t config_reg;
20  if (this->read_register(TC74_REGISTER_CONFIGURATION, &config_reg, 1) != i2c::ERROR_OK) {
21  this->mark_failed();
22  return;
23  }
24  this->data_ready_ = config_reg & TC74_DATA_READY_MASK;
25 }
26 
28 
30  LOG_SENSOR("", "TC74", this);
31  LOG_I2C_DEVICE(this);
32  if (this->is_failed()) {
33  ESP_LOGE(TAG, "Connection with TC74 failed!");
34  }
35  LOG_UPDATE_INTERVAL(this);
36 }
37 
39  if (!this->data_ready_) {
40  uint8_t config_reg;
41  if (this->read_register(TC74_REGISTER_CONFIGURATION, &config_reg, 1) != i2c::ERROR_OK) {
42  this->status_set_warning();
43  return;
44  }
45 
46  if (config_reg & TC74_DATA_READY_MASK) {
47  this->data_ready_ = true;
48  } else {
49  ESP_LOGD(TAG, "TC74 not ready");
50  return;
51  }
52  }
53 
54  uint8_t temperature_reg;
55  if (this->read_register(TC74_REGISTER_TEMPERATURE, &temperature_reg, 1) != i2c::ERROR_OK) {
56  this->status_set_warning();
57  return;
58  }
59 
60  ESP_LOGD(TAG, "Got Temperature=%d °C", temperature_reg);
61  this->publish_state(temperature_reg);
62  this->status_clear_warning();
63 }
64 
66 
67 } // namespace tc74
68 } // namespace esphome
void read_temperature_()
Internal method to read the temperature from the component after it has been scheduled.
Definition: tc74.cpp:38
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition: i2c.cpp:10
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool is_failed() const
Definition: component.cpp:143
No error found during execution of method.
Definition: i2c_bus.h:13
void status_clear_warning()
Definition: component.cpp:166
void setup() override
Setup the sensor and check connection.
Definition: tc74.cpp:17
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
float get_setup_priority() const override
Definition: tc74.cpp:65
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
void dump_config() override
Definition: tc74.cpp:29
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void update() override
Update the sensor value (temperature).
Definition: tc74.cpp:27