ESPHome 2025.9.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 {
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, ESP_LOG_MSG_COMM_FAIL);
48 return;
49 }
50 ESP_LOGCONFIG(TAG,
51 " limit low : %.4f °C\n"
52 " limit high : %.4f °C\n"
53 " oneshot : %d\n"
54 " rate : %d\n"
55 " fault_count: %d\n"
56 " polarity : %d\n"
57 " alert_mode : %d\n"
58 " shutdown : %d",
61}
62
63void TMP1075Sensor::set_fault_count(const int faults) {
64 if (faults < 1) {
65 ESP_LOGE(TAG, "'%s' - fault_count too low: %d", this->name_.c_str(), faults);
66 return;
67 }
68 if (faults > 4) {
69 ESP_LOGE(TAG, "'%s' - fault_count too high: %d", this->name_.c_str(), faults);
70 return;
71 }
72 config_.fields.faults = faults - 1;
73}
74
76 ESP_LOGV(TAG, " oneshot : %d", config_.fields.oneshot);
77 ESP_LOGV(TAG, " rate : %d", config_.fields.rate);
78 ESP_LOGV(TAG, " faults : %d", config_.fields.faults);
79 ESP_LOGV(TAG, " polarity : %d", config_.fields.polarity);
80 ESP_LOGV(TAG, " alert_mode: %d", config_.fields.alert_mode);
81 ESP_LOGV(TAG, " shutdown : %d", config_.fields.shutdown);
82}
83
89
91 ESP_LOGV(TAG, "'%s' - sending configuration %02x", this->name_.c_str(), config_.regvalue);
93 if (!this->write_byte(REG_CFGR, config_.regvalue)) {
94 ESP_LOGW(TAG, "'%s' - unable to write configuration register", this->name_.c_str());
95 return;
96 }
97}
98
100 ESP_LOGV(TAG, "'%s' - sending alert limit low %.3f °C", this->name_.c_str(), alert_limit_low_);
101 const uint16_t regvalue = temp2regvalue(alert_limit_low_);
102 if (!this->write_byte_16(REG_LLIM, regvalue)) {
103 ESP_LOGW(TAG, "'%s' - unable to write low limit register", this->name_.c_str());
104 return;
105 }
106}
107
109 ESP_LOGV(TAG, "'%s' - sending alert limit high %.3f °C", this->name_.c_str(), alert_limit_high_);
110 const uint16_t regvalue = temp2regvalue(alert_limit_high_);
111 if (!this->write_byte_16(REG_HLIM, regvalue)) {
112 ESP_LOGW(TAG, "'%s' - unable to write high limit register", this->name_.c_str());
113 return;
114 }
115}
116
117static uint16_t temp2regvalue(const float temp) {
118 const uint16_t regvalue = temp / 0.0625f;
119 return regvalue << 4;
120}
121
122static float regvalue2temp(const uint16_t regvalue) {
123 const int16_t signed_value = regvalue;
124 return (signed_value >> 4) * 0.0625f;
125}
126
127} // namespace tmp1075
128} // namespace esphome
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
constexpr const char * c_str() const
Definition string_ref.h:69
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:45
void set_fault_count(int faults)
Definition tmp1075.cpp:63
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::@149::@151 fields