ESPHome  2024.10.2
mcp4728.cpp
Go to the documentation of this file.
1 #include "mcp4728.h"
2 
3 #include "esphome/core/helpers.h"
4 #include "esphome/core/log.h"
5 
6 namespace esphome {
7 namespace mcp4728 {
8 
9 static const char *const TAG = "mcp4728";
10 
12  ESP_LOGCONFIG(TAG, "Setting up MCP4728 (0x%02X)...", this->address_);
13  auto err = this->write(nullptr, 0);
14  if (err != i2c::ERROR_OK) {
15  this->mark_failed();
16  return;
17  }
18 }
19 
21  ESP_LOGCONFIG(TAG, "MCP4728:");
22  LOG_I2C_DEVICE(this);
23  if (this->is_failed()) {
24  ESP_LOGE(TAG, "Communication with MCP4728 failed!");
25  }
26 }
27 
29  if (this->update_) {
30  this->update_ = false;
31  if (this->store_in_eeprom_) {
32  if (!this->seq_write_()) {
33  this->status_set_error();
34  } else {
35  this->status_clear_error();
36  }
37  } else {
38  if (!this->multi_write_()) {
39  this->status_set_error();
40  } else {
41  this->status_clear_error();
42  }
43  }
44  }
45 }
46 
48  uint8_t cn = 0;
49  if (channel == MCP4728_CHANNEL_A) {
50  cn = 'A';
51  } else if (channel == MCP4728_CHANNEL_B) {
52  cn = 'B';
53  } else if (channel == MCP4728_CHANNEL_C) {
54  cn = 'C';
55  } else {
56  cn = 'D';
57  }
58  ESP_LOGV(TAG, "Setting MCP4728 channel %c to %d!", cn, value);
59  reg_[channel].data = value;
60  this->update_ = true;
61 }
62 
64  i2c::ErrorCode err[4];
65  for (uint8_t i = 0; i < 4; ++i) {
66  uint8_t wd[3];
67  wd[0] = ((uint8_t) CMD::MULTI_WRITE | (i << 1)) & 0xFE;
68  wd[1] = ((uint8_t) reg_[i].vref << 7) | ((uint8_t) reg_[i].pd << 5) | ((uint8_t) reg_[i].gain << 4) |
69  (reg_[i].data >> 8);
70  wd[2] = reg_[i].data & 0xFF;
71  err[i] = this->write(wd, sizeof(wd));
72  }
73  bool ok = true;
74  for (auto &e : err) {
75  if (e != i2c::ERROR_OK) {
76  ok = false;
77  break;
78  }
79  }
80  return ok;
81 }
82 
84  uint8_t wd[9];
85  wd[0] = (uint8_t) CMD::SEQ_WRITE;
86  for (uint8_t i = 0; i < 4; i++) {
87  wd[i * 2 + 1] = ((uint8_t) reg_[i].vref << 7) | ((uint8_t) reg_[i].pd << 5) | ((uint8_t) reg_[i].gain << 4) |
88  (reg_[i].data >> 8);
89  wd[i * 2 + 2] = reg_[i].data & 0xFF;
90  }
91  auto err = this->write(wd, sizeof(wd));
92  return err == i2c::ERROR_OK;
93 }
94 
96  reg_[channel].vref = vref;
97 
98  this->update_ = true;
99 }
100 
102  reg_[channel].pd = pd;
103 
104  this->update_ = true;
105 }
106 
108  reg_[channel].gain = gain;
109 
110  this->update_ = true;
111 }
112 
113 } // namespace mcp4728
114 } // namespace esphome
void set_channel_value_(MCP4728ChannelIdx channel, uint16_t value)
Definition: mcp4728.cpp:47
void select_power_down_(MCP4728ChannelIdx channel, MCP4728PwrDown pd)
Definition: mcp4728.cpp:101
bool is_failed() const
Definition: component.cpp:143
void select_vref_(MCP4728ChannelIdx channel, MCP4728Vref vref)
Definition: mcp4728.cpp:95
AlsGain501 gain
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:186
void status_set_error(const char *message="unspecified")
Definition: component.cpp:159
No error found during execution of method.
Definition: i2c_bus.h:13
void select_gain_(MCP4728ChannelIdx channel, MCP4728Gain gain)
Definition: mcp4728.cpp:107
uint8_t address_
store the address of the device on the bus
Definition: i2c.h:269
void status_clear_error()
Definition: component.cpp:172
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11