Repository navigation
Fix preview/firmware palette mismatch, plus a WASM parity harness - #156
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 afastled: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.rainbowis 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 apaldef_*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:
rainbowrainbowBeyond 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.
generateCppnow 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:That sketch was at 89.4% of an Uno's SRAM purely from palettes it never referenced.
fbuild symbolsnames them individually (LTO disabled, which is required or AVR collapses everything into onestartupsymbol):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:
.showfile stores a palette id resolved bySET_PALETTEat playback, so any palette may be selected by a file the sketch was never compiled against.generateCppruns whose palette use isn't visible there; ESP32-class hardware, <0.5% of RAM.PaletteSelectorresolves 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 behindPARITY_WASM=1so it skips in the normal suite:Per case it generates the sketch with the real
generateCpp, rewrites its clock somillis()advances exactly 1000/60 ms perloop()(matching the evaluator tick), compiles viafastled --just-compile, loads the module headless in Node, stepsloop()frame by frame, and diffs the bytes fromgetStripPixelDataagainstevaluateGraphat 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.mdunder[Unreleased].Verification
tsc -bclean.Known remaining
samplePalettespreads over 15 and clamps. Fixing it properly would visibly shift every existing preview — a product call, left alone here.estimateFirmwareRamdoesn'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 --jsonlooks like the right ground truth to replace those heuristics with, rather than hand-maintaining another per-node-type estimate.fastled2.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