ESPHome  2023.5.5
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  if (this->outenable_pin_ != nullptr)
31  LOG_PIN(" OE Pin: ", this->outenable_pin_);
32  ESP_LOGCONFIG(TAG, " Number of chips: %u", this->num_chips_);
33 }
34 
35 void TLC5947::loop() {
36  if (!this->update_)
37  return;
38 
39  this->lat_pin_->digital_write(false);
40 
41  // push the data out, MSB first, 12 bit word per channel, 24 channels per chip
42  for (int32_t ch = N_CHANNELS_PER_CHIP * num_chips_ - 1; ch >= 0; ch--) {
43  uint16_t word = pwm_amounts_[ch];
44  for (uint8_t bit = 0; bit < 12; bit++) {
45  this->clock_pin_->digital_write(false);
46  this->data_pin_->digital_write(word & 0x800);
47  word <<= 1;
48 
49  this->clock_pin_->digital_write(true);
50  this->clock_pin_->digital_write(true); // TWH0>12ns, so we should be fine using this as delay
51  }
52  }
53 
54  this->clock_pin_->digital_write(false);
55 
56  // latch the values, so they will be applied
57  this->lat_pin_->digital_write(true);
58  delayMicroseconds(1); // TWH1 > 30ns
59  this->lat_pin_->digital_write(false);
60 
61  this->update_ = false;
62 }
63 
64 } // namespace tlc5947
65 } // namespace esphome
virtual void digital_write(bool value)=0
std::vector< uint16_t > pwm_amounts_
Definition: tlc5947.h:65
void setup() override
Definition: tlc5947.cpp:9
virtual void setup()=0
const uint8_t N_CHANNELS_PER_CHIP
Definition: tlc5947.h:17
void dump_config() override
Definition: tlc5947.cpp:25
Definition: a4988.cpp:4
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition: core.cpp:30
void loop() override
Send new values if they were updated.
Definition: tlc5947.cpp:35
GPIOPin * outenable_pin_
Definition: tlc5947.h:62