Skip to content

Commit 268b73f

Browse files
committed
feat(channels): add optional engine affinity to channels
1 parent eb3600c commit 268b73f

28 files changed

Lines changed: 212 additions & 124 deletions

‎examples/Validation/ValidationTest_PARLIO.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include <FastLED.h>
66
#include "ValidationTest.h"
7-
#include "fl/channels/channel_bus_manager.h"
7+
#include "fl/channels/bus_manager.h"
88

99
// ESP-IDF example configuration test (only on ESP32-C6)
1010
#ifdef ESP32C6

‎src/FastLED.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
#include "fl/export.h"
77
#include "fl/int.h"
88
#include "fl/channels/channel.h"
9-
#include "fl/channels/channel_config.h"
10-
#include "fl/channels/channel_bus_manager.h"
9+
#include "fl/channels/config.h"
10+
#include "fl/channels/bus_manager.h"
1111

1212
/// @file FastLED.cpp
1313
/// Central source file for FastLED, implements the CFastLED class/object
@@ -454,7 +454,7 @@ fl::ChannelPtr CFastLED::addChannel(const fl::ChannelConfig& config) {
454454
fl::ChannelBusManager& manager = fl::channelBusManager();
455455
FL_ASSERT(manager.getDriverCount() > 0,
456456
"No channel drivers available - channel API requires at least one registered driver");
457-
return fl::Channel::create(config, &manager);
457+
return fl::Channel::create(config);
458458
}
459459

460460
// ============================================================================

‎src/FastLED.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
#include "fl/fastled.h"
109109

110110
#include "fl/channels/channel.h"
111-
#include "fl/channels/channel_bus_manager.h"
111+
#include "fl/channels/bus_manager.h"
112112

113113
// ============================================================================
114114
// C STRING FUNCTION USING DECLARATIONS
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/// @file channel_bus_manager.cpp
1+
/// @file bus_manager.cpp
22
/// @brief Implementation of unified channel bus manager
33

4-
#include "channel_bus_manager.h"
4+
#include "fl/channels/bus_manager.h"
55
#include "fl/singleton.h"
66
#include "fl/dbg.h"
77
#include "fl/warn.h"
@@ -136,6 +136,19 @@ fl::span<const DriverInfo> ChannelBusManager::getDriverInfos() const {
136136
return fl::span<const DriverInfo>(mCachedDriverInfo.data(), mCachedDriverInfo.size());
137137
}
138138

139+
IChannelEngine* ChannelBusManager::getEngineByName(const char* name) const {
140+
if (!name || !name[0]) {
141+
return nullptr; // Null or empty name = not found
142+
}
143+
144+
for (const auto& entry : mEngines) {
145+
if (entry.enabled && entry.name == name) {
146+
return entry.engine.get();
147+
}
148+
}
149+
return nullptr; // Not found or not enabled
150+
}
151+
139152
// IChannelEngine interface implementation
140153
void ChannelBusManager::enqueue(ChannelDataPtr channelData) {
141154
// Select engine on first call if not already selected
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// @file channel_bus_manager.h
1+
/// @file bus_manager.h
22
/// @brief Unified bus manager for channel engines with priority-based fallback
33
///
44
/// The ChannelBusManager coordinates multiple channel engines (e.g., PARLIO, SPI, RMT)
@@ -22,8 +22,8 @@
2222

2323
#pragma once
2424

25-
#include "fl/channels/channel_engine.h"
26-
#include "fl/channels/channel_data.h"
25+
#include "fl/channels/engine.h"
26+
#include "fl/channels/data.h"
2727
#include "fl/engine_events.h"
2828
#include "ftl/vector.h"
2929
#include "ftl/shared_ptr.h"
@@ -101,6 +101,12 @@ class ChannelBusManager : public IChannelEngine, public EngineEvents::Listener {
101101
/// @note fl::string copies are cheap (shared pointer internally, no heap allocation)
102102
fl::span<const DriverInfo> getDriverInfos() const;
103103

104+
/// @brief Get engine by name for affinity binding
105+
/// @param name Engine name to look up (case-sensitive, e.g., "RMT", "SPI", "PARLIO")
106+
/// @return Pointer to engine if found and enabled, nullptr otherwise
107+
/// @note Used by Channel affinity system to bind channels to specific engines
108+
IChannelEngine* getEngineByName(const char* name) const;
109+
104110
/// @brief Enqueue channel data for transmission
105111
/// @param channelData Channel data to transmit
106112
/// @note Selects engine on first call, then batches channel data

‎src/fl/channels/channel.cpp‎

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/// @file channel.cpp
22
/// @brief LED channel implementation
33

4-
#include "channel.h"
5-
#include "channel_config.h"
6-
#include "channel_data.h"
7-
#include "channel_engine.h"
4+
#include "fl/channels/channel.h"
5+
#include "fl/channels/config.h"
6+
#include "fl/channels/data.h"
7+
#include "fl/channels/engine.h"
8+
#include "fl/channels/bus_manager.h"
89
#include "ftl/atomic.h"
910
#include "fl/dbg.h"
10-
#include "fl/led_settings.h"
11+
#include "fl/warn.h"
12+
#include "fl/channels/options.h"
1113
#include "fl/pixel_iterator_any.h"
1214
#include "pixel_controller.h"
1315

@@ -25,13 +27,31 @@ int32_t Channel::nextId() {
2527
return gNextChannelId.fetch_add(1);
2628
}
2729

28-
ChannelPtr Channel::create(const ChannelConfig &config, IChannelEngine* engine) {
30+
ChannelPtr Channel::create(const ChannelConfig &config) {
31+
IChannelEngine* selectedEngine = nullptr;
32+
33+
// Handle affinity binding: if affinity is specified, look up engine by name
34+
if (config.options.affinity && config.options.affinity[0]) {
35+
selectedEngine = ChannelBusManager::instance().getEngineByName(config.options.affinity);
36+
if (selectedEngine) {
37+
FL_DBG("Channel: Bound to engine '" << config.options.affinity << "' via affinity");
38+
} else {
39+
FL_WARN("Channel: Affinity engine '" << config.options.affinity << "' not found or not enabled - using ChannelBusManager");
40+
}
41+
}
42+
43+
// If no affinity or affinity engine not found, use ChannelBusManager
44+
if (!selectedEngine) {
45+
selectedEngine = &ChannelBusManager::instance();
46+
FL_DBG("Channel: Using ChannelBusManager for engine selection");
47+
}
48+
2949
return fl::make_shared<Channel>(config.pin, config.timing, config.mLeds,
30-
config.rgb_order, engine, config.settings);
50+
config.rgb_order, selectedEngine, config.options);
3151
}
3252

3353
Channel::Channel(int pin, const ChipsetTimingConfig& timing, fl::span<CRGB> leds,
34-
EOrder rgbOrder, IChannelEngine* engine, const LEDSettings& settings)
54+
EOrder rgbOrder, IChannelEngine* engine, const ChannelOptions& options)
3555
: CPixelLEDController<RGB>()
3656
, mPin(pin)
3757
, mTiming(timing)
@@ -48,11 +68,11 @@ Channel::Channel(int pin, const ChipsetTimingConfig& timing, fl::span<CRGB> leds
4868
// Set the LED data array
4969
setLeds(leds);
5070

51-
// Set color correction/temperature/dither/rgbw from LEDSettings
52-
setCorrection(settings.correction);
53-
setTemperature(settings.temperature);
54-
setDither(settings.ditherMode);
55-
setRgbw(settings.rgbw);
71+
// Set color correction/temperature/dither/rgbw from ChannelOptions
72+
setCorrection(options.correction);
73+
setTemperature(options.temperature);
74+
setDither(options.ditherMode);
75+
setRgbw(options.rgbw);
5676

5777
// Create ChannelData during construction
5878
mChannelData = ChannelData::create(mPin, mTiming);

‎src/fl/channels/channel.h‎

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
#include "ftl/stdint.h"
1919
#include "fl/singleton.h"
2020
#include "pixeltypes.h"
21-
#include "channel_config.h"
21+
#include "fl/channels/config.h"
2222

2323
namespace fl {
2424

2525
// Forward declarations
2626
class IChannelEngine;
2727
class ChannelData;
28-
struct LEDSettings;
28+
struct ChannelOptions;
2929

3030
class Channel;
3131
FASTLED_SHARED_PTR(Channel);
@@ -38,21 +38,12 @@ FASTLED_SHARED_PTR(ChannelData);
3838
/// RGB_ORDER is set to RGB - reordering is handled internally by the Channel
3939
class Channel: public CPixelLEDController<RGB> {
4040
public:
41-
/// @brief Create a new channel with compile-time engine type
42-
/// @tparam CHANNEL_ENGINE Engine type (e.g., fl::ChannelEnginePARLIO)
43-
/// @param config Channel configuration
41+
/// @brief Create a new channel with optional affinity binding
42+
/// @param config Channel configuration (includes optional affinity for engine selection)
4443
/// @return Shared pointer to channel (auto-cleanup when out of scope)
45-
template<typename CHANNEL_ENGINE>
46-
static ChannelPtr create(const ChannelConfig& config) {
47-
IChannelEngine* engine = fl::Singleton<CHANNEL_ENGINE>::instance();
48-
return create(config, engine);
49-
}
50-
51-
/// @brief Create a new channel with runtime engine instance
52-
/// @param config Channel configuration
53-
/// @param engine Channel engine this channel belongs to (singleton pointer, not owned)
54-
/// @return Shared pointer to the channel
55-
static ChannelPtr create(const ChannelConfig& config, IChannelEngine* engine);
44+
/// @note Channels always use ChannelBusManager by default
45+
/// @note If config.affinity is set, binds to the named engine from ChannelBusManager
46+
static ChannelPtr create(const ChannelConfig& config);
5647

5748
/// @brief Destructor
5849
virtual ~Channel();
@@ -88,7 +79,7 @@ class Channel: public CPixelLEDController<RGB> {
8879

8980
/// @brief Private constructor (use create() factory method)
9081
Channel(int pin, const ChipsetTimingConfig& timing, fl::span<CRGB> leds,
91-
EOrder rgbOrder, IChannelEngine* engine, const LEDSettings& settings);
82+
EOrder rgbOrder, IChannelEngine* engine, const ChannelOptions& options);
9283

9384
// Non-copyable, non-movable
9485
Channel(const Channel&) = delete;
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
/// @file channel_config.cpp
1+
/// @file config.cpp
22
/// @brief Implementation of MultiChannelConfig builder pattern methods
33

4-
#include "channel_config.h"
4+
#include "fl/channels/config.h"
55

66
namespace fl {
77

88
// Constructors
99
ChannelConfig::ChannelConfig(int pin, const ChipsetTimingConfig& timing, fl::span<CRGB> leds,
10-
EOrder rgbOrder, const LEDSettings& settings)
11-
: pin(pin), timing(timing), mLeds(leds), rgb_order(rgbOrder), settings(settings) {}
10+
EOrder rgbOrder, const ChannelOptions& options)
11+
: pin(pin), timing(timing), mLeds(leds), rgb_order(rgbOrder), options(options) {}
1212

1313
ChannelConfig::ChannelConfig(const ChannelConfig& other)
1414
: pin(other.pin), timing(other.timing), mLeds(other.mLeds), rgb_order(other.rgb_order),
15-
settings(other.settings) {}
15+
options(other.options) {}
1616

1717
ChannelConfig::ChannelConfig(ChannelConfig&& other)
1818
: pin(other.pin), timing(fl::move(other.timing)), mLeds(other.mLeds), rgb_order(other.rgb_order),
19-
settings(fl::move(other.settings)) {}
19+
options(fl::move(other.options)) {}
2020

2121
// MultiChannelConfig constructors
2222

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "fl/screenmap.h"
99
#include "fl/chipsets/chipset_timing_config.h"
1010
#include "fl/eorder.h"
11-
#include "fl/led_settings.h"
11+
#include "fl/channels/options.h"
1212
#include "color.h"
1313
#include "dither_mode.h"
1414

@@ -27,12 +27,12 @@ struct ChannelConfig {
2727
// Template constructor with TIMING type
2828
template<typename TIMING>
2929
ChannelConfig(int pin, fl::span<CRGB> leds, EOrder rgbOrder = RGB,
30-
const LEDSettings& settings = LEDSettings())
31-
: ChannelConfig(pin, makeTimingConfig<TIMING>(), leds, rgbOrder, settings) {}
30+
const ChannelOptions& options = ChannelOptions())
31+
: ChannelConfig(pin, makeTimingConfig<TIMING>(), leds, rgbOrder, options) {}
3232

33-
// Basic constructor with timing, leds, rgb_order, and LEDSettings
33+
// Basic constructor with timing, leds, rgb_order, and ChannelOptions
3434
ChannelConfig(int pin, const ChipsetTimingConfig& timing, fl::span<CRGB> leds,
35-
EOrder rgbOrder = RGB, const LEDSettings& settings = LEDSettings());
35+
EOrder rgbOrder = RGB, const ChannelOptions& options = ChannelOptions());
3636

3737
// Copy constructor (needed because of const members)
3838
ChannelConfig(const ChannelConfig& other);
@@ -56,8 +56,8 @@ struct ChannelConfig {
5656
// RGB channel ordering
5757
EOrder rgb_order = RGB;
5858

59-
// LED settings (correction, temperature, dither, rgbw)
60-
LEDSettings settings;
59+
// Optional channel settings (correction, temperature, dither, rgbw, affinity)
60+
ChannelOptions options;
6161

6262
// Screen mapping
6363
fl::ScreenMap mScreenMap;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/// @file channel_data.cpp
1+
/// @file data.cpp
22
/// @brief Channel transmission data implementation
33

4-
#include "channel_data.h"
4+
#include "fl/channels/data.h"
55
#include "ftl/algorithm.h"
66
#include "ftl/cstring.h"
77
#include "fl/log.h"

0 commit comments

Comments
 (0)