ESPHome 2025.6.3
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 ESP_LOGCONFIG(TAG, "Running setup");
13
14 uint8_t whoami = 0xFF;
15 if (!this->read_byte(MPL3115A2_WHOAMI, &whoami, false)) {
16 this->error_code_ = COMMUNICATION_FAILED;
17 this->mark_failed();
18 return;
19 }
20 if (whoami != 0xC4) {
21 this->error_code_ = WRONG_ID;
22 this->mark_failed();
23 return;
24 }
25
26 // reset
28 delay(15);
29
30 // enable data ready events for pressure/altitude and temperature
33}
34
36 ESP_LOGCONFIG(TAG, "MPL3115A2:");
37 LOG_I2C_DEVICE(this);
38 if (this->is_failed()) {
39 switch (this->error_code_) {
41 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
42 break;
43 case WRONG_ID:
44 ESP_LOGE(TAG, "MPL3115A2 has invalid id");
45 break;
46 default:
47 ESP_LOGE(TAG, "Setting up MPL3115A2 registers failed!");
48 break;
49 }
50 }
51 LOG_UPDATE_INTERVAL(this);
52 LOG_SENSOR(" ", "Temperature", this->temperature_);
53 LOG_SENSOR(" ", "Pressure", this->pressure_);
54 LOG_SENSOR(" ", "Altitude", this->altitude_);
55}
56
59 this->write_byte(MPL3115A2_CTRL_REG1, mode, true);
60 // Trigger a new reading
62 if (this->altitude_ != nullptr)
64 this->write_byte(MPL3115A2_CTRL_REG1, mode, true);
65
66 // Wait until status shows reading available
67 uint8_t status = 0;
68 if (!this->read_byte(MPL3115A2_REGISTER_STATUS, &status, false) || (status & MPL3115A2_REGISTER_STATUS_PDR) == 0) {
69 delay(10);
70 if (!this->read_byte(MPL3115A2_REGISTER_STATUS, &status, false) || (status & MPL3115A2_REGISTER_STATUS_PDR) == 0) {
71 return;
72 }
73 }
74
75 uint8_t buffer[5] = {0, 0, 0, 0, 0};
76 this->read_register(MPL3115A2_REGISTER_PRESSURE_MSB, buffer, 5, false);
77
78 float altitude = 0, pressure = 0;
79 if (this->altitude_ != nullptr) {
80 int32_t alt = encode_uint32(buffer[0], buffer[1], buffer[2], 0);
81 altitude = float(alt) / 65536.0;
82 this->altitude_->publish_state(altitude);
83 } else {
84 uint32_t p = encode_uint32(0, buffer[0], buffer[1], buffer[2]);
85 pressure = float(p) / 6400.0;
86 if (this->pressure_ != nullptr)
88 }
89 int16_t t = encode_uint16(buffer[3], buffer[4]);
90 float temperature = float(t) / 256.0;
91 if (this->temperature_ != nullptr)
92 this->temperature_->publish_state(temperature);
93
94 ESP_LOGD(TAG, "Got Temperature=%.1f°C Altitude=%.1f Pressure=%.1f", temperature, altitude, pressure);
95
97}
98
99} // namespace mpl3115a2
100} // 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:200
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:192
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