ESPHome  2022.6.3
mcp4728_output.cpp
Go to the documentation of this file.
1 #include "mcp4728_output.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 
114  const uint16_t max_duty = 4095;
115  const float duty_rounded = roundf(state * max_duty);
116  auto duty = static_cast<uint16_t>(duty_rounded);
117  this->parent_->set_channel_value_(this->channel_, duty);
118 }
119 
120 } // namespace mcp4728
121 } // namespace esphome
void set_channel_value_(MCP4728ChannelIdx channel, uint16_t value)
void select_power_down_(MCP4728ChannelIdx channel, MCP4728PwrDown pd)
void select_vref_(MCP4728ChannelIdx channel, MCP4728Vref vref)
void select_gain_(MCP4728ChannelIdx channel, MCP4728Gain gain)
ErrorCode write(const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:56
void write_state(float state) override
uint8_t address_
Definition: i2c.h:130
void status_clear_error()
Definition: component.cpp:149
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:111
Definition: a4988.cpp:4
bool state
Definition: fan.h:34