Skip to content

Fix preview/firmware palette mismatch, plus a WASM parity harness - #156

Merged
stevenmunn312-tech merged 3 commits into
mainfrom
fix/palette-preview-firmware-parity
Aug 7, 2026
Merged

stevenmunn312-tech merged 3 commits into
mainfrom
fix/palette-preview-firmware-parity

Conversation

@stevenmunn312-tech

@stevenmunn312-tech stevenmunn312-tech commented Aug 7, 2026 •

Copy link
Copy Markdown
Owner

Generated firmware rendered different colours than the preview on the default palette of every palette-consuming node. This fixes that, and adds the harness that found it.

The bug

Six palettes — rainbow, heat, ocean, lava, forest, party — carried a fastled: shortcut that emitted FastLED's same-named built-in (RainbowColors_p, HeatColors_p, …) into the sketch, while the preview rendered this project's own colour stops. Same palette name, different colours.

rainbow is the fallback for every palette-consuming node (props.palette ?? 'rainbow'), so this landed on the common path. The other 23 palettes already baked their stops into a paldef_* table and matched by construction; all 29 now take that path.

Measured

By compiling the generated sketch to WASM and diffing its LED bytes against the evaluator, frame by frame:

Case before after
Static ramp through rainbow mean Δ 43.58/255, max 152 Δ 9.04, max 50
Plasma on rainbow Δ 39.13 → 60.76 → 46.88 Δ 13.65 → 13.56 → 13.43
Solid colour (control) Δ 0 Δ 0

Beyond the ~75% reduction, Plasma's drift over time disappears — it was flat at ~13.5 from tick 0 to tick 900 after the fix. The time-varying component was the palette mismatch beating against the moving index.

An isolation experiment split the original error: making the stops identical (via a CustomPalette) dropped mean Δ from 43.58 to 12.42, so palette contents were ~71% of it and the sampling convention the remaining ~29%.

Also: unused palettes cost real flash and RAM

Every palette was emitted regardless of use. generateCpp now records the canonical id each palette reference resolves to and declares only those.

Measured on a real Arduino Uno build — an 8×8 Plasma sketch on the default palette, built with fbuild:

Flash RAM
Before 15,032 B — 46.6% 1,831 B — 89.4%
After 7,072 B — 21.9% 775 B — 37.8%
Saved 7,960 B 1,056 B

That sketch was at 89.4% of an Uno's SRAM purely from palettes it never referenced.

fbuild symbols names them individually (LTO disabled, which is required or AVR collapses everything into one startup symbol):

paldef_ symbols found: 23, total bytes = 1104
   paldef_opal       size=48  region=ram  section=.bss
   paldef_laguna     size=48  region=ram  section=.bss
   paldef_honeycomb  size=48  region=ram  section=.bss
   ...

23 × 48 B in .bss, down to 1 after.

The flash saving is the larger and less obvious half. These are non-const globals with dynamic initializers — each CRGBPalette16 paldef_x(CRGB(...) ×16) emits constructor code that runs at startup, roughly 360 B of flash apiece on top of its 48 B of RAM. So an unused palette costs ~408 B total, not 48. Build time for that sketch also dropped 9.7 s → 3.0 s.

Two generators keep the full set deliberately:

  • SD-show player must — a .show file stores a palette id resolved by SET_PALETTE at playback, so any palette may be selected by a file the sketch was never compiled against.
  • Generative-show controller does because its pattern bodies are rewritten in from separate generateCpp runs whose palette use isn't visible there; ESP32-class hardware, <0.5% of RAM.

PaletteSelector resolves at codegen time, so normal sketches have no runtime any-palette requirement — that was the thing that could have made narrowing unsafe.

The harness

src/codegen/__tests__/wasmParity.test.ts, gated behind PARITY_WASM=1 so it skips in the normal suite:

PARITY_WASM=1 npx vitest run src/codegen/__tests__/wasmParity.test.ts

Per case it generates the sketch with the real generateCpp, rewrites its clock so millis() advances exactly 1000/60 ms per loop() (matching the evaluator tick), compiles via fastled --just-compile, loads the module headless in Node, steps loop() frame by frame, and diffs the bytes from getStripPixelData against evaluateGraph at the same tick.

Exact per-channel comparison, not a screenshot diff. A solid-colour control pins byte order, brightness and XY mapping at Δ0 so the other numbers are trustworthy. Five cases run in ~35s once FastLED is cached.

Behaviour change

Designs already flashed using any of those six palettes will change colour on the next upload — toward what the preview has always shown. Saved projects are unaffected; nothing about their stored shape changes. Recorded in CHANGELOG.md under [Unreleased].

Verification

  • Full suite 1721 passed (+4 new narrowing tests), lint and tsc -b clean.
  • All five parity cases compile for real and render identical deltas after the narrowing — text assertions can't catch a missing declaration, so this was the check that mattered.
  • Not hardware-validated. The WASM run exercises rendering logic, not LED timing or drivers.

Known remaining

  • Residual Δ ~9–13 from the sampling convention: FastLED spreads 16 entries over 16 intervals and wraps the last to the first; samplePalette spreads over 15 and clamps. Fixing it properly would visibly shift every existing preview — a product call, left alone here.
  • estimateFirmwareRam doesn't count palette globals. It had been under-reporting by ~1.1 KB of RAM on every sketch, and has no concept of the flash cost of dynamic initializers at all; the narrowing shrinks the RAM gap to 48 bytes for normal sketches. The live capacity meter was never affected — it reads the linker's real figure. fbuild symbols --json looks like the right ground truth to replace those heuristics with, rather than hand-maintaining another per-node-type estimate.
  • Stateful/RNG nodes (Fire2012) can't be compared per-pixel; they'd need statistical comparison.
  • fastled 2.0.13 has a packaging bug — compilation succeeds and emits the module, then fails assembling its viewer frontend on a hardcoded CI path (D:\a\fastled-wasm\...). Harmless here since we only use the module, but worth reporting upstream.

🤖 Generated with Claude Code

stevenmunn312-tech and others added 3 commits August 7, 2026 12:41
Design Studio implements every node twice — once in graphEvaluator.ts for
the live preview, once in cppGenerator.ts for firmware — with nothing
enforcing that the two agree. Catching drift meant flashing a board.

This harness closes the loop without hardware. Per case it generates the
sketch with the real generateCpp, rewrites its clock so millis() advances
exactly 1000/60 ms per loop() (matching the evaluator tick), compiles to
WASM via fastled --just-compile, loads the module headless in Node, steps
loop() frame by frame, and diffs the LED bytes read back through
getStripPixelData against evaluateGraph at the same tick.

Comparison is exact per channel, not a screenshot diff. A solid-colour
control case pins byte order, brightness and XY mapping at zero delta so
the other measurements are trustworthy.

Gated behind PARITY_WASM=1 since each case costs a WASM compile; it skips
in the normal suite. Four cases run in about 34s once FastLED is cached.

Exact comparison only holds for time-pure nodes. Stateful/RNG patterns
(Fire2012) diverge by construction and would need statistical comparison.

Co-Authored-By: Claude Opus 5 <[email protected]>
Six palettes — rainbow, heat, ocean, lava, forest and party — carried a
`fastled:` shortcut that emitted FastLED's same-named built-in constant
(RainbowColors_p, HeatColors_p, …) into the generated sketch, while the
preview rendered this project's own colour stops. Same palette name,
different colours, on the default palette of every palette-consuming node
(`props.palette ?? 'rainbow'`). The other 23 palettes already baked their
stops into a paldef_* table and matched by construction; all 29 now take
that path.

Measured with the WASM parity harness: a static ramp through `rainbow`
went from mean Δ 43.6/255 per channel (max 152) to Δ 9.0 (max 50), and
Plasma from Δ 39–61 to a flat Δ 13.5 — its drift over time disappearing
with it, since the time-varying component was the palette mismatch
beating against the moving index.

The remaining delta is a separate, smaller issue left alone here: FastLED
spreads 16 palette entries over 16 intervals and wraps the last back to
the first, while samplePalette spreads them over 15 and clamps. Aligning
those would visibly shift every existing preview.

The SD player's palette cases already resolved through paletteCppRef and
pick this up automatically; only its two `default:` fallbacks needed
updating. Test assertions moved to the paldef_* form the file already
used for the other palettes.

Firmware colours change on next upload for designs using those six.
Saved project data is unaffected.

Co-Authored-By: Claude Opus 5 <[email protected]>
Every palette was emitted into every sketch regardless of use. They are
non-const CRGBPalette16 globals — 48 bytes of RAM each, so all 29 cost
1,392 bytes, over half an Arduino Uno's SRAM, on a design referencing
none of them. A single-palette sketch now spends 48 bytes.

generateCpp records the canonical id each fastledPalette() call resolves
and declares only those. The emit pass already ran before declarations
are written, so the set is complete when it gates them. Recording the
resolved id rather than the raw name means an unknown palette name still
declares the rainbow it falls back to.

Two generators keep the full set on purpose:

- The SD-show player must. A .show file stores a palette id that
  SET_PALETTE resolves during playback, so any palette may be selected by
  a file the sketch was never compiled against.
- The generative-show controller does because its pattern bodies are
  rewritten in from separate generateCpp runs whose palette use is not
  visible at that point. It targets ESP32-class hardware where the full
  set is under half a percent of RAM.

Verified by compiling through the WASM parity harness: all five cases
still build and render identical deltas, so nothing lost a declaration
it needed.

Co-Authored-By: Claude Opus 5 <[email protected]>
@stevenmunn312-tech
stevenmunn312-tech marked this pull request as ready for review August 7, 2026 05:04
@stevenmunn312-tech
stevenmunn312-tech merged commit 4298f10 into main Aug 7, 2026
2 checks passed
@stevenmunn312-tech
stevenmunn312-tech deleted the fix/palette-preview-firmware-parity branch August 7, 2026 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant