Skip to content

feat(fl/fled): FledHardware section schema — strip configs, color processing, RGBW, global FastLED config #3315

Closed

Description

@zackees

Summary

Sub-issue of #3311 (fl::Fled bundle subsystem). Designs the FledHardware section — the bundle's serializable description of physical LED strips and global FastLED config, sufficient to drive video on the Channel API without user-written addLeds<>() boilerplate.

When this section is present in a .fled bundle, fl::FledRunner::applyHardware() reads it and instantiates the matching fl::CLEDControllers (via the Channels API) with pins, chipsets, color order, bus selection, color correction, RGBW handling, and screenmap binding — everything a sketch normally writes by hand at setup() time.

Lives in src/fl/fled/ (path TBD per #3311's "implementation details deliberately left open" policy).


Header design

// fl/fled/fled_hardware.h
#pragma once
#include "fl/channels/bus.h"     // fl::Bus enum (RMT, PARLIO, SPI, I2S, ...)
#include "fl/stl/string.h"
#include "fl/stl/vector.h"
#include "fl/stl/optional.h"
#include "fl/stl/u8.h"

namespace fl {

// ─── Enums (frozen — additions are non-breaking; renames are breaking) ────

enum class Chipset : fl::u8 {
    // Clockless (single-wire)
    WS2811, WS2812, WS2812B, WS2813, WS2814, WS2815, WS2816, WS2818,
    NEOPIXEL, SK6812, SK6812RGBW,
    APA106, GS1903, PL9823,
    TM1803, TM1804, TM1809, TM1812,
    UCS1903, UCS1903B, UCS1904, UCS2903,
    LPD1886, GW6205, GW6205_400,

    // Clocked (two-wire / SPI)
    APA102, APA102HD, DOTSTAR, DOTSTARHD,
    SK9822, SK9822HD,
    P9813, SM16716,
    WS2801, WS2803,
    LPD6803, LPD8806,

    // Custom — chipset-defined timing; see customTiming
    CUSTOM,
};

enum class ColorOrder : fl::u8 { RGB, RBG, GRB, GBR, BRG, BGR };

// SK6812 / WS2814 / etc. 4-channel handling
enum class RgbwMode : fl::u8 {
    Disabled,         // RGB only; W channel zeroed
    NominalEmitter,   // W = min(R,G,B); standard "warm white" coercion
    MaxBrightness,    // R/G/B/W independent — full 4-channel power
    Exact,            // bundle/script supplies pre-computed RGBW frames
};

// ─── Color processing structs (mirror FastLED's existing types) ───────────

struct ColorCorrection {
    fl::u8 r, g, b;
    static ColorCorrection TypicalLEDStrip();    // {255, 176, 240}
    static ColorCorrection TypicalPixelString(); // {255, 224, 140}
    static ColorCorrection UncorrectedColor();   // {255, 255, 255}
};

struct ColorTemperature {
    fl::u8 r, g, b;
    static ColorTemperature TypicalLEDStripTemperature();
    static ColorTemperature Tungsten40W();
    static ColorTemperature Halogen();
    static ColorTemperature DirectSunlight();
    // full mirror of FastLED's named temperatures
};

struct CustomClocklessTiming {
    fl::u16 t1ns;     // high pulse for 0 bit
    fl::u16 t2ns;     // high pulse for 1 bit
    fl::u16 t3ns;     // low pulse common to both
};

// ─── Per-strip configuration ──────────────────────────────────────────────

struct StripConfig {
    // ── Identity ──
    fl::string name;                       // "front", "ring1"; used for overrides + diagnostics

    // ── Required physical config ──
    Chipset    chipset;
    fl::u8     pin;                        // data pin (or single pin for clockless)
    fl::size   ledCount;
    ColorOrder colorOrder    = ColorOrder::GRB;

    // ── Bus / driver selection (fl::Bus from src/fl/channels/bus.h) ──
    fl::Bus bus = fl::Bus::AUTO;           // AUTO = ChannelManager picks by priority

    // ── Clocked-only (APA102, DotStar, SK9822, etc.) ──
    fl::optional<fl::u8>  clockPin;
    fl::optional<fl::u32> spiHz;           // nullopt = chipset/platform default

    // ── Color processing (all optional) ──
    fl::optional<ColorCorrection>  correction;
    fl::optional<ColorTemperature> temperature;
    fl::optional<fl::u8>           gamma;          // 0..255; nullopt = linear (1.0)
    bool                           invertOutput = false;

    // ── RGBW (4-channel strips only) ──
    RgbwMode rgbwMode = RgbwMode::Disabled;

    // ── Custom timing (used only when chipset == Chipset::CUSTOM) ──
    fl::optional<CustomClocklessTiming> customTiming;

    // ── ScreenMap binding ──
    // Empty   = use the bundle's default screenmap (first/only one).
    // Named   = look up by name in the bundle's FledScreenMap section.
    // (Multiple strips can share a screenmap by pointing at the same name.)
    fl::string screenMapName;
};

// ─── Global / FastLED-wide configuration ──────────────────────────────────

struct GlobalConfig {
    fl::u8 brightness = 255;                              // FastLED.setBrightness
    fl::optional<fl::u32> maxPowerMilliwatts;             // FastLED.setMaxPowerInMilliWatts
    fl::optional<fl::u8>  maxPowerVolts;                  // setMaxPowerInVoltsAndMilliamps
    fl::optional<fl::u32> maxPowerMilliamps;              // setMaxPowerInVoltsAndMilliamps
    fl::optional<fl::u16> maxRefreshHz;                   // FastLED.setMaxRefreshRate
    bool dither = true;                                   // FastLED.setDither
};

// ─── The FledHardware section ─────────────────────────────────────────────

class FledHardware {
public:
    GlobalConfig            global;
    fl::vector<StripConfig> strips;

    // Convenience lookup
    StripConfig*       findStrip(const char* name);
    const StripConfig* findStrip(const char* name) const;
};

} // namespace fl

Per #3311's uniform return-type rule, fl::Fled::hardware() returns fl::shared_ptr<FledHardware> (nullptr if no hardware section in the bundle).


Runner integration

FledRunner::applyHardware() reads the section and configures FastLED:

void FledRunner::applyHardware() {
    auto hw = mFled.hardware();        // shared_ptr<FledHardware>
    if (!hw) return;                   // no HW section — overrides only

    fl::size offset = 0;
    for (const auto& strip : hw->strips) {
        auto ledSpan = mLeds.subspan(offset, strip.ledCount);

        auto& controller = FastLED.addLeds(
            strip.chipset, strip.pin, strip.colorOrder, strip.bus,
            ledSpan
        );

        if (strip.correction)   controller.setCorrection(*strip.correction);
        if (strip.temperature)  controller.setTemperature(*strip.temperature);
        if (strip.gamma)        controller.setGamma(*strip.gamma);
        if (strip.invertOutput) controller.setInverted();
        if (strip.rgbwMode != RgbwMode::Disabled)
            controller.setRgbw(toFastLedRgbw(strip.rgbwMode));
        if (strip.clockPin)
            controller.setClockPin(*strip.clockPin, strip.spiHz.value_or(0));

        // Screenmap wiring — pull from the bundle by name
        if (auto sm = mFled.screenMap(strip.screenMapName.c_str()))
            controller.setScreenMap(*sm);

        offset += strip.ledCount;
    }

    FastLED.setBrightness(hw->global.brightness);
    if (hw->global.maxPowerMilliwatts)
        FastLED.setMaxPowerInMilliWatts(*hw->global.maxPowerMilliwatts);
    if (hw->global.maxRefreshHz)
        FastLED.setMaxRefreshRate(*hw->global.maxRefreshHz);
    FastLED.setDither(hw->global.dither ? BINARY_DITHER : DISABLE_DITHER);
}

Override API

Runner overrides mutate the StripConfig array before applyHardware() runs. Two surfaces:

Imperative (Layer 2 — FledRunner)

fl::FledRunner runner(fled);
runner.overridePin("front", 4);                          // by strip name
runner.overrideChipset(0, fl::Chipset::SK6812);          // by index
runner.overrideColorOrder("ring", fl::ColorOrder::BGR);
runner.overrideBus("front", fl::Bus::PARLIO);            // force a specific driver
runner.overrideBrightness(64);                           // global
runner.run();

Declarative (Layer 1 sugar — fl::Fled::run)

fl::Fled::run("sketch.fled", {
    .leds = {leds, NUM_LEDS},
    .stripOverrides = {
        { .strip = "front", .pin = 4, .bus = fl::Bus::PARLIO },
        { .strip = "ring",  .chipset = fl::Chipset::SK6812,
                            .rgbwMode = fl::RgbwMode::NominalEmitter },
    },
    .globalOverrides = { .brightness = 64, .maxRefreshHz = 60 },
});

Override fields are all fl::optional<T> — only specified fields are applied; unspecified fields keep the bundle's value.


On-disk JSON representation

Hardware section lives in the bundle's JSON envelope under the "hardware" key:

{
  "hardware": {
    "global": {
      "brightness": 96,
      "maxPowerMilliwatts": 5000,
      "dither": true
    },
    "strips": [
      {
        "name": "front",
        "chipset": "WS2812B",
        "pin": 2,
        "ledCount": 1024,
        "colorOrder": "GRB",
        "bus": "RMT",
        "correction": "TypicalLEDStrip",
        "temperature": "TypicalLEDStripTemperature",
        "screenMapName": "front_matrix"
      },
      {
        "name": "ring",
        "chipset": "APA102HD",
        "pin": 4,
        "clockPin": 5,
        "spiHz": 8000000,
        "ledCount": 60,
        "colorOrder": "BGR",
        "bus": "SPI",
        "screenMapName": "ring"
      },
      {
        "name": "rgbw_accent",
        "chipset": "SK6812RGBW",
        "pin": 6,
        "ledCount": 32,
        "colorOrder": "GRB",
        "bus": "RMT",
        "rgbwMode": "NominalEmitter"
      }
    ]
  }
}

Enum values are strings. "WS2812B", "RMT", "NominalEmitter" — the on-disk format does NOT depend on the C++ enum's numeric values, so renumbering the enum in code is non-breaking. Adding a new enum variant in C++ is non-breaking on the read path (a v1 reader rejects unknown strings with a clear error per the bundle format spec). Removing or renaming an enum variant requires a bundle format version bump.

Optional / defaulted fields can be omitted. A minimal strip entry is just:

{ "name": "main", "chipset": "WS2812B", "pin": 2, "ledCount": 256 }

Everything else picks up sensible defaults (colorOrder=GRB, bus=AUTO, no correction, no temperature, etc.).


Coordination with the canonical format spec

The on-disk JSON shape for hardware lives in ledmapper's docs/fled-format.md alongside the existing map and video.fps blocks (per the canonical spec FastLED mirrors locally). This issue's PR should land alongside (or just after) an update to ledmapper's spec doc adding the "hardware" keyed section to the JSON envelope schema.

Cross-link to file:


Reserved for future (NOT in v1)

Additive, non-breaking when added:

  • Per-LED color calibration tables — vector<ColorCorrection> of size ledCount. Useful for per-pixel temperature compensation; expensive in flash. Defer until a FledCalibration section lands.
  • Multi-pin parallel-output groups — for PARLIO / I2S 8-wide strips. Today PARLIO's parallel grouping lives inside its driver config; StripConfig can express only "one strip per entry."
  • Per-strip software dimming curves / brightness scalars — sketches that need them can compute in code today.
  • Network controllers (Art-Net, sACN, WLED protocol) — out of scope; Channel API doesn't have a clean "bus = network" surface yet.

Each can be added as new optional fields on StripConfig or as new sibling sections (FledCalibration, etc.) without breaking v1 consumers.


Acceptance criteria

  • fl::FledHardware, fl::StripConfig, fl::GlobalConfig, fl::Chipset, fl::ColorOrder, fl::RgbwMode, fl::ColorCorrection, fl::ColorTemperature, fl::CustomClocklessTiming exist in fl/fled/ and compile.
  • fl::Fled::hardware() returns fl::shared_ptr<FledHardware> (nullptr if absent).
  • FledRunner::applyHardware() instantiates all strips via the Channels API and wires screenmaps from the bundle's FledScreenMap section by name.
  • Override API (both imperative and declarative) lands; overrides mutate StripConfig fields before applyHardware() runs.
  • JSON serializer/deserializer round-trips the section. Unknown enum strings produce a clear error; unknown JSON keys produce a clear error (per the bundle format spec).
  • ColorCorrection::TypicalLEDStrip() etc. return values matching FastLED's existing named constants — bit-exact.
  • At least one example sketch in examples/ demonstrates a .fled file with a hardware section being loaded and run end-to-end with the runner.
  • Coordinated update to ledmapper's canonical docs/fled-format.md adding the "hardware" JSON envelope key.

Parent

#3311 — fl::Fled bundle subsystem (umbrella)

Refs

Labels

area:fl/fled, area:channels, area:video, tracking

Activity

  1. zackees commented on Jun 20, 2026

    @zackees
    MemberAuthor

    Retiring — the FledHardware section design is now embedded as a comment on the umbrella issue #3311 (#3311) so the entire fl::Fled bundle design lives in one place. No content lost; the full schema, runner integration, override API, JSON shape, and acceptance criteria all moved verbatim.

  2. moved this from Triage to Done in FastLED Trackeron Jun 20, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions