ESPHome 2026.6.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
8static const char *const TAG = "mpl3115a2";
9
11 uint8_t whoami = 0xFF;
12 if (!this->read_byte(MPL3115A2_WHOAMI, &whoami)) {
13 this->error_code_ = COMMUNICATION_FAILED;
14 this->mark_failed();
15 return;
16 }
17 if (whoami != 0xC4) {
18 this->error_code_ = WRONG_ID;
19 this->mark_failed();
20 return;
21 }
22
23 // reset
25 delay(15);
26
27 // enable data ready events for pressure/altitude and temperature
30}
31
33 ESP_LOGCONFIG(TAG, "MPL3115A2:");
34 LOG_I2C_DEVICE(this);
35 if (this->is_failed()) {
36 switch (this->error_code_) {
38 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
39 break;
40 case WRONG_ID:
41 ESP_LOGE(TAG, "MPL3115A2 has invalid id");
42 break;
43 default:
44 ESP_LOGE(TAG, "Setting up MPL3115A2 registers failed!");
45 break;
46 }
47 }
48 LOG_UPDATE_INTERVAL(this);
49 LOG_SENSOR(" ", "Temperature", this->temperature_);
50 LOG_SENSOR(" ", "Pressure", this->pressure_);
51 LOG_SENSOR(" ", "Altitude", this->altitude_);
52}
53
57 // Trigger a new reading
59 if (this->altitude_ != nullptr)
62
63 // Wait until status shows reading available
64 uint8_t status = 0;
66 delay(10);
68 return;
69 }
70 }
71
72 uint8_t buffer[5] = {0, 0, 0, 0, 0};
74
75 float altitude = 0, pressure = 0;
76 if (this->altitude_ != nullptr) {
77 int32_t alt = encode_uint32(buffer[0], buffer[1], buffer[2], 0);
78 altitude = float(alt) / 65536.0;
79 this->altitude_->publish_state(altitude);
80 } else {
81 uint32_t p = encode_uint32(0, buffer[0], buffer[1], buffer[2]);
82 pressure = float(p) / 6400.0;
83 if (this->pressure_ != nullptr)
85 }
86 int16_t t = encode_uint16(buffer[3], buffer[4]);
87 float temperature = float(t) / 256.0;
88 if (this->temperature_ != nullptr)
89 this->temperature_->publish_state(temperature);
90
91 ESP_LOGD(TAG, "Got Temperature=%.1f°C Altitude=%.1f Pressure=%.1f", temperature, altitude, pressure);
92
94}
95
96} // namespace esphome::mpl3115a2
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:272
void status_clear_warning()
Definition component.h:289
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
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:25
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
@ MPL3115A2_REGISTER_STATUS_PDR
Definition mpl3115a2.h:39
@ MPL3115A2_REGISTER_PRESSURE_MSB
Definition mpl3115a2.h:14
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:867
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:859
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