ESPHome 2025.5.0
Loading...
Searching...
No Matches
mpl3115a2.cpp
Go to the documentation of this file.
1#include "mpl3115a2.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace mpl3115a2 {
7
8static const char *const TAG = "mpl3115a2";
9
11 ESP_LOGCONFIG(TAG, "Setting up MPL3115A2...");
12
13 uint8_t whoami = 0xFF;
14 if (!this->read_byte(MPL3115A2_WHOAMI, &whoami, false)) {
15 this->error_code_ = COMMUNICATION_FAILED;
16 this->mark_failed();
17 return;
18 }
19 if (whoami != 0xC4) {
20 this->error_code_ = WRONG_ID;
21 this->mark_failed();
22 return;
23 }
24
25 // reset
27 delay(15);
28
29 // enable data ready events for pressure/altitude and temperature
32}
33
35 ESP_LOGCONFIG(TAG, "MPL3115A2:");
36 LOG_I2C_DEVICE(this);
37 if (this->is_failed()) {
38 switch (this->error_code_) {
40 ESP_LOGE(TAG, "Communication with MPL3115A2 failed!");
41 break;
42 case WRONG_ID:
43 ESP_LOGE(TAG, "MPL3115A2 has invalid id");
44 break;
45 default:
46 ESP_LOGE(TAG, "Setting up MPL3115A2 registers failed!");
47 break;
48 }
49 }
50 LOG_UPDATE_INTERVAL(this);
51 LOG_SENSOR(" ", "Temperature", this->temperature_);
52 LOG_SENSOR(" ", "Pressure", this->pressure_);
53 LOG_SENSOR(" ", "Altitude", this->altitude_);
54}
55
58 this->write_byte(MPL3115A2_CTRL_REG1, mode, true);
59 // Trigger a new reading
61 if (this->altitude_ != nullptr)
63 this->write_byte(MPL3115A2_CTRL_REG1, mode, true);
64
65 // Wait until status shows reading available
66 uint8_t status = 0;
67 if (!this->read_byte(MPL3115A2_REGISTER_STATUS, &status, false) || (status & MPL3115A2_REGISTER_STATUS_PDR) == 0) {
68 delay(10);
69 if (!this->read_byte(MPL3115A2_REGISTER_STATUS, &status, false) || (status & MPL3115A2_REGISTER_STATUS_PDR) == 0) {
70 return;
71 }
72 }
73
74 uint8_t buffer[5] = {0, 0, 0, 0, 0};
75 this->read_register(MPL3115A2_REGISTER_PRESSURE_MSB, buffer, 5, false);
76
77 float altitude = 0, pressure = 0;
78 if (this->altitude_ != nullptr) {
79 int32_t alt = encode_uint32(buffer[0], buffer[1], buffer[2], 0);
80 altitude = float(alt) / 65536.0;
81 this->altitude_->publish_state(altitude);
82 } else {
83 uint32_t p = encode_uint32(0, buffer[0], buffer[1], buffer[2]);
84 pressure = float(p) / 6400.0;
85 if (this->pressure_ != nullptr)
87 }
88 int16_t t = encode_uint16(buffer[3], buffer[4]);
89 float temperature = float(t) / 256.0;
90 if (this->temperature_ != nullptr)
91 this->temperature_->publish_state(temperature);
92
93 ESP_LOGD(TAG, "Got Temperature=%.1f°C Altitude=%.1f Pressure=%.1f", temperature, altitude, pressure);
94
96}
97
98} // namespace mpl3115a2
99} // 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:39
@ 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:195
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:191
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:28
uint16_t temperature
Definition sun_gtil2.cpp:12
uint8_t pressure
Definition tt21100.cpp:7