ESPHome 2026.11.0-dev
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
24 ESP_LOGCONFIG(TAG, "MPL3115A2:");
25 LOG_I2C_DEVICE(this);
26 LOG_UPDATE_INTERVAL(this);
27 LOG_SENSOR(" ", "Temperature", this->temperature_);
28 LOG_SENSOR(" ", "Pressure", this->pressure_);
29 LOG_SENSOR(" ", "Altitude", this->altitude_);
30}
31
35 // Trigger a new reading
37 if (this->altitude_ != nullptr)
40
41 // Wait until status shows reading available
42 uint8_t status = 0;
44 delay(10);
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_ != nullptr) {
55 int32_t alt = encode_uint32(buffer[0], buffer[1], buffer[2], 0);
56 altitude = float(alt) / 65536.0f;
57 this->altitude_->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 if (this->pressure_ != nullptr)
63 }
64 int16_t t = encode_value<int16_t>(&buffer[3]);
65 float temperature = float(t) / 256.0f;
66 if (this->temperature_ != nullptr)
67 this->temperature_->publish_state(temperature);
68
69 ESP_LOGD(TAG, "Got Temperature=%.1f°C Altitude=%.1f Pressure=%.1f", temperature, altitude, pressure);
70
72}
73
74} // namespace esphome::mpl3115a2
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
void status_clear_warning()
Definition component.h:296
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:28
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:65
uint32_t buffer
Definition gif.cpp:50
@ 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:1019
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:1006
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