FastLED 3.9.15
Loading...
Searching...
No Matches
/home/runner/work/FastLED/FastLED/src/fl/channels/config.h

Variant type that holds either a clockless or SPI chipset configuration.

Variant type that holds either a clockless or SPI chipset configuration This allows ChannelConfig to support both chipset types with compile-time type safety and runtime polymorphism via the visitor pattern.

// Clockless chipset (WS2812)
ChipsetVariant clockless = ClocklessChipset(5, makeTimingConfig<TIMING_WS2812_800KHZ>());
// SPI chipset (APA102)
ChipsetVariant spi = SpiChipsetConfig(23, 18, SpiEncoder::apa102());
#pragma once
#include "crgb.h" // IWYU pragma: keep
#include "fl/gfx/rgbw.h" // IWYU pragma: keep
#include "fl/stl/span.h"
#include "fl/stl/vector.h"
#include "fl/stl/variant.h"
#include "fl/gfx/eorder.h"
#include "fl/stl/string.h" // fl::string must be complete for Optional<fl::string> below // IWYU pragma: keep
#include "color.h" // IWYU pragma: keep
#include "dither_mode.h" // IWYU pragma: keep
namespace fl {
struct ClocklessChipset {
int pin;
ChipsetTimingConfig timing;
constexpr ClocklessChipset(int pin, const ChipsetTimingConfig& timing,
constexpr ClocklessChipset(int pin, const ChipsetTimingConfig& timing) FL_NOEXCEPT
bool operator==(const ClocklessChipset& other) const FL_NOEXCEPT {
return pin == other.pin &&
timing == other.timing &&
encoder == other.encoder;
}
bool operator!=(const ClocklessChipset& other) const FL_NOEXCEPT {
return !(*this == other);
}
};
template <typename TIMING>
constexpr ClocklessChipset makeClockless(int pin) FL_NOEXCEPT {
return ClocklessChipset(pin, makeTimingConfig<TIMING>(), encoder_for<TIMING>());
}
struct SpiChipsetConfig {
int dataPin;
int clockPin;
SpiEncoder timing;
SpiChipsetConfig(int dataPin, int clockPin, const SpiEncoder& timing) FL_NOEXCEPT
bool operator==(const SpiChipsetConfig& other) const FL_NOEXCEPT {
return dataPin == other.dataPin &&
clockPin == other.clockPin &&
timing == other.timing;
}
bool operator!=(const SpiChipsetConfig& other) const FL_NOEXCEPT {
return !(*this == other);
}
};
using ChipsetVariant = fl::variant<ClocklessChipset, SpiChipsetConfig>;
struct ChannelConfig {
// ========== New Variant-Based Constructors ==========
EOrder rgbOrder = RGB, const ChannelOptions& options = ChannelOptions()) FL_NOEXCEPT;
EOrder rgbOrder = RGB, const ChannelOptions& options = ChannelOptions()) FL_NOEXCEPT;
ChannelConfig(const ClocklessChipset& clockless, fl::span<CRGB> leds,
EOrder rgbOrder = RGB, const ChannelOptions& options = ChannelOptions()) FL_NOEXCEPT;
ChannelConfig(const SpiChipsetConfig& spi, fl::span<CRGB> leds,
EOrder rgbOrder = RGB, const ChannelOptions& options = ChannelOptions()) FL_NOEXCEPT;
// ========== Backwards-Compatible Constructors (Deprecated) ==========
template<typename TIMING>
ChannelConfig(int pin, fl::span<CRGB> leds, EOrder rgbOrder = RGB,
const ChannelOptions& options = ChannelOptions())
ChannelConfig(int pin, const ChipsetTimingConfig& timing, fl::span<CRGB> leds,
EOrder rgbOrder = RGB, const ChannelOptions& options = ChannelOptions()) FL_NOEXCEPT;
// Copy constructor (needed because of variant)
// Move constructor (needed because of variant)
// Note: Assignment operators deleted because const members cannot be reassigned
// ========== Accessors ==========
const ChipsetVariant& getChipset() const FL_NOEXCEPT { return chipset; }
bool isClockless() const FL_NOEXCEPT { return chipset.is<ClocklessChipset>(); }
bool isSpi() const FL_NOEXCEPT { return chipset.is<SpiChipsetConfig>(); }
const ClocklessChipset* getClocklessChipset() const FL_NOEXCEPT { return chipset.ptr<ClocklessChipset>(); }
const SpiChipsetConfig* getSpiChipset() const FL_NOEXCEPT { return chipset.ptr<SpiChipsetConfig>(); }
int getDataPin() const FL_NOEXCEPT;
int getClockPin() const FL_NOEXCEPT;
const fl::ScreenMap& getScreenMap() const FL_NOEXCEPT { return mScreenMap; }
bool hasScreenMap() const FL_NOEXCEPT { return mScreenMap.getLength() > 0; }
// ========== Data Members ==========
ChannelOptions options;
};
template<typename Chipset>
struct ChannelConfigOf {
EOrder rgbOrder = RGB,
const ChannelOptions& options = ChannelOptions()) FL_NOEXCEPT
ChannelConfigOf(const fl::string& name, const Chipset& chipset,
const ChannelOptions& options = ChannelOptions()) FL_NOEXCEPT
: chipset(chipset), mLeds(leds), rgb_order(rgbOrder), options(options), mName(name) {}
operator ChannelConfig() const FL_NOEXCEPT {
ChannelConfig cfg(chipset, mLeds, rgb_order, options);
cfg.mScreenMap = mScreenMap;
if (mName.has_value()) {
cfg.mName = mName;
}
return cfg;
}
ChannelConfig toErased() const FL_NOEXCEPT { return static_cast<ChannelConfig>(*this); }
// ---- Data members (mirror ChannelConfig, but with typed `chipset`) ----
Chipset chipset;
ChannelOptions options;
};
struct MultiChannelConfig {
MultiChannelConfig(fl::initializer_list<ChannelConfigPtr> channels) FL_NOEXCEPT : mChannels(channels.begin(), channels.end()) {}
MultiChannelConfig(fl::initializer_list<ChannelConfig> channels) FL_NOEXCEPT;
MultiChannelConfig& add(ChannelConfigPtr channel) FL_NOEXCEPT;
};
}
fl::CRGB leds[NUM_LEDS]
Runtime chipset timing configuration for clockless LED drivers.
SPI encoder configuration for clocked LED chipsets.
u32 getLength() const FL_NOEXCEPT
iterator begin() FL_NOEXCEPT
Definition span.h:440
iterator end() FL_NOEXCEPT
Definition span.h:444
Encoding pipeline selector for clockless LED chipsets.
Declares dithering options and types.
fl::EOrder EOrder
Definition eorder.h:12
constexpr EOrder RGB
Definition eorder.h:17
Functions for red, green, blue, white (RGBW) output.
ClocklessEncoder
Identifies which encoder to use for clockless chipsets in the Channel API.
@ CLOCKLESS_ENCODER_WS2812
Default, no preamble (WS2812 and compatible)
MapRedBlackTree< Key, T, Compare, fl::allocator_slab< char > > map
Definition map.h:283
constexpr ClocklessChipset makeClockless(int pin) FL_NOEXCEPT
Build a ClocklessChipset from a compile-time TIMING trait.
Definition config.h:94
Optional< T > optional
Definition optional.h:16
constexpr ChipsetTimingConfig makeTimingConfig() FL_NOEXCEPT
Convert compile-time CHIPSET type to runtime timing config.
fl::variant< ClocklessChipset, SpiChipsetConfig > ChipsetVariant
Definition config.h:153
constexpr ClocklessEncoder encoder_for() FL_NOEXCEPT
Extract the encoder selector from a compile-time TIMING type.
@ APA102
APA102 LED chipset.
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
#define FASTLED_SHARED_PTR_STRUCT(type)
Definition shared_ptr.h:540
const fl::ScreenMap & getScreenMap() const FL_NOEXCEPT
Get screen map for JS canvas visualization.
Definition config.h:255
fl::optional< fl::string > mName
Optional user-specified name (if not set, Channel generates one automatically)
Definition config.h:279
ChannelConfig & operator=(const ChannelConfig &) FL_NOEXCEPT=delete
bool isSpi() const FL_NOEXCEPT
Check if this is an SPI chipset.
Definition config.h:235
const ChipsetVariant & getChipset() const FL_NOEXCEPT
Get the chipset configuration variant.
Definition config.h:229
const ClocklessChipset * getClocklessChipset() const FL_NOEXCEPT
Get clockless chipset (returns nullptr if not clockless)
Definition config.h:238
bool hasScreenMap() const FL_NOEXCEPT
Check if screen map is configured.
Definition config.h:259
const SpiChipsetConfig * getSpiChipset() const FL_NOEXCEPT
Get SPI chipset (returns nullptr if not SPI)
Definition config.h:241
int getDataPin() const FL_NOEXCEPT
Get data pin (works for both clockless and SPI)
void setScreenMap(const fl::ScreenMap &map) FL_NOEXCEPT
Set screen map for JS canvas visualization.
Definition config.h:251
fl::ScreenMap mScreenMap
Screen mapping.
Definition config.h:276
ChannelOptions options
Optional channel settings (correction, temperature, dither, rgbw, affinity)
Definition config.h:273
fl::span< CRGB > mLeds
LED data array.
Definition config.h:267
bool isClockless() const FL_NOEXCEPT
Check if this is a clockless chipset.
Definition config.h:232
ChannelConfig(const fl::string &name, const ChipsetVariant &chipset, fl::span< CRGB > leds, EOrder rgbOrder=RGB, const ChannelOptions &options=ChannelOptions()) FL_NOEXCEPT
Named constructor with chipset variant.
ChipsetVariant chipset
Chipset configuration (clockless or SPI)
Definition config.h:264
int getClockPin() const FL_NOEXCEPT
Get clock pin (returns -1 for clockless chipsets)
EOrder rgb_order
RGB channel ordering.
Definition config.h:270
ChannelConfig toErased() const FL_NOEXCEPT
Convenience: build the erased form by value (handy in templates where the implicit conversion is supp...
Definition config.h:330
ChannelConfigOf(const Chipset &chipset, fl::span< CRGB > leds, EOrder rgbOrder=RGB, const ChannelOptions &options=ChannelOptions()) FL_NOEXCEPT
Construct from a typed chipset, LEDs span, and optional metadata.
Definition config.h:305
fl::ScreenMap mScreenMap
Screen mapping (for JS canvas visualization).
Definition config.h:348
fl::span< CRGB > mLeds
LED data span.
Definition config.h:339
ChannelOptions options
Optional channel settings (correction, temperature, dither, rgbw, affinity).
Definition config.h:345
fl::optional< fl::string > mName
Optional user-specified name. If unset, Channel auto-generates one.
Definition config.h:351
EOrder rgb_order
RGB channel ordering.
Definition config.h:342
Chipset chipset
Typed chipset configuration.
Definition config.h:336
bool operator!=(const ClocklessChipset &other) const FL_NOEXCEPT
Inequality operator.
Definition config.h:70
ChipsetTimingConfig timing
T1/T2/T3 timing parameters.
Definition config.h:34
ClocklessChipset & operator=(const ClocklessChipset &) FL_NOEXCEPT=default
Copy assignment.
int pin
GPIO data pin.
Definition config.h:33
ClocklessEncoder encoder
Byte-level encoding pipeline (default: WS2812)
Definition config.h:35
bool operator==(const ClocklessChipset &other) const FL_NOEXCEPT
Equality operator.
Definition config.h:63
constexpr ClocklessChipset() FL_NOEXCEPT
Default constructor.
Definition config.h:47
MultiChannelConfig & operator=(const MultiChannelConfig &) FL_NOEXCEPT=default
fl::vector< ChannelConfigPtr > mChannels
Vector of shared pointers to channel configurations.
Definition config.h:392
MultiChannelConfig() FL_NOEXCEPT=default
~MultiChannelConfig() FL_NOEXCEPT=default
MultiChannelConfig & add(ChannelConfigPtr channel) FL_NOEXCEPT
Add a channel configuration to the multi-channel config.
SpiEncoder timing
SPI encoder configuration.
Definition config.h:105
bool operator!=(const SpiChipsetConfig &other) const FL_NOEXCEPT
Inequality operator.
Definition config.h:135
SpiChipsetConfig() FL_NOEXCEPT
Default constructor (requires explicit protocol specification)
Definition config.h:113
int clockPin
GPIO clock pin (SCK)
Definition config.h:104
int dataPin
GPIO data pin (MOSI)
Definition config.h:103
bool operator==(const SpiChipsetConfig &other) const FL_NOEXCEPT
Equality operator.
Definition config.h:128
SpiChipsetConfig & operator=(const SpiChipsetConfig &) FL_NOEXCEPT=default
Copy assignment.