ESPHome  2022.6.3
tlc5947.h
Go to the documentation of this file.
1 #pragma once
2 // TLC5947 24-Channel, 12-Bit PWM LED Driver
3 // https://www.ti.com/lit/ds/symlink/tlc5947.pdf
4 
6 #include "esphome/core/hal.h"
8 #include <vector>
9 
10 namespace esphome {
11 namespace tlc5947 {
12 
13 class TLC5947 : public Component {
14  public:
15  class Channel;
16 
17  const uint8_t N_CHANNELS_PER_CHIP = 24;
18 
19  void set_data_pin(GPIOPin *data_pin) { data_pin_ = data_pin; }
20  void set_clock_pin(GPIOPin *clock_pin) { clock_pin_ = clock_pin; }
21  void set_lat_pin(GPIOPin *lat_pin) { lat_pin_ = lat_pin; }
22  void set_outenable_pin(GPIOPin *outenable_pin) { outenable_pin_ = outenable_pin; }
23  void set_num_chips(uint8_t num_chips) { num_chips_ = num_chips; }
24 
25  void setup() override;
26 
27  void dump_config() override;
28 
29  float get_setup_priority() const override { return setup_priority::HARDWARE; }
30 
32  void loop() override;
33 
34  class Channel : public output::FloatOutput {
35  public:
36  void set_parent(TLC5947 *parent) { parent_ = parent; }
37  void set_channel(uint8_t channel) { channel_ = channel; }
38 
39  protected:
40  void write_state(float state) override {
41  auto amount = static_cast<uint16_t>(state * 0xfff);
42  this->parent_->set_channel_value_(this->channel_, amount);
43  }
44 
46  uint8_t channel_;
47  };
48 
49  protected:
50  void set_channel_value_(uint16_t channel, uint16_t value) {
51  if (channel >= this->num_chips_ * N_CHANNELS_PER_CHIP)
52  return;
53  if (this->pwm_amounts_[channel] != value) {
54  this->update_ = true;
55  }
56  this->pwm_amounts_[channel] = value;
57  }
58 
63  uint8_t num_chips_;
64 
65  std::vector<uint16_t> pwm_amounts_;
66  bool update_{true};
67 };
68 
69 } // namespace tlc5947
70 } // namespace esphome
std::vector< uint16_t > pwm_amounts_
Definition: tlc5947.h:65
void set_channel_value_(uint16_t channel, uint16_t value)
Definition: tlc5947.h:50
void setup() override
Definition: tlc5947.cpp:9
void write_state(float state) override
Definition: tlc5947.h:40
Base class for all output components that can output a variable level, like PWM.
Definition: float_output.h:31
void set_data_pin(GPIOPin *data_pin)
Definition: tlc5947.h:19
void set_outenable_pin(GPIOPin *outenable_pin)
Definition: tlc5947.h:22
void set_clock_pin(GPIOPin *clock_pin)
Definition: tlc5947.h:20
void set_num_chips(uint8_t num_chips)
Definition: tlc5947.h:23
const uint8_t N_CHANNELS_PER_CHIP
Definition: tlc5947.h:17
void set_parent(TLC5947 *parent)
Definition: tlc5947.h:36
float get_setup_priority() const override
Definition: tlc5947.h:29
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
void dump_config() override
Definition: tlc5947.cpp:25
Definition: a4988.cpp:4
void set_lat_pin(GPIOPin *lat_pin)
Definition: tlc5947.h:21
void set_channel(uint8_t channel)
Definition: tlc5947.h:37
void loop() override
Send new values if they were updated.
Definition: tlc5947.cpp:35
bool state
Definition: fan.h:34
GPIOPin * outenable_pin_
Definition: tlc5947.h:62