ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
tlc5971.cpp
Go to the documentation of this file.
1#include "tlc5971.h"
2#include "esphome/core/log.h"
3
4namespace esphome::tlc5971 {
5
6static const char *const TAG = "tlc5971";
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
14 this->pwm_amounts_.resize(this->num_chips_ * N_CHANNELS_PER_CHIP, 0);
15}
17 ESP_LOGCONFIG(TAG,
18 "TLC5971:\n"
19 " Number of chips: %u",
20 this->num_chips_);
21 LOG_PIN(" Data Pin: ", this->data_pin_);
22 LOG_PIN(" Clock Pin: ", this->clock_pin_);
23}
24
26 if (!this->update_) {
27 this->disable_loop();
28 return;
29 }
30
31 uint32_t command;
32
33 // Magic word for write
34 command = 0x25;
35
36 command <<= 5;
37 // OUTTMG = 1, EXTGCK = 0, TMGRST = 1, DSPRPT = 1, BLANK = 0 -> 0x16
38 command |= 0x16;
39
40 command <<= 7;
41 command |= 0x7F; // default 100% brigthness
42
43 command <<= 7;
44 command |= 0x7F; // default 100% brigthness
45
46 command <<= 7;
47 command |= 0x7F; // default 100% brigthness
48
49 for (uint8_t n = 0; n < num_chips_; n++) {
50 this->transfer_(command >> 24);
51 this->transfer_(command >> 16);
52 this->transfer_(command >> 8);
53 this->transfer_(command);
54
55 // 12 channels per TLC59711
56 for (int8_t c = 11; c >= 0; c--) {
57 // 16 bits per channel, send MSB first
58 this->transfer_(pwm_amounts_.at(n * 12 + c) >> 8);
59 this->transfer_(pwm_amounts_.at(n * 12 + c));
60 }
61 }
62
63 this->update_ = false;
64}
65
66void TLC5971::transfer_(uint8_t send) {
67 uint8_t startbit = 0x80;
68
69 bool towrite, lastmosi = !(send & startbit);
70
71 for (uint8_t b = startbit; b != 0; b = b >> 1) {
72 towrite = send & b;
73 if ((lastmosi != towrite)) {
74 this->data_pin_->digital_write(towrite);
75 lastmosi = towrite;
76 }
77
78 this->clock_pin_->digital_write(true);
79 this->clock_pin_->digital_write(false);
80 }
81}
82void TLC5971::set_channel_value(uint16_t channel, uint16_t value) {
83 if (channel >= this->num_chips_ * N_CHANNELS_PER_CHIP)
84 return;
85 if (this->pwm_amounts_[channel] != value) {
86 this->update_ = true;
87 this->enable_loop();
88 }
89 this->pwm_amounts_[channel] = value;
90}
91
92} // namespace esphome::tlc5971
void enable_loop()
Enable this component's loop.
Definition component.h:246
void disable_loop()
Disable this component's loop.
virtual void setup()=0
virtual void digital_write(bool value)=0
void transfer_(uint8_t send)
Definition tlc5971.cpp:66
std::vector< uint16_t > pwm_amounts_
Definition tlc5971.h:38
void setup() override
Definition tlc5971.cpp:8
void set_channel_value(uint16_t channel, uint16_t value)
Definition tlc5971.cpp:82
const uint8_t N_CHANNELS_PER_CHIP
Definition tlc5971.h:14
void dump_config() override
Definition tlc5971.cpp:16
void loop() override
Send new values if they were updated.
Definition tlc5971.cpp:25
const char *const TAG
Definition spi.cpp:7
static void uint32_t