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