ESPHome  2024.12.2
fastled_light.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ARDUINO
4 
6 #include "esphome/core/helpers.h"
8 
9 #define FASTLED_ESP8266_RAW_PIN_ORDER
10 #define FASTLED_ESP32_RAW_PIN_ORDER
11 #define FASTLED_RMT_BUILTIN_DRIVER true
12 
13 // Avoid annoying compiler messages
14 #define FASTLED_INTERNAL
15 
16 #include "FastLED.h"
17 
18 namespace esphome {
19 namespace fastled_base {
20 
22  public:
24  CLEDController *get_controller() const { return this->controller_; }
25 
26  inline int32_t size() const override { return this->num_leds_; }
27 
29  void set_max_refresh_rate(uint32_t interval_us) { this->max_refresh_rate_ = interval_us; }
30 
32  CLEDController &add_leds(CLEDController *controller, int num_leds) {
33  this->controller_ = controller;
34  this->num_leds_ = num_leds;
35  this->leds_ = new CRGB[num_leds]; // NOLINT
36 
37  for (int i = 0; i < this->num_leds_; i++)
38  this->leds_[i] = CRGB::Black;
39 
40  return *this->controller_;
41  }
42 
43  template<ESPIChipsets CHIPSET, uint8_t DATA_PIN, uint8_t CLOCK_PIN, EOrder RGB_ORDER, uint32_t SPI_DATA_RATE>
44  CLEDController &add_leds(int num_leds) {
45  switch (CHIPSET) {
46  case LPD8806: {
47  static LPD8806Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> controller;
48  return add_leds(&controller, num_leds);
49  }
50  case WS2801: {
51  static WS2801Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> controller;
52  return add_leds(&controller, num_leds);
53  }
54  case WS2803: {
55  static WS2803Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> controller;
56  return add_leds(&controller, num_leds);
57  }
58  case SM16716: {
59  static SM16716Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> controller;
60  return add_leds(&controller, num_leds);
61  }
62  case P9813: {
63  static P9813Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> controller;
64  return add_leds(&controller, num_leds);
65  }
66  case DOTSTAR:
67  case APA102: {
68  static APA102Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> controller;
69  return add_leds(&controller, num_leds);
70  }
71  case SK9822: {
72  static SK9822Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> controller;
73  return add_leds(&controller, num_leds);
74  }
75  }
76  }
77 
78  template<ESPIChipsets CHIPSET, uint8_t DATA_PIN, uint8_t CLOCK_PIN> CLEDController &add_leds(int num_leds) {
79  switch (CHIPSET) {
80  case LPD8806: {
81  static LPD8806Controller<DATA_PIN, CLOCK_PIN> controller;
82  return add_leds(&controller, num_leds);
83  }
84  case WS2801: {
85  static WS2801Controller<DATA_PIN, CLOCK_PIN> controller;
86  return add_leds(&controller, num_leds);
87  }
88  case WS2803: {
89  static WS2803Controller<DATA_PIN, CLOCK_PIN> controller;
90  return add_leds(&controller, num_leds);
91  }
92  case SM16716: {
93  static SM16716Controller<DATA_PIN, CLOCK_PIN> controller;
94  return add_leds(&controller, num_leds);
95  }
96  case P9813: {
97  static P9813Controller<DATA_PIN, CLOCK_PIN> controller;
98  return add_leds(&controller, num_leds);
99  }
100  case DOTSTAR:
101  case APA102: {
102  static APA102Controller<DATA_PIN, CLOCK_PIN> controller;
103  return add_leds(&controller, num_leds);
104  }
105  case SK9822: {
106  static SK9822Controller<DATA_PIN, CLOCK_PIN> controller;
107  return add_leds(&controller, num_leds);
108  }
109  }
110  }
111 
112  template<ESPIChipsets CHIPSET, uint8_t DATA_PIN, uint8_t CLOCK_PIN, EOrder RGB_ORDER>
113  CLEDController &add_leds(int num_leds) {
114  switch (CHIPSET) {
115  case LPD8806: {
116  static LPD8806Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> controller;
117  return add_leds(&controller, num_leds);
118  }
119  case WS2801: {
120  static WS2801Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> controller;
121  return add_leds(&controller, num_leds);
122  }
123  case WS2803: {
124  static WS2803Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> controller;
125  return add_leds(&controller, num_leds);
126  }
127  case SM16716: {
128  static SM16716Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> controller;
129  return add_leds(&controller, num_leds);
130  }
131  case P9813: {
132  static P9813Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> controller;
133  return add_leds(&controller, num_leds);
134  }
135  case DOTSTAR:
136  case APA102: {
137  static APA102Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> controller;
138  return add_leds(&controller, num_leds);
139  }
140  case SK9822: {
141  static SK9822Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> controller;
142  return add_leds(&controller, num_leds);
143  }
144  }
145  }
146 
147 #ifdef FASTLED_HAS_CLOCKLESS
148  template<template<uint8_t DATA_PIN, EOrder RGB_ORDER> class CHIPSET, uint8_t DATA_PIN, EOrder RGB_ORDER>
149  CLEDController &add_leds(int num_leds) {
150  static CHIPSET<DATA_PIN, RGB_ORDER> controller;
151  return add_leds(&controller, num_leds);
152  }
153 
154  template<template<uint8_t DATA_PIN, EOrder RGB_ORDER> class CHIPSET, uint8_t DATA_PIN>
155  CLEDController &add_leds(int num_leds) {
156  static CHIPSET<DATA_PIN, RGB> controller;
157  return add_leds(&controller, num_leds);
158  }
159 
160  template<template<uint8_t DATA_PIN> class CHIPSET, uint8_t DATA_PIN> CLEDController &add_leds(int num_leds) {
161  static CHIPSET<DATA_PIN> controller;
162  return add_leds(&controller, num_leds);
163  }
164 #endif
165 
166  template<template<EOrder RGB_ORDER> class CHIPSET, EOrder RGB_ORDER> CLEDController &add_leds(int num_leds) {
167  static CHIPSET<RGB_ORDER> controller;
168  return add_leds(&controller, num_leds);
169  }
170 
171  template<template<EOrder RGB_ORDER> class CHIPSET> CLEDController &add_leds(int num_leds) {
172  static CHIPSET<RGB> controller;
173  return add_leds(&controller, num_leds);
174  }
175 
176 #ifdef FASTLED_HAS_BLOCKLESS
177  template<EBlockChipsets CHIPSET, int NUM_LANES, EOrder RGB_ORDER> CLEDController &add_leds(int num_leds) {
178  switch (CHIPSET) {
179 #ifdef PORTA_FIRST_PIN
180  case WS2811_PORTA:
181  return add_leds(
182  new InlineBlockClocklessController<NUM_LANES, PORTA_FIRST_PIN, NS(320), NS(320), NS(640), RGB_ORDER>(),
183  num_leds);
184  case WS2811_400_PORTA:
185  return add_leds(
186  new InlineBlockClocklessController<NUM_LANES, PORTA_FIRST_PIN, NS(800), NS(800), NS(900), RGB_ORDER>(),
187  num_leds);
188  case WS2813_PORTA:
189  return add_leds(new InlineBlockClocklessController<NUM_LANES, PORTA_FIRST_PIN, NS(320), NS(320), NS(640),
190  RGB_ORDER, 0, false, 300>(),
191  num_leds);
192  case TM1803_PORTA:
193  return add_leds(
194  new InlineBlockClocklessController<NUM_LANES, PORTA_FIRST_PIN, NS(700), NS(1100), NS(700), RGB_ORDER>(),
195  num_leds);
196  case UCS1903_PORTA:
197  return add_leds(
198  new InlineBlockClocklessController<NUM_LANES, PORTA_FIRST_PIN, NS(500), NS(1500), NS(500), RGB_ORDER>(),
199  num_leds);
200 #endif
201  }
202  }
203 
204  template<EBlockChipsets CHIPSET, int NUM_LANES> CLEDController &add_leds(int num_leds) {
205  return add_leds<CHIPSET, NUM_LANES, GRB>(num_leds);
206  }
207 #endif
208 
209  // ========== INTERNAL METHODS ==========
210  // (In most use cases you won't need these)
212  auto traits = light::LightTraits();
213  traits.set_supported_color_modes({light::ColorMode::RGB});
214  return traits;
215  }
216  void setup() override;
217  void dump_config() override;
218  void write_state(light::LightState *state) override;
219  float get_setup_priority() const override { return setup_priority::HARDWARE; }
220 
221  void clear_effect_data() override {
222  for (int i = 0; i < this->size(); i++)
223  this->effect_data_[i] = 0;
224  }
225 
226  protected:
227  light::ESPColorView get_view_internal(int32_t index) const override {
228  return {&this->leds_[index].r, &this->leds_[index].g, &this->leds_[index].b, nullptr,
229  &this->effect_data_[index], &this->correction_};
230  }
231 
232  CLEDController *controller_{nullptr};
233  CRGB *leds_{nullptr};
234  uint8_t *effect_data_{nullptr};
235  int num_leds_{0};
236  uint32_t last_refresh_{0};
238 };
239 
240 } // namespace fastled_base
241 } // namespace esphome
242 
243 #endif // USE_ARDUINO
CLEDController & add_leds(CLEDController *controller, int num_leds)
Add some LEDS, can only be called once.
Definition: fastled_light.h:32
CLEDController & add_leds(int num_leds)
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
CLEDController & add_leds(int num_leds)
Definition: fastled_light.h:44
CLEDController & add_leds(int num_leds)
CLEDController & add_leds(int num_leds)
Definition: fastled_light.h:78
CLEDController & add_leds(int num_leds)
light::LightTraits get_traits() override
This class is used to represent the capabilities of a light.
Definition: light_traits.h:11
CLEDController * get_controller() const
Only for custom effects: Get the internal controller.
Definition: fastled_light.h:24
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:18
light::ESPColorView get_view_internal(int32_t index) const override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Color can be controlled using RGB format (includes a brightness control for the color).
void write_state(light::LightState *state) override
void set_max_refresh_rate(uint32_t interval_us)
Set a maximum refresh rate in µs as some lights do not like being updated too often.
Definition: fastled_light.h:29
bool state
Definition: fan.h:34