Skip to content

Convert FxSdCard example to load .fled containers (deprecate the sidecar pattern) #3072

Closed

Description

@zackees

Motivation

The examples/Fx/FxSdCard/ sketch is the canonical reference for "play a video off the SD card." It demonstrates exactly the problem the FLED container format solves: every user has to ship three loose files (video.rgb + color_line_bubbles.rgb + screenmap.json) and keep them in sync. Mismatched files silently break the mapping; the hardcoded MATRIX_WIDTH × MATRIX_HEIGHT has to match total_bytes / (led_count * 3) or the frame slicing is wrong with no diagnostic.

The FLED container (spec, generator zackees/ledmapper#122) packs the screenmap directly into the video file. Converting this sketch to load a single .fled makes it the reference for the new format and lets us delete the entire sidecar-mismatch failure mode.

Companion / dependency: FastLED/FastLED#3063 — the consumer-side FLED parser plus the embeddedScreenMapJson() accessor on Video / PixelStream. This sketch change lands on top of that PR; it's the demo for the new API.

Scope

Convert examples/Fx/FxSdCard/FxSdCard.ino to load .fled files instead of .rgb + screenmap.json sidecars. Two open decisions for the maintainer (calling them out, not pre-deciding):

  1. Replace vs. add-alongside. Replace the existing example (cleaner endgame; legacy sidecar pattern is deprecated) or ship a new examples/Fx/FxSdCardFled/ next to it (preserves the legacy reference). The remaining proposal assumes replace; flip if you prefer the parallel.
  2. Hardcode NUM_LEDS vs. derive from embedded JSON. A hardware sketch can't dynamically resize CRGB leds[] (it needs a compile-time size for the addLeds pin binding), so NUM_LEDS stays a #define. The proposal asserts the embedded screenmap's LED count matches NUM_LEDS at setup and bails with a clear error otherwise — best of both worlds.

Code sketch (proposed FxSdCard.ino diff)

// Before:
// video = filesystem.openVideo("data/video.rgb", NUM_LEDS, FPS, 2);
// video2 = filesystem.openVideo("data/color_line_bubbles.rgb", NUM_LEDS, FPS, 2);
// fl::ScreenMap screenMap;
// bool ok = filesystem.readScreenMap("data/screenmap.json", "strip1", &screenMap);

// After:
video = filesystem.openVideo("data/video.fled", NUM_LEDS, FPS, 2);
if (!video) { FL_WARN("Failed to open video.fled"); gError = true; return; }

video2 = filesystem.openVideo("data/color_line_bubbles.fled", NUM_LEDS, FPS, 2);
if (!video2) { FL_WARN("Failed to open color_line_bubbles.fled"); gError = true; return; }

// New API from #3063: pull the screenmap out of the video file itself.
fl::ScreenMap screenMap;
{
    fl::string err;
    const fl::string& json = video.embeddedScreenMapJson();
    if (json.empty()) {
        FL_WARN("video.fled has no embedded screenmap");
        gError = true; return;
    }
    if (!fl::ScreenMap::ParseJson(json.c_str(), "strip1", &screenMap, &err)) {
        FL_WARN(err.c_str());
        gError = true; return;
    }
    if (screenMap.getLength() != NUM_LEDS) {
        FL_WARN("embedded screenmap LED count does not match NUM_LEDS");
        gError = true; return;
    }
}

// addLeds() / setScreenMap unchanged.

The filesystem.readScreenMap("data/screenmap.json", ...) call disappears entirely.

Sample data conversion

The two .rgb payloads (data/video.rgb, data/color_line_bubbles.rgb) need to be wrapped in the FLED header along with the existing data/screenmap.json. Either:

  • Generate fresh .fled files from the Mapped Video Maker at ledmapper.com (re-record), or

  • Run a one-liner Node script in the FastLED repo's dev/ directory:

    // dev/wrap-fled.mjs
    import fs from 'node:fs';
    const [, , rgbPath, jsonPath, outPath] = process.argv;
    const rgb = fs.readFileSync(rgbPath);
    const jsonBytes = fs.readFileSync(jsonPath);
    const header = Buffer.alloc(12);
    header.write('FLED', 0, 'ascii');
    header[4] = 1;    // version
    header[5] = 0;    // pixel_format = rgb8
    header.writeUInt32LE(jsonBytes.length, 8);
    fs.writeFileSync(outPath, Buffer.concat([header, jsonBytes, rgb]));

    Run twice (one per video). Then git rm data/*.rgb data/screenmap.json && git add data/*.fled.

The ledmapper repo also ships scripts/inspect-fled.mjs which verifies a .fled file is well-formed; use it to sanity-check the conversion.

Documentation

Update data/readme.txt:

video.fled                 — FLED v1 container (32x32 mapped video, ~10s)
color_line_bubbles.fled    — FLED v1 container (32x32 mapped video, looping)

Both files carry the screenmap embedded in their header — no sidecar
.json needed. Spec: https://github.com/zackees/ledmapper/blob/main/docs/fled-format.md
Created with Mapped Video Maker at https://www.ledmapper.com/

Add 2–3 lines of header comment to FxSdCard.ino pointing at the spec link.

Acceptance

  • examples/Fx/FxSdCard/data/ ships two files (the two .fled videos), not four. The .rgb and screenmap.json files are deleted.
  • FxSdCard.ino no longer calls filesystem.readScreenMap and no longer references screenmap.json.
  • Sketch compiles cleanly for at least one supported board (ESP32 or Teensy) and runs on the FastLED web compiler (fastled command) with both videos selectable via whichVideo UI control.
  • If the embedded JSON parses to a different LED count than the hardcoded NUM_LEDS, the sketch surfaces a clear FL_WARN and bails — no silent frame-slicing corruption.
  • data/readme.txt describes the new format and links the spec.

Files involved

  • examples/Fx/FxSdCard/FxSdCard.ino — sketch source
  • examples/Fx/FxSdCard/data/video.rgb → video.fled
  • examples/Fx/FxSdCard/data/color_line_bubbles.rgb → color_line_bubbles.fled
  • examples/Fx/FxSdCard/data/screenmap.json → deleted (embedded in both .fled files)
  • examples/Fx/FxSdCard/data/readme.txt — short rewrite
  • (Optional) dev/wrap-fled.mjs — one-time conversion helper

Out of scope

Activity

  1. zackees commented on Jun 15, 2026

    @zackees
    MemberAuthor

    Attaching the FLED v1 spec inline so FastLED contributors don't need to cross-repo navigate. The canonical, kept-up-to-date copy lives at https://github.com/zackees/ledmapper/blob/main/docs/fled-format.md.


    FLED Video Container Format — v1

    A self-describing container for LED-data video files. A .fled file holds
    raw pixel frames for an LED layout alongside the screenmap that drove the
    recording, so a video and its display geometry stay paired in one file.

    This document is the canonical spec. The generator lives at
    ledmapper and the consumer lives at
    FastLED.

    Status

    Property Value
    Format version 1
    File extension .fled
    MIME type application/vnd.fastled.video
    Magic FLED (0x46 0x4C 0x45 0x44)
    Endianness little-endian (every multi-byte field)

    File layout

    offset size field notes
    0 4 magic ASCII FLED
    4 1 version u8, currently 1
    5 1 pixel_format u8 enum (see below)
    6 2 reserved must be 0
    8 4 json_length u32 LE — length of JSON in bytes
    12 N json_bytes UTF-8 JSON, no NUL terminator, no BOM
    12 + N … payload frame data, stride = ledCount × bytesPerLed[format]

    The fixed header is 12 bytes. json_length is variable (typically 100–2000
    bytes). Total payload size is frame_count × ledCount × bytesPerLed.

    Pixel format enum

    value name bytes/LED channel order notes
    0x00 rgb8 3 R, G, B Default. Phase 1 generator emits only this.
    0x01 gray8 1 V Brightness or effect mask.
    0x02 rgba8 4 R, G, B, A Canvas-native; alpha = effect.
    0x03 rgbw8 4 R, G, B, W SK6812 RGBW strips.
    0x04 rgb565le 2 RRRRR GGGGGG BBBBB (LE) 16-bit packed, little-endian.
    0x05–0xFF — — Reserved.

    Consumers must reject unknown pixel_format values with a clear error
    ("video format 0xNN is not supported by this player"). They must not
    attempt to fall back to rgb8 on unknown values.

    JSON payload

    A superset of the existing screenmap.json. The pixel format is
    deliberately not carried in JSON — it lives in the binary header so
    consumers can slice frames without parsing JSON at all.

    {
      "map": {
        "strip1": { "x": [0.0, 1.0], "y": [0.0, 0.0], "diameter": 0.25 }
      },
      "video": {
        "fps": 60
      }
    }
    field required notes
    map yes Standard ScreenMap schema. LED count derives from the total point count across all strips.
    video.fps no Playback frame rate. Consumers default to 60 if absent.

    Authors must not write video.format — it would be a redundant second
    source of truth. Consumers must ignore any video.format key if
    present (a v1 reader cannot trust JSON over the header).

    Versioning

    • version starts at 1. A bump is required for any breaking change to:
      • the binary header layout
      • the JSON schema (required fields, type changes)
      • existing pixel-format enum semantics
    • Adding a new value to the pixel_format enum is not a version
      bump — unknown values already have a defined rejection behavior, so
      forward-compatibility is free.
    • A v1 consumer encountering a version > 1 file must reject it with
      "format version N is not supported".

    Backwards compatibility with legacy .rgb

    Files without the FLED magic in the first 4 bytes are not FLED files.
    Tools that previously read raw headerless .rgb (the legacy format
    produced before this spec) should magic-check before assuming the format:

    • Movie Player rejects legacy headerless files outright. Users must
      re-record with the current Mapped Video Maker to get an embedded
      screenmap.
    • The Mapped Video Maker only emits .fled files going forward.
      Legacy headerless .rgb is never written again.

    Test vectors

    The canonical reference vectors live in
    tests/unit/rgb-video.test.ts (ledmapper) and the equivalent FastLED
    tests. A minimal valid file is:

    • header: 46 4C 45 44 01 00 00 00 <json_length_u32_LE>
    • JSON: {"map":{"a":{"x":[0],"y":[0]}}} (31 bytes UTF-8)
    • payload: FF 00 00 (one frame, one LED, pure red)
    • total file size: 12 + 31 + 3 = 46 bytes

    A "metadata-only" .fled with zero frames (12 + 31 = 43 bytes) is valid
    and useful as a screenmap-only carrier.

    See also

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