FastLED 3.10.6
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 138 of file manager.cpp.hpp.

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

References addDriverSlow(), fl::basic_string::c_str(), DUPLICATE_NAME, fl::basic_string::empty(), EMPTY_NAME, FL_DBG, mDrivers, mExclusiveDriver, mPollNeededCallback, NULL_DRIVER, fl::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: