|
| 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 | +} |
0 commit comments