ESPHome  2024.5.2
tmp1075.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
2 #include "tmp1075.h"
3 
4 namespace esphome {
5 namespace tmp1075 {
6 
7 static const char *const TAG = "tmp1075";
8 
9 constexpr uint8_t REG_TEMP = 0x0; // Temperature result
10 constexpr uint8_t REG_CFGR = 0x1; // Configuration
11 constexpr uint8_t REG_LLIM = 0x2; // Low limit
12 constexpr uint8_t REG_HLIM = 0x3; // High limit
13 constexpr uint8_t REG_DIEID = 0xF; // Device ID
14 
15 constexpr uint16_t EXPECT_DIEID = 0x0075; // Expected Device ID.
16 
17 static uint16_t temp2regvalue(float temp);
18 static float regvalue2temp(uint16_t regvalue);
19 
21  uint8_t die_id;
22  if (!this->read_byte(REG_DIEID, &die_id)) {
23  ESP_LOGW(TAG, "'%s' - unable to read ID", this->name_.c_str());
24  this->mark_failed();
25  return;
26  }
27  if (die_id != EXPECT_DIEID) {
28  ESP_LOGW(TAG, "'%s' - unexpected ID 0x%x found, expected 0x%x", this->name_.c_str(), die_id, EXPECT_DIEID);
29  this->mark_failed();
30  return;
31  }
32 
33  this->write_config();
34 }
35 
37  uint16_t regvalue;
38  if (!read_byte_16(REG_TEMP, &regvalue)) {
39  ESP_LOGW(TAG, "'%s' - unable to read temperature register", this->name_.c_str());
40  this->status_set_warning();
41  return;
42  }
43 
44  const float temp = regvalue2temp(regvalue);
45  this->publish_state(temp);
46 }
47 
49  LOG_SENSOR("", "TMP1075 Sensor", this);
50  if (this->is_failed()) {
51  ESP_LOGE(TAG, " Communication with TMP1075 failed!");
52  return;
53  }
54  ESP_LOGCONFIG(TAG, " limit low : %.4f °C", alert_limit_low_);
55  ESP_LOGCONFIG(TAG, " limit high : %.4f °C", alert_limit_high_);
56  ESP_LOGCONFIG(TAG, " oneshot : %d", config_.fields.oneshot);
57  ESP_LOGCONFIG(TAG, " rate : %d", config_.fields.rate);
58  ESP_LOGCONFIG(TAG, " fault_count: %d", config_.fields.faults);
59  ESP_LOGCONFIG(TAG, " polarity : %d", config_.fields.polarity);
60  ESP_LOGCONFIG(TAG, " alert_mode : %d", config_.fields.alert_mode);
61  ESP_LOGCONFIG(TAG, " shutdown : %d", config_.fields.shutdown);
62 }
63 
64 void TMP1075Sensor::set_fault_count(const int faults) {
65  if (faults < 1) {
66  ESP_LOGE(TAG, "'%s' - fault_count too low: %d", this->name_.c_str(), faults);
67  return;
68  }
69  if (faults > 4) {
70  ESP_LOGE(TAG, "'%s' - fault_count too high: %d", this->name_.c_str(), faults);
71  return;
72  }
73  config_.fields.faults = faults - 1;
74 }
75 
77  ESP_LOGV(TAG, " oneshot : %d", config_.fields.oneshot);
78  ESP_LOGV(TAG, " rate : %d", config_.fields.rate);
79  ESP_LOGV(TAG, " faults : %d", config_.fields.faults);
80  ESP_LOGV(TAG, " polarity : %d", config_.fields.polarity);
81  ESP_LOGV(TAG, " alert_mode: %d", config_.fields.alert_mode);
82  ESP_LOGV(TAG, " shutdown : %d", config_.fields.shutdown);
83 }
84 
88  send_config_();
89 }
90 
92  ESP_LOGV(TAG, "'%s' - sending configuration %04x", this->name_.c_str(), config_.regvalue);
93  log_config_();
94  if (!this->write_byte_16(REG_CFGR, config_.regvalue)) {
95  ESP_LOGW(TAG, "'%s' - unable to write configuration register", this->name_.c_str());
96  return;
97  }
98 }
99 
101  ESP_LOGV(TAG, "'%s' - sending alert limit low %.3f °C", this->name_.c_str(), alert_limit_low_);
102  const uint16_t regvalue = temp2regvalue(alert_limit_low_);
103  if (!this->write_byte_16(REG_LLIM, regvalue)) {
104  ESP_LOGW(TAG, "'%s' - unable to write low limit register", this->name_.c_str());
105  return;
106  }
107 }
108 
110  ESP_LOGV(TAG, "'%s' - sending alert limit high %.3f °C", this->name_.c_str(), alert_limit_high_);
111  const uint16_t regvalue = temp2regvalue(alert_limit_high_);
112  if (!this->write_byte_16(REG_HLIM, regvalue)) {
113  ESP_LOGW(TAG, "'%s' - unable to write high limit register", this->name_.c_str());
114  return;
115  }
116 }
117 
118 static uint16_t temp2regvalue(const float temp) {
119  const uint16_t regvalue = temp / 0.0625f;
120  return regvalue << 4;
121 }
122 
123 static float regvalue2temp(const uint16_t regvalue) {
124  const int16_t signed_value = regvalue;
125  return (signed_value >> 4) * 0.0625f;
126 }
127 
128 } // namespace tmp1075
129 } // namespace esphome
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:246
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
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
void set_fault_count(int faults)
Definition: tmp1075.cpp:64
constexpr uint8_t REG_CFGR
Definition: tmp1075.cpp:10
void dump_config() override
Definition: tmp1075.cpp:48
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
constexpr uint16_t EXPECT_DIEID
Definition: tmp1075.cpp:15
constexpr const char * c_str() const
Definition: string_ref.h:68
struct esphome::tmp1075::TMP1075Config::@105::@107 fields
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition: i2c.h:266
constexpr uint8_t REG_HLIM
Definition: tmp1075.cpp:12