ESPHome 2026.6.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
6static const char *const 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", this->name_.c_str());
23 this->mark_failed();
24 return;
25 }
26
27 this->write_config();
28}
29
31 uint16_t regvalue;
32 if (!read_byte_16(REG_TEMP, &regvalue)) {
33 ESP_LOGW(TAG, "'%s' - unable to read temperature register", this->name_.c_str());
34 this->status_set_warning(LOG_STR("can't read"));
35 return;
36 }
38
39 const float temp = regvalue2temp(regvalue);
40 this->publish_state(temp);
41}
42
44 LOG_SENSOR("", "TMP1075 Sensor", this);
45 if (this->is_failed()) {
46 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
47 return;
48 }
49 ESP_LOGCONFIG(TAG,
50 " limit low : %.4f °C\n"
51 " limit high : %.4f °C\n"
52 " oneshot : %d\n"
53 " rate : %d\n"
54 " fault_count: %d\n"
55 " polarity : %d\n"
56 " alert_mode : %d\n"
57 " shutdown : %d",
60}
61
62void TMP1075Sensor::set_fault_count(const int faults) {
63 if (faults < 1) {
64 ESP_LOGE(TAG, "'%s' - fault_count too low: %d", this->name_.c_str(), faults);
65 return;
66 }
67 if (faults > 4) {
68 ESP_LOGE(TAG, "'%s' - fault_count too high: %d", this->name_.c_str(), faults);
69 return;
70 }
71 config_.fields.faults = faults - 1;
72}
73
75 ESP_LOGV(TAG,
76 " oneshot : %d\n"
77 " rate : %d\n"
78 " faults : %d\n"
79 " polarity : %d\n"
80 " alert_mode: %d\n"
81 " shutdown : %d",
84}
85
91
93 ESP_LOGV(TAG, "'%s' - sending configuration %02x", this->name_.c_str(), config_.regvalue);
95 if (!this->write_byte(REG_CFGR, config_.regvalue)) {
96 ESP_LOGW(TAG, "'%s' - unable to write configuration register", this->name_.c_str());
97 return;
98 }
99}
100
102 ESP_LOGV(TAG, "'%s' - sending alert limit low %.3f °C", this->name_.c_str(), alert_limit_low_);
103 const uint16_t regvalue = temp2regvalue(alert_limit_low_);
104 if (!this->write_byte_16(REG_LLIM, regvalue)) {
105 ESP_LOGW(TAG, "'%s' - unable to write low limit register", this->name_.c_str());
106 return;
107 }
108}
109
111 ESP_LOGV(TAG, "'%s' - sending alert limit high %.3f °C", this->name_.c_str(), alert_limit_high_);
112 const uint16_t regvalue = temp2regvalue(alert_limit_high_);
113 if (!this->write_byte_16(REG_HLIM, regvalue)) {
114 ESP_LOGW(TAG, "'%s' - unable to write high limit register", this->name_.c_str());
115 return;
116 }
117}
118
119static uint16_t temp2regvalue(const float temp) {
120 const int16_t regvalue = static_cast<int16_t>(temp / 0.0625f);
121 return static_cast<uint16_t>(regvalue << 4);
122}
123
124static float regvalue2temp(const uint16_t regvalue) {
125 const int16_t signed_value = regvalue;
126 return (signed_value >> 4) * 0.0625f;
127}
128
129} // namespace esphome::tmp1075
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void status_clear_warning()
Definition component.h:289
constexpr const char * c_str() const
Definition string_ref.h:73
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:249
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
void set_fault_count(int faults)
Definition tmp1075.cpp:62
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
constexpr uint8_t REG_CFGR
Definition tmp1075.cpp:9
struct esphome::tmp1075::TMP1075Config::@169::@171 fields