ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
mpl3115a2.cpp
Go to the documentation of this file.
1#include "mpl3115a2.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
7
8ESPHOME_LOG_TAG(TAG, "mpl3115a2");
9
11 if (!this->check_chip_id(this, MPL3115A2_WHOAMI, 0xC4))
12 return;
13
14 // reset
16 delay(15);
17
18 // enable data ready events for pressure/altitude and temperature
21}
22
23void MPL3115A2Component::dump_config() {
24 ESP_LOGCONFIG(TAG, "MPL3115A2:");
25 LOG_I2C_DEVICE(this);
26 LOG_UPDATE_INTERVAL(this);
27 LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
28 LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
29 LOG_SENSOR(" ", "Altitude", this->altitude_sensor_);
30}
31
32void MPL3115A2Component::update() {
35 // Trigger a new reading
37 if (this->altitude_sensor_ != nullptr)
40
41 // Wait until status shows reading available
42 uint8_t status = 0;
43 if (!this->read_byte(MPL3115A2_REGISTER_STATUS, &status) || (status & MPL3115A2_REGISTER_STATUS_PDR) == 0) {
44 delay(10);
45 if (!this->read_byte(MPL3115A2_REGISTER_STATUS, &status) || (status & MPL3115A2_REGISTER_STATUS_PDR) == 0) {
46 return;
47 }
48 }
49
50 uint8_t buffer[5] = {0, 0, 0, 0, 0};
52
53 float altitude = 0, pressure = 0;
54 if (this->altitude_sensor_ != nullptr) {
55 int32_t alt = encode_uint32(buffer[0], buffer[1], buffer[2], 0);
56 altitude = float(alt) / 65536.0f;
57 this->altitude_sensor_->publish_state(altitude);
58 } else {
59 uint32_t p = encode_uint32(0, buffer[0], buffer[1], buffer[2]);
60 pressure = float(p) / 6400.0f;
61 PUBLISH_STATE(this->pressure_sensor_, pressure);
62 }
63 int16_t t = encode_value<int16_t>(&buffer[3]);
64 float temperature = float(t) / 256.0f;
65 PUBLISH_STATE(this->temperature_sensor_, temperature);
66
67 ESP_LOGD(TAG, "Got Temperature=%.1f°C Altitude=%.1f Pressure=%.1f", temperature, altitude, pressure);
68
70}
71
72} // namespace esphome::mpl3115a2
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void status_clear_warning()
Definition component.h:309
bool check_chip_id(Component *component, uint8_t a_register, uint8_t expected, uint8_t mask=0xFF)
Read the 8-bit ID at a_register and accept it when it matches expected in the bits set in mask.
Definition i2c.h:232
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
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:45
uint32_t buffer
Definition gif.cpp:50
static float float float t
FLAG_HAS_TRANSITION float
@ MPL3115A2_REGISTER_STATUS_PDR
Definition mpl3115a2.h:39
@ MPL3115A2_REGISTER_PRESSURE_MSB
Definition mpl3115a2.h:14
ESPHOME_LOG_TAG(TAG, "mpl3115a2")
constexpr T encode_value(const uint8_t *bytes)
Encode a value from its constituent bytes (from most to least significant) in an array with length si...
Definition helpers.h:1064
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:1051
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7