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

◆ addDriver()

void fl::ChannelManager::addDriver ( int priority,
fl::shared_ptr< IChannelDriver > driver )

Add a driver with priority (higher priority = preferred)

Parameters
priorityDriver priority (higher values = higher priority)
driverShared driver implementation (allows caller to retain reference for testing)
Note
Platform-specific code calls this during static initialization
Drivers are automatically sorted by priority on each insertion
Driver name is obtained via driver->getName()
If driver->getName() returns empty string, driver is rejected (emits FL_WARN and returns)
If a driver with the same name already exists, it will be replaced:
  1. FL_WARN emitted about replacement
  2. All drivers polled until READY state (1 second timeout)
  3. Old driver removed (shared_ptr may trigger deletion)
  4. New driver added with specified priority

Definition at line 145 of file manager.cpp.hpp.

145 {
146 if (!driver) {
147 (void)addDriverSlow(priority, driver, nullptr, AddDriverSlowReason::NULL_DRIVER);
148 return;
149 }
150
151 // Get driver name from the driver itself
152 fl::string engineName = driver->getName();
153
154 // Reject drivers with empty names
155 if (engineName.empty()) {
156 (void)addDriverSlow(priority, driver, &engineName, AddDriverSlowReason::EMPTY_NAME);
157 return;
158 }
159
160 // A duplicate name takes the uncommon replacement/idempotency path.
161 for (const auto& entry : mDrivers) {
162 if (entry.name == engineName) {
163 if (!addDriverSlow(priority, driver, &engineName, AddDriverSlowReason::DUPLICATE_NAME)) {
164 return;
165 }
166 break;
167 }
168 }
169
170 // Respect exclusive driver mode: auto-disable if name doesn't match exclusive driver
171 bool enabled = true; // Default: enabled
172 if (!mExclusiveDriver.empty()) {
173 enabled = (engineName == mExclusiveDriver); // Only enable if matches exclusive driver
174 }
175
176 mDrivers.push_back({priority, driver, engineName, enabled});
177 driver->setPollNeededCallback(mPollNeededCallback);
178
179 // Build capability string for debug output. Gate the entire block behind
180 // FASTLED_HAS_DBG because the `capStr` exists ONLY to feed the FL_DBG
181 // line below. On release builds (FASTLED_HAS_DBG=0 — i.e. the default
182 // SKETCH_HAS_LARGE_MEMORY=0 path AND any -DFASTLED_LOG_VERBOSITY=0
183 // opt-in build via the gating in fl/log/log.h) the FL_DBG itself is a
184 // no-op, but without this guard the `fl::string capStr` allocation +
185 // two `if` branches still emitted code. See #2773 item 2.3 follow-up.
186#if FASTLED_HAS_DBG
187 IChannelDriver::Capabilities caps = driver->getCapabilities();
188 fl::string capStr;
189 if (caps.supportsClockless) {
190 capStr += "CLOCKLESS";
191 }
192 if (caps.supportsSpi) {
193 if (!capStr.empty()) capStr += "|";
194 capStr += "SPI";
195 }
196 if (capStr.empty()) {
197 capStr = "NONE";
198 }
199
200 FL_DBG("ChannelManager: Added driver '" << engineName.c_str() << "' (priority " << priority << ", caps: " << capStr.c_str() << ")");
201#endif
202
203 // Sort drivers by priority descending (higher values first) after each insertion
204 // Higher priority values = higher precedence (e.g., priority 50 selected over priority 10)
205 // Only 1-4 drivers expected — sort_small skips the quicksort_impl
206 // instantiation entirely (see #2907 for the bloat motivation).
207 fl::sort_small(mDrivers.begin(), mDrivers.end());
208}
IChannelDriver::PollNeededCallback mPollNeededCallback
Shared callback installed on drivers that can signal poll-needed events.
Definition manager.h:342
FL_NO_INLINE FL_COLD bool addDriverSlow(int priority, const fl::shared_ptr< IChannelDriver > &driver, const fl::string *engineName, AddDriverSlowReason reason) FL_NO_EXCEPT
Handle validation failures and the uncommon replacement flow outside the normal driver-registration p...
fl::vector< EngineEntry > mDrivers
Shared drivers sorted by priority descending (higher values first)
Definition manager.h:330
fl::string mExclusiveDriver
Exclusive driver name (empty if no exclusive mode)
Definition manager.h:339
bool empty() const FL_NO_EXCEPT
const char * c_str() const FL_NO_EXCEPT
#define FL_DBG
Definition log.h:553
void sort_small(Iterator first, Iterator last, Compare comp) FL_NO_EXCEPT
Definition algorithm.h:651

References addDriverSlow(), fl::basic_string::c_str(), DUPLICATE_NAME, fl::basic_string::empty(), EMPTY_NAME, FL_DBG, fl::FL_NO_EXCEPT, mDrivers, mExclusiveDriver, mPollNeededCallback, NULL_DRIVER, fl::adl_barrier::sort_small(), fl::IChannelDriver::Capabilities::supportsClockless, and fl::IChannelDriver::Capabilities::supportsSpi.

Referenced by setExclusiveDriver().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: