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