ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
mcp3221_sensor.cpp
Go to the documentation of this file.
1#include "mcp3221_sensor.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace mcp3221 {
7
8static const char *const TAG = "mcp3221";
9
11 uint8_t data[2];
12 if (this->read(data, 2) != i2c::ERROR_OK) {
13 ESP_LOGW(TAG, "Read failed");
14 this->status_set_warning();
15 return NAN;
16 }
18
19 uint16_t value = encode_uint16(data[0], data[1]);
20 float voltage = value * this->reference_voltage_ / 4096.0f;
21
22 return voltage;
23}
24
26 float v = this->sample();
27 this->publish_state(v);
28}
29
30} // namespace mcp3221
31} // namespace esphome
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:164
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:75
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:33
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:394