ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
pca9685_output.cpp
Go to the documentation of this file.
1#include "pca9685_output.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace pca9685 {
8
9static const char *const TAG = "pca9685";
10
11// PCA9685 mode constants are now inline constexpr in pca9685_output.h
12
13static const uint8_t PCA9685_REGISTER_SOFTWARE_RESET = 0x06;
14static const uint8_t PCA9685_REGISTER_MODE1 = 0x00;
15static const uint8_t PCA9685_REGISTER_MODE2 = 0x01;
16static const uint8_t PCA9685_REGISTER_LED0 = 0x06;
17static const uint8_t PCA9685_REGISTER_PRE_SCALE = 0xFE;
18
19static const uint8_t PCA9685_MODE1_RESTART = 0b10000000;
20static const uint8_t PCA9685_MODE1_EXTCLK = 0b01000000;
21static const uint8_t PCA9685_MODE1_AUTOINC = 0b00100000;
22static const uint8_t PCA9685_MODE1_SLEEP = 0b00010000;
23
25 ESP_LOGV(TAG, " Resetting devices");
26 if (!this->write_bytes(PCA9685_REGISTER_SOFTWARE_RESET, nullptr, 0)) {
27 this->mark_failed();
28 return;
29 }
30
31 if (!this->write_byte(PCA9685_REGISTER_MODE1, PCA9685_MODE1_RESTART | PCA9685_MODE1_AUTOINC)) {
32 this->mark_failed();
33 return;
34 }
35 if (!this->write_byte(PCA9685_REGISTER_MODE2, this->mode_)) {
36 this->mark_failed();
37 return;
38 }
39
40 uint8_t mode1;
41 if (!this->read_byte(PCA9685_REGISTER_MODE1, &mode1)) {
42 this->mark_failed();
43 return;
44 }
45 mode1 = (mode1 & ~PCA9685_MODE1_RESTART) | PCA9685_MODE1_SLEEP;
46 if (!this->write_byte(PCA9685_REGISTER_MODE1, mode1)) {
47 this->mark_failed();
48 return;
49 }
50
51 int pre_scaler = 3;
52 if (this->extclk_) {
53 mode1 = mode1 | PCA9685_MODE1_EXTCLK;
54 if (!this->write_byte(PCA9685_REGISTER_MODE1, mode1)) {
55 this->mark_failed();
56 return;
57 }
58 } else {
59 pre_scaler = static_cast<int>((25000000 / (4096 * this->frequency_)) - 1);
60 pre_scaler = clamp(pre_scaler, 3, 255);
61
62 ESP_LOGV(TAG, " -> Prescaler: %d", pre_scaler);
63 }
64 if (!this->write_byte(PCA9685_REGISTER_PRE_SCALE, pre_scaler)) {
65 this->mark_failed();
66 return;
67 }
68
69 mode1 = (mode1 & ~PCA9685_MODE1_SLEEP) | PCA9685_MODE1_RESTART;
70 if (!this->write_byte(PCA9685_REGISTER_MODE1, mode1)) {
71 this->mark_failed();
72 return;
73 }
75
76 this->loop();
77}
78
80 ESP_LOGCONFIG(TAG,
81 "PCA9685:\n"
82 " Mode: 0x%02X",
83 this->mode_);
84 if (this->extclk_) {
85 ESP_LOGCONFIG(TAG, " EXTCLK: enabled");
86 } else {
87 ESP_LOGCONFIG(TAG,
88 " EXTCLK: disabled\n"
89 " Frequency: %.0f Hz",
90 this->frequency_);
91 }
92 if (this->is_failed()) {
93 ESP_LOGE(TAG, "Setting up PCA9685 failed!");
94 }
95}
96
98 if (this->min_channel_ == 0xFF || !this->update_)
99 return;
100
101 const uint16_t num_channels = this->max_channel_ - this->min_channel_ + 1;
102 const uint16_t phase_delta_begin = 4096 / num_channels;
103 for (uint8_t channel = this->min_channel_; channel <= this->max_channel_; channel++) {
104 uint16_t phase_begin;
105 switch (this->balancer_) {
107 phase_begin = 0;
108 break;
110 phase_begin = (channel - this->min_channel_) * phase_delta_begin;
111 break;
112 default:
113 ESP_LOGE(TAG, "Unknown phase balancer %d", static_cast<int>(this->balancer_));
114 return;
115 }
116 uint16_t phase_end;
117 uint16_t amount = this->pwm_amounts_[channel];
118 if (amount == 0) {
119 phase_end = 4096;
120 } else if (amount >= 4096) {
121 phase_begin = 4096;
122 phase_end = 0;
123 } else {
124 phase_end = phase_begin + amount;
125 if (phase_end >= 4096)
126 phase_end -= 4096;
127 }
128
129 ESP_LOGVV(TAG, "Channel %02u: amount=%04u phase_begin=%04u phase_end=%04u", channel, amount, phase_begin,
130 phase_end);
131
132 uint8_t data[4];
133 data[0] = phase_begin & 0xFF;
134 data[1] = (phase_begin >> 8) & 0xFF;
135 data[2] = phase_end & 0xFF;
136 data[3] = (phase_end >> 8) & 0xFF;
137
138 uint8_t reg = PCA9685_REGISTER_LED0 + 4 * channel;
139 if (!this->write_bytes(reg, data, 4)) {
140 this->status_set_warning();
141 return;
142 }
143 }
144
145 this->status_clear_warning();
146 this->update_ = false;
147}
148
150 auto c = channel->channel_;
151 this->min_channel_ = std::min(this->min_channel_, c);
152 this->max_channel_ = std::max(this->max_channel_, c);
153 channel->set_parent(this);
154}
155
157 const uint16_t max_duty = 4096;
158 const float duty_rounded = roundf(state * max_duty);
159 auto duty = static_cast<uint16_t>(duty_rounded);
160 this->parent_->set_channel_value_(this->channel_, duty);
161}
162
163} // namespace pca9685
164} // namespace esphome
void mark_failed()
Mark this component as failed.
bool is_failed() const
void status_set_warning(const char *message=nullptr)
void status_clear_warning()
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
I2CRegister reg(uint8_t a_register)
calls the I2CRegister constructor
Definition i2c.h:152
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:251
void write_state(float state) override
void set_parent(PCA9685Output *parent)
void set_channel_value_(uint8_t channel, uint16_t value)
void register_channel(PCA9685Channel *channel)
bool state
Definition fan.h:2
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:29