FastLED 3.9.15
Loading...
Searching...
No Matches
cled_controller.h
Go to the documentation of this file.
1#pragma once
2
5
6#include "color.h"
7
9#include "dither_mode.h"
11#include "fl/math/screenmap.h"
12#include "fl/stl/int.h"
13#include "fl/stl/bit_cast.h"
14#include "fl/channels/options.h"
15#include "fl/stl/span.h"
16#include "fl/stl/noexcept.h"
17
19//
20// LED Controller interface definition
21//
23
29
30namespace fl {
31
33protected:
34 friend class CFastLED;
38 bool mEnabled = true;
41
47
52
53public:
58
61 bool isInList() const FL_NOEXCEPT;
62
67 virtual void showColor(const CRGB & data, int nLeds, fl::u8 brightness) FL_NOEXCEPT = 0;
68
73 virtual void show(const CRGB *data, int nLeds, fl::u8 brightness) FL_NOEXCEPT = 0;
74
76 // Note that at this time (Sept 13th, 2024) this is only implemented in the ESP32 driver
77 // directly. For an emulated version please see RGBWEmulatedController in chipsets.h
78 //
79 // (#2558) mSettings.mWhiteCfg is now a fl::variant<Empty, Rgbw, Rgbww>;
80 // assigning Rgbw selects the 4-channel alternative. The legacy
81 // "setRgbw(RgbwInvalid::value()) → disable" semantics are preserved
82 // by translating an inactive Rgbw into Empty so observers see the
83 // same "no white channel" state they did before the variant migration.
84 if (!arg.active()) {
85 mSettings.mWhiteCfg.reset();
86 } else {
87 mSettings.mWhiteCfg = arg;
88 }
89 return *this; // builder pattern.
90 }
91
98 if (!arg.active()) {
99 mSettings.mWhiteCfg.reset();
100 } else {
101 mSettings.mWhiteCfg = arg;
102 }
103 return *this;
104 }
105
109 mSettings.mWhiteCfg.reset();
110 return *this;
111 }
112
113 void setEnabled(bool enabled) FL_NOEXCEPT { mEnabled = enabled; }
114 bool getEnabled() FL_NOEXCEPT { return mEnabled; }
115
117 // If we added virtual to the AVR boards then we are going to add 600 bytes of memory to the binary
118 // flash size. This is because the virtual destructor pulls in malloc and free, which are the largest
119 // Testing shows that this virtual destructor adds a 600 bytes to the binary on
120 // attiny85 and about 1k for the teensy 4.X series.
121 // Attiny85:
122 // With CLEDController destructor virtual: 11018 bytes to binary.
123 // Without CLEDController destructor virtual: 10666 bytes to binary.
125
129 Rgbw getRgbw() const FL_NOEXCEPT { return mSettings.rgbw(); }
130
133 Rgbww getRgbww() const FL_NOEXCEPT { return mSettings.rgbww(); }
134
136 virtual void init() FL_NOEXCEPT = 0;
137
142 showLeds(0);
143 }
144
145 // Compatibility with the 3.8.x codebase.
147#if FASTLED_HAS_ENGINE_EVENTS
149#endif
150 void* data = beginShowLeds(mLeds.size());
152 endShowLeds(data);
153#if FASTLED_HAS_ENGINE_EVENTS
156#endif
157 }
158
160
168 void showInternal(const CRGB *data, int nLeds, fl::u8 brightness) FL_NOEXCEPT {
169 if (mEnabled) {
170 show(data, nLeds,brightness);
171 }
172 }
173
181 void showColorInternal(const CRGB &data, int nLeds, fl::u8 brightness) FL_NOEXCEPT {
182 if (mEnabled) {
183 showColor(data, nLeds, brightness);
184 }
185 }
186
191 if (mEnabled) {
192 show(mLeds.data(), mLeds.size(), brightness);
193 }
194 }
195
202 if (mEnabled) {
203 showColor(data, mLeds.size(), brightness);
204 }
205 }
206
209 static CLEDController *head() FL_NOEXCEPT { return mPHead; }
210
214
219 template<typename Visitor>
220 static void visitControllers(Visitor&& visitor) FL_NOEXCEPT {
221 const CLEDController *pCur = head();
222 while(pCur) {
223 visitor(pCur, fl::span<const CRGB>(pCur->leds(), pCur->size()));
224 pCur = pCur->next();
225 }
226 }
227
230 const CLEDController *next() const FL_NOEXCEPT { return mPNext; }
231
236 mLeds = fl::span<CRGB>(data, nLeds);
237 return *this;
238 }
239
243 mLeds = leds;
244 return *this;
245 }
246
248 void clearLedDataInternal(int nLeds = -1) FL_NOEXCEPT;
249
255
260
263 virtual int size() const FL_NOEXCEPT { return mLeds.size(); }
264
267 virtual int lanes() FL_NOEXCEPT { return 1; }
268
271 CRGB* leds() FL_NOEXCEPT { return mLeds.data(); }
272
275 const CRGB* leds() const FL_NOEXCEPT { return mLeds.data(); }
276
280
284 CRGB &operator[](int x) FL_NOEXCEPT { return mLeds[x]; }
285
289 inline CLEDController & setDither(fl::u8 ditherMode = BINARY_DITHER) FL_NOEXCEPT { mSettings.mDitherMode = ditherMode; return *this; }
290
291 CLEDController& setScreenMap(const fl::XYMap& map, float diameter = -1.f) FL_NOEXCEPT {
292 // EngineEvents::onCanvasUiSet(this, map);
293 fl::ScreenMap screenmap = map.toScreenMap();
294 if (diameter <= 0.0f) {
295 screenmap.setDiameter(.15f); // Assume small matrix is being used.
296 }
298 return *this;
299 }
300
305
306 CLEDController& setScreenMap(fl::u16 width, fl::u16 height, float diameter = -1.f) FL_NOEXCEPT {
308 return setScreenMap(xymap, diameter);
309 }
310
313 inline fl::u8 getDither() FL_NOEXCEPT { return mSettings.mDitherMode; }
314
315 virtual void* beginShowLeds(int size) FL_NOEXCEPT {
317 // By default, emit an integer. This integer will, by default, be passed back.
318 // If you override beginShowLeds() then
319 // you should also override endShowLeds() to match the return state.
320 //
321 // For async led controllers this should be used as a sync point to block
322 // the caller until the leds from the last draw frame have completed drawing.
323 // for each controller:
324 // beginShowLeds();
325 // for each controller:
326 // showLeds();
327 // for each controller:
328 // endShowLeds();
329 uintptr_t d = getDither();
330 void* out = fl::int_to_ptr<void>(d);
331 return out;
332 }
333
334 virtual void endShowLeds(void* data) FL_NOEXCEPT {
335 // By default recieves the integer that beginShowLeds() emitted.
336 //For async controllers this should be used to signal the controller
337 // to begin transmitting the current frame to the leds.
338 uintptr_t d = fl::ptr_to_int(data);
339 setDither(static_cast<fl::u8>(d));
340 }
341
345 CLEDController & setCorrection(CRGB correction) FL_NOEXCEPT { mSettings.mCorrection = correction; return *this; }
346
348 CLEDController & setCorrection(LEDColorCorrection correction) FL_NOEXCEPT { mSettings.mCorrection = correction; return *this; }
349
352 CRGB getCorrection() FL_NOEXCEPT { return mSettings.mCorrection; }
353
357 CLEDController & setTemperature(CRGB temperature) FL_NOEXCEPT { mSettings.mTemperature = temperature; return *this; }
358
360 CLEDController & setTemperature(ColorTemperature temperature) FL_NOEXCEPT { mSettings.mTemperature = temperature; return *this; }
361
364 CRGB getTemperature() FL_NOEXCEPT { return mSettings.mTemperature; }
365
372
375 virtual fl::u16 getMaxRefreshRate() const FL_NOEXCEPT { return 0; }
376};
377
378} // namespace fl
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
XYMap xymap
fl::ScreenMap screenmap
FastLED show()
CLEDController * controller
fl::UISlider scale("Scale", 4,.1, 4,.1)
const CLEDController * next() const FL_NOEXCEPT
Get the next controller in the linked list after this one (const version).
CLEDController & setCorrection(CRGB correction) FL_NOEXCEPT
The color corrction to use for this controller, expressed as a CRGB object.
virtual void * beginShowLeds(int size) FL_NOEXCEPT
VIRTUAL_IF_NOT_AVR void showLeds(fl::u8 brightness) FL_NOEXCEPT
void showInternal(const CRGB *data, int nLeds, fl::u8 brightness) FL_NOEXCEPT
void clearLedDataInternal(int nLeds=-1) FL_NOEXCEPT
Zero out the LED data managed by this controller.
CLEDController & setRgbw(const Rgbw &arg=RgbwDefault::value()) FL_NOEXCEPT
Rgbww getRgbww() const FL_NOEXCEPT
virtual void endShowLeds(void *data) FL_NOEXCEPT
static CLEDController * mPTail
pointer to the last LED controller in the linked list
fl::u8 getDither() FL_NOEXCEPT
Get the dithering option currently set for this controller.
void removeFromDrawList() FL_NOEXCEPT
Remove this controller from the draw list.
void showColorInternal(const CRGB &data, fl::u8 brightness) FL_NOEXCEPT
CLEDController & setScreenMap(const fl::ScreenMap &map) FL_NOEXCEPT
RegistrationMode
Registration mode for constructor.
@ AutoRegister
Automatically add to linked list (default, backward compatible)
@ DeferRegister
Defer registration until addToList() is called.
CLEDController & setRgbww(const Rgbww &arg=RgbwwDefault::value()) FL_NOEXCEPT
Configure this channel for 5-channel RGBWW (RGB + warm-W + cool-W) output.
bool isInList() const FL_NOEXCEPT
Check if this controller is in the linked list.
virtual void show(const CRGB *data, int nLeds, fl::u8 brightness) FL_NOEXCEPT=0
Write the passed in RGB data out to the LEDs managed by this controller.
CLEDController & clearWhiteChannel() FL_NOEXCEPT
Reset this channel to plain 3-channel RGB (clears any RGBW/RGBWW configuration).
const CRGB * leds() const FL_NOEXCEPT
Const pointer to the CRGB array for this controller.
CLEDController & setLeds(CRGB *data, int nLeds) FL_NOEXCEPT
Set the default array of LEDs to be used by this controller.
void showLedsInternal(fl::u8 brightness) FL_NOEXCEPT
Write the data to the LEDs managed by this controller.
CLEDController(RegistrationMode mode) FL_NOEXCEPT
Protected constructor with registration mode.
CLEDController & setLeds(fl::span< CRGB > leds) FL_NOEXCEPT
Set the default array of LEDs to be used by this controller (span version)
CLEDController & setDither(fl::u8 ditherMode=BINARY_DITHER) FL_NOEXCEPT
Set the dithering mode for this controller to use.
CLEDController * next() FL_NOEXCEPT
Get the next controller in the linked list after this one.
virtual void init() FL_NOEXCEPT=0
Initialize the LED controller.
void setEnabled(bool enabled) FL_NOEXCEPT
CLEDController & setScreenMap(const fl::XYMap &map, float diameter=-1.f) FL_NOEXCEPT
CLEDController & setTemperature(ColorTemperature temperature) FL_NOEXCEPT
Set the color temperature, aka white point, for this controller.
CRGB * leds() FL_NOEXCEPT
Pointer to the CRGB array for this controller.
virtual fl::u16 getMaxRefreshRate() const FL_NOEXCEPT
Gets the maximum possible refresh rate of the strip.
void addToList() FL_NOEXCEPT
Add this controller to the linked list.
static void visitControllers(Visitor &&visitor) FL_NOEXCEPT
Visit all controllers in the linked list with a visitor The visitor must be a callable that accepts (...
fl::span< CRGB > ledsSpan() FL_NOEXCEPT
Span of LEDs managed by this controller.
Rgbw getRgbw() const FL_NOEXCEPT
CRGB getAdjustment(fl::u8 scale) FL_NOEXCEPT
Get the combined brightness/color adjustment for this controller.
CLEDController() FL_NOEXCEPT
Create an led controller object, add it to the chain of controllers.
ChannelOptions mSettings
Optional channel settings (correction, temperature, dither, rgbw, affinity)
static void removeFromList(CLEDController *controller) FL_NOEXCEPT
Remove a controller from the linked list.
virtual int size() const FL_NOEXCEPT
How many LEDs does this controller manage?
CLEDController * mPNext
pointer to the next LED controller in the linked list
static CLEDController * mPHead
pointer to the first LED controller in the linked list
friend class CFastLED
CRGB getCorrection() FL_NOEXCEPT
Get the correction value used by this controller.
CLEDController & setTemperature(CRGB temperature) FL_NOEXCEPT
Set the color temperature, aka white point, for this controller.
ColorAdjustment getAdjustmentData(fl::u8 brightness) FL_NOEXCEPT
static CLEDController * head() FL_NOEXCEPT
Get the first LED controller in the linked list of controllers.
bool getEnabled() FL_NOEXCEPT
CRGB getTemperature() FL_NOEXCEPT
Get the color temperature, aka white point, for this controller.
CLEDController & setCorrection(LEDColorCorrection correction) FL_NOEXCEPT
The color corrction to use for this controller, expressed as a CRGB object.
virtual void showColor(const CRGB &data, int nLeds, fl::u8 brightness) FL_NOEXCEPT=0
Set all the LEDs to a given color.
VIRTUAL_IF_NOT_AVR void clearLeds(int nLeds=-1) FL_NOEXCEPT
Clear out/zero out the given number of LEDs.
CLEDController & setScreenMap(fl::u16 width, fl::u16 height, float diameter=-1.f) FL_NOEXCEPT
void showColorInternal(const CRGB &data, int nLeds, fl::u8 brightness) FL_NOEXCEPT
fl::span< CRGB > mLeds
span of LED data used by this controller
CRGB & operator[](int x) FL_NOEXCEPT
Reference to the n'th LED managed by the controller.
virtual int lanes() FL_NOEXCEPT
How many Lanes does this controller manage?
static void onEndShowLeds() FL_NOEXCEPT
static void onCanvasUiSet(CLEDController *strip, const ScreenMap &xymap) FL_NOEXCEPT
static void onEndFrame() FL_NOEXCEPT
static void onBeginFrame() FL_NOEXCEPT
static XYMap constructRectangularGrid(u16 width, u16 height, u16 offset=0) FL_NOEXCEPT
Definition xymap.cpp.hpp:35
#define BINARY_DITHER
Enable dithering using binary dithering (only option)
Definition dither_mode.h:12
Declares dithering options and types.
ColorTemperature
Color temperature values.
Definition color.h:42
LEDColorCorrection
Color correction starting points.
Definition color.h:11
unsigned char u8
Definition s16x16x4.h:132
unsigned char u8
Definition stdint.h:131
constexpr int type_rank< T >::value
T * int_to_ptr(uptr value) FL_NOEXCEPT
Definition bit_cast.h:76
uptr ptr_to_int(T *ptr) FL_NOEXCEPT
Definition bit_cast.h:71
MapRedBlackTree< Key, T, Compare, fl::allocator_slab< char > > map
Definition map.h:283
u8 u8 height
Definition blur.h:186
fl::uptr uintptr_t
Definition s16x16x4.h:224
u8 width
Definition blur.h:186
Base definition for an LED controller.
Definition crgb.hpp:179
#define VIRTUAL_IF_NOT_AVR
#define FASTLED_UNUSED(x)
#define FL_NOEXCEPT
Color adjustment structure for pixel output.
static CRGB computeAdjustment(u8 scale, const CRGB &colorCorrection, const CRGB &colorTemperature) FL_NOEXCEPT
Calculates the combined color adjustment to the LEDs at a given scale, color correction,...
Definition crgb.cpp.hpp:29
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
Optional channel configuration parameters All fields have sensible defaults and can be overridden as ...
Definition options.h:43
static Rgbww value() FL_NOEXCEPT
Definition rgbww.h:97
Per-strip RGBWW configuration.
Definition rgbww.h:60