ESPHome  2024.11.0
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 
5 namespace esphome {
6 namespace mpl3115a2 {
7 
8 static 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 
95  this->status_clear_warning();
96 }
97 
98 } // namespace mpl3115a2
99 } // namespace esphome
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:235
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
float altitude
Definition: qmp6988.h:73
uint8_t pressure
Definition: tt21100.cpp:19
bool is_failed() const
Definition: component.cpp:143
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:187
void status_clear_warning()
Definition: component.cpp:166
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
void publish_state(float state)
Publish a new state to the front-end.
Definition: sensor.cpp:39
uint16_t temperature
Definition: sun_gtil2.cpp:26
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:183
uint8_t status
Definition: bl0942.h:74
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:262
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delay(uint32_t ms)
Definition: core.cpp:26