FastLED 3.10.10
Loading...
Searching...
No Matches

◆ begin()

fl::optional< fl::task::Error > fl::spi::ParallelDevice::begin ( )

Initialize hardware and setup LUT.

Returns
Optional error (nullopt on success)
Note
Auto-selects backend based on pin count and mode

Definition at line 155 of file parallel_device.cpp.hpp.

155 {
156 if (!pImpl) {
157 return fl::task::Error("Device not initialized");
158 }
159
160 if (pImpl->initialized) {
161 // Already initialized - idempotent
162 return fl::nullopt;
163 }
164
165 size_t num_pins = pImpl->config.gpio_pins.size();
166
167 // Validate pin count
168 if (num_pins == 0 || num_pins > 32) {
169 return fl::task::Error("Invalid number of GPIO pins (must be 1-32)");
170 }
171
172 // buildDefaultLUT shifts each pin into a 32-bit mask, so reject anything it
173 // cannot represent before it runs.
174 if (!spiPinMappingValid(pImpl->config.gpio_pins.data(), num_pins,
175 pImpl->config.clock_pin)) {
176 return fl::task::Error("Invalid pin mapping (pins 0-31; data lanes 0-7 distinct and off the clock)");
177 }
178
179 // Determine backend width (round up to next supported width: 1,2,4,8,16,32)
180 u8 backend_width = 1;
181 if (num_pins > 16) backend_width = 32;
182 else if (num_pins > 8) backend_width = 16;
183 else if (num_pins > 4) backend_width = 8;
184 else if (num_pins > 2) backend_width = 4;
185 else if (num_pins > 1) backend_width = 2;
186
187 pImpl->backend_width = backend_width;
188
189 // Determine execution mode
190 bool use_isr = (pImpl->config.mode == SpiParallelMode::ISR_ASYNC) ||
191 (pImpl->config.mode == SpiParallelMode::AUTO); // Default to ISR if AUTO
192 pImpl->is_isr_mode = use_isr;
193
194 // Build default LUT
195 u32 set_masks[256];
196 u32 clear_masks[256];
197 buildDefaultLUT(pImpl->config.gpio_pins, set_masks, clear_masks);
198
199 // Create and initialize backend
200 // Note: We can't use polymorphism here due to different backend types
201 // Instead, we use type-erasure with void* and manual dispatch
202
203 if (use_isr) {
204 // ISR-based backend
205 FL_DBG("ParallelDevice: Initializing ISR mode (width=" << ((int)backend_width) << ")");
206
207 // Note: These need to be heap-allocated for type-erasure
208 // For now, return error indicating ISR mode not yet implemented
209 return fl::task::Error("ISR mode not yet implemented for ParallelDevice");
210
211 } else {
212 // Bit-bang (blocking) backend
213 FL_DBG("ParallelDevice: Initializing bit-bang mode (width=" << ((int)backend_width) << ")");
214
215 // Note: These need to be heap-allocated for type-erasure
216 // For now, return error indicating bit-bang mode not yet implemented
217 return fl::task::Error("Bit-bang mode not yet implemented for ParallelDevice");
218 }
219
220 pImpl->initialized = true;
221 return fl::nullopt;
222}
fl::unique_ptr< Impl > pImpl
#define FL_DBG
Definition log.h:553
void buildDefaultLUT(const fl::vector< u8 > &gpio_pins, u32 *set_masks, u32 *clear_masks) FL_NO_EXCEPT
Build default LUT for parallel GPIO mapping.
unsigned char u8
Definition stdint.h:132
constexpr nullopt_t nullopt
Definition optional.h:13
@ ISR_ASYNC
ISR-driven async mode.
Definition config.h:35
@ AUTO
Auto-select best mode (default: ISR)
Definition config.h:34

References fl::AUTO, FL_DBG, fl::FL_NO_EXCEPT, fl::ISR_ASYNC, fl::nullopt, and pImpl.

Referenced by operator=().

+ Here is the caller graph for this function: