ESPHome 2025.5.0
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 {
5namespace tmp1075 {
6
7static const char *const TAG = "tmp1075";
8
9constexpr uint8_t REG_TEMP = 0x0; // Temperature result
10constexpr uint8_t REG_CFGR = 0x1; // Configuration
11constexpr uint8_t REG_LLIM = 0x2; // Low limit
12constexpr uint8_t REG_HLIM = 0x3; // High limit
13constexpr uint8_t REG_DIEID = 0xF; // Device ID
14
15constexpr uint16_t EXPECT_DIEID = 0x0075; // Expected Device ID.
16
17static uint16_t temp2regvalue(float temp);
18static float regvalue2temp(uint16_t regvalue);
19
21 uint8_t cfg;
22 if (!this->read_byte(REG_CFGR, &cfg)) {
23 ESP_LOGE(TAG, "'%s' - unable to read", this->name_.c_str());
24 this->mark_failed();
25 return;
26 }
27
28 this->write_config();
29}
30
32 uint16_t regvalue;
33 if (!read_byte_16(REG_TEMP, &regvalue)) {
34 ESP_LOGW(TAG, "'%s' - unable to read temperature register", this->name_.c_str());
35 this->status_set_warning("can't read");
36 return;
37 }
39
40 const float temp = regvalue2temp(regvalue);
41 this->publish_state(temp);
42}
43
45 LOG_SENSOR("", "TMP1075 Sensor", this);
46 if (this->is_failed()) {
47 ESP_LOGE(TAG, " Communication with TMP1075 failed!");
48 return;
49 }
50 ESP_LOGCONFIG(TAG, " limit low : %.4f °C", alert_limit_low_);
51 ESP_LOGCONFIG(TAG, " limit high : %.4f °C", alert_limit_high_);
52 ESP_LOGCONFIG(TAG, " oneshot : %d", config_.fields.oneshot);
53 ESP_LOGCONFIG(TAG, " rate : %d", config_.fields.rate);
54 ESP_LOGCONFIG(TAG, " fault_count: %d", config_.fields.faults);
55 ESP_LOGCONFIG(TAG, " polarity : %d", config_.fields.polarity);
56 ESP_LOGCONFIG(TAG, " alert_mode : %d", config_.fields.alert_mode);
57 ESP_LOGCONFIG(TAG, " shutdown : %d", config_.fields.shutdown);
58}
59
60void TMP1075Sensor::set_fault_count(const int faults) {
61 if (faults < 1) {
62 ESP_LOGE(TAG, "'%s' - fault_count too low: %d", this->name_.c_str(), faults);
63 return;
64 }
65 if (faults > 4) {
66 ESP_LOGE(TAG, "'%s' - fault_count too high: %d", this->name_.c_str(), faults);
67 return;
68 }
69 config_.fields.faults = faults - 1;
70}
71
73 ESP_LOGV(TAG, " oneshot : %d", config_.fields.oneshot);
74 ESP_LOGV(TAG, " rate : %d", config_.fields.rate);
75 ESP_LOGV(TAG, " faults : %d", config_.fields.faults);
76 ESP_LOGV(TAG, " polarity : %d", config_.fields.polarity);
77 ESP_LOGV(TAG, " alert_mode: %d", config_.fields.alert_mode);
78 ESP_LOGV(TAG, " shutdown : %d", config_.fields.shutdown);
79}
80
86
88 ESP_LOGV(TAG, "'%s' - sending configuration %02x", this->name_.c_str(), config_.regvalue);
90 if (!this->write_byte(REG_CFGR, config_.regvalue)) {
91 ESP_LOGW(TAG, "'%s' - unable to write configuration register", this->name_.c_str());
92 return;
93 }
94}
95
97 ESP_LOGV(TAG, "'%s' - sending alert limit low %.3f °C", this->name_.c_str(), alert_limit_low_);
98 const uint16_t regvalue = temp2regvalue(alert_limit_low_);
99 if (!this->write_byte_16(REG_LLIM, regvalue)) {
100 ESP_LOGW(TAG, "'%s' - unable to write low limit register", this->name_.c_str());
101 return;
102 }
103}
104
106 ESP_LOGV(TAG, "'%s' - sending alert limit high %.3f °C", this->name_.c_str(), alert_limit_high_);
107 const uint16_t regvalue = temp2regvalue(alert_limit_high_);
108 if (!this->write_byte_16(REG_HLIM, regvalue)) {
109 ESP_LOGW(TAG, "'%s' - unable to write high limit register", this->name_.c_str());
110 return;
111 }
112}
113
114static uint16_t temp2regvalue(const float temp) {
115 const uint16_t regvalue = temp / 0.0625f;
116 return regvalue << 4;
117}
118
119static float regvalue2temp(const uint16_t regvalue) {
120 const int16_t signed_value = regvalue;
121 return (signed_value >> 4) * 0.0625f;
122}
123
124} // namespace tmp1075
125} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message="unspecified")
void status_clear_warning()
constexpr const char * c_str() const
Definition string_ref.h:68
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:250
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition i2c.h:270
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
void set_fault_count(int faults)
Definition tmp1075.cpp:60
constexpr uint16_t EXPECT_DIEID
Definition tmp1075.cpp:15
constexpr uint8_t REG_LLIM
Definition tmp1075.cpp:11
constexpr uint8_t REG_TEMP
Definition tmp1075.cpp:9
constexpr uint8_t REG_DIEID
Definition tmp1075.cpp:13
constexpr uint8_t REG_HLIM
Definition tmp1075.cpp:12
constexpr uint8_t REG_CFGR
Definition tmp1075.cpp:10
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
struct esphome::tmp1075::TMP1075Config::@144::@146 fields