ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
lm75b.cpp
Go to the documentation of this file.
1#include "lm75b.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome {
6namespace lm75b {
7
8static const char *const TAG = "lm75b";
9
11 ESP_LOGCONFIG(TAG, "LM75B:");
12 LOG_I2C_DEVICE(this);
13 if (this->is_failed()) {
14 ESP_LOGE(TAG, "Setting up LM75B failed!");
15 }
16 LOG_UPDATE_INTERVAL(this);
17 LOG_SENSOR(" ", "Temperature", this);
18}
19
21 // Create a temporary buffer
22 uint8_t buff[2];
23 if (this->read_register(LM75B_REG_TEMPERATURE, buff, 2) != i2c::ERROR_OK) {
24 this->status_set_warning();
25 return;
26 }
27 // Obtain combined 16-bit value
28 int16_t raw_temperature = (buff[0] << 8) | buff[1];
29 // Read the 11-bit raw temperature value
30 raw_temperature >>= 5;
31 // Publish the temperature in °C
32 this->publish_state(raw_temperature * 0.125);
33 if (this->status_has_warning()) {
35 }
36}
37
38} // namespace lm75b
39} // namespace esphome
bool is_failed() const
void status_set_warning(const char *message=nullptr)
bool status_has_warning() const
void status_clear_warning()
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:35
void dump_config() override
Definition lm75b.cpp:10
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:75
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:33
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7