Skip to content

Commit e092fd5

Browse files
committed
refactor(clockless): add BulkClockless API for multi-strip management
1 parent b129899 commit e092fd5

26 files changed

Lines changed: 2872 additions & 34 deletions

‎examples/Bulk/Bulk.ino‎

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// BulkControllerDemo.ino
2+
//
3+
// Demonstrates the BulkClockless API for managing multiple LED strips
4+
// with individual per-strip settings while sharing bulk peripheral hardware.
5+
//
6+
// This example shows:
7+
// - Creating bulk controllers for LCD_I80 and RMT peripherals
8+
// - Setting individual color correction, temperature, dither, and RGBW modes per strip
9+
// - Dynamic add/remove of strips while preserving settings
10+
// - ScreenMap integration for spatial positioning
11+
//
12+
// Hardware Requirements:
13+
// - ESP32 or ESP32-S3
14+
// - WS2812 LED strips connected to:
15+
// - GPIO 8 and 9 for LCD_I80 (ESP32-S3/P4 only)
16+
// - GPIO 2 for RMT
17+
//
18+
// Platform Support:
19+
// - LCD_I80: ESP32-S3, ESP32-P4 (CPU fallback on other platforms)
20+
// - RMT: ESP32, ESP32-S3, ESP32-C3, ESP32-C6, ESP32-H2 (CPU fallback on other platforms)
21+
// - I2S: ESP32, ESP32-S3 (CPU fallback on other platforms)
22+
//
23+
// NOTE: With CPU fallback, this example compiles on ALL platforms.
24+
// On platforms without hardware peripheral support, the code runs with a warning
25+
// but LED output will not work (CPU fallback is not yet fully implemented).
26+
27+
#include <FastLED.h>
28+
#include "fl/screenmap.h"
29+
30+
using namespace fl;
31+
32+
// LED buffers (just data - no spatial information)
33+
CRGB rmt_strip[100];
34+
CRGB lcd_strip1[100];
35+
CRGB lcd_strip2[100];
36+
37+
// Controllers
38+
BulkClockless<WS2812_CHIPSET, RMT>* rmt_bulk = nullptr;
39+
BulkClockless<WS2812_CHIPSET, LCD_I80>* lcd_bulk = nullptr;
40+
41+
void setup() {
42+
Serial.begin(115200);
43+
delay(1000); // Give serial time to initialize
44+
45+
FL_WARN("BulkControllerDemo starting...");
46+
47+
// Create screenmaps for where each controller output appears
48+
ScreenMap map_position_left = ScreenMap::DefaultStrip(100, 1.5f, 0.4f);
49+
map_position_left.addOffsetX(0.0f); // Left position
50+
51+
ScreenMap map_position_center = ScreenMap::DefaultStrip(100, 1.5f, 0.4f);
52+
map_position_center.addOffsetX(150.0f); // Center position
53+
54+
// LCD bulk controller: Two sub-controllers with INDIVIDUAL settings per strip
55+
// Initializer format: {pin, buffer_ptr, num_leds, screenMap}
56+
// On ESP32-S3/P4: Uses LCD_I80 peripheral
57+
// On other platforms: Uses CPU fallback (warning will be printed)
58+
auto& lcd_ref = FastLED.addBulkLeds<WS2812_CHIPSET, LCD_I80>({
59+
{8, lcd_strip1, 100, map_position_left}, // Pin 8: lcd_strip1, appears at left
60+
{9, lcd_strip2, 100, map_position_center} // Pin 9: lcd_strip2, appears at center
61+
});
62+
// Set global defaults (optional - will be overridden per-strip below)
63+
lcd_ref.setCorrection(UncorrectedColor)
64+
.setTemperature(UncorrectedTemperature)
65+
.setDither(BINARY_DITHER);
66+
lcd_bulk = &lcd_ref;
67+
68+
// Configure each LCD strip INDIVIDUALLY with different settings
69+
auto* lcd_strip_8 = lcd_bulk->get(8);
70+
if (!lcd_strip_8) {
71+
FL_WARN("ERROR: Failed to get strip on pin 8");
72+
} else {
73+
lcd_strip_8->setCorrection(TypicalLEDStrip) // Pin 8: Typical LED correction
74+
.setTemperature(Tungsten100W) // Pin 8: Warm tungsten lighting
75+
.setDither(BINARY_DITHER); // Pin 8: Binary dithering
76+
}
77+
78+
auto* lcd_strip_9 = lcd_bulk->get(9);
79+
if (!lcd_strip_9) {
80+
FL_WARN("ERROR: Failed to get strip on pin 9");
81+
} else {
82+
lcd_strip_9->setCorrection(TypicalSMD5050) // Pin 9: SMD5050 correction
83+
.setTemperature(Candle) // Pin 9: Warm candle lighting
84+
.setDither(DISABLE_DITHER) // Pin 9: No dithering
85+
.setRgbw(Rgbw(6000, kRGBWExactColors, W3)); // Pin 9: RGBW mode
86+
}
87+
88+
FL_WARN("LCD bulk controller initialized with " << lcd_bulk->stripCount() << " strips (individually configured)");
89+
90+
// RMT bulk controller: Single sub-controller with GLOBAL settings
91+
// On ESP32/S3/C3/C6/H2: Uses RMT peripheral
92+
// On other platforms: Uses CPU fallback (warning will be printed)
93+
ScreenMap map_position_right = ScreenMap::DefaultStrip(100, 1.5f, 0.4f);
94+
map_position_right.addOffsetX(300.0f); // Right position
95+
96+
auto& rmt_ref = FastLED.addBulkLeds<WS2812_CHIPSET, RMT>({
97+
{2, rmt_strip, 100, map_position_right} // Pin 2: rmt_strip, appears at right
98+
});
99+
// Global settings - simple and clean for single-strip or uniform multi-strip
100+
rmt_ref.setCorrection(TypicalLEDStrip)
101+
.setTemperature(ClearBlueSky)
102+
.setDither(BINARY_DITHER)
103+
.setRgbw(RgbwDefault::value());
104+
rmt_bulk = &rmt_ref;
105+
106+
FastLED.setBrightness(128);
107+
108+
FL_WARN("RMT bulk controller initialized with " << rmt_bulk->stripCount() << " strips (globally configured)");
109+
FL_WARN("Setup complete! Starting animation loop...");
110+
}
111+
112+
void loop() {
113+
static uint8_t hue = 0;
114+
static bool use_pin_9 = true;
115+
116+
// Update LED buffers (just data)
117+
fill_rainbow(lcd_strip1, 100, hue, 7);
118+
fill_rainbow(lcd_strip2, 100, hue + 64, 7);
119+
fill_rainbow(rmt_strip, 100, hue + 128, 7);
120+
121+
FastLED.show();
122+
123+
// Every second: swap LCD pin 9 <-> pin 10
124+
// This demonstrates dynamic add/remove with settings preservation
125+
EVERY_N_MILLISECONDS(1000) {
126+
if (!lcd_bulk) return; // Skip if LCD not initialized
127+
128+
// Create the screenmap for center position (where pin 9/10 controller appears)
129+
ScreenMap map_center = ScreenMap::DefaultStrip(100, 1.5f, 0.4f);
130+
map_center.addOffsetX(150.0f);
131+
132+
if (use_pin_9) {
133+
// Save per-strip settings before removing (preserves individual configuration)
134+
auto* strip_9 = lcd_bulk->get(9);
135+
if (!strip_9) {
136+
FL_WARN("ERROR: Strip on pin 9 not found");
137+
return;
138+
}
139+
auto settings = strip_9->settings;
140+
141+
// Remove sub-controller on pin 9 (removes its screenmap too)
142+
lcd_bulk->remove(9);
143+
144+
// Add new sub-controller on pin 10
145+
// Format: add(pin, buffer_ptr, num_leds, screenMap)
146+
auto* strip_10 = lcd_bulk->add(10, lcd_strip2, 100, map_center);
147+
if (!strip_10) {
148+
FL_WARN("ERROR: Failed to add strip on pin 10");
149+
return;
150+
}
151+
152+
// Restore per-strip settings (preserves individual SMD5050/Candle/RGBW config)
153+
strip_10->settings = settings;
154+
155+
FL_WARN("Swapped: pin 9 -> pin 10 (settings preserved)");
156+
} else {
157+
// Save per-strip settings before removing
158+
auto* strip_10 = lcd_bulk->get(10);
159+
if (!strip_10) {
160+
FL_WARN("ERROR: Strip on pin 10 not found");
161+
return;
162+
}
163+
auto settings = strip_10->settings;
164+
165+
lcd_bulk->remove(10);
166+
167+
auto* strip_9 = lcd_bulk->add(9, lcd_strip2, 100, map_center);
168+
if (!strip_9) {
169+
FL_WARN("ERROR: Failed to add strip on pin 9");
170+
return;
171+
}
172+
173+
// Restore per-strip settings
174+
strip_9->settings = settings;
175+
176+
FL_WARN("Swapped: pin 10 -> pin 9 (settings preserved)");
177+
}
178+
179+
use_pin_9 = !use_pin_9;
180+
}
181+
182+
hue++;
183+
delay(20);
184+
}

‎src/FastLED.h‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@
121121

122122
#include "fl/leds.h"
123123

124+
// Bulk controller support - single anchor includes all components
125+
#include "fl/clockless.h"
126+
127+
// Platform-specific bulk controller implementations are included
128+
// via the platform headers (e.g., platforms/esp/32/core/fastled_esp32.h)
129+
124130
/// LED chipsets with SPI interface
125131
enum ESPIChipsets {
126132
LPD6803, ///< LPD6803 LED chipset
@@ -816,6 +822,78 @@ class CFastLED {
816822
/// @} Adding parallel output controllers
817823
#endif
818824

825+
/// @name Adding Bulk Controllers
826+
/// Add a bulk LED controller that manages multiple strips dynamically.
827+
/// Bulk controllers share a single hardware peripheral (LCD_I80, RMT, SPI, I2S)
828+
/// across multiple LED strips, each with independent settings.
829+
///
830+
/// Multiple calls with the same CHIPSET and PERIPHERAL will create separate instances,
831+
/// allowing multiple bulk controllers of the same type.
832+
///
833+
/// @note The controller is dynamically allocated and lives for the program's lifetime.
834+
/// Do not delete the returned reference. This matches embedded system patterns.
835+
///
836+
/// @tparam CHIPSET the LED chipset type (e.g., WS2812)
837+
/// @tparam PERIPHERAL the hardware peripheral type (e.g., LCD_I80, RMT)
838+
/// @param strips initializer list of strip configurations {pin, buffer, count, screenmap}
839+
/// @returns reference to the bulk controller
840+
/// @{
841+
842+
template<typename CHIPSET, typename PERIPHERAL>
843+
static ::fl::BulkClockless<CHIPSET, PERIPHERAL>& addBulkLeds(
844+
fl::initializer_list<fl::BulkStripConfig> strips
845+
) {
846+
// Use dynamic allocation to support multiple instances of the same type
847+
// Unlike regular addLeds(), BulkClockless controllers are identified by
848+
// CHIPSET+PERIPHERAL only, so static storage would limit to one instance
849+
auto* controller = new ::fl::BulkClockless<CHIPSET, PERIPHERAL>(strips);
850+
851+
// Register with FastLED controller list
852+
::CLEDController* pLed = static_cast<::CLEDController*>(controller);
853+
pLed->init();
854+
855+
// Calculate total LED count for refresh rate
856+
int totalLeds = 0;
857+
for (const auto& strip : strips) {
858+
totalLeds += strip.count;
859+
}
860+
861+
// addLeds() adds the controller to FastLED's linked list for show() calls
862+
// Note: The dynamically allocated controller lives for the program's lifetime.
863+
// This matches embedded system patterns where controllers are never destroyed.
864+
// Users should not call delete on the returned reference.
865+
addLeds(pLed, nullptr, 0, totalLeds);
866+
867+
return *controller;
868+
}
869+
870+
/// Add bulk LED controller with span (for runtime arrays/vectors)
871+
/// @tparam CHIPSET the LED chipset type (e.g., WS2812)
872+
/// @tparam PERIPHERAL the hardware peripheral type (e.g., LCD_I80, RMT)
873+
/// @param strips span of strip configurations
874+
/// @returns reference to the bulk controller
875+
template<typename CHIPSET, typename PERIPHERAL>
876+
static ::fl::BulkClockless<CHIPSET, PERIPHERAL>& addBulkLeds(
877+
fl::span<const fl::BulkStripConfig> strips
878+
) {
879+
auto* controller = new ::fl::BulkClockless<CHIPSET, PERIPHERAL>(strips);
880+
881+
::CLEDController* pLed = static_cast<::CLEDController*>(controller);
882+
pLed->init();
883+
884+
// Calculate total LED count for refresh rate
885+
int totalLeds = 0;
886+
for (const auto& strip : strips) {
887+
totalLeds += strip.count;
888+
}
889+
890+
addLeds(pLed, nullptr, 0, totalLeds);
891+
892+
return *controller;
893+
}
894+
895+
/// @} Adding Bulk Controllers
896+
819897
/// Set the global brightness scaling
820898
/// @param scale a 0-255 value for how much to scale all leds before writing them out
821899
void setBrightness(fl::u8 scale) { m_Scale = scale; }

‎src/chipsets.h‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,25 @@
6262
// - AVR platforms: led_sysdefs_avr.h + platforms/avr/clockless_*.h
6363
// - Other platforms: respective led_sysdefs_*.h files
6464

65-
// So many platforms have specialized WS2812 controllers. Why? Because they
66-
// are the cheapest chipsets use. So we special case this.
65+
// ============================================================================
66+
// PLATFORM-SPECIFIC SPECIALIZED CONTROLLERS (Bottom Includes)
67+
// ============================================================================
68+
// The includes below MUST be at this point in the file because:
69+
//
70+
// 1. C++ templates require full definition in headers (no .cpp separation)
71+
// 2. Base templates and type definitions (above) must be defined BEFORE
72+
// platform-specific specializations (below) can see and use them
73+
// 3. Platform specializations need to instantiate/specialize the base templates
74+
//
75+
// This is intentional C++ template architecture, not a code smell or circular
76+
// dependency. Template-heavy libraries like FastLED naturally have includes at
77+
// the bottom to support platform-specific specializations.
78+
//
79+
// Example: WS2812 is heavily optimized per-platform because it's the most
80+
// popular chipset. Each platform provides specialized implementations that
81+
// fully replace the default template instantiation.
82+
// ============================================================================
83+
6784
#include "platforms/chipsets_specialized_ws2812.h"
6885

6986
#if defined(ARDUINO) && (defined(SoftwareSerial_h) || defined(__SoftwareSerial_h))

‎src/cled_controller.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
CLEDController::~CLEDController() = default;
1313

1414
/// Create an led controller object, add it to the chain of controllers
15-
CLEDController::CLEDController() : m_Data(nullptr), m_ColorCorrection(UncorrectedColor), m_ColorTemperature(UncorrectedTemperature), m_DitherMode(BINARY_DITHER), m_nLeds(0) {
15+
CLEDController::CLEDController() : m_Data(nullptr), mSettings(), m_nLeds(0) {
1616
m_pNext = nullptr;
1717
if(m_pHead==nullptr) { m_pHead = this; }
1818
if(m_pTail != nullptr) { m_pTail->m_pNext = this; }

0 commit comments

Comments
 (0)