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