Skip to content

refactor(channels): separate ClocklessEncoder from ChipsetTimingConfig (Design 2) #2467

Closed

Description

@zackees

Motivation

fl::ChipsetTimingConfig (src/fl/chipsets/chipset_timing_config.h:16) currently bundles bit-period timing (t1_ns, t2_ns, t3_ns, reset_us) with an unrelated concern: ClocklessEncoder encoder (the byte-level pipeline selector used to dispatch WS2812 vs UCS7604 encoding).

The codebase already signals that these fields aren't peers of the same nature:

  • ChipsetTimingConfig::operator== deliberately ignores encoder (chipset_timing_config.h:36-41). A field that doesn't participate in identity isn't really part of the type's concept.
  • ClocklessEncoder lives in src/fl/chipsets/led_timing.h, whose own header comment declares it as "nanosecond-based T1, T2, T3 timing model" — the enum is a misfit there.
  • The encoder field is read in exactly one place (src/fl/channels/channel.cpp.hpp:378-388) to dispatch between writeWS2812() and writeUCS7604().

Call sites pay a small ergonomic tax for the merged design:

auto timing = fl::makeTimingConfig<fl::TIMING_WS2812_800KHZ>();
fl::ChannelConfig cfg("left_strip", fl::ClocklessChipset(PIN1, timing), leds, RGB);

This two-line dance appears in ~80 sites across tests/, examples/, and src/platforms/.

Proposal (Design 2)

Separate the concerns and add an ergonomic helper.

1. Pure-timing ChipsetTimingConfig

Drop the encoder field. operator== then means exactly what it says.

struct ChipsetTimingConfig {
    u32 t1_ns, t2_ns, t3_ns, reset_us;
    const char* name;
    // encoder removed
};

2. Encoder lifted to ClocklessChipset as a peer field

struct ClocklessChipset {
    int pin;
    ChipsetTimingConfig timing;
    ClocklessEncoder encoder;   // peer concern, not a sub-field of timing
};

3. New helper collapses the two-step pattern

template<typename TIMING>
constexpr ClocklessChipset makeClockless(int pin) FL_NOEXCEPT {
    return { pin, makeTimingConfig<TIMING>(),
             detail::get_encoder<TIMING, has_encoder<TIMING>::value>::value };
}

Call sites become one line:

fl::ChannelConfig cfg("left_strip", fl::makeClockless<fl::TIMING_WS2812_800KHZ>(PIN1), leds, RGB);

4. Move ClocklessEncoder enum out of led_timing.h

New header src/fl/chipsets/clockless_encoder.h owns the enum. led_timing.h returns to bit-timing only, matching its documented purpose.

Migration plan

  1. Add clockless_encoder.h; move enum class ClocklessEncoder and the has_encoder / get_encoder SFINAE helpers there.
  2. Update ChipsetTimingConfig: remove encoder field, ctor argument, and the equality-ignore comment.
  3. Add encoder to ClocklessChipset (with CLOCKLESS_ENCODER_WS2812 default for backward-compat constructors).
  4. Add makeClockless<TIMING>(pin) in chipset_timing_config.h (or a new clockless_chipset.h).
  5. Update the single consumer at channel.cpp.hpp:378 (clockless->timing.encoder → clockless->encoder).
  6. Migrate call sites in tests/, examples/, src/platforms/ to makeClockless<>() opportunistically (not a hard requirement — makeTimingConfig<>() still works for the timing-only path).
  7. Update src/fl/channels/README.md and FastLED.h doc examples.

Acceptance criteria

  • bash test passes
  • bash lint clean
  • ChipsetTimingConfig::operator== compares all fields (no ignored field)
  • led_timing.h no longer mentions ClocklessEncoder
  • README and FastLED.h examples use fl::makeClockless<>() for one-liner clockless construction
  • Existing makeTimingConfig<>() continues to work; ClocklessChipset(pin, timing) two-arg form continues to work (defaults encoder to WS2812)

Out of scope

  • Compile-time-only chipset typing via ClocklessChipsetT<TIMING> (that was "Design 1" in the design discussion — too invasive for the variant/driver layer to be worth doing right now).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coretype: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions