ESPHome  2024.11.2
tmp117.cpp
Go to the documentation of this file.
1 // Implementation based on:
2 // - DHT 12 Component
3 
4 #include "tmp117.h"
5 #include "esphome/core/log.h"
6 
7 namespace esphome {
8 namespace tmp117 {
9 
10 static const char *const TAG = "tmp117";
11 
13  int16_t data;
14  if (!this->read_data_(&data)) {
15  this->status_set_warning();
16  return;
17  }
18  if ((uint16_t) data != 0x8000) {
19  float temperature = data * 0.0078125f;
20 
21  ESP_LOGD(TAG, "Got temperature=%.2f°C", temperature);
22  this->publish_state(temperature);
23  this->status_clear_warning();
24  } else {
25  ESP_LOGD(TAG, "TMP117 not ready");
26  }
27 }
29  ESP_LOGCONFIG(TAG, "Setting up TMP117...");
30 
31  if (!this->write_config_(this->config_)) {
32  this->mark_failed();
33  return;
34  }
35 
36  int16_t data;
37  if (!this->read_data_(&data)) {
38  this->mark_failed();
39  return;
40  }
41 }
43  ESP_LOGD(TAG, "TMP117:");
44  LOG_I2C_DEVICE(this);
45  if (this->is_failed()) {
46  ESP_LOGE(TAG, "Communication with TMP117 failed!");
47  }
48  LOG_SENSOR(" ", "Temperature", this);
49 }
51 bool TMP117Component::read_data_(int16_t *data) {
52  if (!this->read_byte_16(0, (uint16_t *) data)) {
53  ESP_LOGW(TAG, "Updating TMP117 failed!");
54  return false;
55  }
56  return true;
57 }
58 
59 bool TMP117Component::read_config_(uint16_t *config) {
60  if (!this->read_byte_16(1, (uint16_t *) config)) {
61  ESP_LOGW(TAG, "Reading TMP117 config failed!");
62  return false;
63  }
64  return true;
65 }
66 
67 bool TMP117Component::write_config_(uint16_t config) {
68  if (!this->write_byte_16(1, config)) {
69  ESP_LOGE(TAG, "Writing TMP117 config failed!");
70  return false;
71  }
72  return true;
73 }
74 
75 } // namespace tmp117
76 } // namespace esphome
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition: i2c.h:246
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
float get_setup_priority() const override
Definition: tmp117.cpp:50
void status_set_warning(const char *message="unspecified")
Definition: component.cpp:151
bool is_failed() const
Definition: component.cpp:143
bool write_config_(uint16_t config)
Definition: tmp117.cpp:67
void dump_config() override
Definition: tmp117.cpp:42
void status_clear_warning()
Definition: component.cpp:166
bool read_config_(uint16_t *config)
Definition: tmp117.cpp:59
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
bool read_data_(int16_t *data)
Definition: tmp117.cpp:51
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool write_byte_16(uint8_t a_register, uint16_t data)
Definition: i2c.h:266