FastLED 3.9.15
Loading...
Searching...
No Matches
Parallel_IO.ino
Go to the documentation of this file.
1
43
44// @filter: (platform is rp2040)
45
46// Enable automatic parallel grouping driver
47#define FASTLED_RP2040_CLOCKLESS_PIO_AUTO 1
48
49#include <FastLED.h>
50
51#if !defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_RP2350)
52#error "This sketch requires RP2040 or RP2350 platform (Raspberry Pi Pico). Use bash compile rp2040 SpecialDrivers/RP instead."
53#endif
54
55// ============================================================================
56// Forward Declarations
57// ============================================================================
58
60void rotate(CRGB* array, int length, uint8_t amount);
61
62// ============================================================================
63// Configuration
64// ============================================================================
65
66// Number of LEDs per strip
67#define LEDS_PER_STRIP 100
68
69// GPIO pins (must be consecutive for parallel output!)
70#define PIN_STRIP_0 2
71#define PIN_STRIP_1 3
72#define PIN_STRIP_2 4
73#define PIN_STRIP_3 5
74
75// RGB order (GRB for WS2812B)
76#define RGB_ORDER GRB
77
78// Create standard FastLED arrays (one per strip)
83
84// ============================================================================
85// Setup
86// ============================================================================
87
88void setup() {
89 delay(500); // Power-up delay for LED strips
90
91 Serial.begin(115200);
92 while (!Serial && millis() < 3000) {
93 delay(100);
94 }
95 Serial.println("\n\n=== RP2040/RP2350 Automatic Parallel WS2812 Example ===\n");
96 Serial.println("This example demonstrates automatic parallel grouping.\n");
97
98 // Standard FastLED.addLeds() calls - automatic parallel grouping!
99 // Because these pins are consecutive (2, 3, 4, 5), they will be
100 // automatically grouped into a single 4-lane parallel output.
105
106 Serial.println("Controller initialized!");
107 Serial.println("Automatic parallel grouping detected consecutive GPIO pins 2-5");
108 Serial.println("Using 4-lane parallel output with single PIO state machine and DMA channel");
109 Serial.println();
110 Serial.println("Compare this with the manual Parallel_IO.ino example - same performance,");
111 Serial.println("but much simpler API! Just use standard FastLED.addLeds() calls.\n");
112}
113
114// ============================================================================
115// Loop
116// ============================================================================
117
118void loop() {
119 // Pattern: Rainbow on each strip (offset by time)
120 // Each strip shows a different hue
121 static uint32_t start_time = millis();
122 uint32_t elapsed = millis() - start_time;
123
124 for (int strip = 0; strip < 4; strip++) {
125 CRGB* strip_leds = nullptr;
126 switch (strip) {
127 case 0: strip_leds = leds0; break;
128 case 1: strip_leds = leds1; break;
129 case 2: strip_leds = leds2; break;
130 case 3: strip_leds = leds3; break;
131 }
132
133 if (strip_leds) {
134 uint8_t hue_offset = strip * 64; // ~90 degrees per strip
135 fill_rainbow(strip_leds, LEDS_PER_STRIP, hue_offset, 10);
136
137 // Add motion
138 uint8_t rotation = (elapsed / 50 + strip * 20) % 256;
139 rotate(strip_leds, LEDS_PER_STRIP, rotation);
140 }
141 }
142
143 // Standard FastLED.show() - all 4 strips output in parallel!
144 FastLED.show();
145
146 // Frame rate control (~50 FPS)
147 delay(20);
148
149 // Every 5 seconds, print stats
150 static uint32_t last_stats = millis();
151 if (millis() - last_stats > 5000) {
152 last_stats = millis();
153 Serial.print("Frame time: ");
154 Serial.print(elapsed);
155 Serial.println(" ms");
156 Serial.println("All 4 strips updating in parallel via automatic grouping");
157 }
158}
159
160// ============================================================================
161// Helper: Rotate array elements
162// ============================================================================
163
164void rotate(CRGB* array, int length, uint8_t amount) {
165 amount = amount % length; // Handle wrap-around
166 if (amount == 0) return;
167
168 CRGB temp[amount];
169
170 // Copy last 'amount' elements to temp
171 for (int i = 0; i < amount; i++) {
172 temp[i] = array[length - amount + i];
173 }
174
175 // Shift remaining elements right
176 for (int i = length - 1; i >= amount; i--) {
177 array[i] = array[i - amount];
178 }
179
180 // Copy temp to beginning
181 for (int i = 0; i < amount; i++) {
182 array[i] = temp[i];
183 }
184}
185
186// ============================================================================
187// Notes
188// ============================================================================
189
190// Benefits of Automatic Parallel Grouping:
191// - Standard FastLED.addLeds() API (no custom controller classes)
192// - Automatic detection of consecutive pins
193// - Same performance as manual parallel setup
194// - Graceful fallback for non-consecutive pins
195// - Works with FastLED.show() - no custom showLeds() call needed
196//
197// Requirements:
198// - Pins must be consecutive GPIO numbers for parallel output
199// - Define FASTLED_RP2040_CLOCKLESS_PIO_AUTO before including FastLED.h
200// - Valid parallel groups: 2, 4, or 8 consecutive pins
201//
202// Mixed Consecutive/Non-consecutive Example:
203// FastLED.addLeds<WS2812, 2>(...); // Group 1: GPIO 2-5 (parallel)
204// FastLED.addLeds<WS2812, 3>(...); //
205// FastLED.addLeds<WS2812, 4>(...); //
206// FastLED.addLeds<WS2812, 5>(...); //
207// FastLED.addLeds<WS2812, 10>(...); // Group 2: GPIO 10 (sequential fallback)
208// FastLED.addLeds<WS2812, 15>(...); // Group 3: GPIO 15 (sequential fallback)
209//
210// The driver automatically creates:
211// - One 4-pin parallel group for GPIO 2-5 (uses 1 PIO SM + 1 DMA channel)
212// - Two single-pin sequential groups for GPIO 10 and 15
CRGB leds2[lengths[RedStrip]]
CRGB leds3[lengths[BlueStrip]]
CRGB leds1[lengths[GreenStrip]]
CRGB leds0[lengths[BlackStrip]]
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
#define LEDS_PER_STRIP
#define PIN_STRIP_2
void setup()
void rotate(CRGB *array, int length, uint8_t amount)
Helper: Rotate array elements.
#define RGB_ORDER
#define PIN_STRIP_1
#define PIN_STRIP_0
#define PIN_STRIP_3
void loop()
void fill_rainbow(CRGB *targetArray, int numToFill, fl::u8 initialhue, fl::u8 deltahue=5) FL_NOEXCEPT
Fill a range of LEDs with a rainbow of colors.
Definition fill.cpp.hpp:29
fl::UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
fl::CRGB CRGB
Definition crgb.h:25
#define Serial
Definition serial.h:304