Repository navigation
Description
Implementation issue capturing the final screenmap-v2 schema design and the parser changes needed to support it alongside the existing v1 (FastLED ScreenMap) format.
Research and design lives in #88 (data-model exploration), #89 (v1 backward-compat unit test), #90 (first design loop), and #91 (second loop — winning v2 schema). This issue is the concrete spec to build against.
Canonical schema (v2)
{
"version": 2,
"groups": {
"trunk": { "color": "#ffffff" },
"branch": { "color": "#ff0000" }
},
"segments": [
{ "id": "a", "pin": 1, "group": "trunk",
"x": [0, 1, 2, 3, 4],
"y": [0, 0, 0, 0, 0] },
{ "id": "b", "pin": 2, "group": "branch", "parent": "a", "offset": 2,
"x": [2, 2, 2, 2],
"y": [0, 1, 2, 3] }
]
}Rules
version— optional integer. When omitted, the parser auto-detects (see "Version detection" below). When present, must be2for the v2 parser path.segments— ordered array. Order = wiring order within a pin.id— required, string, unique across all segments.pin— required. Hardware output identifier (string or integer; canonical form is integer).group— required. Keys into the top-levelgroupsmap.x,y— required parallel float arrays of equal length. Each LED is one element.z— optional parallel float array, same length asx/ywhen present. Enables 3D installations.parent— omitted on root segments, required on forks. Value is theidof the segment this fork branches off.offset— optional on forks. Integer ornull:- omitted /
null→ tip (parent's last LED) — the default. - non-negative
N→ parent's LED at indexN(0-based forward). - negative
-N→NLEDs before the tip (-1= penultimate,-2= two before, etc.). - Not
-0(JSON serialization collapses it to0; usenullfor tip).
- omitted /
groups[name]— dict (not a bare color string) so future per-group attributes (density,chipset, etc.) extend without schema churn. Required key:color(CSS hex). Optional keys: future-reserved.- No running sums anywhere. No
start,at, derived offset fields. Consumers compute these. - Child's first
(x, y)should match the parent's(x[resolved_offset], y[resolved_offset])— physical contact at the branch point.
Backward compatibility — keep the v1 parser
The existing v1 format (FastLED ScreenMap — { "map": { "<name>": { "x":[…], "y":[…], "diameter":<float?> } } }) stays supported. Do not remove or refactor the v1 parser.
Version detection
In src/common.ts (or equivalent), add a dispatcher:
function parseScreenmap(raw: string | object): ParsedScreenmap {
const obj = typeof raw === 'string' ? JSON.parse(raw) : raw;
if (obj.version === 2) return parseV2(obj); // explicit v2
if (obj.version === 1) return parseV1(obj); // explicit v1
// Auto-detect: v2 if it has `segments` (array) or `groups`; v1 if it has `map`.
if (Array.isArray(obj.segments) || obj.groups) return parseV2(obj);
if (obj.map && typeof obj.map === 'object') return parseV1(obj);
throw new Error('Unrecognized screenmap format (no version, no map, no segments)');
}Auto-detection priorities (top to bottom):
- Explicit
"version": 2→ v2 parser. - Explicit
"version": 1→ v1 parser. - Has
segments(array) orgroupsat the root → v2 parser. - Has
map(object) at the root → v1 parser. - None of the above → error.
A round-tripped v2 file SHOULD include "version": 2 (writer-side default). A hand-edited v2 file MAY omit it; auto-detection covers that case.
A round-tripped v1 file keeps its current shape (no version field added).
TypeScript types
Goes in src/types/domain.ts (alongside the existing ScreenmapJson / ScreenmapStrip v1 types — do NOT modify those):
export interface ScreenmapV2 {
version?: 2;
groups: Record<string, ScreenmapGroup>;
segments: ScreenmapSegment[];
}
export interface ScreenmapGroup {
color: string;
// Future-reserved: density, chipset, etc.
[key: string]: unknown;
}
export interface ScreenmapSegment {
id: string;
pin: number | string;
group: string;
x: number[];
y: number[];
z?: number[];
parent?: string; // present iff this segment is a fork
offset?: number | null; // forks only; null/omitted = tip; negative = N before tip
}Validation invariants (parser must enforce)
- Unique
idacross all segments. groupresolves to a key ingroups.x.length === y.length(and equal toz.lengthifzpresent).parent(when present) resolves to another segment'sid.offset(when present and non-null) is an integer in[-parent.x.length+1, parent.x.length-1]. (Negative range bounded so-Nalways lands in the parent.)- Implementations are encouraged to warn (not error) if child's first
(x, y)differs from the parent's branch point, since this is a soft semantic rule.
Each invariant gets a unit test alongside the parser. Closes / dovetails with #89.
Acceptance
-
parseScreenmapV2(obj): ScreenmapV2lands insrc/common.ts(or a newsrc/screenmap-v2.ts). - v1 parser (
parse_screenmap_data_json,parseScreenmapMultiStrip) untouched. - Dispatcher
parseScreenmap(raw)auto-detects + delegates per the priorities above. -
ScreenmapV2,ScreenmapGroup,ScreenmapSegmentTS types insrc/types/domain.ts. - Unit tests in
tests/unit/screenmap-v2.test.tscovering: round-trip fixtures, every validation invariant, theoffsetresolution rules (omitted, null, positive, negative, edge cases), v1/v2 auto-detection, explicit"version": 2, unknown top-level keys forward-compat. - v1 backward-compat unit test from Pin screenmap.json base schema via a backward-compat unit test #89 stays green.
- Shape editor (
src/shapeeditor/) and moviemaker (src/moviemaker/) consume v2 alongside v1 — no UI changes in this issue unless required for the parse path.
Out of scope (separate issues)
- Editor UI for forks/groups/3D — separate UI work.
- Migration tool to convert legacy v1 files to v2 on disk — not needed (auto-detect handles in-memory load).
- Firmware exporter from v2 to FastLED
ScreenMapshape — separate issue. - Moviemaker depth-aware (z) sampling — separate issue.
Activity
feat(screenmap): add v2 schema parser alongside v1, auto-detect by sh…
feat(screenmap): add v2 parser + dispatcher with auto-detection (#93)