ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
tmp1075.cpp
Go to the documentation of this file.
1#include "esphome/core/log.h"
2#include "tmp1075.h"
3
4namespace esphome::tmp1075 {
5
6ESPHOME_LOG_TAG(TAG, "tmp1075");
7
8constexpr uint8_t REG_TEMP = 0x0; // Temperature result
9constexpr uint8_t REG_CFGR = 0x1; // Configuration
10constexpr uint8_t REG_LLIM = 0x2; // Low limit
11constexpr uint8_t REG_HLIM = 0x3; // High limit
12constexpr uint8_t REG_DIEID = 0xF; // Device ID
13
14constexpr uint16_t EXPECT_DIEID = 0x0075; // Expected Device ID.
15
16static uint16_t temp2regvalue(float temp);
17static float regvalue2temp(uint16_t regvalue);
18
20 uint8_t cfg;
21 if (!this->read_byte(REG_CFGR, &cfg)) {
22 ESP_LOGE(TAG, "'%s' - unable to read", LOG_STR_ARG(this->get_log_name()));
23 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
24 return;
25 }
26
27 this->write_config();
28}
29
31 uint16_t regvalue;
32 if (!this->status_comm_result(this->read_byte_16(REG_TEMP, &regvalue), LOG_STR("can't read"))) {
33 ESP_LOGW(TAG, "'%s' - unable to read temperature register", LOG_STR_ARG(this->get_log_name()));
34 return;
35 }
36
37 const float temp = regvalue2temp(regvalue);
38 this->publish_state(temp);
39}
40
42 LOG_SENSOR("", "TMP1075 Sensor", this);
43 if (this->is_failed())
44 return;
45 ESP_LOGCONFIG(TAG,
46 " limit low : %.4f °C\n"
47 " limit high : %.4f °C\n"
48 " oneshot : %d\n"
49 " rate : %d\n"
50 " fault_count: %d\n"
51 " polarity : %d\n"
52 " alert_mode : %d\n"
53 " shutdown : %d",
54 this->alert_limit_low_, this->alert_limit_high_, this->config_.oneshot, this->config_.rate,
55 this->config_.faults, this->config_.polarity, this->config_.alert_mode, this->config_.shutdown);
56}
57
58void TMP1075Sensor::set_fault_count(const int faults) {
59 if (faults < 1) {
60 ESP_LOGE(TAG, "'%s' - fault_count too low: %d", LOG_STR_ARG(this->get_log_name()), faults);
61 return;
62 }
63 if (faults > 4) {
64 ESP_LOGE(TAG, "'%s' - fault_count too high: %d", LOG_STR_ARG(this->get_log_name()), faults);
65 return;
66 }
67 this->config_.faults = faults - 1;
68}
69
71 ESP_LOGV(TAG,
72 " oneshot : %d\n"
73 " rate : %d\n"
74 " faults : %d\n"
75 " polarity : %d\n"
76 " alert_mode: %d\n"
77 " shutdown : %d",
78 this->config_.oneshot, this->config_.rate, this->config_.faults, this->config_.polarity,
79 this->config_.alert_mode, this->config_.shutdown);
80}
81
87
89 ESP_LOGV(TAG, "'%s' - sending configuration %02x", LOG_STR_ARG(this->get_log_name()), this->config_.to_byte());
90 this->log_config_();
91 if (!this->write_byte(REG_CFGR, this->config_.to_byte())) {
92 ESP_LOGW(TAG, "'%s' - unable to write configuration register", LOG_STR_ARG(this->get_log_name()));
93 return;
94 }
95}
96
98 ESP_LOGV(TAG, "'%s' - sending alert limit low %.3f °C", LOG_STR_ARG(this->get_log_name()), this->alert_limit_low_);
99 const uint16_t regvalue = temp2regvalue(this->alert_limit_low_);
100 if (!this->write_byte_16(REG_LLIM, regvalue)) {
101 ESP_LOGW(TAG, "'%s' - unable to write low limit register", LOG_STR_ARG(this->get_log_name()));
102 return;
103 }
104}
105
107 ESP_LOGV(TAG, "'%s' - sending alert limit high %.3f °C", LOG_STR_ARG(this->get_log_name()), this->alert_limit_high_);
108 const uint16_t regvalue = temp2regvalue(this->alert_limit_high_);
109 if (!this->write_byte_16(REG_HLIM, regvalue)) {
110 ESP_LOGW(TAG, "'%s' - unable to write high limit register", LOG_STR_ARG(this->get_log_name()));
111 return;
112 }
113}
114
115static uint16_t temp2regvalue(const float temp) {
116 const int16_t regvalue = static_cast<int16_t>(temp / 0.0625f);
117 return static_cast<uint16_t>(regvalue << 4);
118}
119
120static float regvalue2temp(const uint16_t regvalue) {
121 const int16_t signed_value = regvalue;
122 return (signed_value >> 4) * 0.0625f;
123}
124
125} // namespace esphome::tmp1075
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:281
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.cpp:101
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
void set_fault_count(int faults)
Definition tmp1075.cpp:58
constexpr uint16_t EXPECT_DIEID
Definition tmp1075.cpp:14
constexpr uint8_t REG_LLIM
Definition tmp1075.cpp:10
constexpr uint8_t REG_TEMP
Definition tmp1075.cpp:8
constexpr uint8_t REG_DIEID
Definition tmp1075.cpp:12
constexpr uint8_t REG_HLIM
Definition tmp1075.cpp:11
ESPHOME_LOG_TAG(TAG, "tmp1075")
constexpr uint8_t REG_CFGR
Definition tmp1075.cpp:9