ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
mcp4728.cpp
Go to the documentation of this file.
1#include "mcp4728.h"
2
4#include "esphome/core/log.h"
5
6namespace esphome::mcp4728 {
7
8static const char *const TAG = "mcp4728";
9
11 auto err = this->write(nullptr, 0);
12 if (err != i2c::ERROR_OK) {
13 this->mark_failed();
14 return;
15 }
16}
17
19 ESP_LOGCONFIG(TAG, "MCP4728:");
20 LOG_I2C_DEVICE(this);
21 if (this->is_failed()) {
22 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
23 }
24}
25
27 if (this->update_) {
28 this->update_ = false;
29 if (this->store_in_eeprom_) {
30 if (!this->seq_write_()) {
31 this->status_set_error();
32 } else {
33 this->status_clear_error();
34 }
35 } else {
36 if (!this->multi_write_()) {
37 this->status_set_error();
38 } else {
39 this->status_clear_error();
40 }
41 }
42 }
43}
44
46 uint8_t cn = 0;
47 if (channel == MCP4728_CHANNEL_A) {
48 cn = 'A';
49 } else if (channel == MCP4728_CHANNEL_B) {
50 cn = 'B';
51 } else if (channel == MCP4728_CHANNEL_C) {
52 cn = 'C';
53 } else {
54 cn = 'D';
55 }
56 ESP_LOGV(TAG, "Setting MCP4728 channel %c to %d!", cn, value);
57 reg_[channel].data = value;
58 this->update_ = true;
59}
60
62 i2c::ErrorCode err[4];
63 for (uint8_t i = 0; i < 4; ++i) {
64 uint8_t wd[3];
65 wd[0] = ((uint8_t) CMD::MULTI_WRITE | (i << 1)) & 0xFE;
66 wd[1] = ((uint8_t) reg_[i].vref << 7) | ((uint8_t) reg_[i].pd << 5) | ((uint8_t) reg_[i].gain << 4) |
67 (reg_[i].data >> 8);
68 wd[2] = reg_[i].data & 0xFF;
69 err[i] = this->write(wd, sizeof(wd));
70 }
71 bool ok = true;
72 for (auto &e : err) {
73 if (e != i2c::ERROR_OK) {
74 ok = false;
75 break;
76 }
77 }
78 return ok;
79}
80
82 uint8_t wd[9];
83 wd[0] = (uint8_t) CMD::SEQ_WRITE;
84 for (uint8_t i = 0; i < 4; i++) {
85 wd[i * 2 + 1] = ((uint8_t) reg_[i].vref << 7) | ((uint8_t) reg_[i].pd << 5) | ((uint8_t) reg_[i].gain << 4) |
86 (reg_[i].data >> 8);
87 wd[i * 2 + 2] = reg_[i].data & 0xFF;
88 }
89 auto err = this->write(wd, sizeof(wd));
90 return err == i2c::ERROR_OK;
91}
92
94 reg_[channel].vref = vref;
95
96 this->update_ = true;
97}
98
100 reg_[channel].pd = pd;
101
102 this->update_ = true;
103}
104
106 reg_[channel].gain = gain;
107
108 this->update_ = true;
109}
110
111} // namespace esphome::mcp4728
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
void status_clear_error()
Definition component.h:312
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:183
void set_channel_value_(MCP4728ChannelIdx channel, uint16_t value)
Definition mcp4728.cpp:45
void select_vref_(MCP4728ChannelIdx channel, MCP4728Vref vref)
Definition mcp4728.cpp:93
void select_gain_(MCP4728ChannelIdx channel, MCP4728Gain gain)
Definition mcp4728.cpp:105
void select_power_down_(MCP4728ChannelIdx channel, MCP4728PwrDown pd)
Definition mcp4728.cpp:99
AlsGain501 gain
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:12
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:14