Skip to content

Implement screenmap v2 schema parser + version dispatch (v1 kept for backward compat) #92

Closed

Description

@zackees

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 be 2 for 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-level groups map.
  • x, y — required parallel float arrays of equal length. Each LED is one element.
  • z — optional parallel float array, same length as x/y when present. Enables 3D installations.
  • parent — omitted on root segments, required on forks. Value is the id of the segment this fork branches off.
  • offset — optional on forks. Integer or null:
    • omitted / null → tip (parent's last LED) — the default.
    • non-negative N → parent's LED at index N (0-based forward).
    • negative -N → N LEDs before the tip (-1 = penultimate, -2 = two before, etc.).
    • Not -0 (JSON serialization collapses it to 0; use null for tip).
  • 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):

  1. Explicit "version": 2 → v2 parser.
  2. Explicit "version": 1 → v1 parser.
  3. Has segments (array) or groups at the root → v2 parser.
  4. Has map (object) at the root → v1 parser.
  5. 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 id across all segments.
  • group resolves to a key in groups.
  • x.length === y.length (and equal to z.length if z present).
  • parent (when present) resolves to another segment's id.
  • offset (when present and non-null) is an integer in [-parent.x.length+1, parent.x.length-1]. (Negative range bounded so -N always 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): ScreenmapV2 lands in src/common.ts (or a new src/screenmap-v2.ts).
  • v1 parser (parse_screenmap_data_json, parseScreenmapMultiStrip) untouched.
  • Dispatcher parseScreenmap(raw) auto-detects + delegates per the priorities above.
  • ScreenmapV2, ScreenmapGroup, ScreenmapSegment TS types in src/types/domain.ts.
  • Unit tests in tests/unit/screenmap-v2.test.ts covering: round-trip fixtures, every validation invariant, the offset resolution 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 ScreenMap shape — separate issue.
  • Moviemaker depth-aware (z) sampling — separate issue.

Cross-refs: #88, #89, #90, #91.

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

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions