FastLED 3.10.8
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 154 of file parallel_device.cpp.hpp.

154 {
155 if (!pImpl) {
156 return fl::task::Error("Device not initialized");
157 }
158
159 if (pImpl->initialized) {
160 // Already initialized - idempotent
161 return fl::nullopt;
162 }
163
164 size_t num_pins = pImpl->config.gpio_pins.size();
165
166 // Validate pin count
167 if (num_pins == 0 || num_pins > 32) {
168 return fl::task::Error("Invalid number of GPIO pins (must be 1-32)");
169 }
170
171 // Determine backend width (round up to next supported width: 1,2,4,8,16,32)
172 u8 backend_width = 1;
173 if (num_pins > 16) backend_width = 32;
174 else if (num_pins > 8) backend_width = 16;
175 else if (num_pins > 4) backend_width = 8;
176 else if (num_pins > 2) backend_width = 4;
177 else if (num_pins > 1) backend_width = 2;
178
179 pImpl->backend_width = backend_width;
180
181 // Determine execution mode
182 bool use_isr = (pImpl->config.mode == SpiParallelMode::ISR_ASYNC) ||
183 (pImpl->config.mode == SpiParallelMode::AUTO); // Default to ISR if AUTO
184 pImpl->is_isr_mode = use_isr;
185
186 // Build default LUT
187 u32 set_masks[256];
188 u32 clear_masks[256];
189 buildDefaultLUT(pImpl->config.gpio_pins, set_masks, clear_masks);
190
191 // Create and initialize backend
192 // Note: We can't use polymorphism here due to different backend types
193 // Instead, we use type-erasure with void* and manual dispatch
194
195 if (use_isr) {
196 // ISR-based backend
197 FL_DBG("ParallelDevice: Initializing ISR mode (width=" << ((int)backend_width) << ")");
198
199 // Note: These need to be heap-allocated for type-erasure
200 // For now, return error indicating ISR mode not yet implemented
201 return fl::task::Error("ISR mode not yet implemented for ParallelDevice");
202
203 } else {
204 // Bit-bang (blocking) backend
205 FL_DBG("ParallelDevice: Initializing bit-bang mode (width=" << ((int)backend_width) << ")");
206
207 // Note: These need to be heap-allocated for type-erasure
208 // For now, return error indicating bit-bang mode not yet implemented
209 return fl::task::Error("Bit-bang mode not yet implemented for ParallelDevice");
210 }
211
212 pImpl->initialized = true;
213 return fl::nullopt;
214}
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:131
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: