ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
lps22.cpp
Go to the documentation of this file.
1#include "lps22.h"
2
3namespace esphome {
4namespace lps22 {
5
6static constexpr const char *const TAG = "lps22";
7
8static constexpr uint8_t WHO_AM_I = 0x0F;
9static constexpr uint8_t LPS22HB_ID = 0xB1;
10static constexpr uint8_t LPS22HH_ID = 0xB3;
11static constexpr uint8_t LPS22DF_ID = 0xB4;
12static constexpr uint8_t CTRL_REG2 = 0x11;
13static constexpr uint8_t CTRL_REG2_ONE_SHOT_MASK = 0b1;
14static constexpr uint8_t STATUS = 0x27;
15static constexpr uint8_t STATUS_T_DA_MASK = 0b10;
16static constexpr uint8_t STATUS_P_DA_MASK = 0b01;
17static constexpr uint8_t TEMP_L = 0x2b;
18static constexpr uint8_t PRES_OUT_XL = 0x28;
19static constexpr uint8_t REF_P_XL = 0x28;
20static constexpr uint8_t READ_ATTEMPTS = 10;
21static constexpr uint8_t READ_INTERVAL = 5;
22static constexpr float PRESSURE_SCALE = 1.0f / 4096.0f;
23static constexpr float TEMPERATURE_SCALE = 0.01f;
24
26 uint8_t value = 0x00;
27 this->read_register(WHO_AM_I, &value, 1);
28 if (value != LPS22HB_ID && value != LPS22HH_ID && value != LPS22DF_ID) {
29 ESP_LOGW(TAG, "device IDs as %02x, which isn't a known LPS22HB/HH/DF ID", value);
30 this->mark_failed();
31 }
32}
33
35 ESP_LOGCONFIG(TAG, "LPS22:");
36 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
37 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
38 LOG_I2C_DEVICE(this);
39 LOG_UPDATE_INTERVAL(this);
40}
41
42static constexpr uint32_t INTERVAL_READ = 0;
43
45 uint8_t value = 0x00;
46 this->read_register(CTRL_REG2, &value, 1);
47 value |= CTRL_REG2_ONE_SHOT_MASK;
48 this->write_register(CTRL_REG2, &value, 1);
49 this->read_attempts_remaining_ = READ_ATTEMPTS;
50 this->set_interval(INTERVAL_READ, READ_INTERVAL, [this]() { this->try_read_(); });
51}
52
54 uint8_t value = 0x00;
55 this->read_register(STATUS, &value, 1);
56 const uint8_t expected_status_mask = STATUS_T_DA_MASK | STATUS_P_DA_MASK;
57 if ((value & expected_status_mask) != expected_status_mask) {
58 ESP_LOGD(TAG, "STATUS not ready: %x", value);
59 if (--this->read_attempts_remaining_ == 0) {
60 this->cancel_interval(INTERVAL_READ);
61 }
62 return;
63 }
64 this->cancel_interval(INTERVAL_READ);
65
66 if (this->temperature_sensor_ != nullptr) {
67 uint8_t t_buf[2]{0};
68 this->read_register(TEMP_L, t_buf, 2);
69 int16_t encoded = static_cast<int16_t>(encode_uint16(t_buf[1], t_buf[0]));
70 float temp = TEMPERATURE_SCALE * static_cast<float>(encoded);
72 }
73 if (this->pressure_sensor_ != nullptr) {
74 uint8_t p_buf[3]{0};
75 this->read_register(PRES_OUT_XL, p_buf, 3);
76 uint32_t p_lsb = encode_uint24(p_buf[2], p_buf[1], p_buf[0]);
77 this->pressure_sensor_->publish_state(PRESSURE_SCALE * static_cast<float>(p_lsb));
78 }
79}
80
81} // namespace lps22
82} // namespace esphome
void mark_failed()
Mark this component as failed.
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_interval(const std voi set_interval)(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a unique name.
Definition component.h:350
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") bool cancel_interval(const std boo cancel_interval)(const char *name)
Cancel an interval function.
Definition component.h:372
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:34
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:25
sensor::Sensor * temperature_sensor_
Definition lps22.h:22
void dump_config() override
Definition lps22.cpp:34
sensor::Sensor * pressure_sensor_
Definition lps22.h:23
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:657
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:653