ESPHome  2024.10.2
tlc5947.cpp
Go to the documentation of this file.
1 #include "tlc5947.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace tlc5947 {
6 
7 static const char *const TAG = "tlc5947";
8 
9 void TLC5947::setup() {
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 
23  ESP_LOGCONFIG(TAG, "Done setting up TLC5947 output component.");
24 }
26  ESP_LOGCONFIG(TAG, "TLC5947:");
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  ESP_LOGCONFIG(TAG, " Number of chips: %u", this->num_chips_);
32 }
33 
34 void TLC5947::loop() {
35  if (!this->update_)
36  return;
37 
38  this->lat_pin_->digital_write(false);
39 
40  // push the data out, MSB first, 12 bit word per channel, 24 channels per chip
41  for (int32_t ch = N_CHANNELS_PER_CHIP * num_chips_ - 1; ch >= 0; ch--) {
42  uint16_t word = pwm_amounts_[ch];
43  for (uint8_t bit = 0; bit < 12; bit++) {
44  this->clock_pin_->digital_write(false);
45  this->data_pin_->digital_write(word & 0x800);
46  word <<= 1;
47 
48  this->clock_pin_->digital_write(true);
49  this->clock_pin_->digital_write(true); // TWH0>12ns, so we should be fine using this as delay
50  }
51  }
52 
53  this->clock_pin_->digital_write(false);
54 
55  // latch the values, so they will be applied
56  this->lat_pin_->digital_write(true);
57  delayMicroseconds(1); // TWH1 > 30ns
58  this->lat_pin_->digital_write(false);
59 
60  this->update_ = false;
61 }
62 
63 void TLC5947::set_channel_value(uint16_t channel, uint16_t value) {
64  if (channel >= this->num_chips_ * N_CHANNELS_PER_CHIP)
65  return;
66  if (this->pwm_amounts_[channel] != value) {
67  this->update_ = true;
68  }
69  this->pwm_amounts_[channel] = value;
70 }
71 
72 } // namespace tlc5947
73 } // namespace esphome
virtual void digital_write(bool value)=0
std::vector< uint16_t > pwm_amounts_
Definition: tlc5947.h:41
void setup() override
Definition: tlc5947.cpp:9
virtual void setup()=0
const uint8_t N_CHANNELS_PER_CHIP
Definition: tlc5947.h:15
const char *const TAG
Definition: spi.cpp:8
void set_channel_value(uint16_t channel, uint16_t value)
Definition: tlc5947.cpp:63
void dump_config() override
Definition: tlc5947.cpp:25
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:28
void loop() override
Send new values if they were updated.
Definition: tlc5947.cpp:34
GPIOPin * outenable_pin_
Definition: tlc5947.h:38