ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
tlc5947.cpp
Go to the documentation of this file.
1#include "tlc5947.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace tlc5947 {
6
7static const char *const TAG = "tlc5947";
8
10 this->data_pin_->setup();
11 this->data_pin_->digital_write(true);
12 this->clock_pin_->setup();
13 this->clock_pin_->digital_write(true);
14 this->lat_pin_->setup();
15 this->lat_pin_->digital_write(true);
16 if (this->outenable_pin_ != nullptr) {
17 this->outenable_pin_->setup();
18 this->outenable_pin_->digital_write(false);
19 }
20
21 this->pwm_amounts_.resize(this->num_chips_ * N_CHANNELS_PER_CHIP, 0);
22}
24 ESP_LOGCONFIG(TAG, "TLC5947:");
25 LOG_PIN(" Data Pin: ", this->data_pin_);
26 LOG_PIN(" Clock Pin: ", this->clock_pin_);
27 LOG_PIN(" LAT Pin: ", this->lat_pin_);
28 LOG_PIN(" OE Pin: ", this->outenable_pin_);
29 ESP_LOGCONFIG(TAG, " Number of chips: %u", this->num_chips_);
30}
31
33 if (!this->update_)
34 return;
35
36 this->lat_pin_->digital_write(false);
37
38 // push the data out, MSB first, 12 bit word per channel, 24 channels per chip
39 for (int32_t ch = N_CHANNELS_PER_CHIP * num_chips_ - 1; ch >= 0; ch--) {
40 uint16_t word = pwm_amounts_[ch];
41 for (uint8_t bit = 0; bit < 12; bit++) {
42 this->clock_pin_->digital_write(false);
43 this->data_pin_->digital_write(word & 0x800);
44 word <<= 1;
45
46 this->clock_pin_->digital_write(true);
47 this->clock_pin_->digital_write(true); // TWH0>12ns, so we should be fine using this as delay
48 }
49 }
50
51 this->clock_pin_->digital_write(false);
52
53 // latch the values, so they will be applied
54 this->lat_pin_->digital_write(true);
55 delayMicroseconds(1); // TWH1 > 30ns
56 this->lat_pin_->digital_write(false);
57
58 this->update_ = false;
59}
60
61void TLC5947::set_channel_value(uint16_t channel, uint16_t value) {
62 if (channel >= this->num_chips_ * N_CHANNELS_PER_CHIP)
63 return;
64 if (this->pwm_amounts_[channel] != value) {
65 this->update_ = true;
66 }
67 this->pwm_amounts_[channel] = value;
68}
69
70} // namespace tlc5947
71} // namespace esphome
virtual void setup()=0
virtual void digital_write(bool value)=0
void loop() override
Send new values if they were updated.
Definition tlc5947.cpp:32
const uint8_t N_CHANNELS_PER_CHIP
Definition tlc5947.h:15
void set_channel_value(uint16_t channel, uint16_t value)
Definition tlc5947.cpp:61
void dump_config() override
Definition tlc5947.cpp:23
std::vector< uint16_t > pwm_amounts_
Definition tlc5947.h:41
void setup() override
Definition tlc5947.cpp:9
const char *const TAG
Definition spi.cpp:8
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:31