ESPHome  2022.6.3
pca9685_output.cpp
Go to the documentation of this file.
1 #include "pca9685_output.h"
2 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
4 #include "esphome/core/hal.h"
5 
6 namespace esphome {
7 namespace pca9685 {
8 
9 static const char *const TAG = "pca9685";
10 
11 const uint8_t PCA9685_MODE_INVERTED = 0x10;
12 const uint8_t PCA9685_MODE_OUTPUT_ONACK = 0x08;
13 const uint8_t PCA9685_MODE_OUTPUT_TOTEM_POLE = 0x04;
14 const uint8_t PCA9685_MODE_OUTNE_HIGHZ = 0x02;
15 const uint8_t PCA9685_MODE_OUTNE_LOW = 0x01;
16 
17 static const uint8_t PCA9685_REGISTER_SOFTWARE_RESET = 0x06;
18 static const uint8_t PCA9685_REGISTER_MODE1 = 0x00;
19 static const uint8_t PCA9685_REGISTER_MODE2 = 0x01;
20 static const uint8_t PCA9685_REGISTER_LED0 = 0x06;
21 static const uint8_t PCA9685_REGISTER_PRE_SCALE = 0xFE;
22 
23 static const uint8_t PCA9685_MODE1_RESTART = 0b10000000;
24 static const uint8_t PCA9685_MODE1_AUTOINC = 0b00100000;
25 static const uint8_t PCA9685_MODE1_SLEEP = 0b00010000;
26 
28  ESP_LOGCONFIG(TAG, "Setting up PCA9685OutputComponent...");
29 
30  ESP_LOGV(TAG, " Resetting devices...");
31  if (!this->write_bytes(PCA9685_REGISTER_SOFTWARE_RESET, nullptr, 0)) {
32  this->mark_failed();
33  return;
34  }
35 
36  if (!this->write_byte(PCA9685_REGISTER_MODE1, PCA9685_MODE1_RESTART | PCA9685_MODE1_AUTOINC)) {
37  this->mark_failed();
38  return;
39  }
40  if (!this->write_byte(PCA9685_REGISTER_MODE2, this->mode_)) {
41  this->mark_failed();
42  return;
43  }
44 
45  int pre_scaler = static_cast<int>((25000000 / (4096 * this->frequency_)) - 1);
46  if (pre_scaler > 255)
47  pre_scaler = 255;
48  if (pre_scaler < 3)
49  pre_scaler = 3;
50 
51  ESP_LOGV(TAG, " -> Prescaler: %d", pre_scaler);
52 
53  uint8_t mode1;
54  if (!this->read_byte(PCA9685_REGISTER_MODE1, &mode1)) {
55  this->mark_failed();
56  return;
57  }
58  mode1 = (mode1 & ~PCA9685_MODE1_RESTART) | PCA9685_MODE1_SLEEP;
59  if (!this->write_byte(PCA9685_REGISTER_MODE1, mode1)) {
60  this->mark_failed();
61  return;
62  }
63  if (!this->write_byte(PCA9685_REGISTER_PRE_SCALE, pre_scaler)) {
64  this->mark_failed();
65  return;
66  }
67 
68  mode1 = (mode1 & ~PCA9685_MODE1_SLEEP) | PCA9685_MODE1_RESTART;
69  if (!this->write_byte(PCA9685_REGISTER_MODE1, mode1)) {
70  this->mark_failed();
71  return;
72  }
73  delayMicroseconds(500);
74 
75  this->loop();
76 }
77 
79  ESP_LOGCONFIG(TAG, "PCA9685:");
80  ESP_LOGCONFIG(TAG, " Mode: 0x%02X", this->mode_);
81  ESP_LOGCONFIG(TAG, " Frequency: %.0f Hz", this->frequency_);
82  if (this->is_failed()) {
83  ESP_LOGE(TAG, "Setting up PCA9685 failed!");
84  }
85 }
86 
88  if (this->min_channel_ == 0xFF || !this->update_)
89  return;
90 
91  const uint16_t num_channels = this->max_channel_ - this->min_channel_ + 1;
92  for (uint8_t channel = this->min_channel_; channel <= this->max_channel_; channel++) {
93  uint16_t phase_begin = uint16_t(channel - this->min_channel_) / num_channels * 4096;
94  uint16_t phase_end;
95  uint16_t amount = this->pwm_amounts_[channel];
96  if (amount == 0) {
97  phase_end = 4096;
98  } else if (amount >= 4096) {
99  phase_begin = 4096;
100  phase_end = 0;
101  } else {
102  phase_end = phase_begin + amount;
103  if (phase_end >= 4096)
104  phase_end -= 4096;
105  }
106 
107  ESP_LOGVV(TAG, "Channel %02u: amount=%04u phase_begin=%04u phase_end=%04u", channel, amount, phase_begin,
108  phase_end);
109 
110  uint8_t data[4];
111  data[0] = phase_begin & 0xFF;
112  data[1] = (phase_begin >> 8) & 0xFF;
113  data[2] = phase_end & 0xFF;
114  data[3] = (phase_end >> 8) & 0xFF;
115 
116  uint8_t reg = PCA9685_REGISTER_LED0 + 4 * channel;
117  if (!this->write_bytes(reg, data, 4)) {
118  this->status_set_warning();
119  return;
120  }
121  }
122 
123  this->status_clear_warning();
124  this->update_ = false;
125 }
126 
128  auto c = channel->channel_;
129  this->min_channel_ = std::min(this->min_channel_, c);
130  this->max_channel_ = std::max(this->max_channel_, c);
131  channel->set_parent(this);
132 }
133 
135  const uint16_t max_duty = 4096;
136  const float duty_rounded = roundf(state * max_duty);
137  auto duty = static_cast<uint16_t>(duty_rounded);
138  this->parent_->set_channel_value_(this->channel_, duty);
139 }
140 
141 } // namespace pca9685
142 } // namespace esphome
bool read_byte(uint8_t a_register, uint8_t *data, bool stop=true)
Definition: i2c.h:96
const uint8_t PCA9685_MODE_OUTPUT_ONACK
Channel update happens upon ACK (post-set) rather than on STOP (endTransmission)
I2CRegister reg(uint8_t a_register)
Definition: i2c.h:46
const uint8_t PCA9685_MODE_INVERTED
Inverts polarity of channel output signal.
void set_parent(PCA9685Output *parent)
void register_channel(PCA9685Channel *channel)
void status_clear_warning()
Definition: component.cpp:148
void status_set_warning()
Definition: component.cpp:140
void write_state(float state) override
bool write_byte(uint8_t a_register, uint8_t data, bool stop=true)
Definition: i2c.h:123
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:111
Definition: a4988.cpp:4
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:29
const uint8_t PCA9685_MODE_OUTPUT_TOTEM_POLE
Use a totem-pole (push-pull) style output rather than an open-drain structure.
const uint8_t PCA9685_MODE_OUTNE_HIGHZ
For active low output enable, sets channel output to high-impedance state.
bool state
Definition: fan.h:34
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len, bool stop=true)
Definition: i2c.h:109
const uint8_t PCA9685_MODE_OUTNE_LOW
Similarly, sets channel output to high if in totem-pole mode, otherwise.