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