ESPHome 2025.9.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
6namespace esphome {
7namespace mpl3115a2 {
8
9static const char *const TAG = "mpl3115a2";
10
12 uint8_t whoami = 0xFF;
13 if (!this->read_byte(MPL3115A2_WHOAMI, &whoami, false)) {
14 this->error_code_ = COMMUNICATION_FAILED;
15 this->mark_failed();
16 return;
17 }
18 if (whoami != 0xC4) {
19 this->error_code_ = WRONG_ID;
20 this->mark_failed();
21 return;
22 }
23
24 // reset
26 delay(15);
27
28 // enable data ready events for pressure/altitude and temperature
31}
32
34 ESP_LOGCONFIG(TAG, "MPL3115A2:");
35 LOG_I2C_DEVICE(this);
36 if (this->is_failed()) {
37 switch (this->error_code_) {
39 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
40 break;
41 case WRONG_ID:
42 ESP_LOGE(TAG, "MPL3115A2 has invalid id");
43 break;
44 default:
45 ESP_LOGE(TAG, "Setting up MPL3115A2 registers failed!");
46 break;
47 }
48 }
49 LOG_UPDATE_INTERVAL(this);
50 LOG_SENSOR(" ", "Temperature", this->temperature_);
51 LOG_SENSOR(" ", "Pressure", this->pressure_);
52 LOG_SENSOR(" ", "Altitude", this->altitude_);
53}
54
57 this->write_byte(MPL3115A2_CTRL_REG1, mode, true);
58 // Trigger a new reading
60 if (this->altitude_ != nullptr)
62 this->write_byte(MPL3115A2_CTRL_REG1, mode, true);
63
64 // Wait until status shows reading available
65 uint8_t status = 0;
66 if (!this->read_byte(MPL3115A2_REGISTER_STATUS, &status, false) || (status & MPL3115A2_REGISTER_STATUS_PDR) == 0) {
67 delay(10);
68 if (!this->read_byte(MPL3115A2_REGISTER_STATUS, &status, false) || (status & MPL3115A2_REGISTER_STATUS_PDR) == 0) {
69 return;
70 }
71 }
72
73 uint8_t buffer[5] = {0, 0, 0, 0, 0};
74 this->read_register(MPL3115A2_REGISTER_PRESSURE_MSB, buffer, 5, false);
75
76 float altitude = 0, pressure = 0;
77 if (this->altitude_ != nullptr) {
78 int32_t alt = encode_uint32(buffer[0], buffer[1], buffer[2], 0);
79 altitude = float(alt) / 65536.0;
80 this->altitude_->publish_state(altitude);
81 } else {
82 uint32_t p = encode_uint32(0, buffer[0], buffer[1], buffer[2]);
83 pressure = float(p) / 6400.0;
84 if (this->pressure_ != nullptr)
86 }
87 int16_t t = encode_uint16(buffer[3], buffer[4]);
88 float temperature = float(t) / 256.0;
89 if (this->temperature_ != nullptr)
90 this->temperature_->publish_state(temperature);
91
92 ESP_LOGD(TAG, "Got Temperature=%.1f°C Altitude=%.1f Pressure=%.1f", temperature, altitude, pressure);
93
95}
96
97} // namespace mpl3115a2
98} // namespace esphome
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_clear_warning()
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition i2c.h:266
ErrorCode read_register(uint8_t a_register, uint8_t *data, size_t len, bool stop=true)
reads an array of bytes from a specific register in the I²C device
Definition i2c.cpp:10
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition i2c.h:239
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
@ MPL3115A2_REGISTER_STATUS_PDR
Definition mpl3115a2.h:40
@ MPL3115A2_REGISTER_PRESSURE_MSB
Definition mpl3115a2.h:15
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:181
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:173
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7