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