diff --git a/.claude/CLAUDE.md b/.claude/CLAUDE.md new file mode 100644 index 0000000..b20ba5b --- /dev/null +++ b/.claude/CLAUDE.md @@ -0,0 +1,3 @@ +# graphify +- **graphify** (`.claude/skills/graphify/SKILL.md`) - any input to knowledge graph. Trigger: `/graphify` +When the user types `/graphify`, use the installed graphify skill or instructions before doing anything else. diff --git a/.claude/skills/graphify/.graphify_version b/.claude/skills/graphify/.graphify_version new file mode 100644 index 0000000..45b6336 --- /dev/null +++ b/.claude/skills/graphify/.graphify_version @@ -0,0 +1 @@ +0.9.101 \ No newline at end of file diff --git a/.claude/skills/graphify/SKILL.md b/.claude/skills/graphify/SKILL.md new file mode 100644 index 0000000..c07aedd --- /dev/null +++ b/.claude/skills/graphify/SKILL.md @@ -0,0 +1,797 @@ +--- +name: graphify +description: "Use for any question about a codebase, its architecture, file relationships, or project content — especially when graphify-out/ exists, where the question should be treated as a graphify query first. Turns any input (code, docs, papers, images, videos) into a persistent knowledge graph with god nodes, community detection, and query/path/explain tools." +--- + +# /graphify + +Turn any folder of files into a navigable knowledge graph with community detection, an honest audit trail, and three outputs: interactive HTML, GraphRAG-ready JSON, and a plain-language GRAPH_REPORT.md. + +## Usage + +``` +/graphify # full pipeline on current directory (HTML viz; add --obsidian for a vault) +/graphify # full pipeline on specific path +/graphify https://github.com// # clone repo then run full pipeline on it +/graphify https://github.com// --branch # clone a specific branch +/graphify ... # clone multiple repos, build each, merge into one cross-repo graph +/graphify --mode deep # thorough extraction, richer INFERRED edges +/graphify --update # incremental - re-extract only new/changed files +/graphify --directed # build directed graph (preserves edge direction: source→target) +/graphify --whisper-model medium # use a larger Whisper model for better transcription accuracy +/graphify --cluster-only # rerun clustering on existing graph +/graphify --no-viz # skip visualization, just report + JSON +/graphify --html # (HTML is generated by default - this flag is a no-op) +/graphify --svg # also export graph.svg (embeds in Notion, GitHub) +/graphify --graphml # export graph.graphml (Gephi, yEd) +/graphify --neo4j # generate graphify-out/cypher.txt for Neo4j +/graphify --neo4j-push bolt://localhost:7687 # push directly to Neo4j +/graphify --falkordb # generate graphify-out/cypher.txt for FalkorDB +/graphify --falkordb-push falkordb://localhost:6379 # push directly to FalkorDB +/graphify --mcp # start MCP stdio server for agent access +/graphify --watch # watch folder, auto-rebuild on code changes (no LLM needed) +/graphify --wiki # build agent-crawlable wiki (index.md + one article per community) +/graphify --obsidian --obsidian-dir ~/vaults/my-project # write vault to custom path (e.g. existing vault) +/graphify add # fetch URL, save to ./raw, update graph +/graphify add --author "Name" # tag who wrote it +/graphify add --contributor "Name" # tag who added it to the corpus +/graphify query "" # BFS traversal - broad context +/graphify query "" --dfs # DFS - trace a specific path +/graphify query "" --budget 1500 # cap answer at N tokens +/graphify path "AuthModule" "Database" # shortest path between two concepts +/graphify explain "SwinTransformer" # plain-language explanation of a node +``` + +## What graphify is for + +Drop any folder of code, docs, papers, images, or video into graphify and get a queryable knowledge graph. Persistent across sessions, honest audit trail (EXTRACTED/INFERRED/AMBIGUOUS), community detection surfaces cross-document connections you wouldn't think to ask about. + +**When a graph is worth building:** this applies when you are choosing a tool yourself; if the user invoked `/graphify`, follow that request as asked. AST extraction of code is fast and free, but semantic extraction of docs, papers and images runs the model over every file, which can cost minutes and tens of thousands of tokens. + +If a graph already exists, query it. With no graph, one targeted lookup ("where is X defined", "what calls Y" in an ordinary-sized codebase) is faster and free with a plain text search such as `grep` and `Read`: search directly instead of running the steps below, and mention that a graph can be built if the user wants one. Build one when you expect many questions against this corpus, the corpus is large or unfamiliar, the question needs links text search cannot see (doc to code, across documents), or it is structural (communities, bridges, import cycles). + +## What You Must Do When Invoked + +If the user invoked `/graphify --help` or `/graphify -h` (with no other arguments), print the contents of the `## Usage` section above verbatim and stop. Do not run any commands, do not detect files, do not default the path to `.`. Just print the Usage block and return. + +**Fast path — existing graph:** Before doing anything else, check whether `graphify-out/graph.json` exists. The expected location is `graphify-out/graph.json` relative to the **current working directory** (i.e. the project root where you are running commands). If it exists AND the user's request is a natural-language question about the codebase (e.g. "How does X work?", "What calls Y?", "Trace the data flow through Z") and NOT an explicit rebuild command (`--update`, `--cluster-only`, or a bare path/URL that implies fresh extraction): **skip Steps 1–5 entirely and jump straight to `## For /graphify query`.** Run `graphify query ""` immediately. Do not run detect. Do not check corpus size. Do not ask the user to narrow. The graph is already built — use it. + +If no path was given, use `.` (current directory). Do not ask the user for a path. + +If the path argument starts with `https://github.com/` or `http://github.com/`, treat it as a GitHub URL - run Step 0 before anything else, then continue with the resolved local path. + +Follow these steps in order unless the fast path above applies. + +### Step 0 - GitHub repos and multi-path merge (only if a URL or several paths) + +Only when the path is one or more `https://github.com/...` URLs, or several local subfolders to merge. See `references/github-and-merge.md` for the clone, cross-repo merge, and monorepo flow, then continue with the resolved local path. A plain local path skips this step. + +### Step 1 - Ensure graphify is installed + +```bash +# Detect the correct Python interpreter (handles uv tool, pipx, venv, system installs) +PYTHON="" +GRAPHIFY_BIN=$(which graphify 2>/dev/null) +# 1. uv tool installs — most reliable on modern Mac/Linux +if [ -z "$PYTHON" ] && command -v uv >/dev/null 2>&1; then + _UV_PY=$(uv tool run --from graphifyy python -c "import sys; print(sys.executable)" 2>/dev/null) + if [ -n "$_UV_PY" ]; then PYTHON="$_UV_PY"; fi +fi +# 2. Read shebang from graphify binary (pipx and direct pip installs) +if [ -z "$PYTHON" ] && [ -n "$GRAPHIFY_BIN" ]; then + _SHEBANG=$(head -1 "$GRAPHIFY_BIN" | tr -d '#!') + case "$_SHEBANG" in + *[!a-zA-Z0-9/_.@-]*) ;; + *) "$_SHEBANG" -c "import graphify" 2>/dev/null && PYTHON="$_SHEBANG" ;; + esac +fi +# 3. Fall back to python3 +if [ -z "$PYTHON" ]; then PYTHON="python3"; fi +if ! "$PYTHON" -c "import graphify" 2>/dev/null; then + if command -v uv >/dev/null 2>&1; then + uv tool install --upgrade graphifyy -q 2>&1 | tail -3 + _UV_PY=$(uv tool run --from graphifyy python -c "import sys; print(sys.executable)" 2>/dev/null) + if [ -n "$_UV_PY" ]; then PYTHON="$_UV_PY"; fi + else + "$PYTHON" -m pip install graphifyy -q 2>/dev/null \ + || "$PYTHON" -m pip install graphifyy -q --break-system-packages 2>&1 | tail -3 + fi +fi +# Write interpreter path for all subsequent steps (persists across invocations) +mkdir -p graphify-out +"$PYTHON" -c "import sys; open('graphify-out/.graphify_python', 'w', encoding='utf-8').write(sys.executable)" +# Save scan root so `graphify update` (no args) knows where to look next time. +# The scan path is passed through a quoted heredoc, never substituted into the +# command line itself: a bare `cd ` (or an unquoted heredoc, which +# still expands $()/backticks in its body) would let a malicious path execute +# as shell code the moment this line runs. +"$PYTHON" -c "import os, sys; out_path = os.path.abspath('graphify-out/.graphify_root'); os.chdir(sys.stdin.readline().rstrip('\n')); open(out_path, 'w', encoding='utf-8').write(os.getcwd())" <<'GRAPHIFY_ROOT_EOF' +INPUT_PATH +GRAPHIFY_ROOT_EOF +``` + +If the import succeeds, print nothing and move straight to Step 2. + +**In every subsequent bash block, replace `python3` with `$(cat graphify-out/.graphify_python)` to use the correct interpreter.** + +### Step 2 - Detect files + +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from graphify.detect import detect +from pathlib import Path +from graphify.paths import out_path +from graphify.watch import _read_build_excludes, _read_build_gitignore +result = detect(Path('INPUT_PATH'), extra_excludes=_read_build_excludes(out_path()), gitignore=_read_build_gitignore(out_path())) +# Write the sidecar from Python, not a shell redirect, so the same block renders +# on PowerShell hosts without console-encoding drift (#2528). +Path('graphify-out/.graphify_detect.json').write_text(json.dumps(result, ensure_ascii=False), encoding=\"utf-8\") +print(f'Detected {result[\"total_files\"]} files') +" +``` + +Replace INPUT_PATH with the actual path the user provided. Do NOT cat or print the JSON - read it silently and present a clean summary instead: + +``` +Corpus: X files · ~Y words + code: N files (.py .ts .go ...) + docs: N files (.md .txt ...) + papers: N files (.pdf ...) + images: N files + video: N files (.mp4 .mp3 ...) +``` + +Omit any category with 0 files from the summary. + +Then act on it: +- If `total_files` is 0: stop with "No supported files found in [path]." +- If `skipped_sensitive` is non-empty: report the count and list the skipped file names, so a wrongly-flagged source or doc is visible and can be renamed or moved (#2106). +- If `total_words` > 2,000,000 OR `total_files` > 500: show the warning. Then compute the top 5 first-level subdirectories by file count: + - Read `scan_root` from the detect JSON (always an absolute path to the resolved INPUT_PATH). + - Concatenate all file lists across all types (`code`, `document`, `paper`, `image`, `video`). + - Filter out any path that starts with `scan_root + "/graphify-out/"` to exclude converted sidecars. + - For each file, strip the `scan_root` prefix and take the first path component. Files directly in `scan_root` with no subdirectory count as `(root)`. + - If all files are in `(root)` with no subdirectories, do not ask to narrow — no subfolders exist. Instead suggest `--no-cluster` to skip the expensive clustering step and proceed. + - Otherwise rank by count, show the top 5 with file counts, then ask which subfolder to run on. Wait for the user's answer before proceeding. +- Otherwise: proceed directly to Step 2.5 if video files were detected, or Step 3 if not. + +### Step 2.5 - Video and audio (only if video files detected) + +Skip this step entirely if `detect` returned zero `video` files. When the corpus has video or audio, see `references/transcribe.md` to transcribe them to text first, then treat the transcripts as doc files in Step 3. + +### Step 3 - Extract entities and relationships + +**Before starting:** note whether `--mode deep` was given. You must pass `DEEP_MODE=true` to every subagent in Step B2 if it was. Track this from the original invocation - do not lose it. + +This step has two parts: **structural extraction** (deterministic, free) and **semantic extraction** (LLM, costs tokens). + +> **graphify needs no API key. Never ask the user for one, and never block on one.** Code is extracted structurally (AST) with no LLM and no key at all — a code-only corpus (the common `/graphify .` on a repo) skips semantic extraction entirely, so it needs nothing here: go straight to Part A and skip Part B. Semantic extraction (only for docs, papers, and images) uses Gemini **only if** `GEMINI_API_KEY`/`GOOGLE_API_KEY` is already set; otherwise the host agent itself is the LLM. graphify does **not** read `ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, or any other provider key. If you catch yourself about to prompt for, wait on, or stop because of a missing API key, that is a misread of this skill — proceed without one. + +**Before semantic extraction:** check whether `GEMINI_API_KEY` or `GOOGLE_API_KEY` is set. If neither is set, print this one-liner to the user: +> Tip: set `GEMINI_API_KEY` or `GOOGLE_API_KEY` to use Gemini for semantic extraction (`pip install 'graphifyy[gemini]'`). + +Print it once, then continue — do not wait for the user to supply a key. If `GEMINI_API_KEY` or `GOOGLE_API_KEY` IS set, use `graphify.llm.extract_corpus_parallel(files, backend="gemini")` for semantic extraction instead of dispatching subagents. The default Gemini model is `gemini-3-flash-preview`; set `GRAPHIFY_GEMINI_MODEL` or pass `--model` in headless CLI flows to override it. + +> **No other API keys are read.** When `GEMINI_API_KEY`/`GOOGLE_API_KEY` are unset, semantic extraction falls to the host agent itself — the running session is the LLM. On a host that dispatches subagents (e.g. Claude Code), dispatch them as written in Part B. On a host that runs the CLI directly in a terminal and cannot dispatch subagents, do not stall: a code-only corpus has no semantic work, so write the empty semantic file (Part B "Fast path") and continue to Part C; for a corpus with docs/papers/images, either set a Gemini key or extract those inline yourself, but in no case prompt for `ANTHROPIC_API_KEY` — that prompt is a misread of this skill. + +**Run Part A (AST) and Part B (semantic) in parallel. Dispatch all semantic subagents AND start AST extraction in the same message. Both can run simultaneously since they operate on different file types. Merge results in Part C as before.** + +Note: Parallelizing AST + semantic saves 5-15s on large corpora. AST is deterministic and fast; start it while subagents are processing docs/papers. + +#### Part A - Structural extraction for code files + +For any code files detected, run AST extraction in parallel with Part B subagents: + +```bash +$(cat graphify-out/.graphify_python) -c " +import sys, json +from graphify.extract import collect_files, extract +from pathlib import Path +import json + +code_files = [] +detect = json.loads(Path('graphify-out/.graphify_detect.json').read_text(encoding=\"utf-8\")) +for f in detect.get('files', {}).get('code', []): + code_files.extend(collect_files(Path(f)) if Path(f).is_dir() else [Path(f)]) + +if code_files: + # root= anchors node ids and source_file on the scan root; cache_root only says + # where graphify-out/cache lives. Pass both: without root= the anchor falls back + # to cache_root, so pointing the cache elsewhere would rename every node (#3727). + result = extract(code_files, cache_root=Path('INPUT_PATH'), root=Path('INPUT_PATH')) + Path('graphify-out/.graphify_ast.json').write_text(json.dumps(result, indent=2, ensure_ascii=False), encoding=\"utf-8\") + print(f'AST: {len(result[\"nodes\"])} nodes, {len(result[\"edges\"])} edges') +else: + Path('graphify-out/.graphify_ast.json').write_text(json.dumps({'nodes':[],'edges':[],'input_tokens':0,'output_tokens':0}, ensure_ascii=False), encoding=\"utf-8\") + print('No code files - skipping AST extraction') +" +``` + +#### Part B - Semantic extraction (parallel subagents) + +**Fast path:** If detection found zero docs, papers, and images (code-only corpus), skip Part B entirely and go straight to Part C. AST handles code - there is nothing for semantic subagents to do. **First write an empty semantic file** so Part C's merge has its input (it reads `.graphify_semantic.json` unconditionally; without this a code-only run hits `FileNotFoundError`): + +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from pathlib import Path +Path('graphify-out/.graphify_semantic.json').write_text(json.dumps({'nodes':[],'edges':[],'hyperedges':[],'input_tokens':0,'output_tokens':0}), encoding='utf-8') +" +``` + +**MANDATORY: You MUST use the Agent tool here. Reading files yourself one-by-one is forbidden - it is 5-10x slower. If you do not use the Agent tool you are doing this wrong.** + +Before dispatching subagents, print a timing estimate: +- Load `total_words` and file counts from `graphify-out/.graphify_detect.json` +- Estimate agents needed: `ceil(uncached_non_code_files / 22)` (chunk size is 20-25 files, capped at ~6000 total lines) +- Estimate time: ~45s per agent batch (they run in parallel, so total ≈ 45s × ceil(agents/parallel_limit)) +- Print: "Semantic extraction: ~N files → X agents, estimated ~Ys" + +**Step B0 - Check extraction cache first** + +Before dispatching any subagents, check which files already have cached extraction results: + +SPEC_PATH below is the **absolute** path of the `references/extraction-spec.md` that ships beside this SKILL.md — the same file Step B2 loads and hands to every subagent. It is the extraction prompt, so cache entries are attributed to it: when a graphify upgrade changes the prompt, entries produced by the old one are re-extracted instead of replayed, and unchanged prompts keep their entries (#1939). Substitute the real path in both Step B0 and Step B3 — pass the same one to each, and do not drop the argument. + +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from graphify.cache import check_semantic_cache +from pathlib import Path + +detect = json.loads(Path('graphify-out/.graphify_detect.json').read_text(encoding=\"utf-8\")) +# Only content files go to semantic extraction. Code is already covered structurally +# by the AST pass (Part A); flattening every category here makes subagents re-read +# every source file (#1392). Video is transcribed to a document in Step 2.5 first. +all_files = [f for cat in ('document', 'paper', 'image') for f in detect['files'].get(cat, [])] + +cached_nodes, cached_edges, cached_hyperedges, uncached = check_semantic_cache(all_files, root='INPUT_PATH', prompt_file='SPEC_PATH') + +# Always (re)write the cache file: write hits, else DELETE any leftover from a prior +# run so Part C never merges a stale .graphify_cached.json (#1392). +if cached_nodes or cached_edges or cached_hyperedges: + Path('graphify-out/.graphify_cached.json').write_text(json.dumps({'nodes': cached_nodes, 'edges': cached_edges, 'hyperedges': cached_hyperedges}, ensure_ascii=False), encoding=\"utf-8\") +else: + Path('graphify-out/.graphify_cached.json').unlink(missing_ok=True) +Path('graphify-out/.graphify_uncached.txt').write_text('\n'.join(uncached), encoding=\"utf-8\") +# Nothing has been dispatched yet, so any chunk file on disk is a leftover from an +# interrupted run, and Step B3 merges every .graphify_chunk_*.json it finds. +for stale in Path('graphify-out').glob('.graphify_chunk_*.json'): + stale.unlink() +print(f'Cache: {len(all_files)-len(uncached)} files hit, {len(uncached)} files need extraction') +" +``` + +Only dispatch subagents for files listed in `graphify-out/.graphify_uncached.txt`. If all files are cached, skip Steps B1 and B2 but still run Step B3's commands: its merge is the only Part B step that writes `graphify-out/.graphify_semantic.json`, and Part C reads that file unconditionally. Do not write an empty `.graphify_semantic.json` instead, because that drops every cached node. + +**Step B1 - Split into chunks** + +Load files from `graphify-out/.graphify_uncached.txt`. Split into chunks bounded by BOTH limits: at most 20-25 files AND at most ~6000 total lines per chunk. A text file longer than 2000 lines gets a chunk to itself - that is past the Read tool's single-call cap, so sharing a chunk with other files guarantees it is only partly read. Each image gets its own chunk (vision needs separate context). When splitting, group files from the same directory together so related artifacts land in the same chunk and cross-file relationships are more likely to be extracted. + +Both bounds matter. The line bound is not only about the 2000-line Read cap: a subagent holding 25 files splits its attention across all of them and skims the dense ones, and a 590-line plan can come back with zero nodes that way. Bounding content as well as file count is what keeps a chunk's coverage honest (#2225). + +**Step B2 - Dispatch ALL subagents in a single message** + +Call the Agent tool multiple times IN THE SAME RESPONSE - one call per chunk. This is the only way they run in parallel. If you make one Agent call, wait, then make another, you are doing it sequentially and defeating the purpose. + +**IMPORTANT - subagent type:** Always use `subagent_type="general-purpose"`. Do NOT use `Explore` - it is read-only and cannot write chunk files to disk, which silently drops extraction results. General-purpose has Write and Bash access which the subagent needs. + +Concrete example for 3 chunks: +``` +[Agent tool call 1: files 1-15, subagent_type="general-purpose"] +[Agent tool call 2: files 16-30, subagent_type="general-purpose"] +[Agent tool call 3: files 31-45, subagent_type="general-purpose"] +``` +All three in one message. Not three separate messages. + +Each subagent receives this exact prompt (substitute FILE_LIST, CHUNK_NUM, TOTAL_CHUNKS, DEEP_MODE, and CHUNK_PATH). + +CHUNK_PATH must be an **absolute** path — derive it before dispatching: +```bash +PROJECT_ROOT=$(pwd) # cwd — where Part C globs graphify-out/ (NOT .graphify_root/scan dir, #1392) +# Then for chunk N: CHUNK_PATH="${PROJECT_ROOT}/graphify-out/.graphify_chunk_0N.json" +``` + +Subagent prompt template: + +See `references/extraction-spec.md` for the exact subagent prompt (JSON schema, node-ID rules, confidence rubric, frontmatter, hyperedge, and vision rules). Load it only here, only when at least one chunk holds a doc, paper, or image; a pure-code corpus has skipped Part B and never reads it. Pass each subagent that prompt verbatim with FILE_LIST, CHUNK_NUM, TOTAL_CHUNKS, DEEP_MODE, and CHUNK_PATH substituted, and have it write the result to CHUNK_PATH. + +**Step B3 - Collect, cache, and merge** + +Wait for all subagents. For each result: +- Check that `graphify-out/.graphify_chunk_NN.json` exists on disk — this is the success signal +- If the file exists and contains valid JSON with `nodes` and `edges`, include it and save to cache +- If the file is missing, the subagent was likely dispatched as read-only (Explore type) — print a warning: "chunk N missing from disk — subagent may have been read-only. Re-run with general-purpose agent." Do not silently skip. +- If a subagent failed or returned invalid JSON, print a warning and skip that chunk - do not abort + +If more than half the chunks failed or are missing, stop and tell the user to re-run and ensure `subagent_type="general-purpose"` is used. + +**Step B3.5 - Verify each reported file was actually read to its end** + +A subagent that only read the first Read call's worth of a long file still writes a well-formed chunk, so "chunk file exists and parses" is not evidence of coverage. Compare each file's self-reported `coverage` against its real length: + +```bash +$(cat graphify-out/.graphify_python) -c " +import json, glob +from pathlib import Path + +covered = {} +for c in sorted(glob.glob('graphify-out/.graphify_chunk_*.json')): + d = json.loads(Path(c).read_text(encoding=\"utf-8\")) + for src, n in (d.get('coverage') or {}).items(): + covered[src] = max(covered.get(src, 0), int(n)) + +partial = [] +for src, n in sorted(covered.items()): + p = Path(src) + if not p.is_absolute(): + p = Path('INPUT_PATH') / src + if not p.is_file(): + continue + total = sum(1 for _ in p.open(encoding='utf-8', errors='replace')) + if total and n < total: + partial.append((src, n, total)) + +Path('graphify-out/.graphify_partial.txt').write_text('\n'.join(s for s, _, _ in partial), encoding=\"utf-8\") +if partial: + print(f'WARNING: {len(partial)} file(s) were only partly read. They are cached as partial and retried on the next --update:') + for src, n, total in partial: + print(f' {src}: read {n} of {total} lines') +else: + print('Coverage OK - every file a subagent reported was read to its last line') +" +``` + +Tell the user which files came up short and what was lost (the line counts are printed above). The nodes already extracted for those files are kept - they are still true - but the file is cached as partial and left unstamped in the manifest below, so the next `--update` re-dispatches it and replace-on-re-extract swaps the partial node set for a complete one. This is the same mechanism the library path uses for a truncated LLM response (#933, #1950), so both paths now recover instead of silently keeping a half-read file forever. A file a subagent did not report at all is not counted as partial - there is nothing to compare against - which is why the prompt rule in `references/extraction-spec.md` asks for the count in the first place. + +Merge all chunk files into `.graphify_semantic_new.json`. **After each Agent call completes, read the real token counts from the Agent tool result's `usage` field and write them back into the chunk JSON before merging** — the chunk JSON itself always has placeholder zeros. Then run: +```bash +$(cat graphify-out/.graphify_python) -c " +import json, glob +from pathlib import Path + +chunks = sorted(glob.glob('graphify-out/.graphify_chunk_*.json')) +all_nodes, all_edges, all_hyperedges = [], [], [] +total_in, total_out = 0, 0 +for c in chunks: + d = json.loads(Path(c).read_text(encoding=\"utf-8\")) + all_nodes += d.get('nodes', []) + all_edges += d.get('edges', []) + all_hyperedges += d.get('hyperedges', []) + total_in += d.get('input_tokens', 0) + total_out += d.get('output_tokens', 0) +Path('graphify-out/.graphify_semantic_new.json').write_text(json.dumps({ + 'nodes': all_nodes, 'edges': all_edges, 'hyperedges': all_hyperedges, + 'input_tokens': total_in, 'output_tokens': total_out, +}, indent=2, ensure_ascii=False), encoding=\"utf-8\") +print(f'Merged {len(chunks)} chunks: {total_in:,} in / {total_out:,} out tokens') +" +``` + +Save new results to cache. Pass the same SPEC_PATH as Step B0 — it stamps each entry with the prompt that produced it, and a write under a different prompt than the read lands where the next run won't look (#1939): +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from graphify.cache import save_semantic_cache +from pathlib import Path + +new = json.loads(Path('graphify-out/.graphify_semantic_new.json').read_text(encoding=\"utf-8\")) if Path('graphify-out/.graphify_semantic_new.json').exists() else {'nodes':[],'edges':[],'hyperedges':[]} +uncached = [line for line in Path('graphify-out/.graphify_uncached.txt').read_text(encoding=\"utf-8\").splitlines() if line] +# A partly-read file is cached with partial: True, which load_cached treats as a MISS, +# so the next --update re-dispatches it instead of serving a truncated extraction forever +# (#2225). Same contract the library path uses for a truncated LLM response (#933/#1950). +partial = [line for line in Path('graphify-out/.graphify_partial.txt').read_text(encoding=\"utf-8\").splitlines() if line] if Path('graphify-out/.graphify_partial.txt').exists() else [] +saved = save_semantic_cache(new.get('nodes', []), new.get('edges', []), new.get('hyperedges', []), root='INPUT_PATH', allowed_source_files=uncached, prompt_file='SPEC_PATH', partial_source_files=partial) +note = f' ({len(partial)} left partial for retry)' if partial else '' +print(f'Cached {saved} files' + note) +" +``` + +Merge cached + new results into `graphify-out/.graphify_semantic.json`: +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from pathlib import Path + +cached = json.loads(Path('graphify-out/.graphify_cached.json').read_text(encoding=\"utf-8\")) if Path('graphify-out/.graphify_cached.json').exists() else {'nodes':[],'edges':[],'hyperedges':[]} +new = json.loads(Path('graphify-out/.graphify_semantic_new.json').read_text(encoding=\"utf-8\")) if Path('graphify-out/.graphify_semantic_new.json').exists() else {'nodes':[],'edges':[],'hyperedges':[]} + +all_nodes = cached['nodes'] + new.get('nodes', []) +all_edges = cached['edges'] + new.get('edges', []) +all_hyperedges = cached.get('hyperedges', []) + new.get('hyperedges', []) +seen = set() +deduped = [] +for n in all_nodes: + if n['id'] not in seen: + seen.add(n['id']) + deduped.append(n) + +merged = { + 'nodes': deduped, + 'edges': all_edges, + 'hyperedges': all_hyperedges, + 'input_tokens': new.get('input_tokens', 0), + 'output_tokens': new.get('output_tokens', 0), +} +Path('graphify-out/.graphify_semantic.json').write_text(json.dumps(merged, indent=2, ensure_ascii=False), encoding=\"utf-8\") +print(f'Extraction complete - {len(deduped)} nodes, {len(all_edges)} edges ({len(cached[\"nodes\"])} from cache, {len(new.get(\"nodes\",[]))} new)') +" +``` +Clean up temp files: `rm -f graphify-out/.graphify_cached.json graphify-out/.graphify_uncached.txt graphify-out/.graphify_semantic_new.json` + +#### Part C - Merge AST + semantic into final extraction + +```bash +$(cat graphify-out/.graphify_python) -c " +import sys, json +from pathlib import Path + +ast = json.loads(Path('graphify-out/.graphify_ast.json').read_text(encoding=\"utf-8\")) +sem = json.loads(Path('graphify-out/.graphify_semantic.json').read_text(encoding=\"utf-8\")) + +# Stamp tier provenance on the semantic side. build._is_ast_tier reads +# _origin when present and otherwise guesses from the SHAPE of +# source_location ('L' means AST). A subagent that writes 'L12' for a +# doc section therefore made that node read as AST, and the next +# graphify update deleted it along with the re-extracted code file it +# seemed to belong to (#2843). extract() stamps its own items 'ast'; the +# semantic side must say so explicitly. +for n in sem['nodes']: + n.setdefault('_origin', 'semantic') +for e in sem['edges']: + e.setdefault('_origin', 'semantic') + +# Merge: AST nodes first, semantic nodes deduplicated by id +seen = {n['id'] for n in ast['nodes']} +merged_nodes = list(ast['nodes']) +for n in sem['nodes']: + if n['id'] not in seen: + merged_nodes.append(n) + seen.add(n['id']) + +merged_edges = ast['edges'] + sem['edges'] +merged_hyperedges = sem.get('hyperedges', []) +merged = { + 'nodes': merged_nodes, + 'edges': merged_edges, + 'hyperedges': merged_hyperedges, + 'input_tokens': sem.get('input_tokens', 0), + 'output_tokens': sem.get('output_tokens', 0), +} +Path('graphify-out/.graphify_extract.json').write_text(json.dumps(merged, indent=2, ensure_ascii=False), encoding=\"utf-8\") +total = len(merged_nodes) +edges = len(merged_edges) +print(f'Merged: {total} nodes, {edges} edges ({len(ast[\"nodes\"])} AST + {len(sem[\"nodes\"])} semantic)') +" +``` + +### Step 4 - Build graph, cluster, analyze, generate outputs + +**Before starting:** the code blocks below pass `directed=IS_DIRECTED` to `build_from_json()`. Replace `IS_DIRECTED` with `True` if `--directed` was given (builds a `DiGraph` preserving edge direction source→target), otherwise `False` (the default undirected `Graph`). Substitute it the same way you substitute `INPUT_PATH` — do not leave the literal `IS_DIRECTED` in the code. + +```bash +mkdir -p graphify-out +$(cat graphify-out/.graphify_python) -c " +import sys, json +from graphify.build import build_from_json +from graphify.cluster import cluster, score_all +from graphify.analyze import god_nodes, surprising_connections, suggest_questions +from graphify.report import generate +from graphify.export import to_json +from pathlib import Path + +extraction = json.loads(Path('graphify-out/.graphify_extract.json').read_text(encoding=\"utf-8\")) +detection = json.loads(Path('graphify-out/.graphify_detect.json').read_text(encoding=\"utf-8\")) + +# root= mirrors the --update runbook (#1361): relativize source_file to the same +# base so the full build and incremental --update never drift apart on re-extract. +G = build_from_json(extraction, root='INPUT_PATH', directed=IS_DIRECTED) +# Guard BEFORE any write: an empty extraction must not clobber a good graph.json / +# GRAPH_REPORT.md / analysis sidecar. Check immediately after build (#1392). +if G.number_of_nodes() == 0: + print('ERROR: Graph is empty - extraction produced no nodes.') + print('Possible causes: all files were skipped, binary-only corpus, or extraction failed.') + raise SystemExit(1) +communities = cluster(G) +cohesion = score_all(G, communities) +tokens = {'input': extraction.get('input_tokens', 0), 'output': extraction.get('output_tokens', 0)} +gods = god_nodes(G) +surprises = surprising_connections(G, communities) +labels = {cid: 'Community ' + str(cid) for cid in communities} +# Placeholder questions - regenerated with real labels in Step 5 +questions = suggest_questions(G, communities, labels) + +# Export FIRST and honor the #479 shrink-guard: to_json returns False (writing +# nothing) when the new graph is smaller than the existing graph.json. Only write +# GRAPH_REPORT.md + the analysis sidecar when the graph was actually written, so +# they never describe a graph that graph.json doesn't contain (#1392). +wrote = to_json(G, communities, 'graphify-out/graph.json') +if not wrote: + print('ERROR: refused to shrink graphify-out/graph.json (existing graph has more nodes; #479).') + print('If this shrink is intentional (you deleted files), re-run a full build with --force.') + raise SystemExit(1) +report = generate(G, communities, cohesion, labels, gods, surprises, detection, tokens, 'INPUT_PATH', suggested_questions=questions) +Path('graphify-out/GRAPH_REPORT.md').write_text(report, encoding=\"utf-8\") +analysis = { + 'communities': {str(k): v for k, v in communities.items()}, + 'cohesion': {str(k): v for k, v in cohesion.items()}, + 'gods': gods, + 'surprises': surprises, + 'questions': questions, +} +Path('graphify-out/.graphify_analysis.json').write_text(json.dumps(analysis, indent=2, ensure_ascii=False), encoding=\"utf-8\") +print(f'Graph: {G.number_of_nodes()} nodes, {G.number_of_edges()} edges, {len(communities)} communities') +" +``` + +If this step prints `ERROR: Graph is empty`, stop and tell the user what happened - do not proceed to labeling or visualization. + +Replace INPUT_PATH with the actual path. + +### Step 4.5 - Graph health check (read-only integrity gate) + +A non-destructive diagnostic on the extraction, before labeling. It surfaces edge collapse, dangling/missing endpoints, and self-loops — the silent-corruption modes of incremental updates and AST/LLM id mismatches. Read-only; never aborts. + +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from pathlib import Path +from graphify.diagnostics import diagnose_extraction, format_diagnostic_report + +extraction = json.loads(Path('graphify-out/.graphify_extract.json').read_text(encoding=\"utf-8\")) +summary = diagnose_extraction(extraction, directed=IS_DIRECTED, root='INPUT_PATH') +print(format_diagnostic_report(summary)) +flags = [f'{summary[k]} {label}' for k, label in ( + ('dangling_endpoint_edges', 'dangling-endpoint edges'), + ('missing_endpoint_edges', 'missing-endpoint edges'), + ('self_loop_edges', 'self-loop edges'), + ('directed_same_endpoint_collapsed_edges', 'collapsed (directed) edges'), + ('undirected_same_endpoint_collapsed_edges', 'collapsed (undirected) edges'), +) if summary.get(k, 0)] +print('GRAPH HEALTH WARNING: ' + '; '.join(flags) + ' - graph may be incomplete/corrupt.' if flags else 'Graph health: OK (no dangling/missing/collapsed edges).') +" +``` + +Substitute `IS_DIRECTED` and `INPUT_PATH` as in Step 4. If a `GRAPH HEALTH WARNING` prints, surface it in the final summary (do not abort — the graph is still usable, but the integrity issue must be visible, per the Honesty Rules). + +### Step 5 - Label communities + +Read `graphify-out/.graphify_analysis.json`. For each community key, look at its node labels and write a 2-5 word plain-language name (e.g. "Attention Mechanism", "Training Pipeline", "Data Loading"). + +Then regenerate the report and save the labels for the visualizer: + +```bash +$(cat graphify-out/.graphify_python) -c " +import sys, json +from graphify.build import build_from_json +from graphify.cluster import score_all +from graphify.analyze import god_nodes, surprising_connections, suggest_questions +from graphify.report import generate +from graphify.export import to_json +from pathlib import Path + +extraction = json.loads(Path('graphify-out/.graphify_extract.json').read_text(encoding=\"utf-8\")) +detection = json.loads(Path('graphify-out/.graphify_detect.json').read_text(encoding=\"utf-8\")) +analysis = json.loads(Path('graphify-out/.graphify_analysis.json').read_text(encoding=\"utf-8\")) + +# root= as in Step 4 / the --update runbook (#1361) — same base for node-key parity. +G = build_from_json(extraction, root='INPUT_PATH', directed=IS_DIRECTED) +communities = {int(k): v for k, v in analysis['communities'].items()} +cohesion = {int(k): v for k, v in analysis['cohesion'].items()} +tokens = {'input': extraction.get('input_tokens', 0), 'output': extraction.get('output_tokens', 0)} + +# LABELS - replace these with the names you chose above +labels = LABELS_DICT + +# Regenerate questions with real community labels (labels affect question phrasing) +questions = suggest_questions(G, communities, labels) + +report = generate(G, communities, cohesion, labels, analysis['gods'], analysis['surprises'], detection, tokens, 'INPUT_PATH', suggested_questions=questions) +Path('graphify-out/GRAPH_REPORT.md').write_text(report, encoding=\"utf-8\") +Path('graphify-out/.graphify_labels.json').write_text(json.dumps({str(k): v for k, v in labels.items()}, ensure_ascii=False), encoding=\"utf-8\") +# Re-export so graph.json nodes carry the curated community_name (#2490). +# Same extraction as Step 4, so the #479 shrink-guard passes on node count; +# if it still refuses, surface the guard message - do not force past it. +wrote = to_json(G, communities, 'graphify-out/graph.json', community_labels=labels) +if not wrote: + print('ERROR: refused to shrink graphify-out/graph.json (existing graph has more nodes; #479).') + print('If this shrink is intentional (you deleted files), re-run a full build with --force.') +print('Report updated with community labels') +" +``` + +Replace `LABELS_DICT` with the actual dict you constructed (e.g. `{0: "Attention Mechanism", 1: "Training Pipeline"}`). +Replace INPUT_PATH with the actual path. + +### Step 6 - Generate Obsidian vault (opt-in) + HTML + +**Generate HTML always** (unless `--no-viz`). **Obsidian vault only if `--obsidian` was explicitly given** — skip it otherwise, it generates one file per node. + +If `--obsidian` was given: + +- If `--obsidian-dir ` was also given, pass it via `--dir`. Otherwise defaults to `graphify-out/obsidian`. + +```bash +graphify export obsidian +# or with custom dir: graphify export obsidian --dir ~/vaults/my-project +``` + +Generate the HTML graph (always, unless `--no-viz`): + +```bash +graphify export html # auto-aggregates to community view if graph > 5000 nodes +# or: graphify export html --no-viz +``` + +### Steps 6b-8 - Wiki, Neo4j, FalkorDB, SVG, GraphML, MCP, benchmark (only on their flags) + +These run only when their flag is present (`--wiki`, `--neo4j`/`--neo4j-push`, `--falkordb`/`--falkordb-push`, `--svg`, `--graphml`, `--mcp`) or, for the token-reduction benchmark, when `total_words` exceeds 5,000. A default run with no export flags skips all of them. See `references/exports.md` for each one. Run any `--wiki` export before Step 9 cleanup so `.graphify_labels.json` is still available. + +--- + +### Step 9 - Save manifest, update cost tracker, clean up, and report + +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from pathlib import Path +from datetime import datetime, timezone +from graphify.detect import save_manifest + +# Save manifest for --update +detect = json.loads(Path('graphify-out/.graphify_detect.json').read_text(encoding=\"utf-8\")) +extract = json.loads(Path('graphify-out/.graphify_extract.json').read_text(encoding=\"utf-8\")) +# In --update mode, 'all_files' carries the full corpus; 'files' is the changed +# subset. Full-rebuild mode populates only 'files', so the fallback handles that. +# root= relativizes the manifest keys to the scan root (same base as the build), +# so the on-disk manifest is portable across clones/machines and a later --update +# matches cached files instead of missing every one (#1417). +# +# Only stamp semantic files (docs/papers/images) that ACTUALLY produced output: +# a detected file whose chunk failed or was omitted must stay unstamped so the +# next --update re-queues it, otherwise it is marked done and its content is lost +# forever (#2015). This mirrors the library extract path exactly +# (cli._stamped_manifest_files + clear_semantic + scan_corpus); do not stamp the +# raw corpus. Code files are always stamped (AST is deterministic); only semantic +# types are gated on output. +from graphify.cli import _stamped_manifest_files +_corpus = detect.get('all_files') or detect['files'] +# A file a subagent only partly read DID produce output, so the stamp filter alone would +# keep it - but its node set covers just the head of the file, and stamping it means +# detect_incremental reads it as done and never re-queues it, leaving the truncated graph +# live forever. Name those files so they stay unstamped and are retried next run (#2225). +_pf = Path('graphify-out/.graphify_partial.txt') +_partial = {l.strip() for l in _pf.read_text(encoding=\"utf-8\").splitlines() if l.strip()} if _pf.exists() else set() +_manifest_files = _stamped_manifest_files(_corpus, extract, Path('INPUT_PATH'), partial_source_files=_partial) +# Files dispatched this run (the changed subset) but NOT stamped above still carry +# a stale semantic_hash from a prior run; clear it so detect_incremental re-queues +# them instead of reading them as unchanged (#1948). +_sem_types = ('document', 'paper', 'image') +_dispatched = {f for t, fl in detect['files'].items() if t in _sem_types for f in fl} +_stamped = {f for fl in _manifest_files.values() for f in fl} +_cleared = _dispatched - _stamped +# scan_corpus = the RAW full corpus (not the stamp-filtered subset) so in-root +# files newly excluded since last run are dropped rather than masquerading as +# deletions; untouched files' prior rows are still preserved (#1908). +_scan = {f for fl in _corpus.values() for f in fl} +save_manifest(_manifest_files, root='INPUT_PATH', scan_corpus=_scan, clear_semantic=_cleared or None) + +# Update cumulative cost tracker +input_tok = extract.get('input_tokens', 0) +output_tok = extract.get('output_tokens', 0) + +cost_path = Path('graphify-out/cost.json') +if cost_path.exists(): + cost = json.loads(cost_path.read_text(encoding=\"utf-8\")) +else: + cost = {'runs': [], 'total_input_tokens': 0, 'total_output_tokens': 0} + +cost['runs'].append({ + 'date': datetime.now(timezone.utc).isoformat(), + 'input_tokens': input_tok, + 'output_tokens': output_tok, + 'files': detect.get('total_files', 0), +}) +cost['total_input_tokens'] += input_tok +cost['total_output_tokens'] += output_tok +cost_path.write_text(json.dumps(cost, indent=2, ensure_ascii=False), encoding=\"utf-8\") + +print(f'This run: {input_tok:,} input tokens, {output_tok:,} output tokens') +print(f'All time: {cost[\"total_input_tokens\"]:,} input, {cost[\"total_output_tokens\"]:,} output ({len(cost[\"runs\"])} runs)') +" +rm -f graphify-out/.graphify_detect.json graphify-out/.graphify_extract.json graphify-out/.graphify_ast.json graphify-out/.graphify_semantic.json graphify-out/.graphify_analysis.json graphify-out/.graphify_partial.txt +find graphify-out -maxdepth 1 -name '.graphify_chunk_*.json' -delete 2>/dev/null +rm -f graphify-out/.needs_update 2>/dev/null || true +``` + +Replace INPUT_PATH with the actual path (same value used in Steps 4-5) so the manifest is relativized to the scan root. + +Tell the user (omit the obsidian line unless --obsidian was given): +``` +Graph complete. Outputs in PATH_TO_DIR/graphify-out/ + + graph.html - interactive graph, open in browser + GRAPH_REPORT.md - audit report + graph.json - raw graph data + obsidian/ - Obsidian vault (only if --obsidian was given) +``` + +If graphify saved you time, consider supporting it: https://github.com/sponsors/safishamsi + +Replace PATH_TO_DIR with the actual absolute path of the directory that was processed. + +If PATH_TO_DIR is inside a git repository, add one line to the report: `graphify-out/` was written into the working tree, so unless the repository already ignores it, it will show in `git status`; the user can add `graphify-out/` to `.gitignore`, or commit only the outputs their team should share (see https://docs.graphify.com/guides/team-workflows). + +Then paste these sections from GRAPH_REPORT.md directly into the chat: +- God Nodes +- Surprising Connections +- Suggested Questions + +Do NOT paste the full report - just those three sections. Keep it concise. + +Then immediately offer to explore. Pick the single most interesting suggested question from the report - the one that crosses the most community boundaries or has the most surprising bridge node - and ask: + +> "The most interesting question this graph can answer: **[question]**. Want me to trace it?" + +If the user says yes, run `/graphify query "[question]"` on the graph and walk them through the answer using the graph structure - which nodes connect, which community boundaries get crossed, what the path reveals. Keep going as long as they want to explore. Each answer should end with a natural follow-up ("this connects to X - want to go deeper?") so the session feels like navigation, not a one-shot report. + +The graph is the map. Your job after the pipeline is to be the guide. + +--- + +## Interpreter guard for subcommands + +Before running any subcommand below (`--update`, `--cluster-only`, `query`, `path`, `explain`, `add`), check that `.graphify_python` exists. If it's missing (e.g. user deleted `graphify-out/`), re-resolve the interpreter first: + +```bash +if [ ! -f graphify-out/.graphify_python ]; then + GRAPHIFY_BIN=$(which graphify 2>/dev/null) + if [ -n "$GRAPHIFY_BIN" ]; then + PYTHON=$(head -1 "$GRAPHIFY_BIN" | tr -d '#!') + case "$PYTHON" in *[!a-zA-Z0-9/_.@-]*) PYTHON="python3" ;; esac + else + PYTHON="python3" + fi + mkdir -p graphify-out + "$PYTHON" -c "import sys; open('graphify-out/.graphify_python', 'w', encoding='utf-8').write(sys.executable)" +fi +``` + +## For --update and --cluster-only + +Both are non-default subcommands. `--update` re-extracts only new or changed files; `--cluster-only` reruns clustering on the existing graph. See `references/update.md` for both flows. + +--- + +## For /graphify query + +When `graphify-out/graph.json` already exists and the user asks a question about the corpus, answer from the graph rather than rebuilding it: + +```bash +graphify query "" +``` + +Before traversal, expand the question against the graph's own vocabulary so a wording mismatch does not collapse the answer to noise. If the `graphify query` CLI is unavailable, fall back to an inline NetworkX traversal of `graphify-out/graph.json`. Answer using only what the graph output contains, and quote `source_location` when citing a specific fact. For that vocab-expansion step, the BFS/DFS traversal modes, the `--budget` cap, the NetworkX fallback, `save-result` feedback, and the `/graphify path` and `/graphify explain` flows, see `references/query.md`. + +--- + +## For /graphify add and --watch + +Neither is part of the default build. When the user runs `/graphify add ` to fetch a URL into the corpus, or passes `--watch` to auto-rebuild on file changes, see `references/add-watch.md`. + +--- + +## For the commit hook and native CLAUDE.md integration + +When the user asks to install the post-commit auto-rebuild hook or wire graphify into a project's CLAUDE.md, see `references/hooks.md`. + +--- + +## Honesty Rules + +- Never invent an edge. If unsure, use AMBIGUOUS. +- Never skip the corpus check warning. +- Always show token cost in the report. +- Never hide cohesion scores behind symbols - show the raw number. +- Never run HTML viz on a graph with more than 5,000 nodes without warning the user. diff --git a/.claude/skills/graphify/references/add-watch.md b/.claude/skills/graphify/references/add-watch.md new file mode 100644 index 0000000..349f8b4 --- /dev/null +++ b/.claude/skills/graphify/references/add-watch.md @@ -0,0 +1,56 @@ +# graphify reference: add a URL and watch a folder + +Load this when the user ran `/graphify add ` or passed `--watch`. Neither is part of the default build. + +## For /graphify add + +Fetch a URL and add it to the corpus, then update the graph. + +```bash +$(cat graphify-out/.graphify_python) -c " +import sys +from graphify.ingest import ingest +from pathlib import Path + +try: + out = ingest('URL', Path('./raw'), author='AUTHOR', contributor='CONTRIBUTOR') + print(f'Saved to {out}') +except ValueError as e: + print(f'error: {e}', file=sys.stderr) + sys.exit(1) +except RuntimeError as e: + print(f'error: {e}', file=sys.stderr) + sys.exit(1) +" +``` + +Replace `URL` with the actual URL, `AUTHOR` with the user's name if provided, `CONTRIBUTOR` likewise. If the command exits with an error, tell the user what went wrong - do not silently continue. After a successful save, automatically run the `--update` pipeline on `./raw` to merge the new file into the existing graph. + +Supported URL types (auto-detected): +- YouTube / any video URL → audio downloaded via yt-dlp, transcribed to `.txt` on next run (requires `pip install 'graphifyy[video]'`) +- Twitter/X → fetched via oEmbed, saved as `.md` with tweet text and author +- arXiv → abstract + metadata saved as `.md` +- PDF → downloaded as `.pdf` +- Images (.png/.jpg/.webp) → downloaded, Claude vision extracts on next run +- Any webpage → converted to markdown via html2text + +--- + +## For --watch + +Start a background watcher that monitors a folder and auto-updates the graph when files change. + +```bash +$(cat graphify-out/.graphify_python) -m graphify.watch "$(cat graphify-out/.graphify_root)" --debounce 3 +``` + +This watches the same folder graphify extracted, read from the trusted `graphify-out/.graphify_root` that Step 1 resolved - there is no path to substitute, so a scan root containing shell metacharacters can never be re-interpreted here. Behavior depends on what changed: + +- **Code files only (.py, .ts, .go, etc.):** re-runs AST extraction + rebuild + cluster immediately, no LLM needed. `graph.json` and `GRAPH_REPORT.md` are updated automatically. +- **Docs, papers, or images:** writes a `graphify-out/needs_update` flag and prints a notification to run `/graphify --update` (LLM semantic re-extraction required). + +Debounce (default 3s): waits until file activity stops before triggering, so a wave of parallel agent writes doesn't trigger a rebuild per file. + +Press Ctrl+C to stop. + +For agentic workflows: run `--watch` in a background terminal. Code changes from agent waves are picked up automatically between waves. If agents are also writing docs or notes, you'll need a manual `/graphify --update` after those waves. diff --git a/.claude/skills/graphify/references/exports.md b/.claude/skills/graphify/references/exports.md new file mode 100644 index 0000000..96e400f --- /dev/null +++ b/.claude/skills/graphify/references/exports.md @@ -0,0 +1,91 @@ +# graphify reference: extra exports and benchmark + +Load this when the user passed one of the export flags (`--wiki`, `--neo4j`, `--neo4j-push`, `--falkordb`, `--falkordb-push`, `--svg`, `--graphml`, `--mcp`), or when the corpus is large enough for the token-reduction benchmark. Each step runs only for its own flag. + +### Step 6b - Wiki (only if --wiki flag) + +**Only run this step if `--wiki` was explicitly given in the original command.** + +Run this before Step 9 (cleanup) so `.graphify_labels.json` is still available. + +```bash +graphify export wiki +``` + +### Step 7 - Neo4j export (only if --neo4j or --neo4j-push flag) + +**If `--neo4j`** - generate a Cypher file for manual import: + +```bash +graphify export neo4j +``` + +**If `--neo4j-push`** - push directly to Neo4j using the destination URI supplied with the original `/graphify` command. Only push when this flag was explicitly given. + +Pass the exact URI from the original `/graphify` command as one argument. When constructing a shell command, quote the URI for the shell that will execute the command (use `shlex.quote` for POSIX shells), or use a structured argument interface. Keep the URI as data; never interpolate it unquoted or evaluate it as shell code. Never replace a supplied URI with localhost. If the user explicitly requested a push but supplied no URI, use the documented local default. Do not print the URI or credentials in status output or generated artifacts. + +```bash +graphify export neo4j --push 'bolt://remote-host:7687' --user neo4j +``` + +Default URI is `bolt://localhost:7687`, default user is `neo4j`. Prefer the `NEO4J_PASSWORD` environment variable so the password is kept off argv; ask for credentials only when needed. Uses MERGE - safe to re-run without creating duplicates. + +### Step 7a - FalkorDB export (only if --falkordb or --falkordb-push flag) + +**If `--falkordb`** - generate a Cypher file. The statements are OpenCypher, but FalkorDB's `GRAPH.QUERY` runs one statement at a time (no bulk script import like Neo4j's `cypher-shell`), so prefer `--falkordb-push` to load a graph. Use this only when you want the portable `cypher.txt` artifact: + +```bash +graphify export falkordb +``` + +**If `--falkordb-push`** - push directly to FalkorDB using the destination URI supplied with the original `/graphify` command. Only push when this flag was explicitly given. + +Pass the exact URI from the original `/graphify` command as one argument. When constructing a shell command, quote the URI for the shell that will execute the command (use `shlex.quote` for POSIX shells), or use a structured argument interface. Keep the URI as data; never interpolate it unquoted or evaluate it as shell code. Never replace a supplied URI with localhost. If the user explicitly requested a push but supplied no URI, use the documented local default. Do not print the URI or credentials in status output or generated artifacts. Credentials are optional; ask only if the instance requires authentication, and use `FALKORDB_PASSWORD` for a password rather than printing it. + +```bash +graphify export falkordb --push 'falkordb://remote-host:6379' +``` + +Default URI is `falkordb://localhost:6379` (the scheme is informational - `redis://` or a bare `host:port` work too), auth is optional, and the target graph defaults to `graphify`. Uses MERGE - safe to re-run without creating duplicates. + +### Step 7b - SVG export (only if --svg flag) + +```bash +graphify export svg +``` + +### Step 7c - GraphML export (only if --graphml flag) + +```bash +graphify export graphml +``` + +### Step 7d - MCP server (only if --mcp flag) + +```bash +$(cat graphify-out/.graphify_python) -m graphify.serve graphify-out/graph.json +``` + +This starts a stdio MCP server that exposes tools: `query_graph`, `get_node`, `get_neighbors`, `get_community`, `god_nodes`, `graph_stats`, `shortest_path`. Add to Claude Desktop or any MCP-compatible agent orchestrator so other agents can query the graph live. + +To configure in Claude Desktop, add to `claude_desktop_config.json`. Claude Desktop can't run `$(...)`, and under `uv tool install` the system `python3` can't import graphify — so set `command` to the **absolute interpreter path** printed by `cat graphify-out/.graphify_python`: +```json +{ + "mcpServers": { + "graphify": { + "command": "", + "args": ["-m", "graphify.serve", "/absolute/path/to/graphify-out/graph.json"] + } + } +} +``` + +### Step 8 - Token reduction benchmark (only if total_words > 5000) + +If `total_words` from `graphify-out/.graphify_detect.json` is greater than 5,000, run: + +```bash +graphify benchmark +``` + +Print the output directly in chat. If `total_words <= 5000`, skip silently - the graph value is structural clarity, not token compression, for small corpora. diff --git a/.claude/skills/graphify/references/extraction-spec.md b/.claude/skills/graphify/references/extraction-spec.md new file mode 100644 index 0000000..2ec6682 --- /dev/null +++ b/.claude/skills/graphify/references/extraction-spec.md @@ -0,0 +1,86 @@ +# graphify reference: extraction subagent prompt + +Load this in Step 3 Part B when the corpus has at least one doc, paper, or image chunk. A pure-code corpus skips Part B and never reads this file. Each semantic subagent receives the prompt below verbatim (substitute FILE_LIST, CHUNK_NUM, TOTAL_CHUNKS, DEEP_MODE, and CHUNK_PATH). + +``` +You are a graphify extraction subagent. Read the files listed and extract a knowledge graph fragment. +Output ONLY valid JSON matching the schema below - no explanation, no markdown fences, no preamble. + +Files (chunk CHUNK_NUM of TOTAL_CHUNKS): +FILE_LIST + +Read every file in this list COMPLETELY before extracting anything from it. The Read tool +returns at most 2000 lines per call and reports NO error and NO warning when it truncates, so a +long file silently yields a partial extraction. When a file is longer than one call, keep paging +with offset/limit until you have actually seen its last line - never assume the first call +returned the whole file. Never emit nodes for a file you have only partly read. A partial +extraction is worse than none: replace-on-re-extract first DROPS that file's existing nodes and +then re-adds only what you saw, so the unread tail disappears from the graph, and the file is then +recorded as successfully extracted and never retried (#2225). + +Report the coverage you actually achieved, so a short read is caught instead of guessed at: add a +top-level "coverage" object mapping each FILE_LIST path you extracted to the number of lines you +read, e.g. {"coverage": {"docs/plan.md": 452}}. If you could not reach a file's last line, still +report the count you did read. A short count is recoverable - Step B3 warns, the entry is cached +as partial, and the next --update retries the file - whereas a missing entry is indistinguishable +from a complete read. + +Rules: +- EXTRACTED: relationship explicit in source (import, call, citation, "see §3.2") +- INFERRED: reasonable inference (shared data structure, implied dependency) +- AMBIGUOUS: uncertain - flag for review, do not omit + +Code files: focus on semantic edges AST cannot find (call relationships, shared data, arch patterns). + Do not re-extract imports - AST already has those. +Doc/paper files: extract named concepts, entities, citations. For rationale (WHY decisions were made, trade-offs, design intent): store as a `rationale` attribute on the relevant concept node — do NOT create a separate rationale node or fragment node. Only create a node for something that is itself a named entity or concept. Use `file_type:"rationale"` for concept-like nodes (ideas, principles, mechanisms, design patterns). `file_type` MUST be one of exactly these six values: `code`, `document`, `paper`, `image`, `rationale`, `concept`. Any other value is invalid and will be rejected. +Code files: when adding `calls` edges, source MUST be the caller (the function/class doing the calling), target MUST be the callee. Never reverse this direction. `calls` edges MUST stay within one language: a Python function cannot `calls` a JS/TS/Go/Rust/Java symbol and vice versa — cross-language call edges are phantom artifacts, never emit them. +Image files: use vision to understand what the image IS - do not just OCR. + UI screenshot: layout patterns, design decisions, key elements, purpose. + Chart: metric, trend/insight, data source. + Tweet/post: claim as node, author, concepts mentioned. + Diagram: components and connections. + Research figure: what it demonstrates, method, result. + Handwritten/whiteboard: ideas and arrows, mark uncertain readings AMBIGUOUS. + +DEEP_MODE (if --mode deep was given): be aggressive with INFERRED edges - indirect deps, + shared assumptions, latent couplings. Mark uncertain ones AMBIGUOUS instead of omitting. + +Semantic similarity: if two concepts in this chunk solve the same problem or represent the same idea without any structural link (no import, no call, no citation), add a `semantically_similar_to` edge marked INFERRED with a confidence_score reflecting how similar they are (0.6-0.95). Examples: +- Two functions that both validate user input but never call each other +- A class in code and a concept in a paper that describe the same algorithm +- Two error types that handle the same failure mode differently +Only add these when the similarity is genuinely non-obvious and cross-cutting. Do not add them for trivially similar things. + +Hyperedges: if 3 or more nodes clearly participate together in a shared concept, flow, or pattern that is not captured by pairwise edges alone, add a hyperedge to a top-level `hyperedges` array. Examples: +- All classes that implement a common protocol or interface +- All functions in an authentication flow (even if they don't all call each other) +- All concepts from a paper section that form one coherent idea +Use sparingly — only when the group relationship adds information beyond the pairwise edges. Maximum 3 hyperedges per chunk. + +If a file has YAML frontmatter (--- ... ---), copy source_url, captured_at, author, + contributor onto every node from that file. + +confidence_score is REQUIRED on every edge - never omit it, never use 0.5 as a default: +- EXTRACTED edges: confidence_score = 1.0 always +- INFERRED edges: pick exactly ONE value from this set — never 0.5: + 0.95 direct structural evidence (shared data structure, named cross-file reference). + 0.85 strong inference (clear functional alignment, no direct symbol link). + 0.75 reasonable inference (shared problem domain + similar shape, requires interpretation). + 0.65 weak inference (thematically related, no shape evidence). + 0.55 speculative but plausible (surface-level co-occurrence only). + Models follow discrete rubrics better than continuous ranges; the bimodal + distribution observed in production (>50% at 0.5, >40% at 0.85+) shows the + range guidance is being collapsed to a binary. If no value above fits, mark + the edge AMBIGUOUS rather than picking 0.4 or below. +- AMBIGUOUS edges: 0.1-0.3 + +Node ID format: lowercase, word characters only (`[^\W]` after casefolding — `[a-z0-9_]` for ASCII input, but non-ASCII letters and digits are kept as-is), no dots or slashes. Format: `{stem}_{entity}` where stem is the **full repo-relative path with the extension dropped**, every path segment kept and joined with `_` (each segment lowercased with non-alphanumeric chars replaced by `_`), and entity is the symbol name similarly normalized. Use every directory level, not just the immediate parent — this keeps same-named files in different directories distinct. Examples: `src/auth/session.py` + `ValidateToken` → `src_auth_session_validatetoken`; `lib/utils/helpers.py` + `parse_url` → `lib_utils_helpers_parse_url`; `tests/test_foo.py` + `_helper` → `tests_test_foo_helper`; `docs/v1/api/README.md` + `getUser` → `docs_v1_api_readme_getuser`. Top-level files (no parent dir, e.g. `setup.py`) use just the filename stem: `setup_my_func`. This must match the ID the AST extractor generates — using just the filename (e.g., `session_validatetoken`) or only the immediate parent (e.g., `auth_session_validatetoken`) will create orphan ghost-duplicate nodes. If you are re-extracting a project built under the old immediate-parent format, the user should run `graphify extract --force` to rebuild cleanly. CRITICAL: never append chunk numbers, sequence numbers, or any suffix to an ID (no `_c1`, `_c2`, `_chunk2`, etc.). IDs must be deterministic from the label alone — the same entity must always produce the same ID regardless of which chunk processes it. + +Generate the extraction JSON matching this schema exactly: +{"nodes":[{"id":"auth_session_validatetoken","label":"Human Readable Name","file_type":"code|document|paper|image|rationale|concept","source_file":"","source_location":null,"source_url":null,"captured_at":null,"author":null,"contributor":null}],"edges":[{"source":"node_id","target":"node_id","relation":"calls|implements|references|cites|conceptually_related_to|shares_data_with|semantically_similar_to|rationale_for","confidence":"EXTRACTED|INFERRED|AMBIGUOUS","confidence_score":1.0,"source_file":"","source_location":null,"weight":1.0}],"hyperedges":[{"id":"snake_case_id","label":"Human Readable Label","nodes":["node_id1","node_id2","node_id3"],"relation":"participate_in|implement|form","confidence":"EXTRACTED|INFERRED","confidence_score":0.75,"source_file":""}],"input_tokens":0,"output_tokens":0,"coverage":{"":0}} + +source_file RULE (every node, edge, and hyperedge): set source_file to the path of the originating file EXACTLY as it appears in FILE_LIST — verbatim and absolute. Do NOT shorten to a basename, do NOT re-relativize, do NOT strip any directory prefix, and do NOT change separators (the engine canonicalizes separators and relativizes against the build root downstream). Copy the FILE_LIST entry character-for-character. This keeps the full build and incremental --update on the same base, so build_merge's replace-on-re-extract matches the existing node instead of accumulating a duplicate. + +Then write the JSON to disk using the Write tool at this exact absolute path (no relative paths — Write resolves relative paths against an undefined cwd and the file will be silently lost): +CHUNK_PATH +``` diff --git a/.claude/skills/graphify/references/github-and-merge.md b/.claude/skills/graphify/references/github-and-merge.md new file mode 100644 index 0000000..a41ea06 --- /dev/null +++ b/.claude/skills/graphify/references/github-and-merge.md @@ -0,0 +1,46 @@ +# graphify reference: GitHub clone and cross-repo merge + +Load this when the user passed one or more `https://github.com/...` URLs, or named several local subfolders to merge into one graph. + +### Step 0 - Clone GitHub repo(s) (only if a GitHub URL was given) + +**Single repo:** +```bash +LOCAL_PATH=$(graphify clone [--branch ]) +# Use LOCAL_PATH as the target for all subsequent steps +``` + +**Multiple repos (cross-repo graph):** +```bash +# Clone each repo, run the full pipeline on each, then merge +graphify clone # → ~/.graphify/repos// +graphify clone # → ~/.graphify/repos// +# Run /graphify on each local path to produce their graph.json files +# Then merge: +graphify merge-graphs \ + ~/.graphify/repos///graphify-out/graph.json \ + ~/.graphify/repos///graphify-out/graph.json \ + --out graphify-out/cross-repo-graph.json +``` + +Graphify clones into `~/.graphify/repos//` and reuses existing clones on repeat runs. Each node in the merged graph carries a `repo` attribute so you can filter by origin. + +**Multiple local subfolders (monorepo or multi-service layout):** + +The skill pipeline writes all intermediate and final outputs to `graphify-out/` in the current working directory. Running the skill on each subfolder separately will clobber the same output dir. Instead, use the CLI directly for each subfolder — it places `graphify-out/` *inside* the scanned path: + +```bash +graphify extract ./core/ # → ./core/graphify-out/graph.json +graphify extract ./service/ # → ./service/graphify-out/graph.json +graphify extract ./platform/ # → ./platform/graphify-out/graph.json +# Add --backend gemini|kimi|openai|deepseek|claude-cli depending on which API key you have set + +# Then merge at the project root: +graphify merge-graphs \ + ./core/graphify-out/graph.json \ + ./service/graphify-out/graph.json \ + ./platform/graphify-out/graph.json \ + --out graphify-out/graph.json +``` + +Once `graphify-out/graph.json` exists, the fast path above takes over: any codebase question runs `graphify query` directly on the merged graph — no re-extraction, no size gate. diff --git a/.claude/skills/graphify/references/hooks.md b/.claude/skills/graphify/references/hooks.md new file mode 100644 index 0000000..438b8b1 --- /dev/null +++ b/.claude/skills/graphify/references/hooks.md @@ -0,0 +1,33 @@ +# graphify reference: commit hook and native CLAUDE.md integration + +Load this when the user asked to install the post-commit hook or wire graphify into a project's CLAUDE.md. + +## For git commit hook + +Install a post-commit hook that auto-rebuilds the graph after every commit. No background process needed - triggers once per commit, works with any editor. + +```bash +graphify hook install # install +graphify hook uninstall # remove +graphify hook status # check +``` + +After every `git commit`, the hook detects which code files changed (via `git diff HEAD~1`), re-runs AST extraction on those files, and rebuilds `graph.json` and `GRAPH_REPORT.md`. Doc/image changes are ignored by the hook - run `/graphify --update` manually for those. + +If a post-commit hook already exists, graphify appends to it rather than replacing it. + +--- + +## For native CLAUDE.md integration + +Run once per project to make graphify always-on in Claude Code sessions: + +```bash +graphify claude install +``` + +This writes a `## graphify` section to the local `CLAUDE.md` that instructs Claude to check the graph before answering codebase questions and rebuild it after code changes. No manual `/graphify` needed in future sessions. + +```bash +graphify claude uninstall # remove the section +``` diff --git a/.claude/skills/graphify/references/query.md b/.claude/skills/graphify/references/query.md new file mode 100644 index 0000000..8db31ff --- /dev/null +++ b/.claude/skills/graphify/references/query.md @@ -0,0 +1,317 @@ +# graphify reference: query, path, explain + +Load this when the user asks a question against an existing graph, or runs `/graphify path` or `/graphify explain`. The core's query stub points here for the full traversal flow. These flows use the `graphify query` CLI when it is available and fall back to an inline NetworkX traversal otherwise. + +Two traversal modes - choose based on the question: + +| Mode | Flag | Best for | +|------|------|----------| +| BFS (default) | _(none)_ | "What is X connected to?" - broad context, nearest neighbors first | +| DFS | `--dfs` | "How does X reach Y?" - trace a specific chain or dependency path | + +First check the graph exists: +```bash +$(cat graphify-out/.graphify_python) -c " +from pathlib import Path +if not Path('graphify-out/graph.json').exists(): + print('ERROR: No graph found. Run /graphify first to build the graph.') + raise SystemExit(1) +" +``` +If it fails, stop and tell the user to run `/graphify ` first. + +### Step 0 — Constrained query expansion (REQUIRED before traversal) + +graphify's `query` CLI matches nodes via case-folded substring + IDF — there is **no stemming, no synonyms, no cross-language match** inside the binary, and the inline fallback below matches the same way. If the user's question uses different language or different domain vocabulary than the graph's labels (user says "обработчик" / graph says "handler"; user says "authentication" / graph says "Guardian"), the literal matcher returns 0 hits and the answer collapses to noise. + +Fix this **without inventing tokens** by expanding the query against the actual graph vocabulary first: + +1. Extract the token vocabulary from node labels: +```bash +$(cat graphify-out/.graphify_python) -c " +import json, re +from pathlib import Path +data = json.loads(Path('graphify-out/graph.json').read_text(encoding='utf-8')) +vocab = set() +for n in data['nodes']: + for c in re.findall(r'[^\W\d_]+', n.get('label','') or '', re.UNICODE): + parts = re.findall(r'[A-Z]+(?=[A-Z][a-z])|[A-Z]?[a-z]+|[A-Z]+', c) or [c] + for p in parts: + t = p.lower() + if 3 <= len(t) <= 30: + vocab.add(t) +Path('graphify-out/.vocab.txt').write_text('\n'.join(sorted(vocab)), encoding='utf-8') +print(f'vocab: {len(vocab)} tokens') +" +``` + +2. Read `graphify-out/.vocab.txt`. Then for the user's question, select **up to 12 tokens from this exact list** that semantically match the query intent. Hard constraints: + - You MUST pick only tokens present in the vocabulary file. Do NOT invent tokens. + - If a query concept has no plausible token in the vocab, skip it — do not substitute a near-synonym from training memory. + - If **no** vocab tokens match the query at all, output an empty list and tell the user the corpus has no relevant vocabulary for this question. Do not fabricate a search. + - Translate cross-language: Russian "аутентификация" → look for `auth`, `credential`, `token`, `security` IFF present in vocab. + - Morphology: "handlers" maps to `handler` IFF present; "todos" maps to `todo` IFF present. + +3. Print the selection explicitly to the user before running the query, so the expansion is auditable: +``` +Query expanded to (from graph vocab, N tokens): [token1, token2, ...] +``` +If the list is empty, say so plainly and stop — do not proceed to traversal. + +### Step 1 — Traversal + +Build the **expanded query string** by joining the selected tokens with spaces. Use this string as `QUESTION` below — NOT the original user question. (The original question is preserved only for `save-result` at the end.) + +Prefer the CLI when it is installed: +```bash +graphify query "QUESTION" +# or: graphify query "QUESTION" --dfs --budget 3000 +``` + +If the CLI is unavailable, load `graphify-out/graph.json` and run the traversal inline: + +1. Find the 1-3 nodes whose label best matches the expanded tokens. +2. Run the appropriate traversal from each starting node. +3. Read the subgraph - node labels, edge relations, confidence tags, source locations. +4. Answer using **only** what the graph contains. Quote `source_location` when citing a specific fact. +5. If the graph lacks enough information, say so - do not hallucinate edges. + +```bash +$(cat graphify-out/.graphify_python) -c " +import sys, json +from networkx.readwrite import json_graph +from pathlib import Path + +data = json.loads(Path('graphify-out/graph.json').read_text(encoding='utf-8')) +G = json_graph.node_link_graph(data, edges='links') + +question = 'QUESTION' +mode = 'MODE' # 'bfs' or 'dfs' +terms = [t.lower() for t in question.split() if len(t) >= 3] # match the vocab threshold; keeps api/jwt/ios (#1392) + +# Find best-matching start nodes +scored = [] +for nid, ndata in G.nodes(data=True): + label = ndata.get('label', '').lower() + score = sum(1 for t in terms if t in label) + if score > 0: + scored.append((score, nid)) +scored.sort(reverse=True) +start_nodes = [nid for _, nid in scored[:3]] + +if not start_nodes: + print('No matching nodes found for query terms:', terms) + sys.exit(0) + +subgraph_nodes = set() + +if mode == 'dfs': + # DFS: follow one path as deep as possible before backtracking. + # Depth-limited to 6 to avoid traversing the whole graph. + visited = set() + stack = [(n, 0) for n in reversed(start_nodes)] + while stack: + node, depth = stack.pop() + if node in visited or depth > 6: + continue + visited.add(node) + subgraph_nodes.add(node) + for neighbor in G.neighbors(node): + if neighbor not in visited: + stack.append((neighbor, depth + 1)) +else: + # BFS: explore all neighbors layer by layer up to depth 3. + frontier = set(start_nodes) + subgraph_nodes = set(start_nodes) + for _ in range(3): + next_frontier = set() + for n in frontier: + for neighbor in G.neighbors(n): + if neighbor not in subgraph_nodes: + next_frontier.add(neighbor) + subgraph_nodes.update(next_frontier) + frontier = next_frontier + +# Traversal chooses the nodes; collect relationships separately so seed edges, +# cycles, and cross-links are not lost just because both endpoints were visited. +# Taking the induced subgraph also keeps edges to nodes beyond the depth limit +# out of the result. data=True retains each edge's own attributes, including +# distinct parallel relationships in a MultiGraph. +subgraph_edges = [ + (u, v, edge_data) + for u, v, edge_data in G.subgraph(subgraph_nodes).edges(data=True) + if u != v +] + +# Token-budget aware output: rank by relevance, cut at budget (~4 chars/token) +token_budget = BUDGET # default 2000 +char_budget = token_budget * 4 + +# Score each node by term overlap for ranked output +def relevance(nid): + label = G.nodes[nid].get('label', '').lower() + return sum(1 for t in terms if t in label) + +ranked_nodes = sorted(subgraph_nodes, key=relevance, reverse=True) + +lines = [f'Traversal: {mode.upper()} | Start: {[G.nodes[n].get(\"label\",n) for n in start_nodes]} | {len(subgraph_nodes)} nodes'] +for nid in ranked_nodes: + d = G.nodes[nid] + lines.append(f' NODE {d.get(\"label\", nid)} [src={d.get(\"source_file\",\"\")} loc={d.get(\"source_location\",\"\")}]') +for u, v, edge_data in subgraph_edges: + if u in subgraph_nodes and v in subgraph_nodes: + lines.append(f' EDGE {G.nodes[u].get(\"label\",u)} --{edge_data.get(\"relation\",\"\")} [{edge_data.get(\"confidence\",\"\")}]--> {G.nodes[v].get(\"label\",v)}') + +output = '\n'.join(lines) +if len(output) > char_budget: + output = output[:char_budget] + f'\n... (truncated at ~{token_budget} token budget - use --budget N for more)' +print(output) +" +``` + +Replace `QUESTION` with the **expanded** query string, `MODE` with `bfs` or `dfs`, and `BUDGET` with the token budget (default `2000`, or whatever `--budget N` specifies). Then answer based on the subgraph output above, using only what the graph contains. + +After writing the answer, save it back into the graph so it improves future queries. Include the expanded tokens inside the `--answer` text (e.g. `"Expanded from original query via vocab: [tokens]. Then traversed..."`) so the next `--update` extracts the expansion history as a graph node: + +```bash +$(cat graphify-out/.graphify_python) -m graphify save-result --question "ORIGINAL_QUESTION" --answer "ANSWER" --type query --nodes NODE1 NODE2 +``` + +Replace `ORIGINAL_QUESTION` with the user's verbatim question, `ANSWER` with your full answer text (containing the expanded-token trace), `NODE1 NODE2` with the list of node labels you cited. This closes the feedback loop: the next `--update` will extract this Q&A as a node in the graph. + +**Work memory (self-improving loop).** Add an `--outcome` so future sessions learn from this one — append `--outcome useful|dead_end|corrected` to the `save-result` command (and `--correction "the right answer"` when correcting): + +- `useful` — the cited nodes answered the question well (they become *preferred sources*). +- `dead_end` — the question/path led nowhere; don't re-derive it next time. +- `corrected` — the saved answer was wrong; `--correction` records what was right. + +At the **start** of graph work, refresh and read the lessons: run `graphify reflect --if-stale` (cheap, deterministic, no LLM; `--if-stale` makes it a no-op when `LESSONS.md` is already newer than every input, e.g. when the git hook just refreshed it), then read `graphify-out/reflections/LESSONS.md`. It lists **preferred sources** (start there), **known dead ends** (skip them), and prior **corrections**. Running `reflect` yourself keeps the lessons current even without the git hook installed; if the post-commit hook *is* installed, `--if-stale` means your session-start run costs almost nothing. + +--- + +## For /graphify path + +Find the shortest path between two named concepts in the graph. Prefer the CLI when installed: + +```bash +graphify path "NODE_A" "NODE_B" +``` + +If the CLI is unavailable, run it inline: + +```bash +$(cat graphify-out/.graphify_python) -c " +import json, sys +import networkx as nx +from networkx.readwrite import json_graph +from pathlib import Path + +data = json.loads(Path('graphify-out/graph.json').read_text(encoding='utf-8')) +G = json_graph.node_link_graph(data, edges='links') + +a_term = 'NODE_A' +b_term = 'NODE_B' + +def find_node(term): + term = term.lower() + scored = sorted( + [(sum(1 for w in term.split() if w in G.nodes[n].get('label','').lower()), n) + for n in G.nodes()], + reverse=True + ) + return scored[0][1] if scored and scored[0][0] > 0 else None + +src = find_node(a_term) +tgt = find_node(b_term) + +if not src or not tgt: + print(f'Could not find nodes matching: {a_term!r} or {b_term!r}') + sys.exit(0) + +try: + path = nx.shortest_path(G, src, tgt) + print(f'Shortest path ({len(path)-1} hops):') + for i, nid in enumerate(path): + label = G.nodes[nid].get('label', nid) + if i < len(path) - 1: + _raw = G[nid][path[i+1]]; edge = next(iter(_raw.values()), {}) if isinstance(G, nx.MultiGraph) else _raw + rel = edge.get('relation', '') + conf = edge.get('confidence', '') + print(f' {label} --{rel}--> [{conf}]') + else: + print(f' {label}') +except nx.NetworkXNoPath: + print(f'No path found between {a_term!r} and {b_term!r}') +except nx.NodeNotFound as e: + print(f'Node not found: {e}') +" +``` + +Replace `NODE_A` and `NODE_B` with the actual concept names from the user. Then explain the path in plain language - what each hop means, why it's significant. + +After writing the explanation, save it back: + +```bash +$(cat graphify-out/.graphify_python) -m graphify save-result --question "Path from NODE_A to NODE_B" --answer "ANSWER" --type path_query --nodes NODE_A NODE_B +``` + +--- + +## For /graphify explain + +Give a plain-language explanation of a single node - everything connected to it. Prefer the CLI when installed: + +```bash +graphify explain "NODE_NAME" +``` + +If the CLI is unavailable, run it inline: + +```bash +$(cat graphify-out/.graphify_python) -c " +import json, sys +import networkx as nx +from networkx.readwrite import json_graph +from pathlib import Path + +data = json.loads(Path('graphify-out/graph.json').read_text(encoding='utf-8')) +G = json_graph.node_link_graph(data, edges='links') + +term = 'NODE_NAME' +term_lower = term.lower() + +# Find best matching node +scored = sorted( + [(sum(1 for w in term_lower.split() if w in G.nodes[n].get('label','').lower()), n) + for n in G.nodes()], + reverse=True +) +if not scored or scored[0][0] == 0: + print(f'No node matching {term!r}') + sys.exit(0) + +nid = scored[0][1] +data_n = G.nodes[nid] +print(f'NODE: {data_n.get(\"label\", nid)}') +print(f' source: {data_n.get(\"source_file\",\"unknown\")}') +print(f' type: {data_n.get(\"file_type\",\"unknown\")}') +print(f' degree: {G.degree(nid)}') +print() +print('CONNECTIONS:') +for neighbor in G.neighbors(nid): + _raw = G[nid][neighbor]; edge = next(iter(_raw.values()), {}) if isinstance(G, nx.MultiGraph) else _raw + nlabel = G.nodes[neighbor].get('label', neighbor) + rel = edge.get('relation', '') + conf = edge.get('confidence', '') + src_file = G.nodes[neighbor].get('source_file', '') + print(f' --{rel}--> {nlabel} [{conf}] ({src_file})') +" +``` + +Replace `NODE_NAME` with the concept the user asked about. Then write a 3-5 sentence explanation of what this node is, what it connects to, and why those connections are significant. Use the source locations as citations. + +After writing the explanation, save it back: + +```bash +$(cat graphify-out/.graphify_python) -m graphify save-result --question "Explain NODE_NAME" --answer "ANSWER" --type explain --nodes NODE_NAME +``` diff --git a/.claude/skills/graphify/references/transcribe.md b/.claude/skills/graphify/references/transcribe.md new file mode 100644 index 0000000..b967f83 --- /dev/null +++ b/.claude/skills/graphify/references/transcribe.md @@ -0,0 +1,52 @@ +# graphify reference: transcribe video and audio + +Load this only when `detect` reported one or more `video` files. A corpus with no video never reads this. + +### Step 2.5 - Transcribe video / audio files (only if video files detected) + +Skip this step entirely if `detect` returned zero `video` files. + +Video and audio files cannot be read directly. Transcribe them to text first, then treat the transcripts as doc files in Step 3. + +**Strategy:** Read the god nodes from `graphify-out/.graphify_detect.json` (or the analysis file if it exists from a previous run). You are already a language model — write a one-sentence domain hint yourself from those labels. Then pass it to Whisper as the initial prompt. No separate API call needed. + +**However**, if the corpus has *only* video files and no other docs/code, use the generic fallback prompt: `"Use proper punctuation and paragraph breaks."` + +**Step 1 - Write the Whisper prompt yourself.** + +Read the top god node labels from detect output or analysis, then compose a short domain hint sentence, for example: + +- Labels: `transformer, attention, encoder, decoder` → `"Machine learning research on transformer architectures and attention mechanisms. Use proper punctuation and paragraph breaks."` +- Labels: `kubernetes, deployment, pod, helm` → `"DevOps discussion about Kubernetes deployments and Helm charts. Use proper punctuation and paragraph breaks."` + +**Export** it as `GRAPHIFY_WHISPER_PROMPT` (the exact name the transcriber reads — and it must be `export`ed so the child Python process sees it) for the next command. + +**Step 2 - Transcribe:** + +```bash +export GRAPHIFY_WHISPER_MODEL=base # or whatever --whisper-model the user passed (must be exported) +export GRAPHIFY_WHISPER_PROMPT="" +$(cat graphify-out/.graphify_python) -c " +import json, os, sys +from pathlib import Path +from graphify.transcribe import transcribe_all + +detect = json.loads(Path('graphify-out/.graphify_detect.json').read_text(encoding=\"utf-8\")) +video_files = detect.get('files', {}).get('video', []) +prompt = os.environ.get('GRAPHIFY_WHISPER_PROMPT', 'Use proper punctuation and paragraph breaks.') + +transcript_paths = transcribe_all(video_files, initial_prompt=prompt) +# Write the JSON from Python (NOT a shell '>' redirect): transcribe_all/Whisper +# print progress to stdout, which would otherwise corrupt the JSON file (#1392). +Path('graphify-out/.graphify_transcripts.json').write_text(json.dumps(transcript_paths, ensure_ascii=False), encoding=\"utf-8\") +print(f'Transcribed {len(transcript_paths)} file(s)', file=sys.stderr) +" +``` + +After transcription: +- Read the transcript paths from `graphify-out/.graphify_transcripts.json` +- Add them to the docs list before dispatching semantic subagents in Step 3B +- Print how many transcripts were created: `Transcribed N video file(s) -> treating as docs` +- If transcription fails for a file, print a warning and continue with the rest + +**Whisper model:** Default is `base`. If the user passed `--whisper-model `, `export GRAPHIFY_WHISPER_MODEL=` (it must be exported, not just assigned) before running the command above. diff --git a/.claude/skills/graphify/references/update.md b/.claude/skills/graphify/references/update.md new file mode 100644 index 0000000..b7644de --- /dev/null +++ b/.claude/skills/graphify/references/update.md @@ -0,0 +1,221 @@ +# graphify reference: incremental update and cluster-only + +Load this only when the user passed `--update` or `--cluster-only`. A first-time full build never reads this file. + +## For --update (incremental re-extraction) + +Use when you've added or modified files since the last run. Only re-extracts changed files - saves tokens and time. + +```bash +$(cat graphify-out/.graphify_python) -c " +import sys, json +from graphify.detect import detect_incremental, save_manifest +from pathlib import Path + +from graphify.paths import out_path +from graphify.watch import _read_build_excludes, _read_build_gitignore +result = detect_incremental(Path('INPUT_PATH'), extra_excludes=_read_build_excludes(out_path()), gitignore=_read_build_gitignore(out_path())) +new_total = result.get('new_total', 0) +nf = result.get('new_files', {}) +print(json.dumps({ + 'new_total': new_total, + 'deleted': len(result.get('deleted_files', [])), + 'new_by_type': {t: len(v) for t, v in nf.items()}, + 'total_files': result.get('total_files'), + 'total_words': result.get('total_words'), + 'skipped_sensitive': result.get('skipped_sensitive', []), +}, indent=2, ensure_ascii=False)) +print('Full result: graphify-out/.graphify_incremental.json') +Path('graphify-out/.graphify_incremental.json').write_text(json.dumps(result, ensure_ascii=False), encoding=\"utf-8\") +deleted = list(result.get('deleted_files', [])) +if new_total == 0 and not deleted: + print('No files changed since last run. Nothing to update.') + raise SystemExit(0) +if deleted: + print(f'{len(deleted)} deleted file(s) to prune.') +if new_total > 0: + print(f'{new_total} new/changed file(s) to re-extract.') +" +``` + +Then populate `.graphify_detect.json` so Steps 3A–6 (which read it unconditionally) see the right state for an incremental run. `files` carries the changed subset (drives Step 3A AST + Step 3B0 cache check on only what changed); `all_files` carries the full corpus for any step that needs corpus-wide context: + +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from pathlib import Path +r = json.loads(Path('graphify-out/.graphify_incremental.json').read_text(encoding=\"utf-8\")) +Path('graphify-out/.graphify_detect.json').write_text(json.dumps({ + 'files': r.get('new_files', {}), + 'all_files': r.get('files', {}), + 'total_files': r.get('new_total', 0), + 'total_words': r.get('total_words', 0), + 'skipped_sensitive': r.get('skipped_sensitive', []), + 'needs_graph': True, +}, ensure_ascii=False), encoding=\"utf-8\") +" +``` + +If new files exist, first check whether all changed files are code files: + +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from pathlib import Path + +result = json.loads(open('graphify-out/.graphify_incremental.json', encoding='utf-8').read()) if Path('graphify-out/.graphify_incremental.json').exists() else {} +code_exts = {'.py','.ts','.js','.go','.rs','.java','.cpp','.c','.rb','.swift','.kt','.cs','.scala','.php','.cc','.cxx','.hpp','.h','.kts','.lua','.toc','.f','.F','.f90','.F90','.f95','.F95','.f03','.F03','.f08','.F08'} +new_files = result.get('new_files', {}) +all_changed = [f for files in new_files.values() for f in files] +code_only = all(Path(f).suffix.lower() in code_exts for f in all_changed) +print('code_only:', code_only) +" +``` + +If `code_only` is True: print `[graphify update] Code-only changes detected - skipping semantic extraction (no LLM needed)`, run Step 3A (AST) on the changed files, then follow only Part B's **Fast path** to overwrite `.graphify_semantic.json` with an empty result and run Part C to create `.graphify_extract.json` from the fresh AST result. Skip semantic cache checks/writes and subagents (Steps B0–B3); do not reuse semantic sidecars from a prior run. Continue to the incremental merge and Steps 4–8 only after Part C has completed. + +If `code_only` is False (any changed file is a doc/paper/image/video): **first, if any changed file is in `new_files['video']`, run `references/transcribe.md` (Step 2.5) on those files, then rewrite `.graphify_detect.json` to move the resulting transcript paths into `files['document']` and drop `files['video']`** — otherwise raw `.mp4/.mp3` paths are fed to semantic subagents as unreadable media (#1392). Then run the full Steps 3A–3C pipeline as normal. + + +If no new files exist (only deletions), create an empty extraction so the merge step can prune: + +```bash +if [ ! -f graphify-out/.graphify_extract.json ]; then + echo '[graphify update] Only deletions -- creating empty extraction for merge.' + $(cat graphify-out/.graphify_python) -c " +import json +from pathlib import Path +Path('graphify-out/.graphify_extract.json').write_text(json.dumps({'nodes':[],'edges':[],'hyperedges':[],'input_tokens':0,'output_tokens':0}), encoding='utf-8') +" +fi +``` + + +Then: + +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from pathlib import Path +from graphify.build import build_merge +from graphify.detect import save_manifest + +# Load new extraction and incremental state +new_extraction = json.loads(Path('graphify-out/.graphify_extract.json').read_text(encoding=\"utf-8\")) +incremental = json.loads(Path('graphify-out/.graphify_incremental.json').read_text(encoding=\"utf-8\")) +deleted = list(incremental.get('deleted_files', [])) +# prune_sources is ONLY for genuinely DELETED files. Changed/re-extracted files are +# handled by build_merge's replace-on-re-extract (#1344): every source_file in +# new_chunks is dropped from the base before merge, so old/stale nodes don't survive. +# Do NOT add changed here: with root= passed, prune_set relativizes to the same base +# as the freshly merged nodes and would DELETE the re-extracted content (#1178 is moot +# now that replace — not the dedup pass — reconciles changed files). +prune = list(deleted) or None + +# Use build_merge() — reads graph.json directly without NetworkX round-trip +# so edge direction (calls, implements, imports) is always preserved (#801). +# Pass root= so prune_sources (absolute paths from detect_incremental) are +# relativized to match the graph's relative source_file values; without it +# nothing is pruned and stale nodes accumulate on every update (#1361). +# directed=IS_DIRECTED: replace IS_DIRECTED with True if --directed was given, else +# False. Without it a --directed --update silently rebuilds undirected and collapses +# reciprocal A<->B edges (#1392). +G = build_merge( + [new_extraction], + graph_path='graphify-out/graph.json', + prune_sources=prune, + root='INPUT_PATH', + directed=IS_DIRECTED, +) +print(f'[graphify update] Merged: {G.number_of_nodes()} nodes, {G.number_of_edges()} edges') + +# Write merged result back to .graphify_extract.json so Step 4 sees the full graph +merged_out = { + 'nodes': [{'id': n, **d} for n, d in G.nodes(data=True)], + 'edges': [ + # Explicit source/target last so they win over any stale attrs in d. + {**{k: val for k, val in d.items() if k not in ('_src', '_tgt', 'source', 'target')}, + 'source': d.get('_src', u), 'target': d.get('_tgt', v)} + for u, v, d in G.edges(data=True) + ], + # G.graph["hyperedges"] holds hyperedges from both existing graph.json + # and new_extraction (build_merge combines them). Falling back to + # new_extraction only would silently drop prior-run hyperedges (#801). + 'hyperedges': list(G.graph.get('hyperedges', [])), + 'input_tokens': new_extraction.get('input_tokens', 0), + 'output_tokens': new_extraction.get('output_tokens', 0), +} +Path('graphify-out/.graphify_extract.json').write_text(json.dumps(merged_out, ensure_ascii=False), encoding=\"utf-8\") +print(f'[graphify update] Merged extraction written ({len(merged_out[\"nodes\"])} nodes, {len(merged_out[\"edges\"])} edges)') + +# Save manifest so next --update diffs against today's state, not the +# prior run's baseline (prevents ghost-node reports on subsequent updates). +# root= matches the build_merge call above so the manifest keys stay relative to +# the scan root — portable across clones/machines, so --update keeps matching +# cached files instead of missing every one after a move (#1417). +# +# Only stamp semantic files (docs/papers/images) that ACTUALLY produced output +# THIS run (new_extraction is this run's fresh extraction, read above before the +# merge overwrote the file): a changed doc whose chunk failed must stay unstamped +# so the next --update re-queues it, otherwise it is marked done and its content +# is lost forever (#2015). Mirrors the library extract path +# (cli._stamped_manifest_files + clear_semantic + scan_corpus). +from graphify.cli import _stamped_manifest_files +_manifest_files = _stamped_manifest_files(incremental['files'], new_extraction, Path('INPUT_PATH')) +# Changed semantic files dispatched this run but NOT stamped had their chunk fail +# or be omitted; clear any stale semantic_hash so they are re-queued (#1948). +_sem_types = ('document', 'paper', 'image') +_dispatched = {f for t, fl in incremental.get('new_files', {}).items() if t in _sem_types for f in fl} +_stamped = {f for fl in _manifest_files.values() for f in fl} +_cleared = _dispatched - _stamped +# scan_corpus = the RAW full corpus so in-root files newly excluded since last run +# are dropped rather than masquerading as deletions; untouched rows preserved (#1908). +_scan = {f for fl in incremental['files'].values() for f in fl} +save_manifest(_manifest_files, root='INPUT_PATH', scan_corpus=_scan, clear_semantic=_cleared or None) +print('[graphify update] Manifest saved.') +" +``` + +Then run Steps 4–8 on the merged graph as normal. + +After Step 4, show the graph diff: + +```bash +$(cat graphify-out/.graphify_python) -c " +import json +from graphify.analyze import graph_diff +from graphify.build import build_from_json +from graphify.paths import load_node_link_graph +import networkx as nx +from pathlib import Path + +# Load old graph (before update) from backup written before merge +old_data = json.loads(Path('graphify-out/.graphify_old.json').read_text(encoding=\"utf-8\")) if Path('graphify-out/.graphify_old.json').exists() else None +new_extract = json.loads(Path('graphify-out/.graphify_extract.json').read_text(encoding=\"utf-8\")) +G_new = build_from_json(new_extract, directed=IS_DIRECTED) + +if old_data: + G_old = load_node_link_graph(old_data) + diff = graph_diff(G_old, G_new) + print(diff['summary']) + if diff['new_nodes']: + print('New nodes:', ', '.join(n['label'] for n in diff['new_nodes'][:5])) + if diff['new_edges']: + print('New edges:', len(diff['new_edges'])) +" +``` + +Before the merge step, save the old graph: `cp graphify-out/graph.json graphify-out/.graphify_old.json` +Clean up after: `rm -f graphify-out/.graphify_old.json` + +--- + +## For --cluster-only + +Skip Steps 1–3. Re-run clustering on the existing graph: + +```bash +graphify cluster-only . +``` + +`graphify cluster-only .` is **self-contained**: it re-clusters, names communities, and regenerates `GRAPH_REPORT.md`, `graph.json`, and `graph.html` from the existing graph. **Do not re-run Steps 5–9** — they read intermediate files (`.graphify_extract.json`, `.graphify_detect.json`, `.graphify_analysis.json`) that a prior build's cleanup (Step 9) already deleted, so they raise `FileNotFoundError` (#1392). When it finishes, present the refreshed `GRAPH_REPORT.md` summary as usual. diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..d5a885b --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,10 @@ +## graphify + +This project has a knowledge graph at graphify-out/ with god nodes, community structure, and cross-file relationships. + +Rules: +- When graphify-out/graph.json exists, run `graphify query ""` before your first grep, search or source-file read for a codebase question, and put this rule in every subagent prompt that explores code. Read raw files only for lines the graph or an earlier read already located. Use `graphify path "" ""` for relationships and `graphify explain ""` for focused concepts. These return a scoped subgraph, usually much smaller than GRAPH_REPORT.md or raw grep output. +- If graphify-out/wiki/index.md exists, use it for broad navigation instead of raw source browsing. +- Read graphify-out/GRAPH_REPORT.md only for broad architecture review or when query/path/explain do not surface enough context. +- After modifying code, run `graphify update .` to keep the graph current (AST-only, no API cost). +- A committed graphify-out/graph.json is rewritten whole on every build, so a large diff to it in a pull request is expected. Do not flag it as churn or suggest skipping the graph rebuild or its hooks. diff --git a/Makefile b/Makefile index 8067d82..764e22c 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ TTY := $(shell [ -t 0 ] && echo -it) OWNER := $(shell docker info --format '{{.SecurityOptions}}' 2>/dev/null | grep -q rootless && echo 0:0 || echo $$(id -u):$$(id -g)) FIX_OWNER = chown -R $(OWNER) /project -.PHONY: all help web build flash ota ota-drivers monitor flash-monitor log size menuconfig clean fullclean erase test test-driver test-envelope test-api test-browser +.PHONY: all help web build flash ota ota-drivers monitor flash-monitor log size menuconfig clean fullclean erase test test-driver test-envelope test-api test-browser sim-build sim sim-web test-sim # Default target: build, flash, then open the console (run in order, so not with make -j) all: build flash monitor @@ -49,6 +49,11 @@ help: @echo " make test driver unit tests + C/Python envelope cross-check" @echo " make test-api HOST=ip HTTP API tests against a running controller (data restored afterwards)" @echo " make test-browser HOST=ip Selenium UI tests against a running controller (needs selenium + chromedriver)" + @echo "Simulated rig (no hardware): the controller built for linux + led-simulator's 16 drivers over a UDP bridge" + @echo " make sim-build build the controller for the host (sim-controller/build/led_controller_sim.elf)" + @echo " make sim build, then run the headless rig and the controller (http://localhost:8080/)" + @echo " make sim-web build, then run the browser rig (http://localhost:8765/) and the controller" + @echo " make test-sim rig tests (groups, field patterns, sequences) + the API tests against make sim" @echo "variables: PORT=$(PORT) IDF_IMAGE=$(IDF_IMAGE)" # --- web UI --------------------------------------------------------------- @@ -120,6 +125,24 @@ test-envelope: @tmp=$$(mktemp -d); \ (cd test && python3 env_check.py) > $$tmp/py.txt; \ docker run --rm -v "$(CURDIR)":/project -w /project/test $(IDF_IMAGE) bash -c \ - 'gcc -Wall -I/opt/esp/idf/components/json/cJSON env_host.c ../main/envelope.c /opt/esp/idf/components/json/cJSON/cJSON.c -o /tmp/t -lm && /tmp/t' 2>&1 | tail -2 > $$tmp/c.txt; \ + 'gcc -Wall -I/opt/esp/idf/components/json/cJSON env_host.c ../main/envelope.c /opt/esp/idf/components/json/cJSON/cJSON.c -o /tmp/t -lm && /tmp/t' 2>&1 | tail -4 | head -3 > $$tmp/c.txt; \ if diff -q $$tmp/py.txt $$tmp/c.txt >/dev/null; then echo "envelope: C and Python agree"; rm -rf $$tmp; \ else echo "envelope: MISMATCH"; diff $$tmp/py.txt $$tmp/c.txt; rm -rf $$tmp; exit 1; fi + +# --- simulated rig -------------------------------------------------------- +# The firmware built for ESP-IDF's linux target runs on this computer; its ESP-NOW frames go over UDP to +# led-simulator (bridge_sim.py headless, or server.py in the browser), whose drivers run led-driver/src. +SIM_DB ?= $(CURDIR)/sim-controller/data +SIM_RUN = mkdir -p $(SIM_DB) && LED_SIM_DB=$(SIM_DB) LED_SIM_BRIDGE=127.0.0.1:8766 sim-controller/build/led_controller_sim.elf + +sim-build: web + $(DOCKER_RUN) $(IDF_IMAGE) bash -c 'cd sim-controller && idf.py build; rc=$$?; $(FIX_OWNER); exit $$rc' + +sim: sim-build + @trap 'kill 0' INT TERM EXIT; python3 led-simulator/bridge_sim.py & $(SIM_RUN) + +sim-web: sim-build + @trap 'kill 0' INT TERM EXIT; python3 led-simulator/server.py & $(SIM_RUN) + +test-sim: + python3 -m pytest test/api -q --host localhost:8080 diff --git a/db/audio_run.json b/db/audio_run.json new file mode 100644 index 0000000..b33caf0 --- /dev/null +++ b/db/audio_run.json @@ -0,0 +1,6 @@ +{ + "enabled": true, + "device": "simulated", + "device_override": "", + "device_select": "simulated" +} \ No newline at end of file diff --git a/db/bridges.json b/db/bridges.json new file mode 100644 index 0000000..4175af0 --- /dev/null +++ b/db/bridges.json @@ -0,0 +1,16 @@ +[ + { + "id": "c9ab898d-740", + "label": "bridge1", + "transport": "ip", + "ip": "192.168.4.1", + "ws_port": 80 + }, + { + "id": "18d85e86-17b", + "label": "bridge2", + "transport": "serial", + "serial_port": "/dev/ttyUSB0", + "serial_baudrate": 115200 + } +] diff --git a/db/pattern.json b/db/pattern.json index 378940e..a268390 100644 --- a/db/pattern.json +++ b/db/pattern.json @@ -1 +1 @@ -{"on": {"min_delay": 10, "max_delay": 10000, "max_colors": 1}, "off": {"min_delay": 10, "max_delay": 10000, "max_colors": 0}, "rainbow": {"n1": "Step Rate", "min_delay": 10, "max_delay": 10000, "max_colors": 0}, "colour_cycle": {"n1": "Step Rate", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "transition": {"min_delay": 10, "max_delay": 10000, "max_colors": 10}, "chase": {"n1": "Colour 1 Length", "n2": "Colour 2 Length", "n3": "Step 1", "n4": "Step 2", "min_delay": 10, "max_delay": 10000, "max_colors": 2}, "pulse": {"n1": "Attack", "n2": "Hold", "n3": "Decay", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "circle": {"n1": "Head Rate", "n2": "Max Length", "n3": "Tail Rate", "n4": "Min Length", "min_delay": 10, "max_delay": 10000, "max_colors": 2}, "blink": {"min_delay": 10, "max_delay": 10000, "max_colors": 10}, "flicker": {"n1": "Min brightness", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "flame": {"n1": "Min brightness", "n2": "Breath period (ms)", "n3": "Spark gap min (ms, 0=default 10\u201330 s, -1=off)", "n4": "Spark gap max (ms)", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "twinkle": {"n1": "Twinkle activity (1\u2013255, higher = more changes)", "n2": "Density (0\u2013255, higher = more of the strip lit)", "n3": "Min adjacent LEDs per twinkle (same as max for fixed length)", "n4": "Max adjacent LEDs per twinkle (same as min for fixed length)", "min_delay": 84, "max_delay": 10000, "max_colors": 10}, "radiate": {"n1": "Node spacing (LEDs)", "n2": "Out time (ms)", "n3": "In time (ms)", "min_delay": 10, "max_delay": 10000, "max_colors": 2}, "meteor_rain": {"n1": "Tail length", "n2": "Speed (LEDs per frame)", "n3": "Fade amount (1-255)", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "scanner": {"n1": "Eye width", "n2": "End pause (frames)", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "gradient_scroll": {"n1": "Scroll step rate", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "comet_dual": {"n1": "Tail length", "n2": "Speed", "n3": "Gap", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "sparkle_trail": {"n1": "Spark density", "n2": "Decay", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "wave": {"n1": "Wavelength", "n2": "Amplitude", "n3": "Drift speed", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "plasma": {"n1": "Scale", "n2": "Speed", "n3": "Contrast", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "segment_chase": {"n1": "Segment size", "n2": "Phase step", "n3": "Segment phase offset", "n4": "Gap per segment", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "bar_graph": {"n1": "Level percent", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "breathing_dual": {"n1": "Phase offset", "n2": "Ease", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "strobe_burst": {"n1": "Burst count", "n2": "Burst gap", "n3": "Cooldown", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "rain_drops": {"n1": "Drop rate", "n2": "Ripple width", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "fireflies": {"n1": "Count", "n2": "Twinkle speed", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "clock_sweep": {"n1": "Hand width", "n2": "Marker interval", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "marquee": {"n1": "On length", "n2": "Off length", "n3": "Step", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "aurora": {"n1": "Band count", "n2": "Shimmer", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "snowfall": {"n1": "Flake density", "n2": "Fall speed", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "heartbeat": {"n1": "Pulse 1 ms", "n2": "Pulse 2 ms", "n3": "Pause ms", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "orbit": {"n1": "Orbit count", "n2": "Base speed", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "palette_morph": {"n1": "Morph ms", "n2": "Warp rate", "n3": "Turbulence", "max_colors": 10, "min_delay": 10, "max_delay": 10000}} \ No newline at end of file +{"on": {"min_delay": 10, "max_delay": 10000, "max_colors": 1}, "off": {"min_delay": 10, "max_delay": 10000, "max_colors": 0}, "rainbow": {"n1": "Step Rate", "min_delay": 10, "max_delay": 10000, "max_colors": 0}, "colour_cycle": {"n1": "Step Rate", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "transition": {"min_delay": 10, "max_delay": 10000, "max_colors": 10}, "chase": {"n1": "Colour 1 Length", "n2": "Colour 2 Length", "n3": "Step 1", "n4": "Step 2", "min_delay": 10, "max_delay": 10000, "max_colors": 2}, "pulse": {"n1": "Attack", "n2": "Hold", "n3": "Decay", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "circle": {"n1": "Head Rate", "n2": "Max Length", "n3": "Tail Rate", "n4": "Min Length", "min_delay": 10, "max_delay": 10000, "max_colors": 2}, "blink": {"min_delay": 10, "max_delay": 10000, "max_colors": 10}, "flicker": {"n1": "Min brightness", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "flame": {"n1": "Min brightness", "n2": "Breath period (ms)", "n3": "Spark gap min (ms, 0=default 10\u201330 s, -1=off)", "n4": "Spark gap max (ms)", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "twinkle": {"n1": "Twinkle activity (1\u2013255, higher = more changes)", "n2": "Density (0\u2013255, higher = more of the strip lit)", "n3": "Min adjacent LEDs per twinkle (same as max for fixed length)", "n4": "Max adjacent LEDs per twinkle (same as min for fixed length)", "min_delay": 84, "max_delay": 10000, "max_colors": 10}, "radiate": {"n1": "Node spacing (LEDs)", "n2": "Out time (ms)", "n3": "In time (ms)", "min_delay": 10, "max_delay": 10000, "max_colors": 2}, "meteor_rain": {"n1": "Tail length", "n2": "Speed (LEDs per frame)", "n3": "Fade amount (1-255)", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "scanner": {"n1": "Eye width", "n2": "End pause (frames)", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "gradient_scroll": {"n1": "Scroll step rate", "min_delay": 10, "max_delay": 10000, "max_colors": 10}, "comet_dual": {"n1": "Tail length", "n2": "Speed", "n3": "Gap", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "sparkle_trail": {"n1": "Spark density", "n2": "Decay", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "wave": {"n1": "Wavelength", "n2": "Amplitude", "n3": "Drift speed", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "plasma": {"n1": "Scale", "n2": "Speed", "n3": "Contrast", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "segment_chase": {"n1": "Segment size", "n2": "Phase step", "n3": "Segment phase offset", "n4": "Gap per segment", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "bar_graph": {"n1": "Level percent", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "breathing_dual": {"n1": "Phase offset", "n2": "Ease", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "strobe_burst": {"n1": "Burst count", "n2": "Burst gap", "n3": "Cooldown", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "rain_drops": {"n1": "Drop rate", "n2": "Ripple width", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "fireflies": {"n1": "Count", "n2": "Twinkle speed", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "clock_sweep": {"n1": "Hand width", "n2": "Marker interval", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "marquee": {"n1": "On length", "n2": "Off length", "n3": "Step", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "aurora": {"n1": "Band count", "n2": "Shimmer", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "snowfall": {"n1": "Flake density", "n2": "Fall speed", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "heartbeat": {"n1": "Pulse 1 ms", "n2": "Pulse 2 ms", "n3": "Pause ms", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "orbit": {"n1": "Orbit count", "n2": "Base speed", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "palette_morph": {"n1": "Morph ms", "n2": "Warp rate", "n3": "Turbulence", "max_colors": 10, "min_delay": 10, "max_delay": 10000}, "gradient_wipe": {"n1": "Start LED", "n2": "End LED", "min_delay": 10, "max_delay": 10000, "max_colors": 2}} \ No newline at end of file diff --git a/dependencies.lock b/dependencies.lock index 61d23c2..e1db000 100644 --- a/dependencies.lock +++ b/dependencies.lock @@ -26,6 +26,6 @@ dependencies: direct_dependencies: - espressif/mdns - joltwallet/littlefs -manifest_hash: ca18d7304afd7b13c5257461792fe9f4b54a044914fa18a550807cfc4eb8cf67 +manifest_hash: a5329d49f0154685d9701ccb399916a87107b77ddfe802c911aa2bb41ed5bab0 target: esp32s3 version: 2.0.0 diff --git a/docs/API.md b/docs/API.md index 3526250..20967bf 100644 --- a/docs/API.md +++ b/docs/API.md @@ -34,7 +34,7 @@ Pages and static files are built into the firmware (gzipped, sent with `Content- | Method | Path | Description | |--------|------|-------------| -| GET | `/status` | IP, MAC, uptime, `free_heap`, `free_internal` (internal RAM), mic level, tempo and beat counts, ESP-NOW and link statistics | +| GET | `/status` | IP, `eth` (ip, mask, gw, up), `ap_ip`, `ap_clients` (`[{mac, ip}]` on the Wi-Fi access point), MAC, uptime, `free_heap`, `free_internal` (internal RAM), mic level, tempo and beat counts, ESP-NOW and link statistics | | POST | `/reboot` | Replies `{"message":"rebooting"}`, then restarts after 0.5 s | | GET | `/ota` | Running image: partition, version, build time, IDF version, sha | | POST | `/ota` | Body: the raw firmware `.bin` (web UI included). Written to the idle slot, made the boot image, then the board reboots. A new image confirms itself after staying up a few seconds; one that crashes on start rolls back. `make ota HOST=` | @@ -61,8 +61,9 @@ Keys with meaning on the controller (others are stored as they are): | Key | Checked | Meaning | |-----|---------|---------| -| `ap_enabled` | | Wi-Fi access point (open, SSID = hostname, `192.168.5.1`); default `true`; applies after a restart. Holding the D7 button at boot forces it on | +| `ap_enabled` | | Wi-Fi access point (open, SSID = hostname, `192.168.5.1`); default `true`; applies after a restart. Holding the D3 button at boot forces it on | | `ap_password` | | AP password, 8-63 characters (WPA2); empty (default) is an open network; applies after a restart | +| `bridge` | string under 64 characters | `host:port` (UDP, default port 8766) of led-simulator: every ESP-NOW frame goes there as `dst_mac + packet` instead of over the radio, and frames from it (`src_mac + packet`) count as received. Empty or missing: the radio. Applies after a restart. The host build (`sim-controller/`) always uses a bridge (`LED_SIM_BRIDGE`, default `127.0.0.1:8766`) | | `hostname` | | mDNS name (`.local`, default `led-controller`); applies after a restart | | `static_ips` | | List of `{ip, netmask?, gw, dns?}` tried in order when the Ethernet link comes up; the first whose gateway answers ARP is used, else DHCP, else `192.168.4.1/24` for a direct PC cable. Applies after a restart | | `global_brightness` | integer 0–255 | Global factor of each driver's brightness (see Groups). Stored only: the UI pushes each driver its combined value | @@ -97,7 +98,11 @@ A store with no profile gets one at boot, `default`, with the same seed, a start ## Presets — `/presets` -A preset has `name`, `pattern`, `colors` (`#RRGGBB`), `brightness`, `delay`, `n1`–`n8`, `profile_id`, and optionally `auto`, `palette_refs` (palette index per colour slot), `background` / `background_palette_ref`, `mode` (sent as `n6`) and `manual_beat_n`. New presets start with every field set and belong to the current profile. +A preset has `name`, `pattern`, `colors` (`#RRGGBB`), `brightness`, `delay`, `n1`–`n8`, `profile_id`, and optionally `auto`, `palette_refs` (palette index per colour slot), `background` / `background_palette_ref`, `mode` (sent as `n6`), `manual_beat_n`, `fade` (0–10000 ms: crossfade from what the strip showed when the preset is selected) and `beat_time` (`true`: the delay and timing parameters of patterns that support it count quarter beats, so the preset follows the tempo). New presets start with every field set and belong to the current profile. + +**Animation patterns** (driver patterns, labels in `/patterns`). Each plays once with `auto: false` and holds its end state; with `auto: true` it repeats. +- One-shots: `hit` (flash with attack, hold and decay), `keyframes` (ramp through the colours with easing). +- Field patterns, which span the whole rig using each driver's place from `/devices/layout`: `sweep`, `ripple`, `spin`, `grow`, `field_wave`. | Method | Path | Description | |--------|------|-------------| @@ -113,7 +118,7 @@ A preset has `name`, `pattern`, `colors` (`#RRGGBB`), `brightness`, `delay`, `n1 ## Zones — `/zones` -A zone has `name`, `names` (driver names, the older way to target drivers), `group_ids` (groups it plays to), `presets` (rows of preset ids), `presets_flat`, `default_preset`, `preset_group_ids`, `sequence_ids`, `content_kind` (`presets` or `sequences`) and `brightness` (0–255, the zone slider). +A zone has `name`, `names` (driver names, the older way to target drivers), `group_ids` (groups it plays to), `presets` (rows of preset ids), `presets_flat`, `default_preset`, `preset_group_ids`, `sequence_ids`, `animation_ids` (animations shown as one-shot tiles), `content_kind` (`presets` or `sequences`) and `brightness` (0–255, the zone slider). | Method | Path | Description | |--------|------|-------------| @@ -160,6 +165,7 @@ A device is `{id: MAC, name, type: "led", transport: "espnow" | "wifi", address, | DELETE | `/devices/` | Delete | | GET | `/devices/status` | Per driver: `{"last_ack_ms_ago": ms or -1, "failed_messages": n}` from the acked link | | POST | `/devices/ping` | Body `{"timeout_s": 0.5–30}` (default 3). Broadcasts a ping and collects replies: `{"ok", "ping_id", "timeout_s", "responses": {mac: {mac, name, rtt_ms}}, "registered": n}`. Drivers that answer are added or renamed | +| POST | `/devices/layout` | Body `{"placements": {mac: {x0, y0, x1, y1} \| null}, "center"?: {x, y}}`: where each bar sits, in any units with y up, and the rig's centre (kept in `settings.rig_center`; without one, the layout's bounding-box centre). `x0,y0` is LED 1, the data-in end; `null` removes a bar. Saved as the device's `placement`. Then every placed ESP-NOW driver gets `device_config.place`: its ends relative to the centre, scaled so the furthest end is 1 from it. Reply `{"updated", "sent", "failed"}`; 400 without `placements` | | POST | `/devices/groups` | Send every ESP-NOW driver its groups and wait for the acks: `{"ok", "total", "sent", "failed"}` (503 when any failed, 400 with no ESP-NOW devices) | | POST | `/devices/resolve-brightness` | Body `{"macs": [...], "zone_brightness"?}` → `{"values": {mac: brightness}}` | | POST | `/devices//brightness` | Body `{"zone_brightness"?}`: send the combined brightness, saved on the driver. `{"message", "brightness"}` | @@ -168,7 +174,22 @@ A device is `{id: MAC, name, type: "led", transport: "espnow" | "wifi", address, ## Sequences — `/sequences` -A sequence is `{name, profile_id, lanes: [[step, ...], ...], lanes_group_ids: [[group ids], ...], group_ids, loop (default true), advance_mode: "beats" | "time", steps, step_duration_ms, sequence_transition}`. A step is `{preset_id, beats (default 1), seconds (default 3, 0.1–3600), group_ids?}`. With `advance_mode` `beats` a step lasts `beats` beats; with `time` it lasts `seconds`. +A sequence is `{name, profile_id, kind?, targets?, lanes: [[step, ...], ...], lane_opts?, lanes_group_ids: [[group ids], ...], group_ids, loop (default true), advance_mode: "beats" | "time", steps, step_duration_ms, sequence_transition}`. Every field marked `?` is optional; a sequence without them plays as it always has. + +- `kind`: `"sequence"` (default) or `"animation"`: a clip that is fired once over whatever runs (see Animations). Both are stored here. +- `targets`: the drivers the sequence plays to, in order (up to 32): `[{name, group_ids: [group ids], devices: [MACs]}]`. A target reaches every driver in its groups plus each listed driver (by its own group `d`, see Driver groups). Lanes and steps name targets by index. +- `lane_opts`: one object per lane (same index as `lanes`): + - `mode`: `"beats"` or `"time"`, overriding `advance_mode` for this lane, so beat lanes and time lanes run side by side. + - `offset`: beats (or seconds, time lanes) before the lane starts, on the first pass only. With a copy of a lane this gives a canon or ripple. + - `rate`: 0.25–4, speed multiplier. A 1-beat step at rate 2 lasts half a beat. + - `targets`: indexes into `targets` the lane plays to. Without them the lane's groups come from the older fields: `lanes_group_ids[lane]`, else `group_ids`, else (single lane) the step's `group_ids`, else the zone's groups. + - `chase`: `{order: "forward" | "reverse" | "bounce" | "random", width (default 1), others: "off" (default) | "hold"}`. Each step moves the lit position along the lane's targets (in the order listed in the lane's `targets`). The step's preset goes to `width` targets from there, and the rest go off or keep what they show. +- A step is `{preset_id, beats (default 1, from 0.25), seconds (default 3, 0.1–3600), targets?, others?, rest?, animation_id?, length?, group_ids?}`: + - `beats` / `seconds`: how long it lasts, by its lane's mode. + - `targets`: indexes of the lane's targets this step lights (default: all of the lane's, or the chase position). + - `others: "off"`: the lane's other targets go off for this step. + - `rest: true`: nothing lit; the lane's targets go off. + - `animation_id`: fire that animation instead of a preset. With `length: "animation"` the step lasts as long as the animation (at the current tempo). | Method | Path | Description | |--------|------|-------------| @@ -178,7 +199,9 @@ A sequence is `{name, profile_id, lanes: [[step, ...], ...], lanes_group_ids: [[ | PUT | `/sequences/` | Merge fields; stops it when it is playing | | DELETE | `/sequences/` | Delete; stops it when it is playing | | POST | `/sequences//play` | Body `{"zone_id"}` (required). Reply `{"ok": true, "pending": bool, ...}`. 400 for a missing zone, a sequence without steps or one from another profile | -| POST | `/sequences/stop` | Stop playback and clear the presets on the zone's groups. `{"ok": true}` | +| POST | `/sequences/stop` | Stop playback (and any animation) and clear the presets on the zone's groups. `{"ok": true}` | +| POST | `/sequences//fire` | Play it once as an animation over whatever runs. Body `{"zone_id"?, "wait"?: "now" (default) \| "beat" \| "downbeat"}`; without `zone_id`, the playing sequence's zone. Reply `{"ok": true, "wait"}`. 404 for another profile's, 400 without a zone or steps | +| POST | `/sequences/overlay/stop` | End the animation now; its drivers go back (as when it ends). `{"ok": true}` | | POST | `/sequences/sync-phase` | Body `{"mode": "step" | "pass"}`: `step` restarts the current step's beat count, `pass` restarts the sequence. Also makes this beat bar beat 1. 409 when nothing plays | | GET | `/sequences/status` | Playback status (below) | | GET | `/sequences//export` | Sequence bundle (the sequence and its presets) | @@ -186,12 +209,19 @@ A sequence is `{name, profile_id, lanes: [[step, ...], ...], lanes_group_ids: [[ **Playback.** One sequence plays at a time, in one zone. - **Start.** Each lane's first preset is uploaded to the lane's groups and selected, then the lane's other presets are uploaded, and every driver in the zone gets its combined brightness with the zone slider. -- **Advancing.** In `time` mode a lane moves on when its step's `seconds` are up, whatever the beat; the status then has `lane0_step_elapsed_ms` and `lane0_step_duration_ms` and `beat_readout` is empty. In `beats` mode, on each beat a lane counts towards its step's `beats`, then moves on: the next preset is uploaded (unless already there) and selected. A lane's groups are `lanes_group_ids[lane]`, else `group_ids`, else (single lane) the step's `group_ids`, else the zone's groups. +- **Advancing.** A time lane moves on when its step's `seconds` are up, whatever the beat. When lane 0 is a time lane, the status has `lane0_step_elapsed_ms` and `lane0_step_duration_ms` and `beat_readout` is empty. A beat lane counts quarter beats (the controller ticks between beats from the last beat interval) towards its step's `beats` ÷ `rate`, then moves on. Each step's preset is uploaded to all of the lane's targets (unless already there), then selected on the targets the step lights. - **End.** Without `loop` the sequence ends after one pass. Its presets are cleared from the zone's groups, and the readout keeps `n/n`. - **Switching.** Starting another sequence while one plays waits for the next beat. It waits for the next downbeat only when `sequence_switch_wait` is `downbeat` and the mic is timing. The current sequence holds meanwhile. +- **Animations.** `/sequences//fire` (or a step's `animation_id`) plays a sequence once in a second player over the running one. An animation loops only with `loop: true`. + - While it plays, the sequence keeps time, but its sends leave out the drivers the animation's lanes reach: groups are split into those drivers' own groups. + - When it ends (or `/sequences/overlay/stop`), those drivers get the sequence's current step again. With no sequence playing they get the zone's last played preset, else off. + - Firing another animation replaces the one playing. Drivers the new one doesn't reach go back. + - A sequence that ends while an animation plays clears the zone once the animation is over. - **Beat source.** Beats come from the mic's beat detector while audio timing is on (`/api/audio/start`) and it has a tempo lock. Otherwise they come from a clock at `audio_simulated_bpm`, whose beats also go to the drivers. Beats closer than 0.3 s (200 BPM) are dropped. -Status: `{"active": false, "beat_readout": ""}` when idle, else `{active, advance_mode, sequence_id, zone_id, num_lanes, total_sequence_steps, lane0_current_step, lane0_lane_length, lane0_beat_in_step, lane0_beats_per_step, lane0_preset_id, lane0_preset_name, sequence_beat_at, sequence_beats_per_pass, sequence_loop_beat, beat_readout}`. +Status: `{"active": false, "beat_readout": "", overlay}` when idle, else `{active, advance_mode, sequence_id, zone_id, num_lanes, total_sequence_steps, lane0_current_step, lane0_lane_length, lane0_beat_in_step, lane0_beats_per_step, lane0_preset_id, lane0_preset_name, sequence_beat_at, sequence_beats_per_pass, sequence_loop_beat, beat_readout, lanes, overlay}`. +- `lanes`: one entry per playing lane, `{step (0-based), steps, mode, started, done, pos? (chase: the target index lit first), beat_in_step and beats_in_step (beat lanes) or elapsed_ms and duration_ms (time lanes)}`. +- `overlay`: `{active, pending, pending_id?}`, plus `{sequence_id, zone_id, drivers, progress (0–1), lanes}` while an animation plays. ## Audio — `/api/audio` @@ -287,6 +317,9 @@ Pushes led-driver `.py` files to ESP-NOW drivers, one file per request; `tools/d The UI, `/presets/push`, `/ws` and the controller itself send drivers **v1 messages**: JSON objects with `"v": "1"`. The controller does not send them as they are: - **Binary envelope.** A message that only carries presets, a select, brightness, `save` and `groups` goes out as a compact binary envelope (`main/envelope.c`, mirrored by `test/binary_envelope.py`). + - Version 2 is the usual frame. + - A frame whose presets use `fade` or `beat_time` is version 3: each preset record is followed by `[ext_len][key, len, value]…`, with key 1 the fade ms (u16) and key 2 the beat timing (u8). + - Drivers skip keys they don't know. Drivers without v3 support ignore v3 frames, so they miss only presets that use the new fields. - **JSON.** Any other message goes as JSON with short keys, split across several messages when it does not fit one ESP-NOW frame. A preset with a background colour (`bg`) is one of these: the envelope has no field for it. - **Delivery.** Broadcasts are never acked (envelopes go out twice, JSON once); drivers that miss a state change notice from the state digest and ask to be brought up to date. Messages with `targets` are unicast to each MAC, acked and retried. @@ -300,12 +333,12 @@ The UI, `/presets/push`, `/ws` and the controller itself send drivers **v1 messa | `set_groups` | `sg` | With `groups`: replace the driver's group list | | `save` | `sv` | Persist presets, brightness or config on the driver | | `default` | `df` | Startup preset id | -| `device_config` | `dc` | `name`, `num_leds`, `color_order`, `startup_mode`, `max_brightness`, `reversed` | +| `device_config` | `dc` | `name`, `num_leds`, `color_order`, `startup_mode`, `max_brightness`, `reversed`, `place` ([x0, y0, x1, y1] in rig coordinates, see `/devices/layout`; `null` clears it), `usb_log` (per-beat USB log; on reboot). Drivers print nothing over USB unless their `debug` or `usb_log` setting is on: on the ESP32-C3, unread USB output blocks the driver | | `clear_presets` | `cp` | Drop stored presets | | `manifest` | `mf` | Pattern files to fetch (Wi-Fi drivers) | | `b` | | Driver brightness 0–255 | -Preset fields: `p`/`pattern`, `c`/`colors` (`#RRGGBB`), `d`/`delay` (ms, default 100), `b`/`brightness` (0–255), `a`/`auto` (`true` runs freely, `false` steps once per beat), `bg`/`background`, `n1`–`n6` (pattern parameters; `n5` is reverse, `n6` the style mode). Other fields (`name`, `manual_beat_n`, …) are dropped on the way out. +Preset fields: `p`/`pattern`, `c`/`colors` (`#RRGGBB`), `d`/`delay` (ms, default 100), `b`/`brightness` (0–255), `a`/`auto` (`true` runs freely, `false` steps once per beat), `bg`/`background`, `n1`–`n6` (pattern parameters; `n5` is reverse, `n6` the style mode), `f`/`fade`, `tb`/`beat_time`. Other fields (`name`, `manual_beat_n`, …) are dropped on the way out. Sending `select` again with the same preset is a beat for that preset. Selecting `off` resets the step, so the next select starts every driver at step 0 unless a step is given. @@ -314,7 +347,8 @@ Sending `select` again with the same preset is a beat for that preset. Selecting A driver keeps a list of group ids and applies a group command (or a message with `groups`) only when it holds one of them. The controller sends each ESP-NOW driver its list whenever groups, zones or devices change, when the driver boots, and when it asks to be brought up to date. A driver that has failed three messages in a row gets these updates at most every 30 s until it answers again. The list holds: - every group (`group` collection) whose `devices` lists the driver's MAC, by group id; -- `z` for every zone whose `names` lists the driver's name (zones from before groups). +- `z` for every zone whose `names` lists the driver's name (zones from before groups); +- its own group `d` (12 lowercase hex digits), always first, so a sequence target can name a single driver. It doesn't change any other membership. A zone plays to its `group_ids` and, when it names drivers, to `z`. A message with `groups` that fits the envelope goes out as one group command per group. diff --git a/docs/BENCH_TEST.md b/docs/BENCH_TEST.md index 5f957d4..50edfc7 100644 --- a/docs/BENCH_TEST.md +++ b/docs/BENCH_TEST.md @@ -15,7 +15,7 @@ This walks one ESP32-S3 controller (W5500 Ethernet, INMP441 mic) and one `led-dr docker run --rm -v "$PWD":/project -w /project espressif/idf:v5.5.5 idf.py menuconfig ``` -then *ESP-NOW config → Wi-Fi channel*. Alternatively set `CONFIG_ESPNOW_CHANNEL=` in `sdkconfig.defaults` (delete `sdkconfig` after, so it is regenerated). To use the beat marker pin, also set `CONFIG_BEAT_TEST_GPIO` (for example 4) in the same menu. +then *ESP-NOW config → Wi-Fi channel*. Alternatively set `CONFIG_ESPNOW_CHANNEL=` in `sdkconfig.defaults` (delete `sdkconfig` after, so it is regenerated). To use the beat marker pin, also set `CONFIG_BEAT_TEST_GPIO` (for example 44) in the same menu. ## 1. Build and flash the controller @@ -197,15 +197,15 @@ Close any serial monitor first (only one program can hold a port). Do not run lo ### True latency with a logic analyzer -For a measurement that does not depend on USB. Marker pins are set up already: the controller toggles **GPIO4 (D3)** when the first copy of each beat goes out (`CONFIG_BEAT_TEST_GPIO=4`, in `sdkconfig.defaults`) and the driver toggles **GPIO4 (D3)** when it fires (`"beat_pin": 4` in its `settings.json`). Both are XIAO ESP32S3 boards, where D3 is free. +For a measurement that does not depend on USB. Marker pins are set up already: the controller toggles **GPIO44 (D7)** when the first copy of each beat goes out (`CONFIG_BEAT_TEST_GPIO=44`, in `sdkconfig.defaults`) and the driver toggles **GPIO44 (D7)** when it fires (`"beat_pin": 44` in its `settings.json`). Both are XIAO ESP32S3 boards, where D7 is free. Wiring (3.3 V logic, any 2 or 3 channel analyzer): | analyzer | connect to | |---|---| | GND | GND on the controller **and** GND on the driver (one common ground) | -| channel 0 | controller **D3** | -| channel 1 | driver **D3** | +| channel 0 | controller **D7** | +| channel 1 | driver **D7** | | channel 2 (optional) | driver **D10** (GPIO9, the LED data line) to see when the LEDs really update | Steps: diff --git a/docs/help.md b/docs/help.md index 5e73168..bae0a07 100644 --- a/docs/help.md +++ b/docs/help.md @@ -106,7 +106,7 @@ The preset editor: ## Settings -**Settings** (Edit mode) holds the controller's name, the ESP-NOW channel (for reference: the controller's channel is set at build time), a **Wi-Fi access point** switch (on by default; network named after the hostname, with an optional WPA2 password, controller at `192.168.5.1`; holding the button on D7 at boot forces it on), its mDNS **hostname** and the **static IPs** it tries on the Ethernet link (DHCP when none answers). With no DHCP server, as on a cable straight to a PC, the controller is at `192.168.4.1`; give the PC a static IP such as `192.168.4.2`. Network changes apply after a restart. **Driver** in Settings sends a driver its name, LED count, colour order, startup mode, maximum brightness and direction over ESP-NOW, with no cable. **Update code** there sends a driver, or all of them, the led-driver code built into the controller. `led-cli` on a computer edits a driver's whole `settings.json` over USB. +**Settings** (Edit mode) holds the controller's name, the ESP-NOW channel (for reference: the controller's channel is set at build time), a **Wi-Fi access point** switch (on by default; network named after the hostname, with an optional WPA2 password, controller at `192.168.5.1`; holding the button on D3 at boot forces it on), its mDNS **hostname** and the **static IPs** it tries on the Ethernet link (DHCP when none answers). With no DHCP server, as on a cable straight to a PC, the controller is at `192.168.4.1`; give the PC a static IP such as `192.168.4.2`. Network changes apply after a restart. **Driver** in Settings sends a driver its name, LED count, colour order, startup mode, maximum brightness and direction over ESP-NOW, with no cable. **Update code** there sends a driver, or all of them, the led-driver code built into the controller. `led-cli` on a computer edits a driver's whole `settings.json` over USB. ![Schematic: narrow layout with Menu and the same header actions in a dropdown.](images/help/mobile-menu.svg) diff --git a/led-simulator b/led-simulator index 7ce56b6..4fc3345 160000 --- a/led-simulator +++ b/led-simulator @@ -1 +1 @@ -Subproject commit 7ce56b64df6d154d5d4cf103a9ab8b291e85c248 +Subproject commit 4fc3345fc9220b4ec5593f0a39cf175f3dbcfb22 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 2fd63c8..e306565 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,12 +1,22 @@ -idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c" "drv_ota.c" +if(IDF_TARGET STREQUAL "linux") + # Host build for led-simulator (sim-controller/): no Ethernet, Wi-Fi, mic or OTA. The sim + # component has app_main and stands in for main.c, mic.c and ota.c. + idf_component_register(SRCS "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "drv_ota.c" + "res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "ws.c" + INCLUDE_DIRS "." "../sim-controller/components/sim/include" + REQUIRES esp_http_server esp_timer esp_event json + PRIV_REQUIRES mbedtls lwip esp_app_format) +else() +idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "bridge.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" "ota.c" "drv_ota.c" "res_profile.c" "res_misc.c" "res_device.c" "res_group.c" "seqplay.c" "res_bundle.c" "audio_api.c" "patterns.c" "outputs.c" "ws.c" INCLUDE_DIRS "." REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash app_update esp_app_format esp_http_server esp_timer esp_driver_i2s esp_wifi esp_rom json esp_driver_gpio PRIV_REQUIRES littlefs mdns mbedtls lwip) +endif() # The gzipped web UI is embedded in the app image (run tools/build_web.sh first) -if(NOT EXISTS ${PROJECT_DIR}/www_dist/www.pack) +if(NOT EXISTS ${COMPONENT_DIR}/../www_dist/www.pack) message(FATAL_ERROR "www_dist/www.pack is missing: run tools/build_web.sh (make build does)") endif() -target_add_binary_data(${COMPONENT_LIB} ${PROJECT_DIR}/www_dist/www.pack BINARY) +target_add_binary_data(${COMPONENT_LIB} ${COMPONENT_DIR}/../www_dist/www.pack BINARY) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index f431385..9e15f70 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -106,12 +106,11 @@ endmenu menu "AP config" config AP_BUTTON_GPIO - int "AP button GPIO (XIAO D7), -1 for none" - default 44 + int "AP button GPIO (XIAO D3), -1 for none" + default 4 help - Push button to ground. Held while the controller boots, it turns + Active-low push button to ground (internal pull-up). Held while the controller boots, it turns the Wi-Fi access point on even when the ap_enabled setting is off. - GPIO44 is also UART0 RX; the button only pulls it low at boot. config AP_HOLD_MS int "Hold time at boot (ms)" diff --git a/main/api.c b/main/api.c index b5dc142..b45e197 100644 --- a/main/api.c +++ b/main/api.c @@ -404,6 +404,10 @@ static esp_err_t put_settings(httpd_req_t *req) !(cJSON_IsString(pw) && (!pw->valuestring[0] || (strlen(pw->valuestring) >= 8 && strlen(pw->valuestring) <= 63)))) { bad = "ap_password must be empty (open) or 8 to 63 characters"; } + const cJSON *br = cJSON_GetObjectItem(body, "bridge"); + if (!bad && br && !cJSON_IsNull(br) && !(cJSON_IsString(br) && strlen(br->valuestring) < 64)) { + bad = "bridge must be \"host:port\", empty or null"; + } const cJSON *sw = cJSON_GetObjectItem(body, "sequence_switch_wait"); if (!bad && sw && !cJSON_IsNull(sw) && !(cJSON_IsString(sw) && (!strcasecmp(sw->valuestring, "beat") || !strcasecmp(sw->valuestring, "downbeat")))) { diff --git a/main/bridge.c b/main/bridge.c new file mode 100644 index 0000000..e28d19a --- /dev/null +++ b/main/bridge.c @@ -0,0 +1,99 @@ +#include "bridge.h" + +#include +#include +#include +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "lwip/netdb.h" +#include "lwip/sockets.h" + +static const char *TAG = "bridge"; + +static int s_sock = -1; +static struct sockaddr_in s_peer; +static bridge_rx_cb_t s_cb; +static char s_addr[80]; + +static void rx_task(void *arg) +{ + (void)arg; + uint8_t buf[6 + 256]; + for (;;) { + int n = recv(s_sock, buf, sizeof(buf), 0); + if (n < 0) { + vTaskDelay(pdMS_TO_TICKS(100)); + continue; + } + if (n > 6 && s_cb) { + s_cb(buf, buf + 6, n - 6); + } + } +} + +esp_err_t bridge_start(const char *addr, bridge_rx_cb_t cb) +{ + if (s_sock >= 0 || !addr || !addr[0]) { + return ESP_ERR_INVALID_STATE; + } + char host[64]; + int port = BRIDGE_DEFAULT_PORT; + snprintf(host, sizeof(host), "%s", addr); + char *colon = strrchr(host, ':'); + if (colon) { + *colon = 0; + port = atoi(colon + 1); + } + if (port <= 0 || port > 65535 || !host[0]) { + ESP_LOGE(TAG, "bad bridge address %s", addr); + return ESP_ERR_INVALID_ARG; + } + struct addrinfo hints = { .ai_family = AF_INET, .ai_socktype = SOCK_DGRAM }, *res = NULL; + if (getaddrinfo(host, NULL, &hints, &res) != 0 || !res) { + ESP_LOGE(TAG, "cannot resolve %s", host); + return ESP_FAIL; + } + memcpy(&s_peer, res->ai_addr, sizeof(s_peer)); + freeaddrinfo(res); + s_peer.sin_port = htons(port); + + int s = socket(AF_INET, SOCK_DGRAM, 0); + if (s < 0) { + return ESP_FAIL; + } + // connect() fixes the peer, so only its datagrams are received + if (connect(s, (struct sockaddr *)&s_peer, sizeof(s_peer)) != 0) { + close(s); + return ESP_FAIL; + } + s_sock = s; + s_cb = cb; + snprintf(s_addr, sizeof(s_addr), "%s:%d", host, port); + xTaskCreate(rx_task, "bridge_rx", 4096, NULL, 6, NULL); + ESP_LOGW(TAG, "ESP-NOW frames go to the UDP bridge %s, not the radio", s_addr); + return ESP_OK; +} + +bool bridge_active(void) +{ + return s_sock >= 0; +} + +const char *bridge_addr(void) +{ + return s_sock >= 0 ? s_addr : NULL; +} + +esp_err_t bridge_send(const uint8_t *dst_mac, const void *data, size_t len) +{ + if (s_sock < 0 || len > 250) { + return ESP_ERR_INVALID_STATE; + } + uint8_t buf[6 + 250]; + memcpy(buf, dst_mac, 6); + memcpy(buf + 6, data, len); + // A refused datagram (the simulator is not running yet) is a lost frame, as on air + send(s_sock, buf, 6 + len, 0); + return ESP_OK; +} diff --git a/main/bridge.h b/main/bridge.h new file mode 100644 index 0000000..18dd4ae --- /dev/null +++ b/main/bridge.h @@ -0,0 +1,21 @@ +#pragma once + +// UDP bridge: carries the controller's ESP-NOW frames to a host that stands in for the radio, such as +// led-simulator (settings "bridge": "host:port"). Down: dst_mac(6) + packet. Up: src_mac(6) + packet. +// While it runs, nothing goes out over the air. + +#include +#include +#include +#include "esp_err.h" + +typedef void (*bridge_rx_cb_t)(const uint8_t *src_mac, const uint8_t *data, int len); + +#define BRIDGE_DEFAULT_PORT 8766 + +// addr "host" or "host:port". Starts the receive task; frames arrive on cb. +esp_err_t bridge_start(const char *addr, bridge_rx_cb_t cb); +bool bridge_active(void); +esp_err_t bridge_send(const uint8_t *dst_mac, const void *data, size_t len); +// "host:port" while active, else NULL +const char *bridge_addr(void); diff --git a/main/engine.c b/main/engine.c index 84496cf..4b2e026 100644 --- a/main/engine.c +++ b/main/engine.c @@ -59,7 +59,7 @@ static bool s_save; // append the CMD save byte (driver persists presets / br // drivers in that group apply them. JSON messages go as they are. static size_t wrap_wire(uint8_t *out, const uint8_t *frame, size_t len) { - if (len == 0 || frame[0] != 2) { // not a v2 envelope + if (len == 0 || (frame[0] != 2 && frame[0] != 3)) { // not a v2/v3 envelope (JSON goes as it is) memcpy(out, frame, len); return len; } @@ -257,6 +257,9 @@ static bool in_group(const uint8_t mac[6], const char *name, const char *gid) } char hex[13]; snprintf(hex, sizeof(hex), "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + if (gid[0] == 'd' && strlen(gid) == 13) { // a driver's own group, d + return !strcmp(gid + 1, hex); + } return mac_listed(cJSON_GetObjectItem(cJSON_GetObjectItem(store_coll("group"), gid), "devices"), hex); } @@ -355,6 +358,32 @@ static esp_err_t play_locked(const char *zone_id, const char *preset_id) return ret; } +const char *engine_zone_current(const char *zone_id) +{ + for (int i = 0; i < 16; i++) { + if (s_cur[i].zone[0] && !strcmp(s_cur[i].zone, zone_id)) { + return s_cur[i].preset; + } + } + return NULL; +} + +int engine_group_macs(const char *gid, char out[][13], int max) +{ + int n = 0; + const cJSON *d; + cJSON_ArrayForEach(d, store_coll("device")) + { + uint8_t mac[6]; + const cJSON *name = cJSON_GetObjectItem(d, "name"); + if (n < max && d->string && parse_mac(d->string, mac) && + in_group(mac, cJSON_IsString(name) ? name->valuestring : NULL, gid)) { + snprintf(out[n++], 13, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + } + } + return n; +} + esp_err_t engine_play(const char *zone_id, const char *preset_id) { store_lock(); @@ -493,8 +522,9 @@ static const char *const TOP_SHORT[][2] = { }; static const char *const PRESET_SHORT[][2] = { { "pattern", "p" }, { "colors", "c" }, { "delay", "d" }, { "brightness", "b" }, { "auto", "a" }, { "background", "bg" }, + { "fade", "f" }, { "beat_time", "tb" }, }; -static const char *const PRESET_KEYS[] = { "p", "c", "d", "b", "a", "bg", "n1", "n2", "n3", "n4", "n5", "n6" }; +static const char *const PRESET_KEYS[] = { "p", "c", "d", "b", "a", "bg", "n1", "n2", "n3", "n4", "n5", "n6", "f", "tb" }; static const char *short_key(const char *const map[][2], size_t n, const char *k) { @@ -758,17 +788,22 @@ static esp_err_t send_cmd(const uint8_t mac[6], const uint8_t *env, size_t len, return rel_send(mac, wire, len + 2, commit, 1); } -// GROUPS packet: the ids of every zone whose driver names include `name`. An empty list replaces -// whatever the driver stored, so it is only sent when allow_empty is set (a zone or device was edited); -// a driver that merely announced itself and is in no zone keeps the groups it already has. -// GROUPS packet for a driver: [4C][02][count] then per zone [len][zone id]. Returns its length; -// *count gets the number of zones. +// GROUPS packet: the driver's own group d (sequences address single drivers with it), then every +// group and zone it belongs to. A list with nothing but its own group replaces whatever the driver +// stored, so it is only sent when allow_empty is set (a zone or device was edited); a driver that merely +// announced itself and is in no zone keeps the groups it already has. +// Wire: [4C][02][count] then per id [len][id]. Returns its length; *count gets the number of groups and +// zones (not counting its own group). static size_t build_groups(uint8_t pkt[REL_FRAG_MAX], const uint8_t mac[6], const char *name, int *count) { size_t off = 3; *count = 0; const cJSON *g; char gid[GROUP_ID_MAX]; + // Its own group first, so a long list never cuts it off + pkt[off++] = 13; + off += snprintf((char *)pkt + off, REL_FRAG_MAX - off, "d%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], + mac[4], mac[5]); cJSON_ArrayForEach(g, store_coll("group")) { size_t gl = strlen(g->string); @@ -795,7 +830,8 @@ static size_t build_groups(uint8_t pkt[REL_FRAG_MAX], const uint8_t mac[6], cons } pkt[0] = 0x4C; pkt[1] = 0x02; // GROUPS - pkt[2] = *count; + pkt[2] = *count + 1; // *count leaves out the driver's own group + return off; } diff --git a/main/engine.h b/main/engine.h index cd6a233..e2220b0 100644 --- a/main/engine.h +++ b/main/engine.h @@ -19,6 +19,12 @@ int engine_zone_targets(const char *zone_id, char out[][GROUP_ID_MAX], int max); // NULL when there is no such preset. Call with the store locked. cJSON *engine_preset_payload(const char *preset_id); +// Drivers in group gid (a group id, "z" or a driver's own "d"), as 12-hex MACs from the +// device list. Call with the store locked. +int engine_group_macs(const char *gid, char out[][13], int max); +// The preset a zone last played (engine_play / engine_stop), or NULL. Call with the store locked. +const char *engine_zone_current(const char *zone_id); + // Select a preset on every driver in the zone's groups (broadcast, sent twice) esp_err_t engine_play(const char *zone_id, const char *preset_id); // Turn the zone's drivers off diff --git a/main/envelope.c b/main/envelope.c index 24785ee..9b896c6 100644 --- a/main/envelope.c +++ b/main/envelope.c @@ -7,6 +7,7 @@ void env_blob_init(env_blob_t *b) { b->data[0] = 0; b->len = 1; + b->v3 = false; } static bool put(env_blob_t *b, const void *src, size_t n) @@ -112,6 +113,15 @@ bool env_add_preset(env_blob_t *b, const char *name, const cJSON *p) if (nl > 250 || pl > 250 || ncol > 255) { return false; } + const cJSON *fade = get2(p, "f", "fade"), *tb = get2(p, "tb", "beat_time"); + int fade_ms = clamp(num_or(fade, 0), 0, 10000); + bool beat = truthy(tb); + bool ext = fade_ms > 0 || beat; + if (b->data[0] == 0) { + b->v3 = ext; + } else if (b->v3 != ext) { + return false; + } uint8_t rgbs[255][3]; int nvalid = 0; @@ -136,6 +146,16 @@ bool env_add_preset(env_blob_t *b, const char *name, const cJSON *p) char key[4] = { 'n', '0' + i, 0 }; ok = put_le16(b, (uint16_t)(int16_t)clamp(num_or(cJSON_GetObjectItem(p, key), 0), -32768, 32767)); } + if (ok && b->v3) { + uint8_t ext_len = (fade_ms ? 4 : 0) + (beat ? 3 : 0); + ok = put_u8(b, ext_len); + if (ok && fade_ms) { + ok = put_u8(b, ENV_EXT_FADE) && put_u8(b, 2) && put_le16(b, fade_ms); + } + if (ok && beat) { + ok = put_u8(b, ENV_EXT_BEAT) && put_u8(b, 1) && put_u8(b, 1); + } + } if (!ok) { b->len = save_len; b->data[0] = save_count; @@ -172,7 +192,7 @@ size_t env_frame(uint8_t *out, int brightness, const env_blob_t *presets, const if (brightness >= 0) { wire = brightness > 255 ? 127 : (brightness * 127 + 127) / 255; // 0-127 scales to 0-255 } - out[0] = 2; + out[0] = lp && presets->v3 ? 3 : 2; out[1] = wire; out[2] = lp; out[3] = ls; diff --git a/main/envelope.h b/main/envelope.h index 1f4c84e..91a31c1 100644 --- a/main/envelope.h +++ b/main/envelope.h @@ -14,11 +14,18 @@ typedef struct { uint8_t data[ENV_BLOB_MAX + 1]; // data[0] is the entry count size_t len; + bool v3; // preset records carry an extension block (fade, beat timing): a v3 frame } env_blob_t; +// Envelope v3: v2, except that each preset record is followed by [ext_len][key, len, value]... Keys: +#define ENV_EXT_FADE 1 // u16: fade-in ms when the preset is selected +#define ENV_EXT_BEAT 2 // u8: 1 = durations are quarter beats, not ms + void env_blob_init(env_blob_t *b); -// Append one preset (short keys p,c,d,b,a,n1..n6 or long pattern/colors/...); false if it does not fit +// Append one preset (short keys p,c,d,b,a,n1..n6,f,tb or long pattern/colors/.../fade/beat_time); false if +// it does not fit. A preset with a fade or beat timing makes the blob v3; v2 and v3 records never share a +// blob, so mixing them also returns false (callers send what they have and start a new blob). bool env_add_preset(env_blob_t *b, const char *name, const cJSON *preset); // Append one select entry; step < 0 means none bool env_add_select(env_blob_t *b, const char *device, const char *preset, int step); diff --git a/main/espnow_tx.c b/main/espnow_tx.c index 6ec758b..9112b17 100644 --- a/main/espnow_tx.c +++ b/main/espnow_tx.c @@ -4,13 +4,18 @@ #include "esp_check.h" #include "esp_event.h" #include "esp_log.h" -#include "esp_netif.h" -#include "esp_now.h" #include "esp_rom_sys.h" #include "esp_random.h" #include "esp_timer.h" #include "bench.h" +#include "bridge.h" +#if !CONFIG_IDF_TARGET_LINUX +#include "esp_netif.h" +#include "esp_now.h" #include "esp_wifi.h" +#include "esp_mac.h" +#include "esp_wifi_ap_get_sta_list.h" +#endif #include "freertos/FreeRTOS.h" #include "freertos/queue.h" #include "freertos/semphr.h" @@ -32,7 +37,7 @@ typedef struct { static QueueHandle_t s_bulk_q; static SemaphoreHandle_t s_bulk_done; -static volatile esp_now_send_status_t s_bulk_status; +static volatile bool s_bulk_ok; static volatile bool s_bulk_inflight; static volatile int64_t s_last_beat_us; static esp_timer_handle_t s_copy_timer; @@ -44,6 +49,26 @@ static bool s_have_peer; static espnow_stats_t s_stats; static volatile int s_loss_pct; static volatile bool s_loss_beats = true; +static espnow_rx_cb_t s_rx_cb; +static char s_bridge[80]; + +void espnow_tx_set_bridge(const char *addr) +{ + snprintf(s_bridge, sizeof(s_bridge), "%s", addr ? addr : ""); +} + +// One frame onto the air, or to the UDP bridge while one is set +static esp_err_t radio_send(const uint8_t *dst, const void *data, size_t len) +{ + if (bridge_active()) { + return bridge_send(dst, data, len); + } +#if CONFIG_IDF_TARGET_LINUX + return ESP_ERR_NOT_SUPPORTED; +#else + return esp_now_send(dst, data, len); +#endif +} bool espnow_tx_drop(void) { @@ -64,17 +89,39 @@ int espnow_tx_loss(bool *include_beats) return s_loss_pct; } +#if !CONFIG_IDF_TARGET_LINUX static void IRAM_ATTR send_cb(const esp_now_send_info_t *info, esp_now_send_status_t status) { (void)info; if (s_bulk_inflight) { - s_bulk_status = status; + s_bulk_ok = status == ESP_NOW_SEND_SUCCESS; BaseType_t woken = pdFALSE; xSemaphoreGiveFromISR(s_bulk_done, &woken); portYIELD_FROM_ISR(woken); } } +static void recv_cb(const esp_now_recv_info_t *info, const uint8_t *data, int len) +{ + if (s_rx_cb) { + s_rx_cb(info->src_addr, data, len); + } +} +#endif + +static void bridge_rx(const uint8_t *src, const uint8_t *data, int len) +{ + if (s_rx_cb) { + s_rx_cb(src, data, len); + } +} + +esp_err_t espnow_tx_set_rx_cb(espnow_rx_cb_t cb) +{ + s_rx_cb = cb; + return ESP_OK; +} + // Each copy carries a fire_in_ms reduced by the time already spent, so every // copy points at the same instant. static void send_beat_copy(void *arg) @@ -87,7 +134,7 @@ static void send_beat_copy(void *arg) if (s_loss_beats && espnow_tx_drop()) { s_stats.sim_dropped++; // lossy-link test: this copy never reaches the radio - } else if (esp_now_send(BROADCAST, (const uint8_t *)&s_pending, sizeof(s_pending)) != ESP_OK) { + } else if (radio_send(BROADCAST, &s_pending, sizeof(s_pending)) != ESP_OK) { s_stats.beat_send_fail++; } s_last_beat_us = esp_timer_get_time(); @@ -134,6 +181,12 @@ static void on_beat(const beat_info_t *info, float bpm, bool downbeat) static esp_err_t select_peer(const uint8_t *mac) { + if (bridge_active()) { + return ESP_OK; // no peer table behind the bridge + } +#if CONFIG_IDF_TARGET_LINUX + return ESP_ERR_NOT_SUPPORTED; +#else if (s_have_peer && memcmp(s_cur_peer, mac, 6) == 0) { return ESP_OK; } @@ -149,6 +202,7 @@ static esp_err_t select_peer(const uint8_t *mac) memcpy(s_cur_peer, mac, 6); s_have_peer = true; return ESP_OK; +#endif } static void bulk_task(void *arg) @@ -180,9 +234,11 @@ static void bulk_task(void *arg) if (espnow_tx_drop()) { s_stats.sim_dropped++; // lossy-link test: pretend the radio sent it ok = true; - } else if (esp_now_send(dst, b.data, b.len) == ESP_OK && + } else if (bridge_active()) { + ok = bridge_send(dst, b.data, b.len) == ESP_OK; // no MAC-layer ack: rel.c's acks retry + } else if (radio_send(dst, b.data, b.len) == ESP_OK && xSemaphoreTake(s_bulk_done, pdMS_TO_TICKS(30)) == pdTRUE) { - ok = s_bulk_status == ESP_NOW_SEND_SUCCESS || b.broadcast; + ok = s_bulk_ok || b.broadcast; } s_bulk_inflight = false; } @@ -210,6 +266,7 @@ void espnow_tx_stats(espnow_stats_t *out) *out = s_stats; } +#if !CONFIG_IDF_TARGET_LINUX // A link-local IPv6 address on the AP lets the mDNS responder answer the AAAA half of a browser's // lookup; without it the browser waits about a second for an answer that never comes. static esp_netif_t *s_ap_netif; @@ -220,7 +277,22 @@ static void ap_event(void *arg, esp_event_base_t base, int32_t id, void *data) } } -esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password) +int espnow_tx_ap_clients(char *out, size_t n) +{ + int len = snprintf(out, n, "["); + wifi_sta_list_t sl; + wifi_sta_mac_ip_list_t ml; + if (s_ap_netif && esp_wifi_ap_get_sta_list(&sl) == ESP_OK && esp_wifi_ap_get_sta_list_with_ip(&sl, &ml) == ESP_OK) { + for (int i = 0; i < ml.num && len < (int)n - 64; i++) { + len += snprintf(out + len, n - len, "%s{\"mac\":\"" MACSTR "\",\"ip\":\"" IPSTR "\"}", i ? "," : "", + MAC2STR(ml.sta[i].mac), IP2STR(&ml.sta[i].ip)); + } + } + len += snprintf(out + len, n - len, "]"); + return len; +} + +static esp_err_t radio_init(const char *ap_ssid, const char *ap_password) { wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_RETURN_ON_ERROR(esp_wifi_init(&cfg), TAG, "wifi init"); @@ -264,6 +336,32 @@ esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password) esp_now_peer_info_t bc = { .channel = 0, .ifidx = WIFI_IF_STA, .encrypt = false }; memcpy(bc.peer_addr, BROADCAST, 6); ESP_RETURN_ON_ERROR(esp_now_add_peer(&bc), TAG, "broadcast peer"); + ESP_RETURN_ON_ERROR(esp_now_register_recv_cb(recv_cb), TAG, "recv cb"); + ESP_LOGI(TAG, "ESP-NOW up on channel %d, %d beat copies", CONFIG_ESPNOW_CHANNEL, CONFIG_BEAT_TX_COPIES); + return ESP_OK; +} +#endif + +#if CONFIG_IDF_TARGET_LINUX +int espnow_tx_ap_clients(char *out, size_t n) +{ + return snprintf(out, n, "[]"); +} +#endif + +esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password) +{ +#if CONFIG_IDF_TARGET_LINUX + (void)ap_ssid; + (void)ap_password; + ESP_RETURN_ON_FALSE(s_bridge[0], ESP_ERR_INVALID_STATE, TAG, "the host build needs a bridge address"); +#else + // The radio still comes up behind a bridge: the access point serves the web UI + ESP_RETURN_ON_ERROR(radio_init(ap_ssid, ap_password), TAG, "radio"); +#endif + if (s_bridge[0]) { + ESP_RETURN_ON_ERROR(bridge_start(s_bridge, bridge_rx), TAG, "bridge"); + } s_bulk_q = xQueueCreate(BULK_QUEUE_LEN, sizeof(bulk_t)); s_bulk_done = xSemaphoreCreateBinary(); @@ -274,6 +372,5 @@ esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password) xTaskCreate(bulk_task, "espnow_bulk", 4096, NULL, 5, NULL); beat_set_callback(on_beat); - ESP_LOGI(TAG, "ESP-NOW up on channel %d, %d beat copies", CONFIG_ESPNOW_CHANNEL, CONFIG_BEAT_TX_COPIES); return ESP_OK; } diff --git a/main/espnow_tx.h b/main/espnow_tx.h index 7d0ecd4..29427c2 100644 --- a/main/espnow_tx.h +++ b/main/espnow_tx.h @@ -35,6 +35,17 @@ typedef struct { // ap_password (8-63 chars), open when it is NULL or empty. esp_err_t espnow_tx_init(const char *ap_ssid, const char *ap_password); +// JSON array [{"mac","ip"}] of stations on the access point (DHCP leases); "[]" when it is off. Returns its length. +int espnow_tx_ap_clients(char *out, size_t n); + +// Before espnow_tx_init: send every frame to this UDP bridge ("host:port", see bridge.h) instead of +// the radio. NULL or "" keeps the radio. +void espnow_tx_set_bridge(const char *addr); + +// Frames received from drivers (radio or bridge). Runs in the Wi-Fi or bridge task. +typedef void (*espnow_rx_cb_t)(const uint8_t *src_mac, const uint8_t *data, int len); +esp_err_t espnow_tx_set_rx_cb(espnow_rx_cb_t cb); + // Queue a bulk frame (<= 240 bytes). mac NULL broadcasts. Unicast frames are // retried; they never go out within the hold-off window after a beat. esp_err_t espnow_tx_send(const uint8_t *mac, const void *data, size_t len); diff --git a/main/idf_component.yml b/main/idf_component.yml index 24c8f0d..2c8036e 100644 --- a/main/idf_component.yml +++ b/main/idf_component.yml @@ -1,3 +1,9 @@ dependencies: - joltwallet/littlefs: "^1.19.0" - espressif/mdns: "^1.4.0" + joltwallet/littlefs: + version: "^1.19.0" + rules: + - if: "target != linux" + espressif/mdns: + version: "^1.4.0" + rules: + - if: "target != linux" diff --git a/main/main.c b/main/main.c index 43cd415..f24dd5b 100644 --- a/main/main.c +++ b/main/main.c @@ -8,6 +8,7 @@ #include "esp_log.h" #include "esp_check.h" #include "esp_event.h" +#include #include "esp_netif.h" #include "esp_eth.h" #include "esp_mac.h" @@ -71,7 +72,11 @@ static esp_err_t status_get_handler(httpd_req_t *req) rel_stats_t rs; esp_netif_ip_info_t ip_info = { 0 }; uint8_t mac[6] = { 0 }; - char ip[16], macs[18], buf[1024]; + char ip[16], macs[18], mask[16], gw[16], clients[400]; + char *buf = malloc(2048); + if (!buf) { + return httpd_resp_send_500(req); + } beat_get_info(&bi); mic_get_level(&level); espnow_tx_stats(&es); @@ -82,15 +87,18 @@ static esp_err_t status_get_handler(httpd_req_t *req) } snprintf(ip, sizeof(ip), IPSTR, IP2STR(&ip_info.ip)); snprintf(macs, sizeof(macs), MACSTR, MAC2STR(mac)); - snprintf(buf, sizeof(buf), - "{\"ap\":%s,\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"reset_reason\":\"%s\",\"free_heap\":%lu,\"free_internal\":%lu," + snprintf(mask, sizeof(mask), IPSTR, IP2STR(&ip_info.netmask)); + snprintf(gw, sizeof(gw), IPSTR, IP2STR(&ip_info.gw)); + espnow_tx_ap_clients(clients, sizeof(clients)); + snprintf(buf, 2048, + "{\"ap\":%s,\"eth\":{\"ip\":\"%s\",\"mask\":\"%s\",\"gw\":\"%s\",\"up\":%s},\"ap_ip\":\"192.168.5.1\",\"ap_clients\":%s,\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"reset_reason\":\"%s\",\"free_heap\":%lu,\"free_internal\":%lu," "\"mic_rms_dbfs\":%.1f,\"mic_peak_dbfs\":%.1f,\"mic_spl_db\":%.1f," "\"bpm\":%.1f,\"beat_confidence\":%.2f,\"beat_locked\":%s,\"beats\":%lu,\"onsets\":%lu," "\"meter\":%d,\"bar_beat\":%d,\"bar_sure\":%s,\"downbeats\":%lu," "\"espnow\":{\"beats_sent\":%lu,\"beat_send_fail\":%lu,\"bulk_sent\":%lu," "\"bulk_retries\":%lu,\"bulk_failed\":%lu,\"sim_dropped\":%lu}," "\"link\":{\"ver\":%u,\"acked\":%lu,\"ack_timeouts\":%lu,\"retransmits\":%lu,\"resyncs\":%lu,\"digests\":%lu}}", - s_ap_on ? "true" : "false", ip, macs, (long long)(esp_timer_get_time() / 1000000), reset_reason(), (unsigned long)esp_get_free_heap_size(), + s_ap_on ? "true" : "false", ip, mask, gw, ip_info.ip.addr ? "true" : "false", clients, ip, macs, (long long)(esp_timer_get_time() / 1000000), reset_reason(), (unsigned long)esp_get_free_heap_size(), (unsigned long)heap_caps_get_free_size(MALLOC_CAP_INTERNAL), level.rms_dbfs, level.peak_dbfs, level.spl_db, bi.bpm, bi.confidence, bi.locked ? "true" : "false", (unsigned long)bi.beat_count, (unsigned long)bi.onset_count, bi.meter, bi.bar_beat, bi.bar_sure ? "true" : "false", @@ -100,7 +108,9 @@ static esp_err_t status_get_handler(httpd_req_t *req) (unsigned long)rs.acked, (unsigned long)rs.ack_timeouts, (unsigned long)rs.retransmits, (unsigned long)rs.resyncs, (unsigned long)rs.digests); httpd_resp_set_type(req, "application/json"); - return httpd_resp_sendstr(req, buf); + esp_err_t err = httpd_resp_sendstr(req, buf); + free(buf); + return err; } #define DEFAULT_HOSTNAME "led-controller" @@ -446,6 +456,7 @@ void app_main(void) bool held = ap_button_held(); s_ap_on = settings_bool("ap_enabled", true) || held; ESP_LOGI(TAG, "AP %s%s", s_ap_on ? "on" : "off", held ? " (button held)" : ""); + espnow_tx_set_bridge(settings_str("bridge")); // led-simulator stands in for the radio ESP_ERROR_CHECK(espnow_tx_init(s_ap_on ? (host ? host : DEFAULT_HOSTNAME) : NULL, settings_str("ap_password"))); // Ethernet is optional: without it the web UI is still served over the AP diff --git a/main/patterns.c b/main/patterns.c index a41b6f9..391e70e 100644 --- a/main/patterns.c +++ b/main/patterns.c @@ -139,13 +139,21 @@ static esp_err_t write_file(const char *base, const char *code, size_t len) return ok ? ESP_OK : ESP_FAIL; } -// Patterns the driver has built in (no .py file); the seed presets use them +// Definitions used when the pattern collection has none: the driver's built-ins (no .py file, the seed +// presets use them) and the animation patterns (field patterns span the rig, see led-driver/src/rig.py) static const struct { const char *name, *json; } BUILTIN[] = { { "on", "{\"min_delay\":10,\"max_delay\":10000,\"max_colors\":1}" }, { "off", "{\"min_delay\":10,\"max_delay\":10000,\"max_colors\":0}" }, { "rainbow", "{\"n1\":\"Step Rate\",\"min_delay\":10,\"max_delay\":10000,\"max_colors\":0}" }, + { "hit", "{\"n1\":\"Attack (ms / ¼ beats)\",\"n2\":\"Hold\",\"n3\":\"Decay\",\"n4\":\"End: 0 background, 1 stay lit\",\"min_delay\":0,\"max_delay\":10000,\"max_colors\":10}" }, + { "keyframes", "{\"n1\":\"Easing: 0 linear, 1 in-out, 2 step, 3 in, 4 out\",\"n2\":\"End: 0 last colour, 1 background, 2 off\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" }, + { "sweep", "{\"n1\":\"Direction (degrees)\",\"n2\":\"Band width (‰ of rig)\",\"n3\":\"Trail (‰)\",\"n4\":\"1 = leave lit (wipe)\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" }, + { "ripple", "{\"n1\":\"Ring width (‰)\",\"n2\":\"Rings at once\",\"n3\":\"1 = from the edge in\",\"n4\":\"Fade with distance (‰)\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" }, + { "spin", "{\"n1\":\"Arms\",\"n2\":\"Arm width (degrees)\",\"n3\":\"Tail (degrees)\",\"n4\":\"1 = clockwise\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" }, + { "grow", "{\"n1\":\"From: 0 centre, 1 edges, 2 round, 3 across\",\"n2\":\"Then: 0 stay, 1 empty back, 2 empty on, 3 off\",\"n3\":\"Direction (degrees, from = 3)\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" }, + { "field_wave", "{\"n1\":\"Direction (degrees)\",\"n2\":\"Wavelength (‰ of rig)\",\"n3\":\"Kind: 0 linear, 1 rings, 2 swirl, 3 plasma\",\"min_delay\":10,\"max_delay\":60000,\"max_colors\":10}" }, }; // Stored metadata plus the built-ins and {} for every other pattern file, as {name: {...}} diff --git a/main/rel.c b/main/rel.c index 16c896c..1bb8b81 100644 --- a/main/rel.c +++ b/main/rel.c @@ -4,7 +4,6 @@ #include #include "esp_check.h" #include "esp_log.h" -#include "esp_now.h" #include "esp_random.h" #include "esp_timer.h" #include "bench.h" @@ -292,7 +291,7 @@ void rel_set_announce_cb(rel_announce_cb_t cb) s_announce_cb = cb; } -static void recv_cb(const esp_now_recv_info_t *info, const uint8_t *data, int len) +static void recv_cb(const uint8_t *src_mac, const uint8_t *data, int len) { int64_t now = esp_timer_get_time(); // before anything else: echoes are timing measurements if (len < 3 || len > (int)sizeof(((rx_t *)0)->data)) { @@ -311,11 +310,11 @@ static void recv_cb(const esp_now_recv_info_t *info, const uint8_t *data, int le return; } if (reply && data[1] == M_ECHO) { - bench_on_echo(info->src_addr, data[2] | (data[3] << 8), now); + bench_on_echo(src_mac, data[2] | (data[3] << 8), now); return; } rx_t rx = { .len = len }; - memcpy(rx.mac, info->src_addr, 6); + memcpy(rx.mac, src_mac, 6); memcpy(rx.data, data, len); xQueueSend(s_rx, &rx, 0); // dropping an ack only costs a retransmit } @@ -445,7 +444,7 @@ esp_err_t rel_init(void) ESP_RETURN_ON_FALSE(s_lock && s_rx, ESP_ERR_NO_MEM, TAG, "alloc"); s_seq = esp_random() & 0xFFFF; // drivers remember recent (mac, seq); a restart must not reuse them s_ver = (esp_random() % 0x7FFE) + 1; // new epoch per boot, so a rebooted controller never matches stale drivers - ESP_RETURN_ON_ERROR(esp_now_register_recv_cb(recv_cb), TAG, "recv cb"); + ESP_RETURN_ON_ERROR(espnow_tx_set_rx_cb(recv_cb), TAG, "recv cb"); xTaskCreate(rel_task, "rel", 4096, NULL, 6, NULL); ESP_LOGI(TAG, "reliable link up, state version %u", s_ver); return ESP_OK; diff --git a/main/res_bundle.c b/main/res_bundle.c index bbb4366..bc70b7e 100644 --- a/main/res_bundle.c +++ b/main/res_bundle.c @@ -1,6 +1,7 @@ // Import/export bundles: main's util/profile_bundle.py. A profile bundle carries the profile, its // palette colours, zones, the presets they use and their sequences; preset and sequence bundles carry -// one record (a sequence with its presets). Imports get new ids and belong to the importing profile. +// one record (a sequence with its presets). Animations are sequences with kind "animation"; a profile +// bundle carries the ones its zones and sequence steps fire. Imports get new ids and belong to the importing profile. #include #include @@ -50,6 +51,54 @@ static void sequence_preset_ids(const cJSON *seq, cJSON *set) } } +// Animations (sequences played once) that steps of seq fire +static void sequence_animation_ids(const cJSON *seq, cJSON *set) +{ + const cJSON *lane, *step; + cJSON_ArrayForEach(lane, cJSON_GetObjectItem(seq, "lanes")) + { + cJSON_ArrayForEach(step, lane) + { + add_ref(set, cJSON_GetObjectItem(step, "animation_id")); + } + } +} + +// Point a list field (or each step's animation_id) at the new sequence ids +static void remap_ids(cJSON *rec, const char *key, const cJSON *smap) +{ + cJSON *list = cJSON_GetObjectItem(rec, key); + if (!cJSON_IsArray(list)) { + return; + } + cJSON *mapped = cJSON_CreateArray(), *it; + cJSON_ArrayForEach(it, list) + { + char k[24]; + if (api_id(it, k, sizeof(k))) { + const cJSON *ns = cJSON_GetObjectItem(smap, k); + cJSON_AddItemToArray(mapped, cJSON_CreateString(ns ? ns->valuestring : k)); + } + } + api_set(rec, key, mapped); +} + +static void remap_animation_steps(cJSON *seq, const cJSON *smap) +{ + cJSON *lane, *step; + cJSON_ArrayForEach(lane, cJSON_GetObjectItem(seq, "lanes")) + { + cJSON_ArrayForEach(step, lane) + { + char k[24]; + const cJSON *ns = api_id(cJSON_GetObjectItem(step, "animation_id"), k, sizeof(k)) ? cJSON_GetObjectItem(smap, k) : NULL; + if (ns) { + api_set(step, "animation_id", cJSON_CreateString(ns->valuestring)); + } + } + } +} + static bool owned_by(const cJSON *rec, const char *profile) { char pid[24]; @@ -107,6 +156,7 @@ static cJSON *export_profile(const char *id) cJSON_AddItemToObject(zones, key, cJSON_Duplicate(z, true)); zone_preset_ids(z, pset); add_ref(sset, cJSON_GetObjectItem(z, "sequence_ids")); + add_ref(sset, cJSON_GetObjectItem(z, "animation_ids")); } const cJSON *it; cJSON_ArrayForEach(it, sset) @@ -115,6 +165,7 @@ static cJSON *export_profile(const char *id) if (s && owned_by(s, id)) { cJSON_AddItemToObject(seqs, it->string, cJSON_Duplicate(s, true)); sequence_preset_ids(s, pset); + sequence_animation_ids(s, sset); // appended, so this loop reaches them too } } cJSON_ArrayForEach(it, pset) @@ -249,6 +300,11 @@ static const char *import_profile(const cJSON *b, const char *name, char *pid, s api_add("sequence", copy, nid, sizeof(nid)); cJSON_AddStringToObject(smap, s->string, nid); } + cJSON *imported; + cJSON_ArrayForEach(imported, smap) + { + remap_animation_steps(cJSON_GetObjectItem(store_coll("sequence"), imported->valuestring), smap); + } const cJSON *zones_in = cJSON_GetObjectItem(b, "zones"); const cJSON *order = cJSON_GetObjectItem(src, "zones"); @@ -293,19 +349,8 @@ static const char *import_profile(const cJSON *b, const char *name, char *pid, s if (to) { api_set(copy, "default_preset", cJSON_Duplicate(to, false)); } - const cJSON *sids = cJSON_GetObjectItem(z, "sequence_ids"); - if (cJSON_IsArray(sids)) { - cJSON *mapped = cJSON_CreateArray(), *sid; - cJSON_ArrayForEach(sid, sids) - { - char k[24]; - if (api_id(sid, k, sizeof(k))) { - const cJSON *ns = cJSON_GetObjectItem(smap, k); - cJSON_AddItemToArray(mapped, cJSON_CreateString(ns ? ns->valuestring : k)); - } - } - api_set(copy, "sequence_ids", mapped); - } + remap_ids(copy, "sequence_ids", smap); + remap_ids(copy, "animation_ids", smap); char nid[16]; api_add("zone", copy, nid, sizeof(nid)); cJSON_AddItemToArray(zone_ids, cJSON_CreateString(nid)); diff --git a/main/res_device.c b/main/res_device.c index 73384b2..c3905ac 100644 --- a/main/res_device.c +++ b/main/res_device.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "api_util.h" #include "engine.h" @@ -626,6 +627,133 @@ static esp_err_t device_status(httpd_req_t *req) return api_json(req, "200 OK", o, true); } +/* ------------------------------------------------------------- rig layout */ + +static bool placement_of(const cJSON *pl, double v[4]) +{ + static const char *const K[4] = { "x0", "y0", "x1", "y1" }; + for (int i = 0; i < 4; i++) { + const cJSON *x = cJSON_GetObjectItem(pl, K[i]); + if (!cJSON_IsNumber(x)) { + return false; + } + v[i] = x->valuedouble; + } + return true; +} + +// Send every placed ESP-NOW driver its strip ends in rig coordinates: centred on settings.rig_center +// (else the layout's bounding box) and scaled so the furthest end is 1 from the centre. *sent / *failed +// count the drivers. +static void push_layout(int *sent, int *failed) +{ + double lo[2] = { 1e9, 1e9 }, hi[2] = { -1e9, -1e9 }, v[4]; + const cJSON *d; + cJSON_ArrayForEach(d, store_coll("device")) + { + if (placement_of(cJSON_GetObjectItem(d, "placement"), v)) { + for (int i = 0; i < 4; i++) { + lo[i & 1] = v[i] < lo[i & 1] ? v[i] : lo[i & 1]; + hi[i & 1] = v[i] > hi[i & 1] ? v[i] : hi[i & 1]; + } + } + } + double cx = (lo[0] + hi[0]) / 2, cy = (lo[1] + hi[1]) / 2, far = 0; + const cJSON *rc = cJSON_GetObjectItem(store_settings(), "rig_center"); + if (cJSON_IsNumber(cJSON_GetObjectItem(rc, "x")) && cJSON_IsNumber(cJSON_GetObjectItem(rc, "y"))) { + cx = cJSON_GetObjectItem(rc, "x")->valuedouble; + cy = cJSON_GetObjectItem(rc, "y")->valuedouble; + } + cJSON_ArrayForEach(d, store_coll("device")) + { + if (placement_of(cJSON_GetObjectItem(d, "placement"), v)) { + for (int i = 0; i < 4; i += 2) { + double dx = v[i] - cx, dy = v[i + 1] - cy, r2 = dx * dx + dy * dy; + far = r2 > far ? r2 : far; + } + } + } + double scale = far > 0 ? 1.0 / sqrt(far) : 1.0; + *sent = *failed = 0; + cJSON_ArrayForEach(d, store_coll("device")) + { + if (!is_espnow(d) || !placement_of(cJSON_GetObjectItem(d, "placement"), v)) { + continue; + } + cJSON *dc = cJSON_CreateObject(), *place = cJSON_AddArrayToObject(dc, "place"); + for (int i = 0; i < 4; i++) { + double c = (v[i] - (i & 1 ? cy : cx)) * scale; + cJSON_AddItemToArray(place, cJSON_CreateNumber(round(c * 1000) / 1000)); + } + if (push_device_config(d, dc) == ESP_OK) { + (*sent)++; + } else { + (*failed)++; + } + } +} + +// POST /devices/layout {"placements": {mac: {x0, y0, x1, y1} | null}, "center"?: {x, y}}: where each bar +// sits (x0,y0 is the data-in end, LED 1), in any units, and the rig's centre (ripples start there, spins +// turn round it). Saved on the devices and in settings.rig_center, then sent to the drivers normalised. +static esp_err_t set_layout(httpd_req_t *req) +{ + cJSON *body = api_body(req); + if (!body) { + return ESP_OK; + } + const cJSON *pls = cJSON_GetObjectItem(body, "placements"), *it; + if (!cJSON_IsObject(pls)) { + cJSON_Delete(body); + return api_error(req, "400 Bad Request", "placements required"); + } + int changed = 0; + cJSON_ArrayForEach(it, pls) + { + char mac[13]; + cJSON *key = cJSON_CreateString(it->string); + bool ok = api_mac(key, mac); + cJSON_Delete(key); + cJSON *dev = ok ? cJSON_GetObjectItem(store_coll("device"), mac) : NULL; + double v[4]; + if (!dev) { + continue; + } + if (cJSON_IsNull(it)) { + cJSON_DeleteItemFromObject(dev, "placement"); + } else if (placement_of(it, v)) { + cJSON *pl = cJSON_CreateObject(); + cJSON_AddNumberToObject(pl, "x0", v[0]); + cJSON_AddNumberToObject(pl, "y0", v[1]); + cJSON_AddNumberToObject(pl, "x1", v[2]); + cJSON_AddNumberToObject(pl, "y1", v[3]); + api_set(dev, "placement", pl); + } else { + continue; + } + changed++; + } + const cJSON *center = cJSON_GetObjectItem(body, "center"); + if (cJSON_IsNumber(cJSON_GetObjectItem(center, "x")) && cJSON_IsNumber(cJSON_GetObjectItem(center, "y"))) { + cJSON *rc = cJSON_CreateObject(); + cJSON_AddNumberToObject(rc, "x", cJSON_GetObjectItem(center, "x")->valuedouble); + cJSON_AddNumberToObject(rc, "y", cJSON_GetObjectItem(center, "y")->valuedouble); + api_set(store_settings(), "rig_center", rc); + store_save("settings"); + } + cJSON_Delete(body); + if (changed) { + store_save("device"); + } + int sent, failed; + push_layout(&sent, &failed); + cJSON *o = cJSON_CreateObject(); + cJSON_AddNumberToObject(o, "updated", changed); + cJSON_AddNumberToObject(o, "sent", sent); + cJSON_AddNumberToObject(o, "failed", failed); + return api_json(req, "200 OK", o, true); +} + bool devices_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret) { const int method = req->method; @@ -641,6 +769,10 @@ bool devices_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret) *ret = push_groups(req); return true; } + if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "layout")) { + *ret = set_layout(req); + return true; + } if (n == 2 && method == HTTP_POST && !strcmp(seg[1], "resolve-brightness")) { *ret = resolve_brightness(req); return true; diff --git a/main/res_misc.c b/main/res_misc.c index c569086..60ebd66 100644 --- a/main/res_misc.c +++ b/main/res_misc.c @@ -249,6 +249,36 @@ static esp_err_t play_sequence(httpd_req_t *req, const char *id) return api_json(req, "200 OK", o, true); } +// POST /sequences//fire {zone_id?, wait?: "now" | "beat" | "downbeat"}: play it once over whatever runs +static esp_err_t fire_sequence(httpd_req_t *req, const char *id) +{ + const char *profile = api_current_profile(); + if (!profile) { + return api_error(req, "404 Not Found", "No profile available"); + } + cJSON *body = api_body_opt(req); + if (!body) { + return ESP_OK; + } + char zone[24] = "", wait[12] = "now"; + api_id(cJSON_GetObjectItem(body, "zone_id"), zone, sizeof(zone)); + const cJSON *w = cJSON_GetObjectItem(body, "wait"); + if (cJSON_IsString(w)) { + snprintf(wait, sizeof(wait), "%s", w->valuestring); + } + cJSON_Delete(body); + const char *msg; + esp_err_t err = seqplay_fire(id, zone, profile, wait, &msg); + if (err != ESP_OK) { + return api_error(req, err == ESP_ERR_NOT_FOUND ? "404 Not Found" : "400 Bad Request", + msg ? msg : "animation not found"); + } + cJSON *o = cJSON_CreateObject(); + cJSON_AddTrueToObject(o, "ok"); + cJSON_AddStringToObject(o, "wait", wait); + return api_json(req, "200 OK", o, true); +} + bool sequences_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret) { cJSON *all = store_coll("sequence"); @@ -291,6 +321,17 @@ bool sequences_route(httpd_req_t *req, int n, api_seg_t seg, esp_err_t *ret) *ret = api_json(req, "200 OK", o, true); return true; } + if (n == 3 && method == HTTP_POST && !strcmp(seg[1], "overlay") && !strcmp(seg[2], "stop")) { + seqplay_overlay_stop(); + cJSON *o = cJSON_CreateObject(); + cJSON_AddTrueToObject(o, "ok"); + *ret = api_json(req, "200 OK", o, true); + return true; + } + if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "fire")) { + *ret = fire_sequence(req, seg[1]); + return true; + } if (n == 3 && method == HTTP_POST && !strcmp(seg[2], "play")) { *ret = play_sequence(req, seg[1]); return true; diff --git a/main/seqplay.c b/main/seqplay.c index 807efd0..7e0bf31 100644 --- a/main/seqplay.c +++ b/main/seqplay.c @@ -10,6 +10,7 @@ #include "bench.h" #include "esp_heap_caps.h" #include "esp_log.h" +#include "esp_random.h" #include "esp_timer.h" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" @@ -18,52 +19,96 @@ static const char *TAG = "seqplay"; -#define LANES_MAX 8 +#define LANES_MAX 16 #define STEPS_MAX 64 +#define TARGETS_MAX 32 +#define TGROUPS_MAX 8 // group ids per target +#define MACS_MAX 32 #define ID_MAX 24 #define BPM_MIN 60 #define BPM_MAX 200 #define BEATS_PER_BAR 4 +#define UNITS 4 // beat lanes count in quarter beats + +/* A target is one entry of a sequence's ordered driver list: group ids, "z" ids and drivers' own + "d" groups. Lanes and steps name targets by a bit mask. Older records (lanes_group_ids, a step's + group_ids, the zone's groups) become extra targets when they load. */ +typedef struct { + char g[TGROUPS_MAX][GROUP_ID_MAX]; + int n; +} target_t; typedef struct { - char preset[ID_MAX]; - int beats; - int ms; // time mode: how long the step plays - char group_ids[4][GROUP_ID_MAX]; // a single lane's step may carry its own groups - int ngroups; + char id[ID_MAX]; // preset id, or an animation's (sequence) id when anim + bool anim; // fire animation `id` over the sequence + bool rest; // nothing lit: the lane's targets go off + bool others_off; // the lane's targets this step does not light go off + int units; // beat lanes: quarter beats the step lasts + int ms; // time lanes: how long the step lasts + uint32_t mask; // targets the step lights; 0 = the lane's } step_t; +enum { CHASE_FORWARD, CHASE_REVERSE, CHASE_BOUNCE, CHASE_RANDOM }; + typedef struct { step_t step[STEPS_MAX]; - int n, idx, count; - int64_t t0; // time mode: when the step started (us) - bool done; - char last[ID_MAX]; // preset last uploaded for this lane + int n, idx, count; // count: quarter beats into the step + int wait; // beat lanes: quarter beats before the lane starts (offset) + int64_t t0; // time lanes: when the step started, or (before the start) when the lane starts + bool timed, started, done; + uint32_t mask; // the lane's targets + uint8_t order[TARGETS_MAX]; + int norder; // the lane's targets in order (chase) + bool chase, chase_off; + int corder, width, pos, dir; + char last[ID_MAX]; // preset last uploaded to the lane's targets } lane_t; -// Everything below is guarded by the store lock (the beat task and the HTTP handlers both take it). -// The lanes (about 57 KB) live in PSRAM, allocated by seqplay_init. -static struct { - bool active; +typedef struct { + bool active, overlay; char seq[ID_MAX], zone[ID_MAX], profile[ID_MAX]; lane_t *lane; int nlanes; + target_t *target; + int ntargets; bool loop; - bool timed; // steps last seconds, not beats + bool synced; // a beat has been counted since the start (sub-beat ticks count from then) + bool anchor_next; // the next beat starts a bar + bool lane0_looped; int loop_beat; - bool anchor_on_pass_start; bool pending_switch; // a queued start takes over on the next beat; stop advancing meanwhile -} s_run; +} player_t; + +typedef struct { + char m[MACS_MAX][13]; + int n; +} macs_t; + +// Everything below is guarded by the store lock (the beat task and the HTTP handlers both take it). +// Lanes and targets live in PSRAM, allocated by seqplay_init. +static player_t s_base; // the sequence +static player_t s_ovl; // a one-shot animation over it +static macs_t s_covered; // drivers the animation lights; the sequence's sends skip them +static macs_t s_restore; // while set (n > 0), sends go only to these drivers +static bool s_restoring; +static bool s_base_clear_pending; // the sequence ended under the animation: clear once it is over static struct { bool set; char seq[ID_MAX], zone[ID_MAX], profile[ID_MAX]; } s_pend; +static struct { + bool set, downbeat; + char seq[ID_MAX], zone[ID_MAX], profile[ID_MAX]; +} s_ovl_pend; + static char s_done_readout[24]; // "6/6" kept after a sequence ran to its end static bool s_audio = true; static uint32_t s_sim_beats; // simulated clock beats, for its bar phase static int64_t s_last_beat_us; +static int64_t s_beat_iv = 500000; // last beat interval (us), for sub-beat ticks +static int s_sub = UNITS - 1; // sub-beat ticks fired since the last beat /* ------------------------------------------------------------- settings */ @@ -88,6 +133,18 @@ static bool wait_for_downbeat(void) return audio_drives() && cJSON_IsString(v) && !strcasecmp(v->valuestring, "downbeat"); } +static double num(const cJSON *o, const char *k, double def) +{ + const cJSON *v = cJSON_GetObjectItem(o, k); + return cJSON_IsNumber(v) ? v->valuedouble : def; +} + +static bool str_is(const cJSON *o, const char *k, const char *want) +{ + const cJSON *v = cJSON_GetObjectItem(o, k); + return cJSON_IsString(v) && !strcmp(v->valuestring, want); +} + /* ---------------------------------------------------------------- targets */ static void add_id(cJSON *arr, const char *id) @@ -102,40 +159,154 @@ static void add_id(cJSON *arr, const char *id) cJSON_AddItemToArray(arr, cJSON_CreateString(id)); } -static void add_list(cJSON *arr, const cJSON *list) +static bool macs_has(const macs_t *s, const char *mac) { + for (int i = 0; i < s->n; i++) { + if (!strcmp(s->m[i], mac)) { + return true; + } + } + return false; +} + +static void macs_add(macs_t *s, const char *mac) +{ + if (s->n < MACS_MAX && !macs_has(s, mac)) { + snprintf(s->m[s->n++], 13, "%s", mac); + } +} + +// Add a target with these group ids (or find the same one); its index, or -1 when the table is full +static int target_add(player_t *p, char g[][GROUP_ID_MAX], int n) +{ + for (int i = 0; i < p->ntargets; i++) { + const target_t *t = &p->target[i]; + bool same = t->n == n; + for (int k = 0; same && k < n; k++) { + same = !strcmp(t->g[k], g[k]); + } + if (same) { + return i; + } + } + if (p->ntargets == TARGETS_MAX) { + return -1; + } + target_t *t = &p->target[p->ntargets]; + t->n = n < TGROUPS_MAX ? n : TGROUPS_MAX; + for (int k = 0; k < t->n; k++) { + snprintf(t->g[k], GROUP_ID_MAX, "%s", g[k]); + } + return p->ntargets++; +} + +// A target made from a JSON list of group ids; -1 when the list is empty +static int target_from_list(player_t *p, const cJSON *list) +{ + char g[TGROUPS_MAX][GROUP_ID_MAX]; + int n = 0; const cJSON *it; cJSON_ArrayForEach(it, list) { - char id[GROUP_ID_MAX]; - if (api_id(it, id, sizeof(id))) { - add_id(arr, id); + if (n < TGROUPS_MAX && api_id(it, g[n], GROUP_ID_MAX)) { + n++; + } + } + return n ? target_add(p, g, n) : -1; +} + +static int target_zone(player_t *p) +{ + char t[ZONE_TARGETS_MAX][GROUP_ID_MAX]; + int n = engine_zone_targets(p->zone, t, ZONE_TARGETS_MAX); + return n > 0 ? target_add(p, t, n) : -1; +} + +static uint32_t bit(int i) +{ + return i >= 0 && i < TARGETS_MAX ? 1u << i : 0; +} + +// The sequence's own target list: {group_ids, devices}. Always the first entries of the table. +static void load_targets(player_t *p, const cJSON *seq) +{ + p->ntargets = 0; + const cJSON *t; + cJSON_ArrayForEach(t, cJSON_GetObjectItem(seq, "targets")) + { + if (p->ntargets == TARGETS_MAX) { + break; + } + target_t *tt = &p->target[p->ntargets++]; + tt->n = 0; + const cJSON *it; + cJSON_ArrayForEach(it, cJSON_GetObjectItem(t, "group_ids")) + { + if (tt->n < TGROUPS_MAX && api_id(it, tt->g[tt->n], GROUP_ID_MAX)) { + tt->n++; + } + } + cJSON_ArrayForEach(it, cJSON_GetObjectItem(t, "devices")) + { + char mac[13]; + if (tt->n < TGROUPS_MAX && api_mac(it, mac)) { + tt->g[tt->n][0] = 'd'; + memcpy(tt->g[tt->n++] + 1, mac, 13); + } } } } -// Groups a lane's step plays to (main's _group_ids_for_lane_step), falling back to the zone's groups -static cJSON *lane_groups(int lane, const step_t *st) +// Mask of the sequence's targets named by indexes in list +static uint32_t mask_of(const player_t *p, const cJSON *list, int nuser) +{ + uint32_t m = 0; + const cJSON *it; + cJSON_ArrayForEach(it, list) + { + if (cJSON_IsNumber(it) && it->valuedouble >= 0 && it->valuedouble < nuser) { + m |= bit((int)it->valuedouble); + } + } + return m; +} + +// Group ids for a target mask; while an animation plays over the sequence, or while drivers are being +// restored after it, they become the drivers' own groups for just the drivers that may be sent to. +static cJSON *groups_for(const player_t *p, uint32_t mask) { cJSON *out = cJSON_CreateArray(); - const cJSON *seq = cJSON_GetObjectItem(store_coll("sequence"), s_run.seq); - const cJSON *lgs = cJSON_GetObjectItem(seq, "lanes_group_ids"); - if (cJSON_IsArray(lgs) && lane < cJSON_GetArraySize(lgs)) { - add_list(out, cJSON_GetArrayItem(lgs, lane)); - } else if (!(s_run.nlanes > 1 && cJSON_IsArray(lgs))) { - add_list(out, cJSON_GetObjectItem(seq, "group_ids")); - for (int i = 0; !out->child && s_run.nlanes == 1 && i < st->ngroups; i++) { - add_id(out, st->group_ids[i]); + for (int i = 0; i < p->ntargets; i++) { + if (mask & bit(i)) { + for (int k = 0; k < p->target[i].n; k++) { + add_id(out, p->target[i].g[k]); + } } } - if (!out->child) { - char t[ZONE_TARGETS_MAX][GROUP_ID_MAX]; - int n = engine_zone_targets(s_run.zone, t, ZONE_TARGETS_MAX); + bool exclude = p == &s_base && s_ovl.active && s_covered.n; + if (!s_restoring && !exclude) { + return out; + } + cJSON *drivers = cJSON_CreateArray(); + const cJSON *g; + cJSON_ArrayForEach(g, out) + { + char macs[MACS_MAX][13]; + int n = engine_group_macs(g->valuestring, macs, MACS_MAX); + if (!n && !s_restoring) { // no known members: nothing to leave out + add_id(drivers, g->valuestring); + } for (int i = 0; i < n; i++) { - add_id(out, t[i]); + bool keep = s_restoring ? macs_has(&s_restore, macs[i]) : !macs_has(&s_covered, macs[i]); + if (keep) { + char gid[GROUP_ID_MAX] = "d"; + memcpy(gid + 1, macs[i], 13); // 12 hex digits and the NUL + add_id(drivers, gid); + } } } - return out; + cJSON_Delete(out); + return drivers; } static void send(cJSON *msg) @@ -146,117 +317,137 @@ static void send(cJSON *msg) cJSON_Delete(messages); } +// A v1 message to groups; NULL (and groups freed) when there are none, so nothing is broadcast to +// every driver in range static cJSON *message(cJSON *groups) { + if (!groups || !groups->child) { + cJSON_Delete(groups); + return NULL; + } cJSON *m = cJSON_CreateObject(); cJSON_AddStringToObject(m, "v", "1"); - if (groups && groups->child) { - cJSON_AddItemToObject(m, "groups", groups); - } else { - cJSON_Delete(groups); - } + cJSON_AddItemToObject(m, "groups", groups); return m; } -// Upload presets [from, to) of a lane (each once) to the lane's groups -static void send_presets(int lane, int from, int to) +static void upload(const player_t *p, uint32_t mask, cJSON *presets) { - lane_t *l = &s_run.lane[lane]; - cJSON *presets = cJSON_CreateObject(); - for (int i = from; i < to && i < l->n; i++) { - const char *id = l->step[i].preset; - cJSON *p = cJSON_GetObjectItem(presets, id) ? NULL : engine_preset_payload(id); - if (p) { - cJSON_AddItemToObject(presets, id, p); - } - } if (!presets->child) { cJSON_Delete(presets); return; } - cJSON *m = message(lane_groups(lane, &l->step[from])); + cJSON *m = message(groups_for(p, mask)); + if (!m) { + cJSON_Delete(presets); + return; + } cJSON_AddItemToObject(m, "presets", presets); send(m); } -static void send_select(int lane) +// Upload presets of steps [from, to) of a lane (each once) to all of the lane's targets +static void send_presets(const player_t *p, int lane, int from, int to) { - lane_t *l = &s_run.lane[lane]; - const step_t *st = &l->step[l->idx]; - cJSON *m = message(lane_groups(lane, st)); - cJSON *sel = cJSON_AddArrayToObject(m, "select"); - cJSON_AddItemToArray(sel, cJSON_CreateString(st->preset)); - send(m); -} - -// Step change: upload the new step's preset unless it is already there, then select it -static void send_lane(int lane) -{ - lane_t *l = &s_run.lane[lane]; - if (l->done || l->idx >= l->n || !l->step[l->idx].preset[0]) { - return; - } - if (strcmp(l->last, l->step[l->idx].preset)) { - send_presets(lane, l->idx, l->idx + 1); - memcpy(l->last, l->step[l->idx].preset, ID_MAX); - } - send_select(lane); -} - -// Every driver the zone reaches gets its combined brightness with the zone's slider value -static void zone_brightness(void) -{ - const cJSON *zone = cJSON_GetObjectItem(store_coll("zone"), s_run.zone); - const cJSON *zb = cJSON_GetObjectItem(zone, "brightness"); - int zone_b = cJSON_IsNumber(zb) ? (int)zb->valuedouble : 255; - zone_b = zone_b < 0 ? 0 : (zone_b > 255 ? 255 : zone_b); - cJSON *macs = cJSON_CreateArray(); - char t[ZONE_TARGETS_MAX][GROUP_ID_MAX]; - int n = engine_zone_targets(s_run.zone, t, ZONE_TARGETS_MAX); - for (int i = 0; i < n; i++) { - const cJSON *it; - if (t[i][0] == 'z') { // drivers named by the zone - const cJSON *d; - cJSON_ArrayForEach(d, store_coll("device")) - { - const cJSON *name = cJSON_GetObjectItem(d, "name"); - cJSON_ArrayForEach(it, cJSON_GetObjectItem(zone, "names")) - { - if (cJSON_IsString(name) && cJSON_IsString(it) && !strcmp(name->valuestring, it->valuestring)) { - add_id(macs, d->string); - } - } - } + const lane_t *l = &p->lane[lane]; + cJSON *presets = cJSON_CreateObject(); + for (int i = from; i < to && i < l->n; i++) { + const step_t *st = &l->step[i]; + if (st->anim || st->rest || !st->id[0] || cJSON_GetObjectItem(presets, st->id)) { continue; } - cJSON_ArrayForEach(it, cJSON_GetObjectItem(cJSON_GetObjectItem(store_coll("group"), t[i]), "devices")) - { - char mac[13]; - if (api_mac(it, mac)) { - add_id(macs, mac); - } + cJSON *pl = engine_preset_payload(st->id); + if (pl) { + cJSON_AddItemToObject(presets, st->id, pl); } } - const cJSON *mac; - cJSON_ArrayForEach(mac, macs) - { - cJSON *m = message(NULL); - cJSON_AddNumberToObject(m, "b", api_effective_brightness(mac->valuestring, zone_b)); - cJSON_AddTrueToObject(m, "save"); - cJSON *messages = cJSON_CreateArray(), *targets = cJSON_CreateArray(); - cJSON_AddItemToArray(messages, m); - cJSON_AddItemToArray(targets, cJSON_CreateString(mac->valuestring)); - engine_push(messages, targets); - cJSON_Delete(messages); - cJSON_Delete(targets); - } - cJSON_Delete(macs); + upload(p, l->mask, presets); } -/* --------------------------------------------------------------- playback */ +static void select_on(const player_t *p, uint32_t mask, const char *preset) +{ + if (!mask) { + return; + } + cJSON *m = message(groups_for(p, mask)); + if (m) { + cJSON *sel = cJSON_AddArrayToObject(m, "select"); + cJSON_AddItemToArray(sel, cJSON_CreateString(preset)); + send(m); + } +} -// The sequence's lanes as steps (main's _normalize_sequence_lanes); empty lanes are dropped -static int load_lanes(const cJSON *seq) +/* ------------------------------------------------------------------ lanes */ + +static uint32_t chase_mask(const lane_t *l) +{ + uint32_t m = 0; + for (int k = 0; k < l->width && k < l->norder; k++) { + m |= bit(l->order[(l->pos + k) % l->norder]); + } + return m; +} + +static void chase_move(lane_t *l) +{ + int n = l->norder; + if (n < 2) { + return; + } + switch (l->corder) { + case CHASE_REVERSE: + l->pos = (l->pos + n - 1) % n; + break; + case CHASE_BOUNCE: + if (l->pos + l->dir < 0 || l->pos + l->dir >= n) { + l->dir = -l->dir; + } + l->pos += l->dir; + break; + case CHASE_RANDOM: { + int next = (int)(esp_random() % (uint32_t)(n - 1)); + l->pos = next >= l->pos ? next + 1 : next; + break; + } + default: + l->pos = (l->pos + 1) % n; + } +} + +static void fire(const char *seq_id, const char *zone_id, const char *profile_id, bool synced); + +// Show a lane's current step: upload its preset unless already there, select it on the targets it +// lights, and turn off the lane's other targets when the step asks for that +static void send_lane(player_t *p, int lane) +{ + lane_t *l = &p->lane[lane]; + if (!l->started || l->done || l->idx >= l->n) { + return; + } + const step_t *st = &l->step[l->idx]; + if (st->anim) { + if (!p->overlay && !s_restoring) { + fire(st->id, p->zone, p->profile, true); + } + return; + } + uint32_t lit = st->rest ? 0 : st->mask ? st->mask : l->chase ? chase_mask(l) : l->mask; + if (lit && st->id[0]) { + if (strcmp(l->last, st->id)) { + send_presets(p, lane, l->idx, l->idx + 1); + memcpy(l->last, st->id, ID_MAX); + } + select_on(p, lit, st->id); + } + if (st->rest || st->others_off || (l->chase && l->chase_off)) { + select_on(p, l->mask & ~lit, "off"); + } +} + +// The sequence's lanes as steps (main's _normalize_sequence_lanes); empty lanes are dropped. Older +// records' groups become targets: lanes_group_ids[lane], else group_ids, else (single lane) the +// step's group_ids, else the zone's groups. +static int load_lanes(player_t *p, const cJSON *seq) { const cJSON *lanes = cJSON_GetObjectItem(seq, "lanes"); bool any = false; @@ -271,14 +462,78 @@ static int load_lanes(const cJSON *seq) cJSON_AddItemReferenceToArray(one, cJSON_GetObjectItem(seq, "steps")); lanes = one; } - int n = 0; + int nonempty = 0; cJSON_ArrayForEach(l, lanes) { + nonempty += cJSON_IsArray(l) && cJSON_GetArraySize(l) > 0; + } + const int nuser = p->ntargets; + const cJSON *lgs = cJSON_GetObjectItem(seq, "lanes_group_ids"); + const cJSON *opts = cJSON_GetObjectItem(seq, "lane_opts"); + const bool seq_timed = str_is(seq, "advance_mode", "time"); + int n = 0, src = -1; + cJSON_ArrayForEach(l, lanes) + { + src++; if (n == LANES_MAX || !cJSON_IsArray(l) || !cJSON_GetArraySize(l)) { continue; } - lane_t *lane = &s_run.lane[n]; + const cJSON *o = cJSON_GetArrayItem(opts, src); + lane_t *lane = &p->lane[n]; memset(lane, 0, sizeof(*lane)); + const cJSON *mode = cJSON_GetObjectItem(o, "mode"); + lane->timed = cJSON_IsString(mode) ? !strcmp(mode->valuestring, "time") : seq_timed; + double rate = num(o, "rate", 1); + rate = rate < 0.25 ? 0.25 : (rate > 4 ? 4 : rate); + // Targets: the lane's own list, else the older group fields, else the zone's + lane->mask = mask_of(p, cJSON_GetObjectItem(o, "targets"), nuser); + bool legacy_steps = false; + if (!lane->mask) { + int ti = -1; + if (cJSON_IsArray(lgs) && n < cJSON_GetArraySize(lgs)) { + ti = target_from_list(p, cJSON_GetArrayItem(lgs, n)); + } else if (!(nonempty > 1 && cJSON_IsArray(lgs))) { + ti = target_from_list(p, cJSON_GetObjectItem(seq, "group_ids")); + legacy_steps = ti < 0 && nonempty == 1; + } + lane->mask = bit(ti >= 0 ? ti : target_zone(p)); + } + for (int i = 0; i < p->ntargets && lane->norder < TARGETS_MAX; i++) { + if (lane->mask & bit(i)) { + lane->order[lane->norder++] = i; + } + } + const cJSON *ord = cJSON_GetObjectItem(o, "targets"); // the lane's own order wins for a chase + if (cJSON_GetArraySize(ord)) { + lane->norder = 0; + const cJSON *it; + cJSON_ArrayForEach(it, ord) + { + int i = cJSON_IsNumber(it) ? (int)it->valuedouble : -1; + if (i >= 0 && i < nuser && lane->norder < TARGETS_MAX) { + lane->order[lane->norder++] = i; + } + } + } + const cJSON *chase = cJSON_GetObjectItem(o, "chase"); + if (cJSON_IsObject(chase) && lane->norder > 1) { + lane->chase = true; + lane->chase_off = !str_is(chase, "others", "hold"); + lane->corder = str_is(chase, "order", "reverse") ? CHASE_REVERSE + : str_is(chase, "order", "bounce") ? CHASE_BOUNCE + : str_is(chase, "order", "random") ? CHASE_RANDOM + : CHASE_FORWARD; + int w = (int)num(chase, "width", 1); + lane->width = w < 1 ? 1 : (w > lane->norder ? lane->norder : w); + lane->dir = 1; + lane->pos = lane->corder == CHASE_REVERSE ? lane->norder - 1 + : lane->corder == CHASE_RANDOM ? (int)(esp_random() % (uint32_t)lane->norder) + : 0; + } + double off = num(o, "offset", 0); + off = off < 0 ? 0 : (off > 3600 ? 3600 : off); + lane->wait = lane->timed ? 0 : (int)(off * UNITS + 0.5); + lane->t0 = lane->timed ? (int64_t)(off * 1000000) : 0; // made absolute at the start const cJSON *s; cJSON_ArrayForEach(s, l) { @@ -286,18 +541,33 @@ static int load_lanes(const cJSON *seq) continue; } step_t *st = &lane->step[lane->n++]; - const cJSON *pid = cJSON_GetObjectItem(s, "preset_id"); - api_id(pid ? pid : cJSON_GetObjectItem(s, "presetId"), st->preset, sizeof(st->preset)); - const cJSON *b = cJSON_GetObjectItem(s, "beats"); - st->beats = cJSON_IsNumber(b) && b->valuedouble >= 1 ? (int)b->valuedouble : 1; - const cJSON *sec = cJSON_GetObjectItem(s, "seconds"); - double secs = cJSON_IsNumber(sec) ? sec->valuedouble : 3.0; - st->ms = secs < 0.1 ? 100 : (secs > 3600 ? 3600000 : (int)(secs * 1000)); - const cJSON *g; - cJSON_ArrayForEach(g, cJSON_GetObjectItem(s, "group_ids")) - { - if (st->ngroups < 4 && api_id(g, st->group_ids[st->ngroups], GROUP_ID_MAX)) { - st->ngroups++; + memset(st, 0, sizeof(*st)); + const cJSON *aid = cJSON_GetObjectItem(s, "animation_id"); + if (api_id(aid, st->id, sizeof(st->id))) { + st->anim = true; + } else { + const cJSON *pid = cJSON_GetObjectItem(s, "preset_id"); + api_id(pid ? pid : cJSON_GetObjectItem(s, "presetId"), st->id, sizeof(st->id)); + } + st->rest = cJSON_IsTrue(cJSON_GetObjectItem(s, "rest")); + st->others_off = str_is(s, "others", "off"); + double beats = num(s, "beats", 1); + beats = beats < 0.25 ? 1 : beats; + double secs = num(s, "seconds", 3); + secs = secs < 0.1 ? 0.1 : (secs > 3600 ? 3600 : secs); + if (st->anim && str_is(s, "length", "animation")) { + seqplay_length(st->id, &beats, &secs); + } + int u = (int)(beats * UNITS / rate + 0.5); + st->units = u < 1 ? 1 : u; + st->ms = (int)(secs * 1000 / rate); + st->ms = st->ms < 50 ? 50 : st->ms; + st->mask = mask_of(p, cJSON_GetObjectItem(s, "targets"), nuser) & lane->mask; + if (legacy_steps && cJSON_GetArraySize(cJSON_GetObjectItem(s, "group_ids"))) { + int ti = target_from_list(p, cJSON_GetObjectItem(s, "group_ids")); + if (ti >= 0) { + st->mask = bit(ti); + lane->mask |= bit(ti); // uploads reach it too } } } @@ -307,6 +577,8 @@ static int load_lanes(const cJSON *seq) return n; } +/* --------------------------------------------------------------- playback */ + static void anchor_bar(void) { s_sim_beats = 0; // the next clock beat is a downbeat @@ -315,26 +587,56 @@ static void anchor_bar(void) } } -static void clear_drivers(void) +static void clear_drivers(const char *zone) { cJSON *groups = cJSON_CreateArray(); char t[ZONE_TARGETS_MAX][GROUP_ID_MAX]; - int n = engine_zone_targets(s_run.zone, t, ZONE_TARGETS_MAX); + int n = engine_zone_targets(zone, t, ZONE_TARGETS_MAX); for (int i = 0; i < n; i++) { add_id(groups, t[i]); } - if (!groups->child) { // never clear every driver in range - cJSON_Delete(groups); - return; + cJSON *m = message(groups); // never clear every driver in range + if (m) { + cJSON_AddTrueToObject(m, "clear_presets"); + cJSON_AddTrueToObject(m, "save"); + send(m); } - cJSON *m = message(groups); - cJSON_AddTrueToObject(m, "clear_presets"); - cJSON_AddTrueToObject(m, "save"); - send(m); } -static esp_err_t start_now(const char *seq_id, const char *zone_id, const char *profile_id, bool handoff, - bool downbeat, const char **msg) +// Every driver the zone reaches gets its combined brightness with the zone's slider value +static void zone_brightness(const char *zone_id) +{ + const cJSON *zone = cJSON_GetObjectItem(store_coll("zone"), zone_id); + const cJSON *zb = cJSON_GetObjectItem(zone, "brightness"); + int zone_b = cJSON_IsNumber(zb) ? (int)zb->valuedouble : 255; + zone_b = zone_b < 0 ? 0 : (zone_b > 255 ? 255 : zone_b); + macs_t macs = { 0 }; + char t[ZONE_TARGETS_MAX][GROUP_ID_MAX]; + int n = engine_zone_targets(zone_id, t, ZONE_TARGETS_MAX); + for (int i = 0; i < n; i++) { + char m[MACS_MAX][13]; + int k = engine_group_macs(t[i], m, MACS_MAX); + for (int j = 0; j < k; j++) { + macs_add(&macs, m[j]); + } + } + for (int i = 0; i < macs.n; i++) { + cJSON *m = cJSON_CreateObject(); + cJSON_AddStringToObject(m, "v", "1"); + cJSON_AddNumberToObject(m, "b", api_effective_brightness(macs.m[i], zone_b)); + cJSON_AddTrueToObject(m, "save"); + cJSON *messages = cJSON_CreateArray(), *targets = cJSON_CreateArray(); + cJSON_AddItemToArray(messages, m); + cJSON_AddItemToArray(targets, cJSON_CreateString(macs.m[i])); + engine_push(messages, targets); + cJSON_Delete(messages); + cJSON_Delete(targets); + } +} + +// Load and show a player's sequence. synced: started on a beat, so the sub-beat ticks after it count. +static esp_err_t load(player_t *p, const char *seq_id, const char *zone_id, const char *profile_id, bool synced, + const char **msg) { const cJSON *seq = cJSON_GetObjectItem(store_coll("sequence"), seq_id); if (!seq) { @@ -344,128 +646,312 @@ static esp_err_t start_now(const char *seq_id, const char *zone_id, const char * *msg = "zone not found"; return ESP_ERR_INVALID_ARG; } - s_pend.set = false; - s_run.active = false; - s_done_readout[0] = 0; - snprintf(s_run.seq, sizeof(s_run.seq), "%s", seq_id); - snprintf(s_run.zone, sizeof(s_run.zone), "%s", zone_id); - snprintf(s_run.profile, sizeof(s_run.profile), "%s", profile_id); - s_run.nlanes = load_lanes(seq); - if (!s_run.nlanes) { + p->active = false; + snprintf(p->seq, sizeof(p->seq), "%s", seq_id); + snprintf(p->zone, sizeof(p->zone), "%s", zone_id); + snprintf(p->profile, sizeof(p->profile), "%s", profile_id); + load_targets(p, seq); + p->nlanes = load_lanes(p, seq); + if (!p->nlanes) { *msg = "sequence has no steps"; return ESP_ERR_INVALID_SIZE; } const cJSON *loop = cJSON_GetObjectItem(seq, "loop"); - s_run.loop = !loop || cJSON_IsTrue(loop) || (cJSON_IsNumber(loop) && loop->valuedouble != 0); - const cJSON *mode = cJSON_GetObjectItem(seq, "advance_mode"); - s_run.timed = cJSON_IsString(mode) && !strcmp(mode->valuestring, "time"); - int64_t t_start = esp_timer_get_time(); - for (int i = 0; i < s_run.nlanes; i++) { - s_run.lane[i].t0 = t_start; + bool on = cJSON_IsTrue(loop) || (cJSON_IsNumber(loop) && loop->valuedouble != 0); + p->loop = p->overlay ? on : (!loop || on); // a sequence loops unless told not to; an animation plays once + p->synced = synced; + p->loop_beat = 0; + p->lane0_looped = false; + p->pending_switch = false; + int64_t now = esp_timer_get_time(); + for (int i = 0; i < p->nlanes; i++) { + lane_t *l = &p->lane[i]; + l->t0 = now + (l->timed ? l->t0 : 0); + l->started = l->timed ? l->t0 <= now : l->wait == 0; } - s_run.loop_beat = 0; - s_run.pending_switch = false; - s_run.anchor_on_pass_start = handoff ? downbeat : true; - // Step-0 presets, the selects, then the rest of each lane's presets (main's _prime_all_lanes) - for (int i = 0; i < s_run.nlanes; i++) { - send_presets(i, 0, 1); - memcpy(s_run.lane[i].last, s_run.lane[i].step[0].preset, ID_MAX); + // Step-0 presets of the lanes that start now, their selects, then every other preset (main's + // _prime_all_lanes) + for (int i = 0; i < p->nlanes; i++) { + lane_t *l = &p->lane[i]; + if (l->started) { + send_presets(p, i, 0, 1); + snprintf(l->last, ID_MAX, "%s", l->step[0].rest || l->step[0].anim ? "" : l->step[0].id); + } } - for (int i = 0; i < s_run.nlanes; i++) { - send_select(i); + p->active = true; + for (int i = 0; i < p->nlanes; i++) { + send_lane(p, i); } - for (int i = 0; i < s_run.nlanes; i++) { - send_presets(i, 1, s_run.lane[i].n); + for (int i = 0; i < p->nlanes; i++) { + lane_t *l = &p->lane[i]; + send_presets(p, i, l->started ? 1 : 0, l->n); } - zone_brightness(); - s_run.active = true; - ESP_LOGI(TAG, "sequence %s in zone %s: %d lane(s)", seq_id, zone_id, s_run.nlanes); return ESP_OK; } -static bool pass_start(void) +static esp_err_t start_now(const char *seq_id, const char *zone_id, const char *profile_id, bool handoff, + bool downbeat, const char **msg) { - const lane_t *l = &s_run.lane[0]; - return !l->done && l->idx == 0 && l->count == 0; + s_pend.set = false; + s_done_readout[0] = 0; + s_base_clear_pending = false; + esp_err_t err = load(&s_base, seq_id, zone_id, profile_id, handoff, msg); + if (err != ESP_OK) { + return err; + } + s_base.anchor_next = handoff ? downbeat : true; + zone_brightness(zone_id); + ESP_LOGI(TAG, "sequence %s in zone %s: %d lane(s), %d target(s)", seq_id, zone_id, s_base.nlanes, + s_base.ntargets); + return ESP_OK; } -static int readout(char *buf, size_t len); // forward - -// Move a lane on once its step is over. Sets *looped when lane 0 wrapped round. -static void step_lane(int i, int64_t now, bool *looped) +// Drivers an animation may light: every target its lanes name +static void covered_by(const player_t *p, macs_t *out) { - lane_t *l = &s_run.lane[i]; + out->n = 0; + uint32_t mask = 0; + for (int i = 0; i < p->nlanes; i++) { + mask |= p->lane[i].mask; + } + for (int i = 0; i < p->ntargets; i++) { + for (int k = 0; (mask & bit(i)) && k < p->target[i].n; k++) { + char m[MACS_MAX][13]; + int n = engine_group_macs(p->target[i].g[k], m, MACS_MAX); + for (int j = 0; j < n; j++) { + macs_add(out, m[j]); + } + } + } +} + +// Bring back what the drivers in `drivers` showed before the animation: the sequence's current steps, +// else the zone's last preset, else off +static void restore(const macs_t *drivers, const char *zone) +{ + if (!drivers->n) { + return; + } + s_restore = *drivers; + s_restoring = true; + if (s_base_clear_pending) { + s_base_clear_pending = false; + clear_drivers(s_base.zone); + } else if (s_base.active) { + for (int i = 0; i < s_base.nlanes; i++) { + s_base.lane[i].last[0] = 0; // they may have missed its upload + send_lane(&s_base, i); + } + } else { + // The zone's last preset, on these drivers only (a pseudo player with one target) + static target_t t; + player_t one = { .target = &t, .ntargets = 1 }; + t.n = 0; + char z[ZONE_TARGETS_MAX][GROUP_ID_MAX]; + int n = engine_zone_targets(zone, z, ZONE_TARGETS_MAX); + for (int i = 0; i < n && t.n < TGROUPS_MAX; i++) { + snprintf(t.g[t.n++], GROUP_ID_MAX, "%s", z[i]); + } + const char *cur = engine_zone_current(zone); + if (cur && strcmp(cur, "off")) { + cJSON *presets = cJSON_CreateObject(); + cJSON *pl = engine_preset_payload(cur); + if (pl) { + cJSON_AddItemToObject(presets, cur, pl); + } + upload(&one, 1, presets); + select_on(&one, 1, cur); + } else { + select_on(&one, 1, "off"); + } + } + s_restoring = false; + s_restore.n = 0; +} + +static void overlay_end(void) +{ + if (!s_ovl.active) { + return; + } + s_ovl.active = false; + ESP_LOGI(TAG, "animation %s over", s_ovl.seq); + macs_t was = s_covered; + s_covered.n = 0; + restore(&was, s_ovl.zone); +} + +// Play animation seq_id once over whatever runs. A new one replaces the one playing; drivers the new one +// leaves alone go back to what they showed. +static void fire(const char *seq_id, const char *zone_id, const char *profile_id, bool synced) +{ + s_ovl_pend.set = false; + macs_t was = s_ovl.active ? s_covered : (macs_t){ 0 }; + s_ovl.active = false; + const char *msg = NULL; + // Work out what the animation covers before it sends anything, so the sequence keeps off those drivers + s_covered.n = 0; + if (load(&s_ovl, seq_id, zone_id, profile_id, synced, &msg) != ESP_OK) { + ESP_LOGW(TAG, "animation %s did not start: %s", seq_id, msg ? msg : "not found"); + restore(&was, zone_id); + return; + } + covered_by(&s_ovl, &s_covered); + macs_t back = { 0 }; + for (int i = 0; i < was.n; i++) { + if (!macs_has(&s_covered, was.m[i])) { + macs_add(&back, was.m[i]); + } + } + restore(&back, zone_id); + ESP_LOGI(TAG, "animation %s in zone %s: %d lane(s), %d driver(s)", seq_id, zone_id, s_ovl.nlanes, s_covered.n); +} + +// Move a lane on once its step is over +static void step_lane(player_t *p, int i, int64_t now) +{ + lane_t *l = &p->lane[i]; if (l->idx + 1 >= l->n) { - if (s_run.loop) { - *looped = *looped || i == 0; + if (p->loop) { + if (i == 0) { + p->lane0_looped = true; + p->anchor_next = true; + } l->count = 0; l->idx = 0; l->t0 = now; l->last[0] = 0; // step 0 goes up again on each wrap - send_lane(i); + if (l->chase) { + chase_move(l); + } + send_lane(p, i); } else { - l->count = l->step[l->idx].beats; + l->count = l->step[l->idx].units; l->done = true; } } else { l->count = 0; l->idx++; l->t0 = now; - send_lane(i); + if (l->chase) { + chase_move(l); + } + send_lane(p, i); } } -static void finish_if_done(void) +static int readout(char *buf, size_t len); // forward + +static void finish_if_done(player_t *p) { - for (int i = 0; i < s_run.nlanes; i++) { - if (!s_run.lane[i].done) { + if (!p->active) { + return; + } + for (int i = 0; i < p->nlanes; i++) { + if (!p->lane[i].done) { return; } } + if (p->overlay) { + overlay_end(); + return; + } readout(s_done_readout, sizeof(s_done_readout)); - clear_drivers(); - s_run.active = false; - ESP_LOGI(TAG, "sequence %s finished", s_run.seq); + p->active = false; + if (s_ovl.active) { + s_base_clear_pending = true; // the animation still shows; clear once it is over + } else { + clear_drivers(p->zone); + } + ESP_LOGI(TAG, "sequence %s finished", p->seq); } -// Time mode: called often; lanes move on when their step's seconds are up -static void advance_time(void) +// Time lanes: called often; they move on when their step's time is up +static void advance_time(player_t *p) { - if (!s_run.active || !s_run.timed || s_run.pending_switch) { + if (!p->active || p->pending_switch) { return; } int64_t now = esp_timer_get_time(); - bool looped = false; - for (int i = 0; i < s_run.nlanes; i++) { - lane_t *l = &s_run.lane[i]; - if (!l->done && l->n && now - l->t0 >= (int64_t)l->step[l->idx].ms * 1000) { - step_lane(i, now, &looped); - } - } - finish_if_done(); -} - -static void advance(void) -{ - if (!s_run.active || s_run.pending_switch || s_run.timed) { - return; - } - if (pass_start() && s_run.anchor_on_pass_start) { - anchor_bar(); - } - s_run.anchor_on_pass_start = true; - bool lane0_looped = false; - for (int i = 0; i < s_run.nlanes; i++) { - lane_t *l = &s_run.lane[i]; - if (l->done || !l->n) { + for (int i = 0; i < p->nlanes && p->active; i++) { + lane_t *l = &p->lane[i]; + if (!l->timed || l->done || !l->n) { continue; } - if (++l->count >= l->step[l->idx].beats) { - step_lane(i, 0, &lane0_looped); + if (!l->started) { + if (now >= l->t0) { + l->started = true; + l->t0 = now; + send_lane(p, i); + } + } else if (now - l->t0 >= (int64_t)l->step[l->idx].ms * 1000) { + step_lane(p, i, now); } } - s_run.loop_beat = lane0_looped ? 1 : s_run.loop_beat + 1; - finish_if_done(); + finish_if_done(p); +} + +// Beat lanes: one quarter beat. beat: this tick is the beat itself (not a sub-beat tick). +static void tick(player_t *p, bool beat) +{ + if (!p->active || p->pending_switch) { + return; + } + if (!p->synced) { + if (!beat) { + return; // sub-beat ticks count from the first beat after the start + } + // The time from the start to the first beat counts as a beat (as main's first ++count): the + // quarter beats before this one are added without moving any lane past a step + p->synced = true; + for (int i = 0; i < p->nlanes; i++) { + lane_t *l = &p->lane[i]; + if (l->timed || l->done || !l->n) { + continue; + } + if (l->started) { + int lim = l->step[l->idx].units - 1; + l->count = l->count + UNITS - 1 > lim ? lim : l->count + UNITS - 1; + } else { + l->wait = l->wait - (UNITS - 1) < 1 ? 1 : l->wait - (UNITS - 1); + } + } + } + if (beat && !p->overlay) { + if (p->anchor_next && !p->lane[0].timed) { + anchor_bar(); + } + p->anchor_next = false; + } + int64_t now = esp_timer_get_time(); + for (int i = 0; i < p->nlanes && p->active; i++) { + lane_t *l = &p->lane[i]; + if (l->timed || l->done || !l->n) { + continue; + } + if (!l->started) { + if (--l->wait <= 0) { + l->started = true; + l->count = 0; + send_lane(p, i); + } + } else if (++l->count >= l->step[l->idx].units) { + step_lane(p, i, now); + } + } + if (beat) { + p->loop_beat = p->lane0_looped ? 1 : p->loop_beat + 1; + p->lane0_looped = false; + } + finish_if_done(p); +} + +static void sub_tick(void) +{ + store_lock(); + tick(&s_base, false); + tick(&s_ovl, false); + store_unlock(); } static void on_beat(bool downbeat) @@ -474,8 +960,17 @@ static void on_beat(bool downbeat) if (now - s_last_beat_us < 60LL * 1000000 / BPM_MAX) { // never faster than BPM_MAX return; } + // Sub-beat ticks the last beat still owes (the tempo went up) go first, so every beat has UNITS + while (s_sub < UNITS - 1) { + s_sub++; + sub_tick(); + } + int64_t iv = now - s_last_beat_us; + s_beat_iv = iv < 60LL * 1000000 / BPM_MAX ? 60LL * 1000000 / BPM_MAX : (iv > 2000000 ? 2000000 : iv); s_last_beat_us = now; + s_sub = 0; store_lock(); + bool switched = false; if (s_pend.set && (downbeat || !wait_for_downbeat())) { const char *msg = NULL; char seq[ID_MAX], zone[ID_MAX], profile[ID_MAX]; @@ -485,8 +980,22 @@ static void on_beat(bool downbeat) if (start_now(seq, zone, profile, true, downbeat, &msg) != ESP_OK) { ESP_LOGW(TAG, "queued sequence %s did not start: %s", seq, msg ? msg : "not found"); } - } else { - advance(); + switched = true; + } + bool fired = false; + if (s_ovl_pend.set && (downbeat || !s_ovl_pend.downbeat)) { + char seq[ID_MAX], zone[ID_MAX], profile[ID_MAX]; + snprintf(seq, sizeof(seq), "%s", s_ovl_pend.seq); + snprintf(zone, sizeof(zone), "%s", s_ovl_pend.zone); + snprintf(profile, sizeof(profile), "%s", s_ovl_pend.profile); + fire(seq, zone, profile, true); + fired = true; + } + if (!switched) { + tick(&s_base, true); + } + if (!fired) { + tick(&s_ovl, true); } store_unlock(); } @@ -500,10 +1009,16 @@ static void beat_task(void *arg) int64_t next = esp_timer_get_time(); for (;;) { store_lock(); - advance_time(); + advance_time(&s_base); + advance_time(&s_ovl); int64_t interval = 60LL * 1000000 / simulated_bpm(); store_unlock(); int64_t now = esp_timer_get_time(); + if (s_sub < UNITS - 1 && now >= s_last_beat_us + (s_sub + 1) * s_beat_iv / UNITS) { + s_sub++; + sub_tick(); + continue; + } if (next <= now) { // clock beat, counted only while the mic is not timing next = (next + interval > now) ? next + interval : now + interval; if (!audio_drives()) { @@ -518,9 +1033,14 @@ static void beat_task(void *arg) continue; } beat_event_t ev; - int64_t wait_ms = (next - now) / 1000; - if (wait_ms > 40) { // wake often enough to time steps in seconds - wait_ms = 40; + int64_t until = next; + if (s_sub < UNITS - 1) { + int64_t sub = s_last_beat_us + (s_sub + 1) * s_beat_iv / UNITS; + until = sub < until ? sub : until; + } + int64_t wait_ms = (until - now) / 1000; + if (wait_ms > 20) { // wake often enough to time steps in seconds + wait_ms = 20; } if (q && xQueueReceive(q, &ev, pdMS_TO_TICKS(wait_ms) + 1) == pdTRUE && ev == BEAT_EVENT_BEAT && audio_drives()) { @@ -535,28 +1055,38 @@ static void beat_task(void *arg) esp_err_t seqplay_init(void) { - s_run.lane = heap_caps_calloc(LANES_MAX, sizeof(lane_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); - if (!s_run.lane) { - return ESP_ERR_NO_MEM; + player_t *ps[] = { &s_base, &s_ovl }; + for (int i = 0; i < 2; i++) { + ps[i]->lane = heap_caps_calloc(LANES_MAX, sizeof(lane_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + ps[i]->target = heap_caps_calloc(TARGETS_MAX, sizeof(target_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT); + if (!ps[i]->lane || !ps[i]->target) { + return ESP_ERR_NO_MEM; + } } + s_ovl.overlay = true; store_lock(); const cJSON *a = cJSON_GetObjectItem(store_settings(), "audio_timing"); s_audio = !cJSON_IsFalse(a); store_unlock(); espnow_tx_set_mic_beats(s_audio); - return xTaskCreate(beat_task, "seqplay", 6144, NULL, 4, NULL) == pdPASS ? ESP_OK : ESP_ERR_NO_MEM; + return xTaskCreate(beat_task, "seqplay", 8192, NULL, 4, NULL) == pdPASS ? ESP_OK : ESP_ERR_NO_MEM; +} + +static bool owned(const char *seq_id, const char *profile_id) +{ + const cJSON *seq = cJSON_GetObjectItem(store_coll("sequence"), seq_id); + char owner[ID_MAX]; + return seq && api_id(cJSON_GetObjectItem(seq, "profile_id"), owner, sizeof(owner)) && !strcmp(owner, profile_id); } esp_err_t seqplay_start(const char *seq_id, const char *zone_id, const char *profile_id, const char **msg) { *msg = NULL; - const cJSON *seq = cJSON_GetObjectItem(store_coll("sequence"), seq_id); - char owner[ID_MAX]; - if (!seq || !api_id(cJSON_GetObjectItem(seq, "profile_id"), owner, sizeof(owner)) || strcmp(owner, profile_id)) { + if (!owned(seq_id, profile_id)) { *msg = "sequence not found"; return ESP_ERR_NOT_FOUND; } - if (!s_run.active) { + if (!s_base.active) { return start_now(seq_id, zone_id, profile_id, false, false, msg); } // Another sequence plays: switch on the next beat (or downbeat) @@ -564,25 +1094,69 @@ esp_err_t seqplay_start(const char *seq_id, const char *zone_id, const char *pro snprintf(s_pend.seq, sizeof(s_pend.seq), "%s", seq_id); snprintf(s_pend.zone, sizeof(s_pend.zone), "%s", zone_id); snprintf(s_pend.profile, sizeof(s_pend.profile), "%s", profile_id); - s_run.pending_switch = true; + s_base.pending_switch = true; return ESP_OK; } +esp_err_t seqplay_fire(const char *seq_id, const char *zone_id, const char *profile_id, const char *wait, + const char **msg) +{ + *msg = NULL; + if (!owned(seq_id, profile_id)) { + *msg = "animation not found"; + return ESP_ERR_NOT_FOUND; + } + if (!zone_id || !zone_id[0]) { + zone_id = s_base.active ? s_base.zone : NULL; + } + if (!zone_id || !cJSON_GetObjectItem(store_coll("zone"), zone_id)) { + *msg = "zone not found"; + return ESP_ERR_INVALID_ARG; + } + if (wait && (!strcmp(wait, "beat") || !strcmp(wait, "downbeat"))) { + s_ovl_pend.set = true; + s_ovl_pend.downbeat = !strcmp(wait, "downbeat") && audio_drives(); + snprintf(s_ovl_pend.seq, sizeof(s_ovl_pend.seq), "%s", seq_id); + snprintf(s_ovl_pend.zone, sizeof(s_ovl_pend.zone), "%s", zone_id); + snprintf(s_ovl_pend.profile, sizeof(s_ovl_pend.profile), "%s", profile_id); + return ESP_OK; + } + fire(seq_id, zone_id, profile_id, false); + if (!s_ovl.active) { + *msg = "animation has no steps"; + return ESP_ERR_INVALID_SIZE; + } + return ESP_OK; +} + +void seqplay_overlay_stop(void) +{ + store_lock(); + s_ovl_pend.set = false; + overlay_end(); + store_unlock(); +} + void seqplay_stop(bool clear) { store_lock(); - bool was = s_run.active; + bool was = s_base.active || s_ovl.active; + const char *zone = s_base.active ? s_base.zone : s_ovl.zone; s_pend.set = false; - s_run.active = false; + s_ovl_pend.set = false; + s_base.active = false; + s_ovl.active = false; // everything stops: the zone's presets are cleared below + s_covered.n = 0; + s_base_clear_pending = false; bool finished = true; - for (int i = 0; i < s_run.nlanes; i++) { - finished = finished && s_run.lane[i].done; + for (int i = 0; i < s_base.nlanes; i++) { + finished = finished && s_base.lane[i].done; } if (!finished) { s_done_readout[0] = 0; } if (was && clear) { - clear_drivers(); + clear_drivers(zone); } store_unlock(); } @@ -590,30 +1164,38 @@ void seqplay_stop(bool clear) void seqplay_stop_if(const char *seq_id) { store_lock(); - bool playing = s_run.active && !strcmp(s_run.seq, seq_id); + bool playing = s_base.active && !strcmp(s_base.seq, seq_id); + bool anim = s_ovl.active && !strcmp(s_ovl.seq, seq_id); store_unlock(); if (playing) { seqplay_stop(true); + } else if (anim) { + seqplay_overlay_stop(); } } bool seqplay_sync(const char *mode) { store_lock(); - bool ok = s_run.active; + bool ok = s_base.active; + int64_t now = esp_timer_get_time(); if (ok && (!strcasecmp(mode, "pass") || !strcasecmp(mode, "sequence") || !strcasecmp(mode, "restart"))) { - for (int i = 0; i < s_run.nlanes; i++) { - s_run.lane[i].idx = s_run.lane[i].count = 0; - s_run.lane[i].done = false; + for (int i = 0; i < s_base.nlanes; i++) { + lane_t *l = &s_base.lane[i]; + l->idx = l->count = 0; + l->done = false; + l->started = true; + l->t0 = now; } - s_run.loop_beat = 0; - for (int i = 0; i < s_run.nlanes; i++) { - send_lane(i); + s_base.loop_beat = 0; + for (int i = 0; i < s_base.nlanes; i++) { + send_lane(&s_base, i); } } else if (ok) { - for (int i = 0; i < s_run.nlanes; i++) { - if (!s_run.lane[i].done) { - s_run.lane[i].count = 0; + for (int i = 0; i < s_base.nlanes; i++) { + if (!s_base.lane[i].done) { + s_base.lane[i].count = 0; + s_base.lane[i].t0 = now; } } } @@ -624,34 +1206,73 @@ bool seqplay_sync(const char *mode) return ok; } -// Lane 0's position in the pass: beats so far and per pass +void seqplay_length(const char *seq_id, double *beats, double *secs) +{ + const cJSON *seq = cJSON_GetObjectItem(store_coll("sequence"), seq_id); + const cJSON *opts = cJSON_GetObjectItem(seq, "lane_opts"); + bool seq_timed = str_is(seq, "advance_mode", "time"); + double b = 0, s = 0; + int src = -1; + const cJSON *l; + cJSON_ArrayForEach(l, cJSON_GetObjectItem(seq, "lanes")) + { + src++; + const cJSON *o = cJSON_GetArrayItem(opts, src); + const cJSON *mode = cJSON_GetObjectItem(o, "mode"); + bool timed = cJSON_IsString(mode) ? !strcmp(mode->valuestring, "time") : seq_timed; + double rate = num(o, "rate", 1); + rate = rate < 0.25 ? 0.25 : (rate > 4 ? 4 : rate); + double total = 0; + const cJSON *st; + cJSON_ArrayForEach(st, l) + { + total += timed ? num(st, "seconds", 3) : num(st, "beats", 1); + } + double one = num(o, "offset", 0) + total / rate; + if (timed) { + s = one > s ? one : s; + } else { + b = one > b ? one : b; + } + } + double beat_s = (double)s_beat_iv / 1e6; + double bb = b > s / beat_s ? b : s / beat_s, ss = s > b * beat_s ? s : b * beat_s; + if (bb > 0) { + *beats = bb; + *secs = ss; + } +} + +// Lane 0's position in the pass, in beats: so far and per pass static void pass_position(int *at, int *per_pass) { - const lane_t *l = &s_run.lane[0]; - *at = *per_pass = 0; + const lane_t *l = &s_base.lane[0]; + int u_at = 0, u_tot = 0; for (int i = 0; i < l->n; i++) { - *per_pass += l->step[i].beats; + u_tot += l->step[i].units; } if (l->done) { - *at = *per_pass; - return; + u_at = u_tot; + } else { + for (int i = 0; i < l->idx && i < l->n; i++) { + u_at += l->step[i].units; + } + int bt = l->idx < l->n ? l->step[l->idx].units : 1; + u_at += l->count < 0 ? 0 : (l->count > bt ? bt : l->count); } - for (int i = 0; i < l->idx && i < l->n; i++) { - *at += l->step[i].beats; - } - int bt = l->idx < l->n ? l->step[l->idx].beats : 1; - *at += l->count < 0 ? 0 : (l->count > bt ? bt : l->count); + *at = u_at / UNITS; + *per_pass = (u_tot + UNITS - 1) / UNITS; } static int readout(char *buf, size_t len) { int at, tot; buf[0] = 0; - if (s_run.timed) { // seconds, not beats: no n/n readout + if (s_base.lane[0].timed) { // seconds, not beats: no n/n readout return 0; } pass_position(&at, &tot); - if (tot > 0 && s_run.lane[0].done) { + if (tot > 0 && s_base.lane[0].done) { return snprintf(buf, len, "%d/%d", tot, tot); } if (tot > 0 && at > 0) { // on the last beat of the pass show n/n @@ -660,39 +1281,95 @@ static int readout(char *buf, size_t len) return 0; } +static cJSON *lanes_status(const player_t *p) +{ + cJSON *arr = cJSON_CreateArray(); + int64_t now = esp_timer_get_time(); + for (int i = 0; i < p->nlanes; i++) { + const lane_t *l = &p->lane[i]; + cJSON *o = cJSON_CreateObject(); + cJSON_AddNumberToObject(o, "step", l->done ? l->n : l->idx); + cJSON_AddNumberToObject(o, "steps", l->n); + cJSON_AddStringToObject(o, "mode", l->timed ? "time" : "beats"); + cJSON_AddBoolToObject(o, "started", l->started); + cJSON_AddBoolToObject(o, "done", l->done); + if (l->chase) { + cJSON_AddNumberToObject(o, "pos", l->order[l->pos % (l->norder ? l->norder : 1)]); + } + if (l->idx < l->n) { + if (l->timed) { + cJSON_AddNumberToObject(o, "elapsed_ms", l->started ? (double)((now - l->t0) / 1000) : 0); + cJSON_AddNumberToObject(o, "duration_ms", l->step[l->idx].ms); + } else { + cJSON_AddNumberToObject(o, "beat_in_step", (double)l->count / UNITS); + cJSON_AddNumberToObject(o, "beats_in_step", (double)l->step[l->idx].units / UNITS); + } + } + cJSON_AddItemToArray(arr, o); + } + return arr; +} + +static cJSON *overlay_status(void) +{ + cJSON *o = cJSON_CreateObject(); + cJSON_AddBoolToObject(o, "active", s_ovl.active); + cJSON_AddBoolToObject(o, "pending", s_ovl_pend.set); + if (s_ovl_pend.set) { + cJSON_AddStringToObject(o, "pending_id", s_ovl_pend.seq); + } + if (s_ovl.active) { + cJSON_AddStringToObject(o, "sequence_id", s_ovl.seq); + cJSON_AddStringToObject(o, "zone_id", s_ovl.zone); + cJSON_AddNumberToObject(o, "drivers", s_covered.n); + // Progress: the furthest lane, in steps + double best = 0; + for (int i = 0; i < s_ovl.nlanes; i++) { + const lane_t *l = &s_ovl.lane[i]; + double f = l->n ? (l->done ? 1.0 : (double)l->idx / l->n) : 1.0; + best = f > best ? f : best; + } + cJSON_AddNumberToObject(o, "progress", best); + cJSON_AddItemToObject(o, "lanes", lanes_status(&s_ovl)); + } + return o; +} + cJSON *seqplay_status(void) { cJSON *o = cJSON_CreateObject(); store_lock(); - if (!s_run.active) { + cJSON_AddItemToObject(o, "overlay", overlay_status()); + if (!s_base.active) { cJSON_AddFalseToObject(o, "active"); cJSON_AddStringToObject(o, "beat_readout", ""); store_unlock(); return o; } - const lane_t *l = &s_run.lane[0]; + const lane_t *l = &s_base.lane[0]; int total = 0; - for (int i = 0; i < s_run.nlanes; i++) { - total += s_run.lane[i].n; + for (int i = 0; i < s_base.nlanes; i++) { + total += s_base.lane[i].n; } int at, per_pass; pass_position(&at, &per_pass); - int bps = !l->done && l->idx < l->n ? l->step[l->idx].beats : 1; - int beat_in = l->done ? 0 : (l->count > bps ? bps : l->count); - const char *pid = !l->done && l->idx < l->n ? l->step[l->idx].preset : ""; + int bps = !l->done && l->idx < l->n ? (l->step[l->idx].units + UNITS - 1) / UNITS : 1; + int beat_in = l->done ? 0 : (l->count / UNITS > bps ? bps : l->count / UNITS); + const char *pid = !l->done && l->idx < l->n && !l->step[l->idx].anim ? l->step[l->idx].id : ""; const cJSON *pdoc = pid[0] ? cJSON_GetObjectItem(store_coll("preset"), pid) : NULL; const cJSON *pname = cJSON_GetObjectItem(pdoc, "name"); char ro[24]; readout(ro, sizeof(ro)); cJSON_AddTrueToObject(o, "active"); - cJSON_AddStringToObject(o, "advance_mode", s_run.timed ? "time" : "beats"); - if (s_run.timed && !l->done && l->idx < l->n) { - cJSON_AddNumberToObject(o, "lane0_step_elapsed_ms", (double)((esp_timer_get_time() - l->t0) / 1000)); + cJSON_AddStringToObject(o, "advance_mode", l->timed ? "time" : "beats"); + if (l->timed && !l->done && l->idx < l->n) { + cJSON_AddNumberToObject(o, "lane0_step_elapsed_ms", + l->started ? (double)((esp_timer_get_time() - l->t0) / 1000) : 0); cJSON_AddNumberToObject(o, "lane0_step_duration_ms", l->step[l->idx].ms); } - cJSON_AddStringToObject(o, "sequence_id", s_run.seq); - cJSON_AddStringToObject(o, "zone_id", s_run.zone); - cJSON_AddNumberToObject(o, "num_lanes", s_run.nlanes); + cJSON_AddStringToObject(o, "sequence_id", s_base.seq); + cJSON_AddStringToObject(o, "zone_id", s_base.zone); + cJSON_AddNumberToObject(o, "num_lanes", s_base.nlanes); cJSON_AddNumberToObject(o, "total_sequence_steps", total); cJSON_AddNumberToObject(o, "lane0_current_step", l->done ? l->n : l->idx + 1); cJSON_AddNumberToObject(o, "lane0_lane_length", l->n); @@ -703,8 +1380,9 @@ cJSON *seqplay_status(void) cJSON_IsString(pname) && pname->valuestring[0] ? pname->valuestring : pid); cJSON_AddNumberToObject(o, "sequence_beat_at", at); cJSON_AddNumberToObject(o, "sequence_beats_per_pass", per_pass); - cJSON_AddNumberToObject(o, "sequence_loop_beat", s_run.loop_beat); + cJSON_AddNumberToObject(o, "sequence_loop_beat", s_base.loop_beat); cJSON_AddStringToObject(o, "beat_readout", ro); + cJSON_AddItemToObject(o, "lanes", lanes_status(&s_base)); store_unlock(); return o; } diff --git a/main/seqplay.h b/main/seqplay.h index 2dc13a0..32ee137 100644 --- a/main/seqplay.h +++ b/main/seqplay.h @@ -4,9 +4,9 @@ #include "cJSON.h" #include "esp_err.h" -// Sequence playback, as main's util/sequence_playback.py: one sequence plays at a time in a zone. -// Each lane is a list of {preset_id, beats} steps sent to the lane's groups; a step lasts its number -// of beats. Beats come from the mic's beat detector while audio timing is on and the tempo is locked, +// Sequence playback, as main's util/sequence_playback.py: one sequence plays at a time in a zone, and +// one animation (a sequence played once) can play over it. Each lane is a list of steps sent to the +// lane's targets; a step lasts its beats (beat lanes, counted in quarter beats) or seconds (time lanes). Beats come from the mic's beat detector while audio timing is on and the tempo is locked, // otherwise from a clock at settings.audio_simulated_bpm (default 120). // Start the beat task (after beat_start) @@ -18,7 +18,19 @@ esp_err_t seqplay_init(void); // Call with the store locked. esp_err_t seqplay_start(const char *seq_id, const char *zone_id, const char *profile_id, const char **msg); -// Stop; with clear, the zone's drivers drop their presets +// Play sequence seq_id once as an animation over whatever runs in the zone (zone_id NULL or "": the +// playing sequence's zone). wait: "now" (default), "beat" or "downbeat". The sequence underneath keeps +// time and skips the drivers the animation lights; when it ends they go back to the sequence's current +// step, or the zone's last preset. A new animation replaces the one playing. Call with the store locked. +esp_err_t seqplay_fire(const char *seq_id, const char *zone_id, const char *profile_id, const char *wait, + const char **msg); +// End the animation now and restore its drivers +void seqplay_overlay_stop(void); +// How long sequence seq_id plays once: *beats and *secs (each converted from the other at the current +// tempo); unchanged when it has no steps. Call with the store locked. +void seqplay_length(const char *seq_id, double *beats, double *secs); + +// Stop the sequence and any animation; with clear, the zone's drivers drop their presets void seqplay_stop(bool clear); // Stop when seq_id is the one playing (it was edited or deleted) void seqplay_stop_if(const char *seq_id); diff --git a/main/store.c b/main/store.c index 0229523..9179bfd 100644 --- a/main/store.c +++ b/main/store.c @@ -12,6 +12,20 @@ static const char *TAG = "store"; +// Where the collections live: the littlefs partition, or on the host build a directory +#if CONFIG_IDF_TARGET_LINUX +#include +static const char *db_root(void) +{ + const char *d = getenv("LED_SIM_DB"); + return d && d[0] ? d : "/db"; +} +#define PATH_LEN 256 +#else +#define db_root() "/db" +#define PATH_LEN 48 +#endif + static const char *const NAMES[] = { "device", "zone", "preset", "profile", "palette", "pattern", "sequence", "scene", "group", "output", "settings", @@ -44,13 +58,13 @@ static esp_err_t mount(const char *label, const char *base) static cJSON *load_file(const char *name) { - char path[48]; - snprintf(path, sizeof(path), "/db/%s.json", name); + char path[PATH_LEN]; + snprintf(path, sizeof(path), "%s/%s.json", db_root(), name); FILE *f = fopen(path, "rb"); if (!f) { // store_save removes the old file before renaming the finished .tmp, so a restart between the two leaves only .tmp - char tmp[48]; - snprintf(tmp, sizeof(tmp), "/db/%s.tmp", name); + char tmp[PATH_LEN]; + snprintf(tmp, sizeof(tmp), "%s/%s.tmp", db_root(), name); if (rename(tmp, path) == 0) { ESP_LOGW(TAG, "%s: recovered from %s", path, tmp); f = fopen(path, "rb"); @@ -92,7 +106,7 @@ esp_err_t store_init(void) cJSON_InitHooks(&hooks); s_lock = xSemaphoreCreateRecursiveMutex(); ESP_RETURN_ON_FALSE(s_lock, ESP_ERR_NO_MEM, TAG, "mutex"); - ESP_RETURN_ON_ERROR(mount("db", "/db"), TAG, "mount db"); + ESP_RETURN_ON_ERROR(mount("db", db_root()), TAG, "mount db"); for (size_t i = 0; i < N_COLL; i++) { s_coll[i] = load_file(NAMES[i]); ESP_LOGI(TAG, "%s: %d records", NAMES[i], cJSON_GetArraySize(s_coll[i])); @@ -118,9 +132,9 @@ esp_err_t store_save(const char *name) char *json = cJSON_PrintUnformatted(s_coll[i]); ESP_RETURN_ON_FALSE(json, ESP_ERR_NO_MEM, TAG, "print"); - char tmp[48], path[48]; - snprintf(tmp, sizeof(tmp), "/db/%s.tmp", name); - snprintf(path, sizeof(path), "/db/%s.json", name); + char tmp[PATH_LEN], path[PATH_LEN]; + snprintf(tmp, sizeof(tmp), "%s/%s.tmp", db_root(), name); + snprintf(path, sizeof(path), "%s/%s.json", db_root(), name); FILE *f = fopen(tmp, "wb"); esp_err_t ret = ESP_FAIL; if (f) { diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 74d29f4..9eabd9a 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -15,7 +15,7 @@ CONFIG_COMPILER_OPTIMIZATION_SIZE=y CONFIG_FREERTOS_HZ=1000 CONFIG_ESP_WIFI_ESPNOW_MAX_ENCRYPT_NUM=7 CONFIG_ESPNOW_CHANNEL=5 -CONFIG_BEAT_TEST_GPIO=4 +CONFIG_BEAT_TEST_GPIO=44 # A new OTA image must confirm itself (ota_confirm_later) or the bootloader goes back to the old one CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y # /ws (browser websocket) diff --git a/sim-controller/.gitignore b/sim-controller/.gitignore new file mode 100644 index 0000000..c5d0f61 --- /dev/null +++ b/sim-controller/.gitignore @@ -0,0 +1,5 @@ +build/ +sdkconfig +sdkconfig.old +data/ +dependencies.lock diff --git a/sim-controller/CMakeLists.txt b/sim-controller/CMakeLists.txt new file mode 100644 index 0000000..e984bbb --- /dev/null +++ b/sim-controller/CMakeLists.txt @@ -0,0 +1,6 @@ +# led-controller firmware built for the ESP-IDF linux target, for led-simulator. See README.md. +cmake_minimum_required(VERSION 3.16) +set(EXTRA_COMPONENT_DIRS ${CMAKE_CURRENT_LIST_DIR}/../main ${CMAKE_CURRENT_LIST_DIR}/components/sim) +set(COMPONENTS main sim) +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(led_controller_sim) diff --git a/sim-controller/README.md b/sim-controller/README.md new file mode 100644 index 0000000..f94c189 --- /dev/null +++ b/sim-controller/README.md @@ -0,0 +1,24 @@ +# sim-controller + +The led-controller firmware (`main/`) built for ESP-IDF's **linux** target, so the whole rig runs on a +computer with no hardware: + +``` +sim-controller (http://localhost:8080) ──UDP 8766──▶ led-simulator ──▶ 16 × led-driver/src + ◀── src_mac + packet ── ◀── +``` + +```bash +make sim # headless drivers (CPython), plus this controller +make sim-web # drivers in the browser at http://localhost:8765/, plus this controller +make test-sim # test/api against it, including test_rig_sim.py +``` + +- Same sources as the board, minus Ethernet, Wi-Fi, mic and firmware OTA (`components/sim`: `app_main`, + `esp_timer`, mic/OTA/littlefs stubs, `driver/gpio.h` and lwip socket headers). The beat clock at + `audio_simulated_bpm` stands in for the mic. +- ESP-NOW goes through `main/bridge.c`: `dst_mac + packet` to the simulator, `src_mac + packet` back. + On the board the same bridge runs when `settings.bridge` is set. +- The store is a directory: `LED_SIM_DB` (default `sim-controller/data` under `make sim`). +- `LED_SIM_HTTP_PORT` (default 8080), `LED_SIM_BRIDGE` (default `127.0.0.1:8766`). +- The binary runs on the host, not in Docker: rootless Docker's `--network host` is its own namespace. diff --git a/sim-controller/components/sim/CMakeLists.txt b/sim-controller/components/sim/CMakeLists.txt new file mode 100644 index 0000000..2bc59ad --- /dev/null +++ b/sim-controller/components/sim/CMakeLists.txt @@ -0,0 +1,6 @@ +idf_component_register(SRCS "sim_main.c" "sim_stubs.c" "sim_timer.c" + INCLUDE_DIRS "include" + REQUIRES esp_http_server esp_timer esp_event json main freertos) +target_link_libraries(${COMPONENT_LIB} INTERFACE m) +# main's objects need esp_timer and libm, which come after it on the link line: link sim (and libm) last +target_link_libraries(__idf_main INTERFACE ${COMPONENT_LIB} m) diff --git a/sim-controller/components/sim/include/driver/gpio.h b/sim-controller/components/sim/include/driver/gpio.h new file mode 100644 index 0000000..f08ebf5 --- /dev/null +++ b/sim-controller/components/sim/include/driver/gpio.h @@ -0,0 +1,21 @@ +#pragma once +// Host build: no pins. Calls succeed and do nothing. +#include +#include "esp_err.h" +typedef int gpio_num_t; +typedef enum { GPIO_MODE_DISABLE = 0, GPIO_MODE_INPUT = 1, GPIO_MODE_OUTPUT = 2, GPIO_MODE_INPUT_OUTPUT = 3 } gpio_mode_t; +typedef enum { GPIO_PULLUP_DISABLE = 0, GPIO_PULLUP_ENABLE = 1 } gpio_pullup_t; +typedef enum { GPIO_PULLDOWN_DISABLE = 0, GPIO_PULLDOWN_ENABLE = 1 } gpio_pulldown_t; +typedef enum { GPIO_INTR_DISABLE = 0 } gpio_int_type_t; +typedef struct { + uint64_t pin_bit_mask; + gpio_mode_t mode; + gpio_pullup_t pull_up_en; + gpio_pulldown_t pull_down_en; + gpio_int_type_t intr_type; +} gpio_config_t; +static inline esp_err_t gpio_config(const gpio_config_t *c) { (void)c; return ESP_OK; } +static inline esp_err_t gpio_set_direction(int pin, gpio_mode_t m) { (void)pin; (void)m; return ESP_OK; } +static inline esp_err_t gpio_set_level(int pin, uint32_t l) { (void)pin; (void)l; return ESP_OK; } +static inline int gpio_get_level(int pin) { (void)pin; return 1; } +static inline esp_err_t gpio_reset_pin(int pin) { (void)pin; return ESP_OK; } diff --git a/sim-controller/components/sim/include/esp_littlefs.h b/sim-controller/components/sim/include/esp_littlefs.h new file mode 100644 index 0000000..d44008b --- /dev/null +++ b/sim-controller/components/sim/include/esp_littlefs.h @@ -0,0 +1,11 @@ +#pragma once +// Host build: the store's /db is a real directory (docker volume), so mounting is a no-op +#include +#include "esp_err.h" +typedef struct { + const char *base_path; + const char *partition_label; + bool format_if_mount_failed; + bool grow_on_mount; +} esp_vfs_littlefs_conf_t; +esp_err_t esp_vfs_littlefs_register(const esp_vfs_littlefs_conf_t *conf); diff --git a/sim-controller/components/sim/include/lwip/netdb.h b/sim-controller/components/sim/include/lwip/netdb.h new file mode 100644 index 0000000..9b4fda5 --- /dev/null +++ b/sim-controller/components/sim/include/lwip/netdb.h @@ -0,0 +1,2 @@ +#pragma once +#include diff --git a/sim-controller/components/sim/include/lwip/sockets.h b/sim-controller/components/sim/include/lwip/sockets.h new file mode 100644 index 0000000..e5324b1 --- /dev/null +++ b/sim-controller/components/sim/include/lwip/sockets.h @@ -0,0 +1,9 @@ +#pragma once +// Host build: lwip's socket API is the host's +#include +#include +#include +#include +#include +#include +#include diff --git a/sim-controller/components/sim/sim_main.c b/sim-controller/components/sim/sim_main.c new file mode 100644 index 0000000..1245f55 --- /dev/null +++ b/sim-controller/components/sim/sim_main.c @@ -0,0 +1,126 @@ +/* + * led-controller on the host (ESP-IDF linux target), for led-simulator. + * + * The same firmware sources as the board, minus the hardware: no Ethernet, Wi-Fi, mic or OTA. + * ESP-NOW frames go to the UDP bridge (led-simulator/bridge_sim.py) instead of the radio. + * + * Environment: + * LED_SIM_BRIDGE simulator address, default 127.0.0.1:8766 (overrides settings "bridge") + * LED_SIM_HTTP_PORT web UI / API port, default 8080 + * The store is the /db directory (mount a volume there to keep it). + */ +#include +#include +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" +#include "esp_event.h" +#include "esp_http_server.h" +#include "esp_timer.h" +#include "cJSON.h" +#include "api.h" +#include "audio.h" +#include "audio_api.h" +#include "beat.h" +#include "drv_ota.h" +#include "engine.h" +#include "espnow_tx.h" +#include "bridge.h" +#include "mic.h" +#include "ota.h" +#include "rel.h" +#include "seqplay.h" +#include "store.h" +#include "web.h" +#include "ws.h" + +static const char *TAG = "sim"; + +static esp_err_t status_get_handler(httpd_req_t *req) +{ + beat_info_t bi; + espnow_stats_t es; + rel_stats_t rs; + char buf[1024]; + beat_get_info(&bi); + espnow_tx_stats(&es); + rel_stats(&rs); + const char *br = bridge_addr(); + snprintf(buf, sizeof(buf), + "{\"sim\":true,\"bridge\":\"%s\",\"ap\":false,\"ip\":\"127.0.0.1\",\"mac\":\"02:51:4d:00:00:00\",\"uptime_s\":%lld," + "\"reset_reason\":\"power_on\",\"free_heap\":0,\"free_internal\":0," + "\"mic_rms_dbfs\":-120.0,\"mic_peak_dbfs\":-120.0,\"mic_spl_db\":0.0," + "\"bpm\":%.1f,\"beat_confidence\":%.2f,\"beat_locked\":%s,\"beats\":%lu,\"onsets\":%lu," + "\"meter\":%d,\"bar_beat\":%d,\"bar_sure\":%s,\"downbeats\":%lu," + "\"espnow\":{\"beats_sent\":%lu,\"beat_send_fail\":%lu,\"bulk_sent\":%lu," + "\"bulk_retries\":%lu,\"bulk_failed\":%lu,\"sim_dropped\":%lu}," + "\"link\":{\"ver\":%u,\"acked\":%lu,\"ack_timeouts\":%lu,\"retransmits\":%lu,\"resyncs\":%lu,\"digests\":%lu}}", + br ? br : "", (long long)(esp_timer_get_time() / 1000000), + bi.bpm, bi.confidence, bi.locked ? "true" : "false", (unsigned long)bi.beat_count, + (unsigned long)bi.onset_count, bi.meter, bi.bar_beat, bi.bar_sure ? "true" : "false", + (unsigned long)bi.downbeat_count, + (unsigned long)es.beats_sent, (unsigned long)es.beat_send_fail, (unsigned long)es.bulk_sent, + (unsigned long)es.bulk_retries, (unsigned long)es.bulk_failed, (unsigned long)es.sim_dropped, rel_ver(), + (unsigned long)rs.acked, (unsigned long)rs.ack_timeouts, (unsigned long)rs.retransmits, + (unsigned long)rs.resyncs, (unsigned long)rs.digests); + httpd_resp_set_type(req, "application/json"); + return httpd_resp_sendstr(req, buf); +} + +static void start_webserver(void) +{ + static httpd_handle_t server; + httpd_config_t config = HTTPD_DEFAULT_CONFIG(); + const char *port = getenv("LED_SIM_HTTP_PORT"); + config.server_port = port ? atoi(port) : 8080; + config.ctrl_port = config.server_port + 10000; + config.uri_match_fn = httpd_uri_match_wildcard; + config.max_uri_handlers = 80; + config.stack_size = 32768; // in StackType_t words on linux (8 bytes); the depth type is 16-bit + config.lru_purge_enable = true; + config.max_open_sockets = 12; + config.backlog_conn = 10; + if (httpd_start(&server, &config) != ESP_OK) { + ESP_LOGE(TAG, "HTTP server start failed on port %d", config.server_port); + abort(); + } + const httpd_uri_t status_uri = { .uri = "/status", .method = HTTP_GET, .handler = status_get_handler }; + httpd_register_uri_handler(server, &status_uri); + web_register(server); + audio_register(server); + audio_api_register(server); + ota_register(server); + drv_ota_register(server); + api_register(server); + ws_register(server); + ESP_LOGI(TAG, "web UI and API on http://localhost:%d/", config.server_port); +} + +static const char *settings_str(const char *key) +{ + cJSON *v = cJSON_GetObjectItem(store_settings(), key); + return cJSON_IsString(v) && v->valuestring[0] ? v->valuestring : NULL; +} + +void app_main(void) +{ + signal(SIGPIPE, SIG_IGN); // a browser that hangs up mid-reply is an error return, as on lwip, not a kill + ESP_ERROR_CHECK(store_init()); + ESP_ERROR_CHECK(esp_event_loop_create_default()); + const char *br = getenv("LED_SIM_BRIDGE"); + if (!br || !br[0]) { + br = settings_str("bridge"); + } + espnow_tx_set_bridge(br ? br : "127.0.0.1:8766"); + ESP_ERROR_CHECK(espnow_tx_init(NULL, NULL)); + start_webserver(); + ESP_ERROR_CHECK(rel_init()); + ESP_ERROR_CHECK(engine_init()); + ESP_ERROR_CHECK(mic_start()); + ESP_ERROR_CHECK(seqplay_init()); + for (;;) { + vTaskDelay(pdMS_TO_TICKS(1000)); + } +} diff --git a/sim-controller/components/sim/sim_stubs.c b/sim-controller/components/sim/sim_stubs.c new file mode 100644 index 0000000..b7188d2 --- /dev/null +++ b/sim-controller/components/sim/sim_stubs.c @@ -0,0 +1,61 @@ +// Host build: what the board's hardware files (mic.c, ota.c, littlefs) provide, without hardware +#include +#include +#include "esp_littlefs.h" +#include "esp_log.h" +#include "beat.h" +#include "mic.h" +#include "ota.h" + +esp_err_t esp_vfs_littlefs_register(const esp_vfs_littlefs_conf_t *conf) +{ + mkdir(conf->base_path, 0755); + return ESP_OK; +} + +// No microphone: audio stays stopped, so the simulated-BPM clock times sequences +esp_err_t mic_start(void) +{ + return beat_start(MIC_SAMPLE_RATE); // never fed: no beats detected, the BPM clock stands in +} + +void mic_get_level(mic_level_t *out) +{ + out->rms_dbfs = -120; + out->peak_dbfs = -120; + out->spl_db = 0; +} + +esp_err_t mic_stream_open(void) +{ + return ESP_ERR_NOT_SUPPORTED; +} + +size_t mic_stream_read(void *buf, size_t len, TickType_t timeout) +{ + (void)buf; + (void)len; + vTaskDelay(timeout ? timeout : 1); + return 0; +} + +void mic_stream_close(void) +{ +} + +void ota_confirm_later(void) +{ +} + +static esp_err_t ota_unsupported(httpd_req_t *req) +{ + httpd_resp_set_status(req, "501 Not Implemented"); + httpd_resp_set_type(req, "application/json"); + return httpd_resp_sendstr(req, "{\"error\":\"firmware OTA is not available in the simulator\"}"); +} + +esp_err_t ota_register(httpd_handle_t server) +{ + const httpd_uri_t u = { .uri = "/ota", .method = HTTP_POST, .handler = ota_unsupported }; + return httpd_register_uri_handler(server, &u); +} diff --git a/sim-controller/components/sim/sim_timer.c b/sim-controller/components/sim/sim_timer.c new file mode 100644 index 0000000..cef403b --- /dev/null +++ b/sim-controller/components/sim/sim_timer.c @@ -0,0 +1,95 @@ +// Host build: esp_timer (the linux target ships only its header). One FreeRTOS task per timer. +#include +#include +#include +#include "esp_timer.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "freertos/task.h" + +struct esp_timer { + esp_timer_cb_t cb; + void *arg; + volatile int64_t due_us; // 0 = stopped + volatile int64_t period_us; + SemaphoreHandle_t wake; +}; + +int64_t esp_timer_get_time(void) +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (int64_t)ts.tv_sec * 1000000 + ts.tv_nsec / 1000; +} + +static void timer_task(void *arg) +{ + struct esp_timer *t = arg; + for (;;) { + int64_t due = t->due_us; + if (!due) { + xSemaphoreTake(t->wake, portMAX_DELAY); + continue; + } + int64_t wait = due - esp_timer_get_time(); + if (wait > 0) { + TickType_t ticks = (TickType_t)((wait + 999) / 1000 / portTICK_PERIOD_MS); + if (xSemaphoreTake(t->wake, ticks ? ticks : 1) == pdTRUE) { + continue; // restarted or stopped: look again + } + if (t->due_us != due || esp_timer_get_time() < due) { + continue; + } + } + t->due_us = t->period_us ? due + t->period_us : 0; + t->cb(t->arg); + } +} + +esp_err_t esp_timer_create(const esp_timer_create_args_t *args, esp_timer_handle_t *out) +{ + struct esp_timer *t = calloc(1, sizeof(*t)); + if (!t) { + return ESP_ERR_NO_MEM; + } + t->cb = args->callback; + t->arg = args->arg; + t->wake = xSemaphoreCreateBinary(); + xTaskCreate(timer_task, args->name ? args->name : "esp_timer", 16384, t, 22, NULL); + *out = t; + return ESP_OK; +} + +static esp_err_t arm(esp_timer_handle_t t, uint64_t us, uint64_t period) +{ + t->period_us = period; + t->due_us = esp_timer_get_time() + (int64_t)us; + if (!t->due_us) { + t->due_us = 1; + } + xSemaphoreGive(t->wake); + return ESP_OK; +} + +esp_err_t esp_timer_start_once(esp_timer_handle_t t, uint64_t timeout_us) +{ + return arm(t, timeout_us, 0); +} + +esp_err_t esp_timer_start_periodic(esp_timer_handle_t t, uint64_t period) +{ + return arm(t, period, period); +} + +esp_err_t esp_timer_stop(esp_timer_handle_t t) +{ + bool was = t->due_us != 0; + t->due_us = 0; + xSemaphoreGive(t->wake); + return was ? ESP_OK : ESP_ERR_INVALID_STATE; +} + +bool esp_timer_is_active(esp_timer_handle_t t) +{ + return t->due_us != 0; +} diff --git a/sim-controller/sdkconfig.defaults b/sim-controller/sdkconfig.defaults new file mode 100644 index 0000000..22c24bb --- /dev/null +++ b/sim-controller/sdkconfig.defaults @@ -0,0 +1,7 @@ +CONFIG_IDF_TARGET="linux" +CONFIG_FREERTOS_HZ=1000 +CONFIG_ESPNOW_CHANNEL=5 +CONFIG_HTTPD_WS_SUPPORT=y +CONFIG_HTTPD_WS_POST_HANDSHAKE_CB_SUPPORT=y +CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024 +CONFIG_LWIP_MAX_SOCKETS=16 diff --git a/test/api/test_devices.py b/test/api/test_devices.py index dff2afd..2608451 100644 --- a/test/api/test_devices.py +++ b/test/api/test_devices.py @@ -146,3 +146,28 @@ def test_update_groups(c): assert data["total"] == len(espnow) assert data["sent"] + data["failed"] == data["total"] assert resp.status_code == (200 if data["failed"] == 0 else 503) + + +def test_layout_saves_and_clears_placement(c, espnow_dev): + resp = c.post("/devices/layout", json={"placements": {"aa:bb:cc:dd:ee:ff": {"x0": 100, "y0": 200, "x1": 300, "y1": 200}}}) + assert resp.status_code == 200, resp.text + assert resp.json()["updated"] == 1 and resp.json()["sent"] >= 1 # this driver is placed, so it is sent + dev = c.get(f"/devices/{ESPNOW_MAC}").json() + assert dev["placement"] == {"x0": 100, "y0": 200, "x1": 300, "y1": 200} + resp = c.post("/devices/layout", json={"placements": {ESPNOW_MAC: None}}) + assert resp.json()["updated"] == 1 + assert "placement" not in c.get(f"/devices/{ESPNOW_MAC}").json() + + +def test_layout_needs_placements(c): + assert c.post("/devices/layout", json={}).status_code == 400 + # Unknown devices and bad placements are skipped + resp = c.post("/devices/layout", json={"placements": {"010203040506": {"x0": 1, "y0": 1, "x1": 2, "y1": 2}, + ESPNOW_MAC: {"x0": "a"}}}) + assert resp.status_code == 200 and resp.json()["updated"] == 0 + + +def test_layout_keeps_rig_center(c, espnow_dev): + resp = c.post("/devices/layout", json={"placements": {}, "center": {"x": 500, "y": 350}}) + assert resp.status_code == 200 + assert c.get("/settings").json()["rig_center"] == {"x": 500, "y": 350} diff --git a/test/api/test_presets.py b/test/api/test_presets.py index c4a105e..7d55f2f 100644 --- a/test/api/test_presets.py +++ b/test/api/test_presets.py @@ -75,3 +75,19 @@ def test_push_requires_payload(c): def test_delete(c, preset): assert c.delete(f"/presets/{preset}").status_code == 200 assert c.get(f"/presets/{preset}").status_code == 404 + + +def test_fade_and_beat_time_round_trip_and_send(c, preset): + resp = c.put(f"/presets/{preset}", json={"pattern": "hit", "fade": 400, "beat_time": True, "n3": 2}) + assert resp.status_code == 200 + rec = c.get(f"/presets/{preset}").json() + assert rec["fade"] == 400 and rec["beat_time"] is True + # Goes out as a v3 frame; made-up driver name, so no real driver applies it + resp = c.post("/presets/send", json={"preset_ids": [preset], "save": False, "targets": ["020000000099"]}) + assert resp.status_code == 200 and resp.json()["presets_sent"] == 1 + + +def test_animation_patterns_have_definitions(c): + pats = c.get("/patterns").json() + for name in ("hit", "keyframes", "sweep", "ripple", "spin", "grow", "field_wave"): + assert name in pats and "n1" in pats[name], name diff --git a/test/api/test_rig_sim.py b/test/api/test_rig_sim.py new file mode 100644 index 0000000..ff65bc1 --- /dev/null +++ b/test/api/test_rig_sim.py @@ -0,0 +1,356 @@ +"""The whole rig, simulated: led-controller's host build (sim-controller/) bridged to led-simulator's 16 +drivers (2 columns x 8 rows, data-in heads in the middle). Checks what the bars actually show. + + make sim # simulator + controller on this computer + make test-sim # this file against them + +Skipped unless the controller reports "sim": true in /status and the simulator answers on LED_SIM_URL +(default http://localhost:8765). +""" + +import os +import sys +import time +from pathlib import Path + +import pytest +import requests + +from helpers import new_id, unique + +SIM_URL = os.environ.get("LED_SIM_URL", "http://localhost:8765") +ROWS, COLS, LEDS = 8, 2, 119 +BARS = list(range(1, ROWS * COLS + 1)) # strip n: row (n-1)//2, column (n-1)%2 (0 left, 1 right) + + +def mac(n): + return "02514d0000%02x" % n + + +def row_of(n): + return (n - 1) // COLS + + +def col_of(n): + return (n - 1) % COLS + + +GROUPS = {"Left": [n for n in BARS if col_of(n) == 0], "Right": [n for n in BARS if col_of(n) == 1]} +GROUPS.update({f"Row {r + 1}": [n for n in BARS if row_of(n) == r] for r in range(ROWS)}) +GROUPS.update({f"Bar {n:02d}": [n] for n in BARS}) + +# The rig layout is the simulator's (led-simulator/layout.py rig_placements); it sends it to the controller +sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "led-simulator")) +from layout import rig_placements # noqa: E402 + + +def places(): + """{bar: [x0, y0, x1, y1]}: where each driver was told it sits (rig units, centre 0, y up).""" + d = requests.get(SIM_URL + "/api/pixels", timeout=5).json() + return {s["n"]: s.get("place") for s in d["strips"]} + + +def led_x(place, i): + x0, _, x1, _ = place + return x0 + (x1 - x0) * (i + 0.5) / LEDS + + +def frames(): + """{bar: [(r, g, b), ...]} as the simulator's strips show them now.""" + d = requests.get(SIM_URL + "/api/pixels", timeout=5).json() + out = {} + for s in d["strips"]: + b = bytes.fromhex(s["rgb"]) + out[s["n"]] = [tuple(b[i:i + 3]) for i in range(0, len(b), 3)] + return out + + +def lit(px, floor=8): + return [i for i, c in enumerate(px) if max(c) > floor] + + +def wait_for(check, timeout=6.0, every=0.1): + """Poll check() until it returns a truthy value; return its last value.""" + end = time.monotonic() + timeout + while True: + v = check() + if v or time.monotonic() > end: + return v + time.sleep(every) + + +def sample(seconds, every=0.04): + out = [] + end = time.monotonic() + seconds + while time.monotonic() < end: + out.append(frames()) + time.sleep(every) + return out + + +@pytest.fixture(scope="module", autouse=True) +def need_sim(c): + try: + st = c.get("/status").json() + requests.get(SIM_URL + "/api/pixels", timeout=3).raise_for_status() + except (requests.RequestException, ValueError): + pytest.skip(f"no simulated rig (controller host build + led-simulator at {SIM_URL})") + if not st.get("sim"): + pytest.skip("the controller is not the simulator build") + + +@pytest.fixture(scope="module") +def rig(c, need_sim): + """All 16 bars registered and placed, a profile, the 26 groups and a zone over the whole rig.""" + want = {mac(n) for n in BARS} + + def registered(): + return want <= set(c.get("/devices").json()) + + if not wait_for(registered, timeout=3): + c.post("/devices/ping", json={"timeout_s": 2}) + assert wait_for(registered, timeout=15), "the simulator's 16 drivers did not register" + + pid = new_id(c.post("/profiles", json={"name": unique("pytest-rig")})) + assert c.post(f"/profiles/{pid}/apply").status_code == 200 + gids = {} + for name, bars in GROUPS.items(): + r = c.post("/groups", json={"name": name, "devices": [mac(n) for n in bars]}) + assert r.status_code == 201, r.text + gids[name] = new_id(r) + zid = new_id(c.post("/zones", json={"name": unique("pytest-rig"), "group_ids": [gids["Left"], gids["Right"]]})) + # Start unplaced, so the simulator sends its whole layout (it never overwrites a placed bar) + c.post("/devices/layout", json={"placements": {mac(n): None for n in BARS}}) + seeded = requests.post(SIM_URL + "/api/seed-layout", timeout=60).json() + yield {"profile": pid, "groups": gids, "zone": zid, "layout": seeded.get("sent")} + c.post("/sequences/stop") + c.post(f"/zones/{zid}/stop") + c.delete(f"/zones/{zid}") + for gid in gids.values(): + c.delete(f"/groups/{gid}") + c.delete(f"/profiles/{pid}") + + +def make_preset(c, **fields): + body = {"name": unique("pytest-rig"), "brightness": 255, "colors": ["#ff0000"], "pattern": "on"} + body.update(fields) + r = c.post("/presets", json=body) + assert r.status_code == 201, r.text + return new_id(r) + + +def all_dark(c, rig): + c.post("/sequences/stop") + c.put(f"/zones/{rig['zone']}", json={"group_ids": [rig["groups"]["Left"], rig["groups"]["Right"]]}) + c.post(f"/zones/{rig['zone']}/stop") + assert wait_for(lambda: not any(lit(px) for px in frames().values())), "the rig did not go dark" + + +def play_all(c, rig, preset_id): + c.put(f"/zones/{rig['zone']}", json={"group_ids": [rig["groups"]["Left"], rig["groups"]["Right"]]}) + r = c.post(f"/zones/{rig['zone']}/play", json={"preset_id": preset_id}) + assert r.status_code == 200, r.text + + +# --- the rig and its groups --------------------------------------------------------------------------- + + +def test_all_bars_registered(c, rig): + devs = c.get("/devices").json() + names = {devs[mac(n)]["name"] for n in BARS} + assert names == {f"sim-{n:02d}" for n in BARS} + assert all(devs[mac(n)]["transport"] == "espnow" for n in BARS) + + +def test_simulator_gives_controller_its_layout(c, rig): + assert rig["layout"] == {"updated": 16, "sent": 16, "failed": 0} + want, centre = rig_placements({"rows": ROWS, "columns": COLS, "leds_per_strip": LEDS}) + devs = c.get("/devices").json() + for n in BARS: + got = devs[mac(n)]["placement"] + assert all(abs(got[k] - want[n - 1][k]) < 0.01 for k in ("x0", "y0", "x1", "y1")), (n, got) + assert c.get("/settings").json()["rig_center"] == centre + + +def test_drivers_place_heads_in_the_middle(rig): + """Each driver's place (as the controller normalised it) starts next to the centre line and runs out.""" + pl = wait_for(lambda: (lambda p: p if all(p.values()) else None)(places())) + assert pl, "drivers did not all get a place" + for n in BARS: + x0, y0, x1, y1 = pl[n] + assert abs(x0) < 0.1 and abs(x1) > 0.8, (n, pl[n]) # far corners are diagonal: x1 is about 0.84 + assert (x1 < 0) == (col_of(n) == 0), (n, pl[n]) + assert y0 == y1 + ys = [pl[2 * r + 1][1] for r in range(ROWS)] + assert ys == sorted(ys, reverse=True), "row 1 should be at the top" + + +def test_seed_keeps_a_moved_bar(c, rig): + moved = {"x0": 480, "y0": 600, "x1": 100, "y1": 300} + c.post("/devices/layout", json={"placements": {mac(1): moved}}) + assert requests.post(SIM_URL + "/api/seed-layout", timeout=60).json()["sent"] is None + assert c.get(f"/devices/{mac(1)}").json()["placement"] == moved + want, _ = rig_placements({"rows": ROWS, "columns": COLS, "leds_per_strip": LEDS}) + c.post("/devices/layout", json={"placements": {mac(1): want[0]}}) + + +def test_bridge_status(c): + st = c.get("/status").json() + assert st["bridge"].endswith(":8766") + assert st["espnow"]["bulk_failed"] == 0 + + +def test_all_26_groups_stored(c, rig): + listing = c.get("/groups").json() + assert len(GROUPS) == 26 + for name, gid in rig["groups"].items(): + g = c.get(f"/groups/{gid}").json() + assert g["name"] == name + assert sorted(g["devices"]) == sorted(mac(n) for n in GROUPS[name]) + assert gid in listing + + +def test_driver_group_membership(c, rig): + """Every driver is told its groups; the controller waits for all 16 acks.""" + r = c.post("/devices/groups") + assert r.status_code == 200, r.text + assert r.json()["failed"] == 0 and r.json()["sent"] >= 16 + + +def test_group_edit_and_delete(c, rig): + gid = new_id(c.post("/groups", json={"name": unique("pytest-tmp"), "devices": [mac(1)]})) + assert c.put(f"/groups/{gid}", json={"name": "Renamed", "devices": [mac(2), mac(3).upper()]}).status_code == 200 + g = c.get(f"/groups/{gid}").json() + assert g["name"] == "Renamed" and sorted(g["devices"]) == [mac(2), mac(3)] + assert c.delete(f"/groups/{gid}").status_code == 200 + assert c.get(f"/groups/{gid}").status_code == 404 + + +@pytest.mark.parametrize("name", list(GROUPS)) +def test_group_lights_only_its_bars(c, rig, name): + all_dark(c, rig) + red = make_preset(c, pattern="on", colors=["#ff0000"]) + c.put(f"/zones/{rig['zone']}", json={"group_ids": [rig["groups"][name]]}) + assert c.post(f"/zones/{rig['zone']}/play", json={"preset_id": red}).status_code == 200 + want = set(GROUPS[name]) + + def shown(): + f = frames() + on = {n for n, px in f.items() if px and all(c_[0] > 200 and c_[1] < 20 and c_[2] < 20 for c_ in px)} + dark = {n for n, px in f.items() if not lit(px)} + return on == want and dark == set(BARS) - want + + assert wait_for(shown), f"{name}: expected bars {sorted(want)} red and the rest dark" + c.delete(f"/presets/{red}") + + +# --- the field patterns follow the bridge layout -------------------------------------------------------- +# A driver restarts its pattern on every beat and the controller's clock beats every 0.5 s (120 BPM), +# so the delays here are shorter than a beat. + +FIELD = ["sweep", "ripple", "spin", "grow", "field_wave"] + + +@pytest.mark.parametrize("pattern", FIELD) +def test_field_pattern_animates_across_rig(c, rig, pattern): + all_dark(c, rig) + pid = make_preset(c, pattern=pattern, colors=["#ff0000", "#0000ff"], delay=400, auto=True) + play_all(c, rig, pid) + shots = sample(1.5) + seen = {n for f in shots for n, px in f.items() if lit(px)} + changes = sum(1 for a, b in zip(shots, shots[1:]) if a != b) + assert len(seen) == 16, f"{pattern}: bars never lit: {sorted(set(BARS) - seen)}" + assert changes > len(shots) // 3, f"{pattern}: the frames hardly change ({changes} of {len(shots)})" + c.delete(f"/presets/{pid}") + + +def test_ripple_is_mirrored_about_the_centre(c, rig): + """Heads meet in the middle, so a ring from the centre lights the same LEDs on both bars of a row. + + Each simulated driver sends frames on its own 30 fps clock, so single samples of a fast ring differ + by a frame or two; the ring's position must agree on average and in most samples.""" + all_dark(c, rig) + pid = make_preset(c, pattern="ripple", colors=["#ffffff"], delay=400, auto=True, n1=200) + play_all(c, rig, pid) + diffs, shifts = [], [] + for f in sample(2.0, every=0.02): + for r in range(ROWS): + a, b = lit(f[2 * r + 1]), lit(f[2 * r + 2]) + if a and b: + diffs.append(len(set(a) ^ set(b))) + shifts.append((min(a) + max(a)) / 2 - (min(b) + max(b)) / 2) + c.delete(f"/presets/{pid}") + assert len(diffs) > 50 + diffs.sort() + mean_shift = sum(shifts) / len(shifts) + assert diffs[len(diffs) // 2] <= 12, f"left and right bars differ by a median {diffs[len(diffs) // 2]} LEDs" + assert abs(mean_shift) <= 3, f"the ring sits {mean_shift:.1f} LEDs further out on one side" + + +def test_ripple_reaches_inner_rows_before_outer_rows(c, rig): + all_dark(c, rig) + pid = make_preset(c, pattern="ripple", colors=["#ffffff"], delay=450, auto=True) + play_all(c, rig, pid) + # Mean distance from the centre of the lit LEDs at their nearest point: rows 4 and 5 (index 3, 4) are + # nearest the centre, so their heads light first in each ring. + first_lit = {} + t0 = time.monotonic() + while time.monotonic() - t0 < 1.0 and len(first_lit) < 8: + f = frames() + for r in range(ROWS): + if r not in first_lit and lit(f[2 * r + 1]) and min(lit(f[2 * r + 1])) < 5: + first_lit[r] = time.monotonic() - t0 + time.sleep(0.01) + assert {3, 4} <= set(first_lit), f"middle rows never lit at their heads: {first_lit}" + c.delete(f"/presets/{pid}") + + +def test_sweep_band_is_one_place_across_both_columns(c, rig): + """A left-to-right sweep is a vertical band: its lit LEDs share one x on every row, so it must not + show on both columns at the same index (it would, if the drivers ignored their placements).""" + all_dark(c, rig) + pid = make_preset(c, pattern="sweep", colors=["#00ff00"], delay=450, auto=True, n1=0, n2=150) + play_all(c, rig, pid) + spreads = [] + pl = places() + for f in sample(1.5): + xs = [led_x(pl[n], i) for n, px in f.items() for i in lit(px, floor=60)] + if len(xs) > 10: + spreads.append(max(xs) - min(xs)) + assert len(spreads) > 10 + spreads.sort() + median = spreads[len(spreads) // 2] + assert median < 0.7, f"sweep band spans {median:.2f} of the rig's 1.9 width: placements not applied" + c.delete(f"/presets/{pid}") + + +# --- sequences over the row groups --------------------------------------------------------------------- + + +def test_time_sequence_walks_down_the_rows(c, rig): + all_dark(c, rig) + red = make_preset(c, pattern="on", colors=["#ff0000"]) + g = rig["groups"] + targets = [{"name": f"Row {r + 1}", "group_ids": [g[f"Row {r + 1}"]]} for r in range(ROWS)] + steps = [{"preset_id": red, "seconds": 0.6, "targets": [r], "others": "off"} for r in range(ROWS)] + seq = c.post("/sequences", json={ + "name": unique("pytest-rows"), "targets": targets, "lanes": [steps], + "lane_opts": [{"mode": "time", "targets": list(range(ROWS))}], "advance_mode": "time", "loop": False, + }) + assert seq.status_code == 201, seq.text + sid = new_id(seq) + assert c.post(f"/sequences/{sid}/play", json={"zone_id": rig["zone"]}).status_code == 200 + order = [] + end = time.monotonic() + 0.6 * ROWS + 1.5 + while time.monotonic() < end: + f = frames() + rows = {row_of(n) for n, px in f.items() if lit(px)} + if len(rows) == 1: + r = rows.pop() + if not order or order[-1] != r: + order.append(r) + time.sleep(0.05) + c.post("/sequences/stop") + c.delete(f"/sequences/{sid}") + c.delete(f"/presets/{red}") + assert order == list(range(ROWS)), f"rows lit in order {order}" diff --git a/test/api/test_sequences.py b/test/api/test_sequences.py index c78bce5..58d318d 100644 --- a/test/api/test_sequences.py +++ b/test/api/test_sequences.py @@ -135,3 +135,151 @@ def test_editing_stops_playback(c, zone, clock): c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) c.put(f"/sequences/{sid}", json={"name": "edited"}) assert c.get("/sequences/status").json()["active"] is False + + +# ---------------------------------------------------------------- multi-driver lanes + +# Made-up groups and drivers, so real drivers ignore every send +TARGETS = [ + {"name": "left", "group_ids": ["pytest-g1"]}, + {"name": "mid", "group_ids": ["pytest-g2"]}, + {"name": "right", "devices": ["02:00:00:00:00:01"]}, +] + + +def make(c, **rec): + rec.setdefault("name", unique("pytest-seq")) + return new_id(c.post("/sequences", json=rec)) + + +def wait_for(c, pred, beats=12): + deadline = time.monotonic() + beats * BEAT_S + st = None + while time.monotonic() < deadline: + st = c.get("/sequences/status").json() + if pred(st): + return st + time.sleep(BEAT_S / 4) + return st + + +def test_targets_and_lane_opts_round_trip(c, profile): + opts = [{"mode": "time", "offset": 1.5, "rate": 2, "targets": [2, 0], + "chase": {"order": "bounce", "width": 1, "others": "off"}}] + sid = make(c, targets=TARGETS, lane_opts=opts, + lanes=[[{"preset_id": "1", "beats": 1, "targets": [0], "others": "off"}, {"rest": True, "beats": 1}]]) + rec = c.get(f"/sequences/{sid}").json() + assert rec["targets"] == TARGETS + assert rec["lane_opts"] == opts + assert rec["lanes"][0][1]["rest"] is True + + +def test_old_sequence_status_unchanged(c, zone, clock): + a, b = presets(c, 2) + sid = make(c, lanes=[[{"preset_id": a, "beats": 2}, {"preset_id": b, "beats": 1}]], + lanes_group_ids=[["pytest-g1"]]) + c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) + st = c.get("/sequences/status").json() + assert st["num_lanes"] == 1 and st["lane0_beats_per_step"] == 2 and st["sequence_beats_per_pass"] == 3 + assert st["lanes"][0]["mode"] == "beats" and st["lanes"][0]["steps"] == 2 + + +def test_beat_and_time_lanes_together(c, zone, clock): + a, b = presets(c, 2) + sid = make(c, lanes=[[{"preset_id": a, "beats": 1}, {"preset_id": b, "beats": 1}], + [{"preset_id": a, "seconds": 5}, {"preset_id": b, "seconds": 5}]], + lane_opts=[{}, {"mode": "time"}]) + c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) + st = wait_for(c, lambda s: s["lanes"][0]["step"] == 1) + assert st["lanes"][0]["step"] == 1, "the beat lane moves on the beat" + assert st["lanes"][1]["mode"] == "time" and st["lanes"][1]["step"] == 0, "the time lane holds its 5 s" + assert st["lanes"][1]["duration_ms"] == 5000 + + +def test_offset_lane_starts_late(c, zone, clock): + a, = presets(c, 1) + sid = make(c, lanes=[[{"preset_id": a, "beats": 1}], [{"preset_id": a, "beats": 1}]], + lane_opts=[{}, {"offset": 4}]) + c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) + st = c.get("/sequences/status").json() + assert st["lanes"][0]["started"] is True and st["lanes"][1]["started"] is False + st = wait_for(c, lambda s: s["lanes"][1]["started"]) + assert st["lanes"][1]["started"] is True + + +def test_rate_doubles_speed(c, zone, clock): + a, b = presets(c, 2) + sid = make(c, lanes=[[{"preset_id": a, "beats": 4}], [{"preset_id": a, "beats": 1}, {"preset_id": b, "beats": 1}]], + lane_opts=[{}, {"rate": 2}]) + c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) + st = c.get("/sequences/status").json() + assert st["lanes"][1]["beats_in_step"] == 0.5 + + +def test_chase_moves_and_bounces(c, zone, clock): + a, = presets(c, 1) + sid = make(c, targets=TARGETS, lanes=[[{"preset_id": a, "beats": 1}]], + lane_opts=[{"targets": [0, 1, 2], "chase": {"order": "bounce"}}]) + c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) + seen = [] + deadline = time.monotonic() + 7 * BEAT_S + while time.monotonic() < deadline: + pos = c.get("/sequences/status").json()["lanes"][0]["pos"] + if not seen or seen[-1] != pos: + seen.append(pos) + time.sleep(BEAT_S / 4) + assert seen[:5] == [0, 1, 2, 1, 0][: len(seen[:5])] and len(seen) >= 4 + + +# ------------------------------------------------------------------- animations + + +def test_animation_plays_once_alone(c, zone, clock): + a, = presets(c, 1) + aid = make(c, kind="animation", loop=False, targets=TARGETS[:1], lanes=[[{"preset_id": a, "beats": 2}]], + lane_opts=[{"targets": [0]}]) + resp = c.post(f"/sequences/{aid}/fire", json={"zone_id": zone}) + assert resp.status_code == 200 and resp.json()["wait"] == "now" + st = c.get("/sequences/status").json() + assert st["active"] is False and st["overlay"]["active"] is True and st["overlay"]["sequence_id"] == aid + st = wait_for(c, lambda s: not s["overlay"]["active"]) + assert st["overlay"]["active"] is False + + +def test_animation_over_sequence_keeps_time(c, zone, clock): + a, b = presets(c, 2) + # A 6-beat pass, so the beat count can't wrap round while the animation plays + sid = make(c, lanes=[[{"preset_id": a, "beats": 3}, {"preset_id": b, "beats": 3}]], + lanes_group_ids=[["pytest-g1"]]) + aid = make(c, kind="animation", loop=False, lanes=[[{"preset_id": b, "beats": 3}]], + targets=[{"group_ids": ["pytest-g1"]}], lane_opts=[{"targets": [0]}]) + c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) + c.post(f"/sequences/{aid}/fire", json={}) # the playing sequence's zone + st = wait_for(c, lambda s: s["sequence_beat_at"] >= 2, beats=4) + assert st["overlay"]["active"] is True and st["active"] is True and st["sequence_id"] == sid + assert st["sequence_beat_at"] >= 2, "the sequence keeps counting under the animation" + st = wait_for(c, lambda s: not s["overlay"]["active"]) + assert st["overlay"]["active"] is False and st["active"] is True and st["sequence_id"] == sid + + +def test_animation_step_fires_overlay(c, zone, clock): + a, b = presets(c, 2) + aid = make(c, kind="animation", loop=False, lanes=[[{"preset_id": b, "beats": 2}]]) + sid = make(c, lanes=[[{"preset_id": a, "beats": 1}, {"animation_id": aid, "beats": 1, "length": "animation"}]]) + c.post(f"/sequences/{sid}/play", json={"zone_id": zone}) + st = wait_for(c, lambda s: s["overlay"]["active"]) + assert st["overlay"]["active"] is True and st["overlay"]["sequence_id"] == aid + assert st["lanes"][0]["beats_in_step"] == 2, "the step lasts as long as the animation" + + +def test_overlay_stop_and_fire_errors(c, zone, clock): + a, = presets(c, 1) + aid = make(c, kind="animation", loop=True, lanes=[[{"preset_id": a, "beats": 1}]]) + assert c.post("/sequences/999999/fire", json={"zone_id": zone}).status_code == 404 + c.post("/sequences/stop") + assert c.post(f"/sequences/{aid}/fire", json={}).status_code == 400 # no zone, nothing playing + c.post(f"/sequences/{aid}/fire", json={"zone_id": zone, "wait": "beat"}) + st = wait_for(c, lambda s: s["overlay"]["active"], beats=3) + assert st["overlay"]["active"] is True + assert c.post("/sequences/overlay/stop").json() == {"ok": True} + assert c.get("/sequences/status").json()["overlay"]["active"] is False diff --git a/test/binary_envelope.py b/test/binary_envelope.py index 9e2cc7d..a39a95d 100644 --- a/test/binary_envelope.py +++ b/test/binary_envelope.py @@ -32,6 +32,10 @@ v2 default blob: u8 target_count each: u8 len; target name utf-8 +v3: as v2, but every preset record is followed by an extension block: + u8 ext_len; then entries u8 key, u8 len, value — key 1 fade-in ms (u16), key 2 beat timing (u8, 1 = durations + in quarter beats). Readers skip keys they don't know. Only frames whose presets use them are v3. + Legacy v1: sections are UTF-8 JSON text (see ``parse_binary_envelope_v1``). Keep ``5 + lp + ls + ld`` ≤ 245 for a single ESP-NOW frame body. @@ -45,7 +49,22 @@ from typing import Any, Dict, List, Optional, Tuple BINARY_ENVELOPE_VERSION_1 = 1 BINARY_ENVELOPE_VERSION_2 = 2 +BINARY_ENVELOPE_VERSION_3 = 3 HEADER_LEN = 5 +EXT_FADE = 1 +EXT_BEAT = 2 + + +def _preset_ext(preset: Dict[str, Any]) -> bytes: + fade = preset.get("f") or preset.get("fade") or 0 + fade = max(0, min(10000, int(fade))) + beat = 1 if (preset.get("tb") or preset.get("beat_time")) else 0 + body = b"" + if fade: + body += bytes([EXT_FADE, 2]) + struct.pack(" int: @@ -126,11 +145,14 @@ def _pack_preset_dict(name: str, preset: Dict[str, Any]) -> bytes: return b"".join(parts) -def _pack_presets_blob(presets: Dict[str, Any]) -> bytes: +def _pack_presets_blob(presets: Dict[str, Any], v3: bool = False) -> bytes: items = [(k, v) for k, v in presets.items() if isinstance(v, dict)] out = [bytes([len(items)])] for name, pdata in items: out.append(_pack_preset_dict(str(name), pdata)) + if v3: + ext = _preset_ext(pdata) + out.append(bytes([len(ext)]) + ext) return b"".join(out) @@ -196,9 +218,10 @@ def pack_binary_envelope_v2( default_targets: Optional[list] = None, brightness_0_255: Optional[int] = None, ) -> bytes: - """Build a v2 envelope (native binary sections, no JSON).""" + """Build a v2 envelope (native binary sections, no JSON); v3 when a preset has a fade or beat timing.""" + v3 = bool(presets) and any(isinstance(v, dict) and _preset_ext(v) for v in presets.values()) presets_bytes = ( - _pack_presets_blob(presets) if presets is not None and presets else b"" + _pack_presets_blob(presets, v3) if presets is not None and presets else b"" ) select_bytes = ( _pack_select_blob(select) if select is not None and select else b"" @@ -220,7 +243,7 @@ def pack_binary_envelope_v2( if brightness_0_255 is None else brightness_wire_from_0_255(brightness_0_255) ) - header = bytes([BINARY_ENVELOPE_VERSION_2, br_wire, lp, ls, ld]) + header = bytes([BINARY_ENVELOPE_VERSION_3 if v3 else BINARY_ENVELOPE_VERSION_2, br_wire, lp, ls, ld]) return header + presets_bytes + select_bytes + default_bytes diff --git a/test/env_check.py b/test/env_check.py index 8fe8568..59bda94 100644 --- a/test/env_check.py +++ b/test/env_check.py @@ -4,3 +4,6 @@ p2 = {"pattern":"on","colors":["#102030"],"delay":0,"brightness":0,"auto":True} a = pack_binary_envelope_v2(presets={"7":p,"8":p2}, brightness_0_255=128).hex() b = pack_binary_envelope_v2(select={"mirror":["7"],"roof":["8",12]}).hex() print(a); print(b) +p3 = {"p":"hit","c":["#ffffff"],"d":0,"b":255,"a":False,"n1":10,"n3":400,"f":250,"tb":1} +p4 = {"pattern":"keyframes","colors":["#ff0000","#0000ff"],"delay":800,"fade":1200} +print(pack_binary_envelope_v2(presets={"9":p3,"10":p4}).hex()) diff --git a/test/env_host.c b/test/env_host.c index 5c176a7..14f41ea 100644 --- a/test/env_host.c +++ b/test/env_host.c @@ -23,5 +23,15 @@ int main(void) env_add_select(&sb, "roof", "8", 12); hex(out, env_frame(out, 128, &pb, NULL)); hex(out, env_frame(out, -1, NULL, &sb)); + // v3: presets with a fade or beat timing + cJSON *p3 = cJSON_Parse("{\"p\":\"hit\",\"c\":[\"#ffffff\"],\"d\":0,\"b\":255,\"a\":false,\"n1\":10,\"n3\":400,\"f\":250,\"tb\":1}"); + cJSON *p4 = cJSON_Parse("{\"pattern\":\"keyframes\",\"colors\":[\"#ff0000\",\"#0000ff\"],\"delay\":800,\"fade\":1200}"); + env_blob_t vb; + env_blob_init(&vb); + env_add_preset(&vb, "9", p3); + env_add_preset(&vb, "10", p4); + hex(out, env_frame(out, -1, &vb, NULL)); + // A v2 preset does not join a v3 blob + printf("%d\n", env_add_preset(&vb, "7", p) ? 1 : 0); return 0; } diff --git a/tools/sim_demo.py b/tools/sim_demo.py new file mode 100644 index 0000000..865d1f9 --- /dev/null +++ b/tools/sim_demo.py @@ -0,0 +1,167 @@ +#!/usr/bin/env python3 +"""Load demo animations for the simulated bridge rig (or any 2-column rig) into led-controller. + + python3 tools/sim_demo.py # controller at localhost:8080, simulator at :8765 + python3 tools/sim_demo.py --host 10.1.1.180 --sim http://localhost:8765 + +Creates (again, if run twice) a "Sim demo" profile and makes it current, with: +- groups: Rig (all bars), Left, Right, Row 1-8 +- presets: the field patterns (sweep, ripple, spin, grow, field_wave) and hit / keyframes, timed in beats +- sequences: row chase, column ping-pong, a two-lane canon, a looks medley +- animations (one-shot clips over whatever plays): flash, rows drop, centre burst +- a zone "Bridge" over the whole rig with all of the above as tiles + +Drivers restart their pattern on every beat, so every preset here lasts one beat (beat_time, delay 4 +quarter beats) and loops cleanly with the clock. +""" + +import argparse +import sys + +import requests + +PROFILE = "Sim demo" + + +class Api: + def __init__(self, base): + self.base = base.rstrip("/") + self.s = requests.Session() + + def __call__(self, method, path, body=None, ok=(200, 201)): + r = self.s.request(method, self.base + path, json=body, timeout=15) + if r.status_code not in ok: + sys.exit(f"{method} {path}: {r.status_code} {r.text[:200]}") + return r.json() if r.content else {} + + +def new_id(reply): + if isinstance(reply, dict) and "id" in reply and not isinstance(reply["id"], dict): + return str(reply["id"]) + return str(next(iter(reply))) + + +def rig_bars(sim_url): + """[(mac, row, col)] from the simulator's strips.""" + d = requests.get(sim_url.rstrip("/") + "/api/pixels", timeout=5).json() + return [(s["mac"].replace(":", ""), s["row"], s["col"]) for s in d["strips"]], d["layout"] + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + ap.add_argument("--host", default="localhost:8080", help="controller address (default localhost:8080)") + ap.add_argument("--sim", default="http://localhost:8765", help="simulator URL, for the bars' rows and columns") + a = ap.parse_args() + api = Api(a.host if a.host.startswith("http") else "http://" + a.host) + bars, layout = rig_bars(a.sim) + rows = layout["rows"] + + # Start over: drop an earlier demo profile and the groups it owned + profiles = api("GET", "/profiles") + for pid, p in (profiles.get("profiles") or profiles).items(): + if isinstance(p, dict) and p.get("name") == PROFILE: + for gid, g in api("GET", "/groups").items(): + if isinstance(g, dict) and g.get("profile_id") == pid: + api("DELETE", f"/groups/{gid}") + api("DELETE", f"/profiles/{pid}") + pid = new_id(api("POST", "/profiles", {"name": PROFILE})) + api("POST", f"/profiles/{pid}/apply") + # A new profile comes with a default zone and presets; the zone goes, so "Bridge" is the only one + for zid in api("GET", "/zones").get("zone_order", []): + api("DELETE", f"/zones/{zid}") + + def group(name, macs): + return new_id(api("POST", "/groups", {"name": name, "devices": macs, "profile_scoped": True})) + + g = {"Rig": group("Rig", [m for m, _, _ in bars])} + g["Left"] = group("Left", [m for m, _, c in bars if c == 0]) + g["Right"] = group("Right", [m for m, _, c in bars if c == 1]) + for r in range(rows): + g[f"Row {r + 1}"] = group(f"Row {r + 1}", [m for m, row, _ in bars if row == r]) + + def preset(name, pattern, colors, **kw): + body = {"name": name, "pattern": pattern, "colors": colors, "brightness": 255, + "delay": 4, "beat_time": True, "auto": True} + body.update(kw) + return new_id(api("POST", "/presets", body)) + + P = { + "Sweep →": preset("Sweep →", "sweep", ["#ff0040", "#ffa000"], n1=0, n2=250, n3=400), + "Sweep ↑": preset("Sweep ↑", "sweep", ["#00c0ff", "#8000ff"], n1=90, n2=200, n3=300), + "Wipe ←": preset("Wipe ←", "sweep", ["#40ff80", "#00a0ff"], n1=180, n2=100, n4=1), + "Ripple": preset("Ripple", "ripple", ["#ff2060", "#20a0ff", "#40ff80"], n1=250, n2=2), + "Ripple in": preset("Ripple in", "ripple", ["#ffffff", "#ff8000"], n1=200, n3=1), + "Radar": preset("Radar", "spin", ["#00ff60", "#004010"], n1=1, n2=25, n3=120), + "Pinwheel": preset("Pinwheel", "spin", ["#ff0080", "#0080ff"], n1=4, n2=20, n3=40, n4=1), + "Grow out": preset("Grow out", "grow", ["#ffd000", "#ff2000"], n1=0, n2=1), + "Grow round": preset("Grow round", "grow", ["#00ffd0", "#0040ff"], n1=2, n2=2), + "Plasma": preset("Plasma", "field_wave", ["#ff0060", "#6000ff", "#00c0ff"], n3=3, n2=800), + "Swirl": preset("Swirl", "field_wave", ["#ff8000", "#ff0040", "#8000ff"], n3=2), + "Wave": preset("Wave", "field_wave", ["#0040ff", "#00ffc0"], n1=0, n2=600, n3=0), + "Hit": preset("Hit", "hit", ["#ffffff", "#ff0040", "#00a0ff"], auto=False, n1=0, n2=1, n3=2), + "Fade": preset("Fade", "keyframes", ["#ff0000", "#00ff00", "#0000ff"], n1=1), + "Red": preset("Red", "on", ["#ff0000"], beat_time=False, delay=100), + "Blue": preset("Blue", "on", ["#0040ff"], beat_time=False, delay=100), + "White": preset("White", "on", ["#ffffff"], beat_time=False, delay=100), + } + + row_targets = [{"name": f"Row {r + 1}", "group_ids": [g[f"Row {r + 1}"]]} for r in range(rows)] + + def sequence(name, kind="sequence", **body): + body = {"name": name, "kind": kind, "loop": kind != "animation", "advance_mode": "beats", **body} + return new_id(api("POST", "/sequences", body)) + + seqs = { + "Rows chase": sequence( + "Rows chase", targets=row_targets, + lanes=[[{"preset_id": P["Ripple"], "beats": 0.5}] * rows], + lane_opts=[{"targets": list(range(rows)), "chase": {"order": "bounce", "width": 1}}], + ), + "Ping-pong": sequence( + "Ping-pong", + targets=[{"name": "Left", "group_ids": [g["Left"]]}, {"name": "Right", "group_ids": [g["Right"]]}], + lanes=[[{"preset_id": P["Sweep →"], "beats": 1, "targets": [0], "others": "off"}, + {"preset_id": P["Wipe ←"], "beats": 1, "targets": [1], "others": "off"}]], + lane_opts=[{"targets": [0, 1]}], + ), + "Canon": sequence( + "Canon", + targets=[{"name": f"L{r + 1}", "devices": [m for m, row, c in bars if row == r and c == 0]} for r in range(rows)] + + [{"name": f"R{r + 1}", "devices": [m for m, row, c in bars if row == r and c == 1]} for r in range(rows)], + lanes=[[{"preset_id": P["Hit"], "beats": 0.5}] * rows, [{"preset_id": P["Hit"], "beats": 0.5}] * rows], + lane_opts=[{"targets": list(range(rows)), "chase": {"order": "forward"}}, + {"targets": list(range(rows, 2 * rows)), "chase": {"order": "forward"}, "offset": 2}], + ), + "Looks": sequence( + "Looks", group_ids=[g["Rig"]], + lanes=[[{"preset_id": P[k], "beats": 4} for k in + ("Ripple", "Sweep →", "Radar", "Grow out", "Plasma", "Pinwheel", "Swirl", "Sweep ↑")]], + ), + } + anims = { + "Flash": sequence("Flash", kind="animation", group_ids=[g["Rig"]], + lanes=[[{"preset_id": P["Hit"], "beats": 1}]]), + "Rows drop": sequence( + "Rows drop", kind="animation", targets=row_targets, + lanes=[[{"preset_id": P["White"], "beats": 0.25}] * rows], + lane_opts=[{"targets": list(range(rows)), "chase": {"order": "forward", "width": 1}}], + ), + "Burst": sequence("Burst", kind="animation", group_ids=[g["Rig"]], + lanes=[[{"preset_id": P["Ripple in"], "beats": 1}, + {"preset_id": P["Ripple"], "beats": 1}]]), + } + + names = list(P) + zone = new_id(api("POST", "/zones", { + "name": "Bridge", "group_ids": [g["Rig"]], + "presets": [[P[n] for n in names[i:i + 6]] for i in range(0, len(names), 6)], + "sequence_ids": list(seqs.values()), "animation_ids": list(anims.values()), + })) + api("POST", f"/zones/{zone}/set-current") + api("POST", f"/zones/{zone}/play", {"preset_id": P["Ripple"]}) + print(f"profile {pid} '{PROFILE}', zone {zone} 'Bridge': {len(g)} groups, {len(P)} presets, " + f"{len(seqs)} sequences ({', '.join(seqs)}), {len(anims)} animations ({', '.join(anims)}). Playing Ripple.") + + +if __name__ == "__main__": + main() diff --git a/web/static/help.js b/web/static/help.js index fa2e684..497c9cd 100644 --- a/web/static/help.js +++ b/web/static/help.js @@ -81,6 +81,51 @@ document.addEventListener('DOMContentLoaded', () => { }, 5000); }; + const rebootButton = document.getElementById('reboot-btn'); + if (rebootButton) { + rebootButton.addEventListener('click', async () => { + if (!confirm('Reboot the controller now? The page will be unavailable for a few seconds.')) return; + rebootButton.disabled = true; + try { + await fetch('/reboot', { method: 'POST' }); + rebootButton.textContent = 'Rebooting…'; + setTimeout(() => location.reload(), 8000); + } catch (error) { + rebootButton.disabled = false; + console.error('Reboot failed:', error); + } + }); + } + + async function loadNetworkStatus() { + const box = document.getElementById('network-status'); + if (!box) return; + const row = (label, value) => { + const r = document.createElement('div'); + const b = document.createElement('strong'); + b.textContent = label + ': '; + r.append(b, value); + return r; + }; + try { + const st = await fetch('/status', { cache: 'no-store' }).then((r) => r.json()); + box.textContent = ''; + const eth = st.eth || { ip: st.ip }; + const up = eth.ip && eth.ip !== '0.0.0.0'; + box.append(row('Ethernet', up ? eth.ip + (eth.mask ? ' / ' + eth.mask : '') + (eth.gw && eth.gw !== '0.0.0.0' ? ', gateway ' + eth.gw : '') : 'no address')); + if (st.ap) { + box.append(row('Access point', st.ap_ip || '192.168.5.1')); + const clients = Array.isArray(st.ap_clients) ? st.ap_clients : []; + box.append(row('Wi-Fi clients', clients.length ? clients.map((c) => c.ip + ' (' + c.mac + ')').join(', ') : 'none')); + } else { + box.append(row('Access point', 'off')); + } + if (st.mac) box.append(row('MAC', st.mac)); + } catch (error) { + box.textContent = 'Could not read network status.'; + } + } + async function loadDeviceSettings() { try { const response = await fetch('/settings'); @@ -101,6 +146,8 @@ document.addEventListener('DOMContentLoaded', () => { setApToggle(data.ap_enabled !== false); const apPw = document.getElementById('ap-password-input'); if (apPw) apPw.value = data.ap_password || ''; + const bridge = document.getElementById('bridge-input'); + if (bridge) bridge.value = data.bridge || ''; staticIpsList.innerHTML = ''; (Array.isArray(data.static_ips) ? data.static_ips : []).forEach(addStaticIpRow); } @@ -196,6 +243,7 @@ document.addEventListener('DOMContentLoaded', () => { panel.classList.toggle('active', on); panel.hidden = !on; } + if (tabId === 'network') loadNetworkStatus(); settingsModal.classList.toggle('settings-modal--led-tool', tabId === 'led-tool'); if (ledToolIframe) { // Loaded on demand, so the Controller tab costs no requests @@ -292,6 +340,7 @@ document.addEventListener('DOMContentLoaded', () => { hostname, static_ips: staticIps, ap_password: apPassword, + bridge: (document.getElementById('bridge-input') || { value: '' }).value.trim(), ap_enabled: !apToggle || apToggle.getAttribute('aria-pressed') === 'true', }), }); diff --git a/web/static/presets.js b/web/static/presets.js index c712719..3ae08cc 100644 --- a/web/static/presets.js +++ b/web/static/presets.js @@ -282,6 +282,8 @@ document.addEventListener('DOMContentLoaded', () => { const presetNewColorInput = document.getElementById('preset-new-color'); const presetBrightnessInput = document.getElementById('preset-brightness-input'); const presetDelayInput = document.getElementById('preset-delay-input'); + const presetFadeInput = document.getElementById('preset-fade-input'); + const presetBeatTimeInput = document.getElementById('preset-beat-time-input'); const presetDelayField = presetDelayInput ? presetDelayInput.closest('.preset-editor-field') : null; const presetBackgroundInput = document.getElementById('preset-background-input'); const presetBackgroundButton = document.getElementById('preset-background-btn'); @@ -747,6 +749,8 @@ document.addEventListener('DOMContentLoaded', () => { renderPresetColors(colors, paletteRefs); presetBrightnessInput.value = preset.brightness || 0; presetDelayInput.value = preset.delay || 0; + if (presetFadeInput) presetFadeInput.value = preset.fade || preset.f || 0; + if (presetBeatTimeInput) presetBeatTimeInput.checked = !!(preset.beat_time || preset.tb); if (presetBackgroundInput) { const rawBgRef = preset.background_palette_ref ?? preset.backgroundPaletteRef; let bgRef = null; @@ -983,6 +987,8 @@ document.addEventListener('DOMContentLoaded', () => { // Use canonical field names expected by the device / API brightness: presetBrightnessInput ? parseInt(presetBrightnessInput.value, 10) || 0 : 0, delay: presetDelayInput ? parseInt(presetDelayInput.value, 10) || 0 : 0, + fade: presetFadeInput ? Math.max(0, Math.min(10000, parseInt(presetFadeInput.value, 10) || 0)) : 0, + beat_time: !!(presetBeatTimeInput && presetBeatTimeInput.checked), background: presetBackgroundInput ? presetBackgroundInput.value : '#000000', background_palette_ref: currentBackgroundPaletteRef != null ? currentBackgroundPaletteRef : null, auto: presetManualModeInput ? !presetManualModeInput.checked : true, diff --git a/web/static/sequences.js b/web/static/sequences.js index 5c74486..907143d 100644 --- a/web/static/sequences.js +++ b/web/static/sequences.js @@ -184,7 +184,12 @@ function normalizeSequenceLanes(doc) { .filter((s) => s && typeof s === 'object') .map((s) => ({ preset_id: s.preset_id != null ? String(s.preset_id) : String(s.presetId || ''), - beats: Math.max(1, parseInt(String(s.beats != null ? s.beats : 1), 10) || 1), + animation_id: s.animation_id != null ? String(s.animation_id) : '', + rest: s.rest === true, + others: s.others === 'off' ? 'off' : '', + length: s.length === 'animation' ? 'animation' : '', + targets: Array.isArray(s.targets) ? s.targets.map((x) => parseInt(String(x), 10)).filter((x) => x >= 0) : [], + beats: Math.max(0.25, parseFloat(String(s.beats != null ? s.beats : 1)) || 1), seconds: Math.max(0.1, parseFloat(String(s.seconds != null ? s.seconds : 3)) || 3), group_ids: Array.isArray(s.group_ids) ? s.group_ids.map((x) => String(x).trim()).filter(Boolean) @@ -366,6 +371,41 @@ async function requestBackendSequencePlay(sequenceId, zoneId, sequenceDoc) { } } +/** Play an animation once over whatever runs in the zone; its tile shows progress while it plays. */ +async function fireAnimation(sequenceId, zoneId) { + const res = await fetch(`/sequences/${encodeURIComponent(sequenceId)}/fire`, { + method: 'POST', + headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, + credentials: 'same-origin', + body: JSON.stringify({ zone_id: zoneId ? String(zoneId) : '', wait: 'now' }), + }); + if (!res.ok) { + const err = await res.json().catch(() => ({})); + throw new Error((err && err.error) || res.statusText); + } + pollAnimationProgress(); +} + +let animProgressTimer = null; +async function pollAnimationProgress() { + if (animProgressTimer) clearTimeout(animProgressTimer); + animProgressTimer = null; + let ov = null; + try { + const r = await fetch('/sequences/status', { cache: 'no-store', headers: { Accept: 'application/json' } }); + const st = r.ok ? await r.json() : null; + ov = st && st.overlay ? st.overlay : null; + } catch (_) { + ov = null; + } + document.querySelectorAll('.animation-tile-row').forEach((row) => { + const on = !!(ov && (ov.active || ov.pending) && String(ov.sequence_id || ov.pending_id) === row.dataset.sequenceId); + row.classList.toggle('firing', on); + row.style.setProperty('--anim-progress', on && ov.active ? String(ov.progress || 0) : '0'); + }); + if (ov && (ov.active || ov.pending)) animProgressTimer = setTimeout(pollAnimationProgress, 250); +} + async function fetchSequencesMap() { try { const res = await fetch('/sequences', { cache: 'no-store', headers: { Accept: 'application/json' } }); @@ -395,7 +435,8 @@ async function fetchGroupsMapSeq() { function createSequenceTileRow(sequenceId, sequenceDoc, zoneId, zoneDoc, allPresets, uiMode) { const row = document.createElement('div'); const canDrag = false; - row.className = `preset-tile-row preset-tile-row--${uiMode} sequence-tile-row${canDrag ? ' draggable-preset' : ''}`; + const isAnim = sequenceDoc && sequenceDoc.kind === 'animation'; + row.className = `preset-tile-row preset-tile-row--${uiMode} sequence-tile-row${isAnim ? ' animation-tile-row' : ''}${canDrag ? ' draggable-preset' : ''}`; row.dataset.sequenceId = sequenceId; const button = document.createElement('button'); @@ -404,12 +445,24 @@ function createSequenceTileRow(sequenceId, sequenceDoc, zoneId, zoneDoc, allPres button.title = sequenceDoc.name || `Sequence ${sequenceId}`; const label = document.createElement('span'); - label.textContent = sequenceDoc.name || sequenceId; + label.textContent = `${isAnim ? '⚡ ' : ''}${sequenceDoc.name || sequenceId}`; label.style.fontWeight = 'bold'; label.className = 'pattern-button-label'; button.appendChild(label); - button.addEventListener('click', () => { + if (isAnim) { + // One shot: never becomes the active tile, so the sequence or preset underneath stays highlighted + button.title = `${sequenceDoc.name || sequenceId} — plays once, then back to what was running`; + const bar = document.createElement('span'); + bar.className = 'animation-tile-progress'; + button.appendChild(bar); + button.addEventListener('click', () => { + fireAnimation(sequenceId, zoneId).catch((e) => { + console.error(e); + alert(e.message || 'Animation failed.'); + }); + }); + } else button.addEventListener('click', () => { const strip = document.getElementById('presets-list-zone'); const clearActiveStrip = () => { if (strip) strip.querySelectorAll('.pattern-button').forEach((btn) => btn.classList.remove('active')); @@ -486,7 +539,10 @@ function createSequenceTileRow(sequenceId, sequenceDoc, zoneId, zoneDoc, allPres async function appendZoneSequenceTiles(zoneId, zoneDoc, allPresets, paletteColors, presetsListEl) { if (!presetsListEl || !zoneId) return; - const ids = Array.isArray(zoneDoc.sequence_ids) ? zoneDoc.sequence_ids.map((x) => String(x)) : []; + const ids = [ + ...(Array.isArray(zoneDoc.sequence_ids) ? zoneDoc.sequence_ids.map((x) => String(x)) : []), + ...(Array.isArray(zoneDoc.animation_ids) ? zoneDoc.animation_ids.map((x) => String(x)) : []), + ]; if (!ids.length) return; const sequences = await fetchSequencesMap(); @@ -513,13 +569,15 @@ async function addSequenceToTab(sequenceId, zoneId) { const tabResponse = await fetch(`/zones/${zoneId}`, { headers: { Accept: 'application/json' } }); if (!tabResponse.ok) throw new Error('Failed to load zone'); const tabData = await tabResponse.json(); - const list = Array.isArray(tabData.sequence_ids) ? tabData.sequence_ids.map(String) : []; + const seqs = await fetchSequencesMap(); + const field = seqs[sequenceId] && seqs[sequenceId].kind === 'animation' ? 'animation_ids' : 'sequence_ids'; + const list = Array.isArray(tabData[field]) ? tabData[field].map(String) : []; if (list.includes(String(sequenceId))) { - alert('Sequence is already on this zone.'); + alert('Already on this zone.'); return; } list.push(String(sequenceId)); - tabData.sequence_ids = list; + tabData[field] = list; const updateResponse = await fetch(`/zones/${zoneId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, @@ -542,12 +600,15 @@ async function removeSequenceFromTab(zoneId, sequenceId) { if (!tabResponse.ok) throw new Error('Failed to load zone'); const tabData = await tabResponse.json(); const list = Array.isArray(tabData.sequence_ids) ? tabData.sequence_ids.map(String) : []; + const alist = Array.isArray(tabData.animation_ids) ? tabData.animation_ids.map(String) : []; const next = list.filter((x) => String(x) !== String(sequenceId)); - if (next.length === list.length) { - alert('Sequence is not on this zone.'); + const anext = alist.filter((x) => String(x) !== String(sequenceId)); + if (next.length === list.length && anext.length === alist.length) { + alert('Not on this zone.'); return; } tabData.sequence_ids = next; + tabData.animation_ids = anext; const updateResponse = await fetch(`/zones/${zoneId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, @@ -575,7 +636,10 @@ async function refreshEditTabSequencesUi(zoneId) { const zoneRes = await fetch(`/zones/${zoneId}`, { headers: { Accept: 'application/json' } }); if (!zoneRes.ok) throw new Error('zone'); const zone = await zoneRes.json(); - const onZone = Array.isArray(zone.sequence_ids) ? zone.sequence_ids.map(String) : []; + const onZone = [ + ...(Array.isArray(zone.sequence_ids) ? zone.sequence_ids.map(String) : []), + ...(Array.isArray(zone.animation_ids) ? zone.animation_ids.map(String) : []), + ]; const seqMap = await fetchSequencesMap(); const onSet = new Set(onZone); @@ -585,7 +649,7 @@ async function refreshEditTabSequencesUi(zoneId) { } else { for (const sid of onZone) { const sdoc = seqMap[sid] || {}; - const name = sdoc.name || sid; + const name = `${sdoc.kind === 'animation' ? '⚡ ' : ''}${sdoc.name || sid}`; const row = document.createElement('div'); row.className = 'profiles-row edit-zone-item-row'; const span = document.createElement('span'); @@ -626,7 +690,8 @@ async function refreshEditTabSequencesUi(zoneId) { }) .forEach((id) => { const n = (seqMap[id] && seqMap[id].name) || id; - sel.appendChild(new Option(`${n} — ${id}`, id)); + const a = seqMap[id] && seqMap[id].kind === 'animation' ? '⚡ ' : ''; + sel.appendChild(new Option(`${a}${n} — ${id}`, id)); }); const addBtn = document.createElement('button'); addBtn.type = 'button'; @@ -723,6 +788,232 @@ function wireSequenceLaneStepsDragReorder(stepsHost) { }); } +/* ------------------------------------------------------------------ editor + * Sequence editor state: the sequence's targets (an ordered driver list: groups and/or single drivers), + * the presets and animations steps can play, and the groups/devices targets can name. Targets keep a + * local uid while editing; lanes and steps store the uids and save them as indexes. + */ +const seqEd = { + targets: [], // [{uid, name, group_ids: [], devices: []}] + presetsMap: {}, + groupsMap: {}, + devicesMap: {}, + seqMap: {}, + zoneGroupIds: [], + nextUid: 1, +}; + +const LANES_MAX_UI = 16; + +function seqEdTargetLabel(t, i) { + const n = t && t.name ? String(t.name).trim() : ''; + return n || `Target ${i + 1}`; +} + +/** Checkbox chips, one per target; `selected` is a set of uids. */ +function renderTargetChips(host, selected, cls) { + host.innerHTML = ''; + host.dataset.chipClass = cls; + if (!seqEd.targets.length) { + const m = document.createElement('span'); + m.className = 'muted-text'; + m.textContent = 'No targets yet (add them above).'; + host.appendChild(m); + return; + } + seqEd.targets.forEach((t, i) => { + const lbl = document.createElement('label'); + lbl.className = 'seq-chip'; + const cb = document.createElement('input'); + cb.type = 'checkbox'; + cb.className = cls; + cb.value = String(t.uid); + cb.checked = selected.has(String(t.uid)); + const sp = document.createElement('span'); + sp.textContent = seqEdTargetLabel(t, i); + lbl.appendChild(cb); + lbl.appendChild(sp); + host.appendChild(lbl); + }); +} + +function chipSelection(host) { + const out = new Set(); + if (!host) return out; + host.querySelectorAll('input[type=checkbox]:checked').forEach((c) => out.add(String(c.value))); + return out; +} + +/** Re-render every chip row after targets were added, renamed, moved or removed (keeping selections). */ +function refreshAllTargetChips() { + document.querySelectorAll('#sequence-editor-modal .seq-target-chips').forEach((host) => { + renderTargetChips(host, chipSelection(host), host.dataset.chipClass || 'seq-chip-cb'); + }); + document.querySelectorAll('#sequence-editor-lanes .sequence-lane').forEach((laneEl) => { + const legacy = laneEl.querySelector('.sequence-lane-groups-wrap'); + if (legacy) legacy.style.display = seqEd.targets.length ? 'none' : ''; + }); + renderSequenceGrid(); +} + +function renderTargetsPanel() { + const host = document.getElementById('sequence-editor-targets'); + if (!host) return; + host.innerHTML = ''; + seqEd.targets.forEach((t, i) => { + const row = document.createElement('div'); + row.className = 'seq-target-row profiles-row'; + const num = document.createElement('span'); + num.className = 'seq-target-num'; + num.textContent = String(i + 1); + const name = document.createElement('input'); + name.type = 'text'; + name.placeholder = `Target ${i + 1}`; + name.value = t.name || ''; + name.addEventListener('input', () => { + t.name = name.value; + refreshAllTargetChips(); + }); + const det = document.createElement('details'); + const sum = document.createElement('summary'); + const what = () => { + const g = t.group_ids.map((id) => (seqEd.groupsMap[id] && seqEd.groupsMap[id].name) || id); + const d = t.devices.map((m) => (seqEd.devicesMap[m] && seqEd.devicesMap[m].name) || m); + const all = [...g, ...d]; + return all.length ? all.join(', ') : 'Nothing picked'; + }; + sum.textContent = what(); + det.appendChild(sum); + const pick = document.createElement('div'); + pick.className = 'seq-target-pick'; + const addBox = (label, list, value) => { + const l = document.createElement('label'); + l.className = 'seq-chip'; + const cb = document.createElement('input'); + cb.type = 'checkbox'; + cb.checked = list.includes(value); + cb.addEventListener('change', () => { + const at = list.indexOf(value); + if (cb.checked && at < 0) list.push(value); + if (!cb.checked && at >= 0) list.splice(at, 1); + sum.textContent = what(); + }); + const sp = document.createElement('span'); + sp.textContent = label; + l.appendChild(cb); + l.appendChild(sp); + pick.appendChild(l); + }; + const gh = document.createElement('div'); + gh.className = 'muted-text'; + gh.textContent = 'Groups'; + pick.appendChild(gh); + Object.keys(seqEd.groupsMap) + .sort((a, b) => a.localeCompare(b, undefined, { numeric: true })) + .forEach((gid) => addBox(`${(seqEd.groupsMap[gid] && seqEd.groupsMap[gid].name) || gid}`, t.group_ids, gid)); + const dh = document.createElement('div'); + dh.className = 'muted-text'; + dh.textContent = 'Single drivers'; + pick.appendChild(dh); + Object.keys(seqEd.devicesMap) + .sort((a, b) => String((seqEd.devicesMap[a] || {}).name || a).localeCompare(String((seqEd.devicesMap[b] || {}).name || b))) + .forEach((mac) => addBox(`${(seqEd.devicesMap[mac] && seqEd.devicesMap[mac].name) || mac}`, t.devices, mac)); + det.appendChild(pick); + const btns = document.createElement('div'); + btns.className = 'seq-target-btns'; + const mk = (txt, title, fn, disabled) => { + const b = document.createElement('button'); + b.type = 'button'; + b.className = 'btn btn-secondary btn-small'; + b.textContent = txt; + b.title = title; + b.disabled = !!disabled; + b.addEventListener('click', fn); + btns.appendChild(b); + return b; + }; + mk('↑', 'Move up', () => { + seqEd.targets.splice(i - 1, 0, seqEd.targets.splice(i, 1)[0]); + renderTargetsPanel(); + }, i === 0); + mk('↓', 'Move down', () => { + seqEd.targets.splice(i + 1, 0, seqEd.targets.splice(i, 1)[0]); + renderTargetsPanel(); + }, i === seqEd.targets.length - 1); + const rm = mk('Remove', 'Remove target', () => { + seqEd.targets.splice(i, 1); + renderTargetsPanel(); + }); + rm.className = 'btn btn-danger btn-small'; + row.appendChild(num); + row.appendChild(name); + row.appendChild(det); + row.appendChild(btns); + host.appendChild(row); + }); + refreshAllTargetChips(); +} + +function addSeqTarget(t) { + if (seqEd.targets.length >= 32) { + alert('A sequence can have up to 32 targets.'); + return false; + } + seqEd.targets.push({ uid: seqEd.nextUid++, name: '', group_ids: [], devices: [], ...t }); + return true; +} + +/** One target per driver in the current zone's groups, in device-list order (for chases). */ +function addTargetsPerDriver() { + const macs = []; + const gids = seqEd.zoneGroupIds.length ? seqEd.zoneGroupIds : Object.keys(seqEd.groupsMap); + gids.forEach((gid) => { + const g = seqEd.groupsMap[gid]; + (g && Array.isArray(g.devices) ? g.devices : []).forEach((m) => { + const mac = String(m).toLowerCase().replace(/[^0-9a-f]/g, ''); + if (mac.length === 12 && !macs.includes(mac)) macs.push(mac); + }); + }); + if (!macs.length) { + alert('No drivers found in this zone\'s groups.'); + return; + } + const have = new Set(seqEd.targets.filter((t) => t.devices.length === 1 && !t.group_ids.length).map((t) => t.devices[0])); + macs.forEach((mac) => { + if (have.has(mac)) return; + const d = seqEd.devicesMap[mac]; + addSeqTarget({ name: (d && d.name) || mac, devices: [mac] }); + }); + renderTargetsPanel(); +} + +/** Step "what plays" picker: presets, animations, or a rest. Values: p:, a:, rest. */ +function buildStepPlaySelect(step) { + const sel = document.createElement('select'); + sel.className = 'sequence-step-preset zone-device-add-select'; + sel.appendChild(new Option('Select…', '')); + sel.appendChild(new Option('— Rest (off) —', 'rest')); + const byName = (map) => (a, b) => + String((map[a] && map[a].name) || a).localeCompare(String((map[b] && map[b].name) || b), undefined, { sensitivity: 'base' }); + const pg = document.createElement('optgroup'); + pg.label = 'Presets'; + Object.keys(seqEd.presetsMap) + .sort(byName(seqEd.presetsMap)) + .forEach((pid) => pg.appendChild(new Option(`${(seqEd.presetsMap[pid] && seqEd.presetsMap[pid].name) || pid}`, `p:${pid}`))); + sel.appendChild(pg); + const anims = Object.keys(seqEd.seqMap).filter((id) => seqEd.seqMap[id] && seqEd.seqMap[id].kind === 'animation' && id !== sequenceEditorId); + if (anims.length) { + const ag = document.createElement('optgroup'); + ag.label = 'Animations'; + anims.sort(byName(seqEd.seqMap)).forEach((id) => ag.appendChild(new Option(`⚡ ${seqEd.seqMap[id].name || id}`, `a:${id}`))); + sel.appendChild(ag); + } + if (step && step.rest) sel.value = 'rest'; + else if (step && step.animation_id) sel.value = `a:${step.animation_id}`; + else if (step && step.preset_id) sel.value = `p:${step.preset_id}`; + return sel; +} + function renderSequenceStepRow(presetsMap, step) { const row = document.createElement('div'); row.className = 'sequence-step-row profiles-row'; @@ -743,21 +1034,8 @@ function renderSequenceStepRow(presetsMap, step) { const presetWrap = document.createElement('div'); presetWrap.style.cssText = 'display:flex;align-items:center;gap:0.5rem;flex-wrap:wrap;'; const pl = document.createElement('label'); - pl.textContent = 'Preset'; - const psel = document.createElement('select'); - psel.className = 'sequence-step-preset zone-device-add-select'; - psel.appendChild(new Option('Select…', '')); - const pids = Object.keys(presetsMap).sort((a, b) => { - const na = (presetsMap[a] && presetsMap[a].name) || a; - const nb = (presetsMap[b] && presetsMap[b].name) || b; - return String(na).localeCompare(String(nb), undefined, { sensitivity: 'base' }); - }); - const curPreset = step && step.preset_id != null ? String(step.preset_id) : ''; - pids.forEach((pid) => { - const n = (presetsMap[pid] && presetsMap[pid].name) || pid; - psel.appendChild(new Option(`${n} — ${pid}`, pid)); - }); - if (curPreset) psel.value = curPreset; + pl.textContent = 'Plays'; + const psel = buildStepPlaySelect(step); presetWrap.appendChild(pl); presetWrap.appendChild(psel); @@ -769,12 +1047,11 @@ function renderSequenceStepRow(presetsMap, step) { beatsInp.type = 'number'; beatsInp.className = 'sequence-step-beats'; beatsInp.autocomplete = 'off'; - beatsInp.min = '1'; + beatsInp.min = '0.25'; beatsInp.max = '256'; - beatsInp.value = String( - step && step.beats != null ? Math.max(1, parseInt(String(step.beats), 10) || 1) : 1, - ); - beatsInp.style.width = '4rem'; + beatsInp.step = '0.25'; + beatsInp.value = String(step && step.beats != null ? Math.max(0.25, parseFloat(String(step.beats)) || 1) : 1); + beatsInp.style.width = '4.5rem'; beatWrap.appendChild(bl); beatWrap.appendChild(beatsInp); beatWrap.classList.add('sequence-step-beats-wrap'); @@ -791,9 +1068,7 @@ function renderSequenceStepRow(presetsMap, step) { secInp.min = '0.1'; secInp.max = '3600'; secInp.step = '0.1'; - secInp.value = String( - step && step.seconds != null ? Math.max(0.1, parseFloat(String(step.seconds)) || 3) : 3, - ); + secInp.value = String(step && step.seconds != null ? Math.max(0.1, parseFloat(String(step.seconds)) || 3) : 3); secInp.style.width = '5rem'; secWrap.appendChild(sl); secWrap.appendChild(secInp); @@ -801,10 +1076,16 @@ function renderSequenceStepRow(presetsMap, step) { const editPresetBtn = document.createElement('button'); editPresetBtn.type = 'button'; editPresetBtn.className = 'btn btn-secondary btn-small'; - editPresetBtn.textContent = 'Edit preset'; + editPresetBtn.textContent = 'Edit'; + editPresetBtn.title = 'Edit the preset or animation'; editPresetBtn.addEventListener('click', async (e) => { e.preventDefault(); - const presetId = psel.value ? String(psel.value) : ''; + const v = psel.value || ''; + if (v.startsWith('a:')) { + openSequenceEditor(v.slice(2), seqEd.seqMap[v.slice(2)]); + return; + } + const presetId = v.startsWith('p:') ? v.slice(2) : ''; if (!presetId) { alert('Select a preset first.'); return; @@ -831,17 +1112,171 @@ function renderSequenceStepRow(presetsMap, step) { top.appendChild(editPresetBtn); row.appendChild(top); + // More: which targets this step lights, others off, animation length + const more = document.createElement('details'); + more.className = 'seq-step-more'; + const ms = document.createElement('summary'); + ms.textContent = 'Targets and options'; + more.appendChild(ms); + const chips = document.createElement('div'); + chips.className = 'seq-target-chips seq-step-targets'; + const stepSel = new Set(); + (step && Array.isArray(step.targets) ? step.targets : []).forEach((ix) => { + if (seqEd.targets[ix]) stepSel.add(String(seqEd.targets[ix].uid)); + }); + if (step && step._uids) step._uids.forEach((u) => stepSel.add(String(u))); + renderTargetChips(chips, stepSel, 'seq-step-target'); + const chipsHint = document.createElement('div'); + chipsHint.className = 'muted-text'; + chipsHint.textContent = 'Lights only these (none ticked: all of the lane\'s targets, or the chase).'; + more.appendChild(chipsHint); + more.appendChild(chips); + const opts = document.createElement('div'); + opts.style.cssText = 'display:flex;gap:1rem;flex-wrap:wrap;margin-top:0.35rem;'; + const othersLbl = document.createElement('label'); + const othersCb = document.createElement('input'); + othersCb.type = 'checkbox'; + othersCb.className = 'sequence-step-others-off'; + othersCb.checked = !!(step && step.others === 'off'); + othersLbl.appendChild(othersCb); + othersLbl.appendChild(document.createTextNode(' Lane\'s other targets off')); + const lenLbl = document.createElement('label'); + lenLbl.className = 'sequence-step-length-wrap'; + const lenCb = document.createElement('input'); + lenCb.type = 'checkbox'; + lenCb.className = 'sequence-step-anim-length'; + lenCb.checked = !step || step.length === 'animation' || !step.animation_id; + lenLbl.appendChild(lenCb); + lenLbl.appendChild(document.createTextNode(' Step lasts as long as the animation')); + opts.appendChild(othersLbl); + opts.appendChild(lenLbl); + more.appendChild(opts); + if (stepSel.size || othersCb.checked) more.open = true; + row.appendChild(more); + + const syncKind = () => { + const v = psel.value || ''; + const anim = v.startsWith('a:'); + lenLbl.style.display = anim ? '' : 'none'; + editPresetBtn.disabled = !(anim || v.startsWith('p:')); + renderSequenceGrid(); + }; + psel.addEventListener('change', syncKind); + beatsInp.addEventListener('input', () => renderSequenceGrid()); + secInp.addEventListener('input', () => renderSequenceGrid()); + syncKind(); + const rm = document.createElement('button'); rm.type = 'button'; rm.className = 'btn btn-danger btn-small'; rm.textContent = 'Remove step'; - rm.addEventListener('click', () => row.remove()); + rm.addEventListener('click', () => { + row.remove(); + renderSequenceGrid(); + }); row.appendChild(rm); return row; } -function renderSequenceLane(laneIndex, laneSteps, laneGroupIds, presetsMap, groupsMap, zoneGroupIds) { +function laneOptsField(label, input) { + const l = document.createElement('label'); + l.className = 'seq-lane-opt'; + const sp = document.createElement('span'); + sp.textContent = label; + l.appendChild(sp); + l.appendChild(input); + return l; +} + +function renderLaneOptions(opts) { + const o = opts && typeof opts === 'object' ? opts : {}; + const box = document.createElement('div'); + box.className = 'seq-lane-opts'; + + const mode = document.createElement('select'); + mode.className = 'seq-lane-mode zone-device-add-select'; + [['', 'Sequence default'], ['beats', 'Beats'], ['time', 'Seconds']].forEach(([v, t]) => mode.appendChild(new Option(t, v))); + mode.value = o.mode === 'beats' || o.mode === 'time' ? o.mode : ''; + mode.addEventListener('change', applySequenceEditorMode); + + const offset = document.createElement('input'); + offset.type = 'number'; + offset.className = 'seq-lane-offset'; + offset.min = '0'; + offset.step = '0.25'; + offset.value = String(Math.max(0, parseFloat(String(o.offset || 0)) || 0)); + offset.style.width = '4.5rem'; + offset.title = 'Beats (or seconds) before this lane starts'; + + const rate = document.createElement('select'); + rate.className = 'seq-lane-rate zone-device-add-select'; + [['0.25', '¼×'], ['0.5', '½×'], ['1', '1×'], ['2', '2×'], ['4', '4×']].forEach(([v, t]) => rate.appendChild(new Option(t, v))); + rate.value = String(o.rate || 1); + if (!rate.value) rate.value = '1'; + + box.appendChild(laneOptsField('Timing', mode)); + box.appendChild(laneOptsField('Start after', offset)); + box.appendChild(laneOptsField('Speed', rate)); + + const tLbl = document.createElement('div'); + tLbl.className = 'muted-text'; + tLbl.textContent = 'Plays to'; + const chips = document.createElement('div'); + chips.className = 'seq-target-chips seq-lane-targets'; + const sel = new Set(); + (Array.isArray(o.targets) ? o.targets : []).forEach((ix) => { + if (seqEd.targets[ix]) sel.add(String(seqEd.targets[ix].uid)); + }); + renderTargetChips(chips, sel, 'seq-lane-target'); + chips.addEventListener('change', () => renderSequenceGrid()); + + const chase = o.chase && typeof o.chase === 'object' ? o.chase : null; + const chaseRow = document.createElement('div'); + chaseRow.className = 'seq-lane-chase'; + const chaseCb = document.createElement('input'); + chaseCb.type = 'checkbox'; + chaseCb.className = 'seq-lane-chase-on'; + chaseCb.checked = !!chase; + const chaseLbl = document.createElement('label'); + chaseLbl.appendChild(chaseCb); + chaseLbl.appendChild(document.createTextNode(' Chase: each step moves to the next target')); + const order = document.createElement('select'); + order.className = 'seq-lane-chase-order zone-device-add-select'; + [['forward', 'Forward'], ['reverse', 'Reverse'], ['bounce', 'Bounce'], ['random', 'Random']].forEach(([v, t]) => order.appendChild(new Option(t, v))); + order.value = (chase && chase.order) || 'forward'; + const width = document.createElement('input'); + width.type = 'number'; + width.className = 'seq-lane-chase-width'; + width.min = '1'; + width.max = '32'; + width.value = String((chase && chase.width) || 1); + width.style.width = '3.5rem'; + width.title = 'How many targets are lit at once'; + const others = document.createElement('select'); + others.className = 'seq-lane-chase-others zone-device-add-select'; + [['off', 'Others off'], ['hold', 'Others hold']].forEach(([v, t]) => others.appendChild(new Option(t, v))); + others.value = chase && chase.others === 'hold' ? 'hold' : 'off'; + const chaseOpts = document.createElement('span'); + chaseOpts.className = 'seq-lane-chase-opts'; + chaseOpts.appendChild(order); + chaseOpts.appendChild(laneOptsField('Width', width)); + chaseOpts.appendChild(others); + const syncChase = () => { + chaseOpts.style.display = chaseCb.checked ? '' : 'none'; + }; + chaseCb.addEventListener('change', syncChase); + syncChase(); + chaseRow.appendChild(chaseLbl); + chaseRow.appendChild(chaseOpts); + + box.appendChild(tLbl); + box.appendChild(chips); + box.appendChild(chaseRow); + return box; +} + +function renderSequenceLane(laneIndex, laneSteps, laneGroupIds, presetsMap, groupsMap, zoneGroupIds, laneOpts) { const wrap = document.createElement('div'); wrap.className = 'sequence-lane'; wrap.dataset.laneIndex = String(laneIndex); @@ -865,6 +1300,23 @@ function renderSequenceLane(laneIndex, laneSteps, laneGroupIds, presetsMap, grou applySequenceEditorMode(); } }); + const dupLane = document.createElement('button'); + dupLane.type = 'button'; + dupLane.className = 'btn btn-secondary btn-small'; + dupLane.textContent = 'Duplicate'; + dupLane.title = 'Copy this lane (then give the copy an offset for a canon)'; + dupLane.addEventListener('click', () => { + const host = document.getElementById('sequence-editor-lanes'); + if (!host || host.querySelectorAll('.sequence-lane').length >= LANES_MAX_UI) { + alert(`A sequence can have up to ${LANES_MAX_UI} lanes.`); + return; + } + const data = collectLaneFromEl(wrap); + const copy = renderSequenceLane(0, data.steps.map((s) => ({ ...s, _uids: s._uids })), data.groups, presetsMap, groupsMap, zoneGroupIds, data.optsUi); + wrap.after(copy); + refreshSequenceEditorLaneTitles(); + applySequenceEditorMode(); + }); const rmLane = document.createElement('button'); rmLane.type = 'button'; rmLane.className = 'btn btn-danger btn-small sequence-lane-remove-lane'; @@ -877,11 +1329,16 @@ function renderSequenceLane(laneIndex, laneSteps, laneGroupIds, presetsMap, grou refreshSequenceEditorLaneTitles(); }); headBtns.appendChild(addStep); + headBtns.appendChild(dupLane); headBtns.appendChild(rmLane); head.appendChild(headBtns); wrap.appendChild(head); - wrap.appendChild(renderLaneGroupCheckboxes(groupsMap, laneGroupIds, zoneGroupIds)); + wrap.appendChild(renderLaneOptions(laneOpts)); + // Older sequences: groups per lane, shown while the sequence has no targets + const legacy = renderLaneGroupCheckboxes(groupsMap, laneGroupIds, zoneGroupIds); + if (seqEd.targets.length) legacy.style.display = 'none'; + wrap.appendChild(legacy); const stepsHost = document.createElement('div'); stepsHost.className = 'sequence-lane-steps'; @@ -890,6 +1347,7 @@ function renderSequenceLane(laneIndex, laneSteps, laneGroupIds, presetsMap, grou stepsHost.appendChild(renderSequenceStepRow(presetsMap, s)); }); wireSequenceLaneStepsDragReorder(stepsHost); + stepsHost.addEventListener('drop', () => setTimeout(renderSequenceGrid, 0)); wrap.appendChild(stepsHost); return wrap; } @@ -899,23 +1357,104 @@ function refreshSequenceEditorLaneTitles() { if (!host) return; const lanes = [...host.querySelectorAll('.sequence-lane')]; lanes.forEach((el, i) => { + el.dataset.laneIndex = String(i); const t = el.querySelector(':scope > div strong'); if (t) t.textContent = `Lane ${i + 1}`; const rm = el.querySelector('.sequence-lane-remove-lane'); if (rm) rm.disabled = lanes.length <= 1; }); + const add = document.getElementById('sequence-editor-add-lane-btn'); + if (add) add.disabled = lanes.length >= LANES_MAX_UI; + renderSequenceGrid(); } -/** Show the Beats or Seconds field on every step, to match the sequence's advance mode. */ -function applySequenceEditorMode() { +function laneIsTimed(laneEl) { const sel = document.getElementById('sequence-editor-mode'); - const timed = !!sel && sel.value === 'time'; - document.querySelectorAll('#sequence-editor-lanes .sequence-step-beats-wrap').forEach((e) => { - e.style.display = timed ? 'none' : 'flex'; + const def = !!sel && sel.value === 'time'; + const m = laneEl.querySelector('.seq-lane-mode'); + return m && m.value ? m.value === 'time' : def; +} + +/** Show the Beats or Seconds field on every step, to match its lane's timing. */ +function applySequenceEditorMode() { + document.querySelectorAll('#sequence-editor-lanes .sequence-lane').forEach((laneEl) => { + const timed = laneIsTimed(laneEl); + laneEl.querySelectorAll('.sequence-step-beats-wrap').forEach((e) => { + e.style.display = timed ? 'none' : 'flex'; + }); + laneEl.querySelectorAll('.sequence-step-seconds-wrap').forEach((e) => { + e.style.display = timed ? 'flex' : 'none'; + }); + const off = laneEl.querySelector('.seq-lane-offset'); + if (off) off.title = timed ? 'Seconds before this lane starts' : 'Beats before this lane starts'; }); - document.querySelectorAll('#sequence-editor-lanes .sequence-step-seconds-wrap').forEach((e) => { - e.style.display = timed ? 'flex' : 'none'; + renderSequenceGrid(); +} + +function uidsToIndexes(uids) { + const out = []; + seqEd.targets.forEach((t, i) => { + if (uids.has(String(t.uid))) out.push(i); }); + return out; +} + +/** One lane from the editor: its steps (saved form), legacy groups and options. */ +function collectLaneFromEl(laneEl) { + const groups = []; + laneEl.querySelectorAll('.sequence-lane-group:checked').forEach((c) => { + if (c.value) groups.push(String(c.value)); + }); + const steps = []; + const stepsHost = laneEl.querySelector('.sequence-lane-steps'); + const rows = stepsHost ? stepsHost.querySelectorAll(':scope > .sequence-step-row') : []; + rows.forEach((row) => { + const v = (row.querySelector('.sequence-step-preset') || {}).value || ''; + if (!v) return; + const beatsInp = row.querySelector('.sequence-step-beats'); + const secInp = row.querySelector('.sequence-step-seconds'); + const beats = Math.max(0.25, parseFloat(beatsInp && beatsInp.value ? beatsInp.value : '1') || 1); + const seconds = Math.max(0.1, parseFloat(secInp && secInp.value ? secInp.value : '3') || 3); + const st = { beats, seconds }; + if (v === 'rest') { + st.preset_id = ''; + st.rest = true; + } else if (v.startsWith('a:')) { + st.animation_id = v.slice(2); + const len = row.querySelector('.sequence-step-anim-length'); + if (len && len.checked) st.length = 'animation'; + } else { + st.preset_id = v.slice(2); + } + const uids = chipSelection(row.querySelector('.seq-step-targets')); + if (uids.size) { + st.targets = uidsToIndexes(uids); + st._uids = [...uids]; + } + const oo = row.querySelector('.sequence-step-others-off'); + if (oo && oo.checked) st.others = 'off'; + st._row = row; + steps.push(st); + }); + const q = (c) => laneEl.querySelector(c); + const laneUids = chipSelection(q('.seq-lane-targets')); + const opts = {}; + const mode = q('.seq-lane-mode'); + if (mode && mode.value) opts.mode = mode.value; + const off = parseFloat((q('.seq-lane-offset') || {}).value || '0') || 0; + if (off > 0) opts.offset = off; + const rate = parseFloat((q('.seq-lane-rate') || {}).value || '1') || 1; + if (rate !== 1) opts.rate = rate; + if (laneUids.size) opts.targets = uidsToIndexes(laneUids); + const chaseOn = q('.seq-lane-chase-on'); + if (chaseOn && chaseOn.checked) { + opts.chase = { + order: (q('.seq-lane-chase-order') || {}).value || 'forward', + width: Math.max(1, parseInt((q('.seq-lane-chase-width') || {}).value || '1', 10) || 1), + others: (q('.seq-lane-chase-others') || {}).value === 'hold' ? 'hold' : 'off', + }; + } + return { steps, groups, opts, optsUi: { ...opts }, timed: laneIsTimed(laneEl) }; } function collectLanesFromEditor() { @@ -923,28 +1462,158 @@ function collectLanesFromEditor() { const laneEls = host ? [...host.querySelectorAll('.sequence-lane')] : []; const lanes = []; const lanes_group_ids = []; + const lane_opts = []; laneEls.forEach((laneEl) => { - const g = []; - laneEl.querySelectorAll('.sequence-lane-group:checked').forEach((c) => { - if (c.value) g.push(String(c.value)); - }); - lanes_group_ids.push(g); - const steps = []; - const stepsHost = laneEl.querySelector('.sequence-lane-steps'); - const rows = stepsHost ? stepsHost.querySelectorAll(':scope > .sequence-step-row') : []; - rows.forEach((row) => { - const presetSel = row.querySelector('.sequence-step-preset'); - const beatsInp = row.querySelector('.sequence-step-beats'); - const presetId = presetSel && presetSel.value ? String(presetSel.value) : ''; - if (!presetId) return; - const beats = Math.max(1, parseInt(beatsInp && beatsInp.value ? beatsInp.value : '1', 10) || 1); - const secInp = row.querySelector('.sequence-step-seconds'); - const seconds = Math.max(0.1, parseFloat(secInp && secInp.value ? secInp.value : '3') || 3); - steps.push({ preset_id: presetId, beats, seconds }); - }); - lanes.push(steps); + const l = collectLaneFromEl(laneEl); + lanes.push(l.steps.map(({ _row, _uids, ...st }) => st)); + lanes_group_ids.push(l.groups); + lane_opts.push(l.opts); }); - return { lanes, lanes_group_ids }; + return { lanes, lanes_group_ids, lane_opts }; +} + +/* -------------------------------------------------------------- grid view */ + +let seqGridTimer = null; + +function seqStepLabel(st) { + if (st.rest) return { text: 'rest', color: '' }; + if (st.animation_id) { + const a = seqEd.seqMap[st.animation_id]; + return { text: `⚡ ${(a && a.name) || st.animation_id}`, color: '' }; + } + const p = seqEd.presetsMap[st.preset_id]; + const colors = p && Array.isArray(p.colors) ? p.colors : []; + return { text: (p && p.name) || st.preset_id, color: colors[0] || '' }; +} + +/** Lanes side by side on a shared time axis: one row per lane, cells as wide as their steps. */ +function renderSequenceGrid() { + const host = document.getElementById('sequence-editor-grid'); + if (!host || host.hidden) return; + const laneHost = document.getElementById('sequence-editor-lanes'); + const laneEls = laneHost ? [...laneHost.querySelectorAll('.sequence-lane')] : []; + host.innerHTML = ''; + const bpm = parseFloat((document.getElementById('audio-simulated-bpm') || {}).value || '120') || 120; + const beatS = 60 / bpm; + const rows = laneEls.map((el) => collectLaneFromEl(el)); + // Lengths in beats (time lanes converted at the clock tempo) + const len = (l, st) => (l.timed ? st.seconds / beatS : st.beats) / (l.opts.rate || 1); + const offs = (l) => (l.opts.offset || 0) / (l.timed ? beatS : 1); + const total = Math.max(4, ...rows.map((l) => offs(l) + l.steps.reduce((a, st) => a + len(l, st), 0))); + const scale = 100 / total; + rows.forEach((l, li) => { + const row = document.createElement('div'); + row.className = 'seq-grid-row'; + const name = document.createElement('div'); + name.className = 'seq-grid-lane'; + const tnames = (l.opts.targets || []).map((i) => seqEdTargetLabel(seqEd.targets[i], i)); + name.textContent = `${li + 1}${l.opts.chase ? ' ⇢' : ''}`; + name.title = tnames.length ? tnames.join(', ') : 'Lane groups / zone'; + const track = document.createElement('div'); + track.className = 'seq-grid-track'; + if (offs(l) > 0) { + const gap = document.createElement('div'); + gap.className = 'seq-grid-cell seq-grid-gap'; + gap.style.width = `${offs(l) * scale}%`; + track.appendChild(gap); + } + l.steps.forEach((st, si) => { + const cell = document.createElement('button'); + cell.type = 'button'; + cell.className = 'seq-grid-cell'; + cell.dataset.lane = String(li); + cell.dataset.step = String(si); + cell.style.width = `${len(l, st) * scale}%`; + const lab = seqStepLabel(st); + cell.textContent = lab.text; + if (st.rest) cell.classList.add('seq-grid-rest'); + if (st.animation_id) cell.classList.add('seq-grid-anim'); + if (lab.color) cell.style.setProperty('--cell-color', lab.color); + cell.title = `${lab.text} — ${l.timed ? `${st.seconds}s` : `${st.beats} beat(s)`}${st.targets ? ` → ${st.targets.map((i) => seqEdTargetLabel(seqEd.targets[i], i)).join(', ')}` : ''}`; + cell.addEventListener('click', () => { + // Swap the cell for the step's own picker, so changes land on the step + const sel = buildStepPlaySelect(st); + sel.className = 'seq-grid-pick'; + cell.replaceWith(sel); + sel.style.width = cell.style.width; + sel.focus(); + const done = () => { + const src = st._row && st._row.querySelector('.sequence-step-preset'); + if (src && sel.value && src.value !== sel.value) { + src.value = sel.value; + src.dispatchEvent(new Event('change')); + } else { + renderSequenceGrid(); + } + }; + sel.addEventListener('change', done); + sel.addEventListener('blur', () => setTimeout(renderSequenceGrid, 0)); + }); + track.appendChild(cell); + }); + row.appendChild(name); + row.appendChild(track); + host.appendChild(row); + }); + const axis = document.createElement('div'); + axis.className = 'muted-text seq-grid-axis'; + axis.textContent = `${Math.round(total * 100) / 100} beats per pass (seconds shown at ${bpm} BPM)`; + host.appendChild(axis); +} + +/** Highlight the playing steps in the grid while this sequence (or animation) plays. */ +async function pollSequenceGridPlayhead() { + const host = document.getElementById('sequence-editor-grid'); + const modal = document.getElementById('sequence-editor-modal'); + if (!host || host.hidden || !modal || !modal.classList.contains('active') || !sequenceEditorId) { + seqGridTimer = null; + return; + } + try { + const r = await fetch('/sequences/status', { cache: 'no-store', headers: { Accept: 'application/json' } }); + const st = r.ok ? await r.json() : null; + let lanes = null; + if (st && st.active && String(st.sequence_id) === sequenceEditorId) lanes = st.lanes; + else if (st && st.overlay && st.overlay.active && String(st.overlay.sequence_id) === sequenceEditorId) lanes = st.overlay.lanes; + host.querySelectorAll('.seq-grid-cell.playing').forEach((c) => c.classList.remove('playing')); + (Array.isArray(lanes) ? lanes : []).forEach((l, li) => { + if (!l || !l.started || l.done) return; + const c = host.querySelector(`.seq-grid-cell[data-lane="${li}"][data-step="${l.step}"]`); + if (c) c.classList.add('playing'); + }); + } catch (_) { + /* try again */ + } + seqGridTimer = setTimeout(pollSequenceGridPlayhead, 400); +} + +function setSequenceEditorView(grid) { + const g = document.getElementById('sequence-editor-grid'); + const btn = document.getElementById('sequence-editor-view-btn'); + if (!g) return; + g.hidden = !grid; + if (btn) btn.textContent = grid ? 'Hide grid' : 'Show grid'; + try { + localStorage.setItem('seqEditorGrid', grid ? '1' : '0'); + } catch (_) { + /* private mode */ + } + if (grid) { + renderSequenceGrid(); + if (!seqGridTimer) pollSequenceGridPlayhead(); + } +} + +function applySequenceKindUi() { + const kind = document.getElementById('sequence-editor-kind'); + const anim = !!kind && kind.value === 'animation'; + const hint = document.getElementById('sequence-editor-kind-hint'); + if (hint) { + hint.textContent = anim + ? 'An animation plays once over whatever is running, then the drivers go back to it. Fire it from a zone tile or from a step of a sequence.' + : 'A sequence plays in a zone, looping unless Loop is off.'; + } } async function openSequenceEditor(sequenceId, existing) { @@ -954,9 +1623,18 @@ async function openSequenceEditor(sequenceId, existing) { const lanesHost = document.getElementById('sequence-editor-lanes'); if (!modal || !nameInput || !lanesHost) return; - const presetsRes = await fetch('/presets', { headers: { Accept: 'application/json' } }); + const [presetsRes, groupsMap, devicesRes, seqMap] = await Promise.all([ + fetch('/presets', { headers: { Accept: 'application/json' } }), + fetchGroupsMapSeq(), + fetch('/devices', { headers: { Accept: 'application/json' } }).catch(() => null), + fetchSequencesMap(), + ]); const presetsMap = presetsRes.ok ? await presetsRes.json() : {}; - const groupsMap = await fetchGroupsMapSeq(); + let devicesMap = {}; + if (devicesRes && devicesRes.ok) { + const d = await devicesRes.json().catch(() => ({})); + devicesMap = d && typeof d === 'object' ? d : {}; + } let zoneDoc = {}; const zoneIdForEditor = resolveZoneIdForPresetStripRefresh(); @@ -997,19 +1675,41 @@ async function openSequenceEditor(sequenceId, existing) { if (!doc || typeof doc !== 'object') { doc = {}; } + seqEd.presetsMap = presetsMap; + seqEd.groupsMap = groupsMap; + seqEd.devicesMap = devicesMap; + seqEd.seqMap = seqMap; + seqEd.zoneGroupIds = zoneGroupIds; + seqEd.targets = []; + (Array.isArray(doc.targets) ? doc.targets : []).forEach((t) => + addSeqTarget({ + name: t && t.name ? String(t.name) : '', + group_ids: Array.isArray(t && t.group_ids) ? t.group_ids.map(String) : [], + devices: Array.isArray(t && t.devices) ? t.devices.map((m) => String(m).toLowerCase()) : [], + }), + ); + nameInput.value = doc.name || ''; const modeSelOpen = document.getElementById('sequence-editor-mode'); if (modeSelOpen) modeSelOpen.value = doc.advance_mode === 'time' ? 'time' : 'beats'; + const kindSel = document.getElementById('sequence-editor-kind'); + const isAnim = doc.kind === 'animation' || (!sequenceEditorId && seqEditorNewKind === 'animation'); + if (kindSel) kindSel.value = isAnim ? 'animation' : 'sequence'; + const loopCb = document.getElementById('sequence-editor-loop'); + if (loopCb) loopCb.checked = doc.loop == null ? !isAnim : doc.loop === true || doc.loop === 1; + applySequenceKindUi(); const lanes = normalizeSequenceLanes(doc); + const laneOpts = Array.isArray(doc.lane_opts) ? doc.lane_opts : []; + renderTargetsPanel(); lanesHost.innerHTML = ''; if (!lanes.some((l) => l.length > 0)) { const lg0 = buildLaneGroupIdsForEditor(doc, 0, 1); - lanesHost.appendChild(renderSequenceLane(0, [], lg0, presetsMap, groupsMap, zoneGroupIds)); + lanesHost.appendChild(renderSequenceLane(0, [], lg0, presetsMap, groupsMap, zoneGroupIds, laneOpts[0])); } else { lanes.forEach((laneSteps, i) => { const lg = buildLaneGroupIdsForEditor(doc, i, lanes.length); - lanesHost.appendChild(renderSequenceLane(i, laneSteps, lg, presetsMap, groupsMap, zoneGroupIds)); + lanesHost.appendChild(renderSequenceLane(i, laneSteps, lg, presetsMap, groupsMap, zoneGroupIds, laneOpts[i])); }); } refreshSequenceEditorLaneTitles(); @@ -1017,10 +1717,21 @@ async function openSequenceEditor(sequenceId, existing) { const edDel = document.getElementById('sequence-editor-delete-btn'); if (edDel) edDel.style.display = sequenceEditorId ? 'inline-block' : 'none'; + const edTry = document.getElementById('sequence-editor-try-btn'); + if (edTry) edTry.style.display = sequenceEditorId ? 'inline-block' : 'none'; + let grid = false; + try { + grid = localStorage.getItem('seqEditorGrid') === '1'; + } catch (_) { + grid = false; + } modal.classList.add('active'); + setSequenceEditorView(grid); } +let seqEditorNewKind = 'sequence'; + /** Zone id for refreshing the visible preset/sequence strip if `getCurrentZoneId` is not set yet. */ function resolveZoneIdForPresetStripRefresh() { if (window.zonesManager && typeof window.zonesManager.getCurrentZoneId === 'function') { @@ -1039,24 +1750,42 @@ function resolveZoneIdForPresetStripRefresh() { async function saveSequenceEditor() { const modeSel = document.getElementById('sequence-editor-mode'); const nameInput = document.getElementById('sequence-editor-name'); - const { lanes, lanes_group_ids } = collectLanesFromEditor(); + const { lanes, lanes_group_ids, lane_opts } = collectLanesFromEditor(); const idxs = []; lanes.forEach((l, i) => { - if (l.some((s) => s && s.preset_id)) idxs.push(i); + if (l.length) idxs.push(i); }); if (!idxs.length) { - alert('Add at least one step with a preset selected.'); + alert('Add at least one step with a preset, animation or rest.'); return; } - const nonEmpty = idxs.map((i) => lanes[i].filter((s) => s && s.preset_id)); - const nonEmptyLg = idxs.map((i) => (lanes_group_ids[i] ? [...lanes_group_ids[i]] : [])); + const targets = seqEd.targets.map((t) => ({ + name: String(t.name || '').trim(), + group_ids: [...t.group_ids], + devices: [...t.devices], + })); + const nonEmpty = idxs.map((i) => lanes[i]); + const opts = idxs.map((i) => lane_opts[i] || {}); + // Lanes that play to targets also get their groups in lanes_group_ids, for readers that predate targets + const nonEmptyLg = idxs.map((i, k) => { + const own = lanes_group_ids[i] ? [...lanes_group_ids[i]] : []; + if (!targets.length || !opts[k].targets) return own; + const g = []; + opts[k].targets.forEach((ti) => (targets[ti] ? targets[ti].group_ids : []).forEach((x) => g.includes(x) || g.push(x))); + return g; + }); + const kindSel = document.getElementById('sequence-editor-kind'); + const loopCb = document.getElementById('sequence-editor-loop'); const payload = { name: nameInput ? nameInput.value.trim() : '', + kind: kindSel && kindSel.value === 'animation' ? 'animation' : 'sequence', + targets, lanes: nonEmpty, + lane_opts: opts, lanes_group_ids: nonEmptyLg, group_ids: nonEmptyLg[0] ? [...nonEmptyLg[0]] : [], advance_mode: modeSel && modeSel.value === 'time' ? 'time' : 'beats', - loop: true, + loop: loopCb ? !!loopCb.checked : true, steps: nonEmpty.length === 1 ? nonEmpty[0] : [], }; @@ -1088,9 +1817,12 @@ async function saveSequenceEditor() { sequenceEditorId = String(entries[0][0]); const edDel = document.getElementById('sequence-editor-delete-btn'); if (edDel) edDel.style.display = 'inline-block'; + const edTry = document.getElementById('sequence-editor-try-btn'); + if (edTry) edTry.style.display = 'inline-block'; } } } + seqEd.seqMap = await fetchSequencesMap(); await loadSequencesModalList(); const zid = resolveZoneIdForPresetStripRefresh(); if (zid && typeof window.refreshEditTabSequencesUi === 'function') { @@ -1122,9 +1854,11 @@ async function deleteCurrentSequence() { if (tabResponse.ok) { const tabData = await tabResponse.json(); const list = Array.isArray(tabData.sequence_ids) ? tabData.sequence_ids.map(String) : []; + const alist = Array.isArray(tabData.animation_ids) ? tabData.animation_ids.map(String) : []; const sid = String(idToDelete); - if (list.includes(sid)) { + if (list.includes(sid) || alist.includes(sid)) { tabData.sequence_ids = list.filter((x) => x !== sid); + tabData.animation_ids = alist.filter((x) => x !== sid); await fetch(`/zones/${zid}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, @@ -1169,7 +1903,8 @@ async function loadSequencesModalList() { const ln = normalizeSequenceLanes(doc); const nSteps = ln.reduce((a, l) => a + l.length, 0); const nLanes = ln.filter((l) => l.length > 0).length || 1; - title.textContent = `${doc.name || id} — ${nLanes} lane(s), ${nSteps} step(s)`; + const anim = doc.kind === 'animation'; + title.textContent = `${anim ? '⚡ ' : ''}${doc.name || id} — ${anim ? 'animation, ' : ''}${nLanes} lane(s), ${nSteps} step(s)`; const edit = document.createElement('button'); edit.type = 'button'; edit.className = 'btn btn-secondary btn-small'; @@ -1190,6 +1925,7 @@ window.setSequenceDebug = function setSequenceDebug(on) { console.log(seqDebugEnabled() ? 1 : 0); }; window.appendZoneSequenceTiles = appendZoneSequenceTiles; +window.fireAnimation = fireAnimation; window.refreshEditTabSequencesUi = refreshEditTabSequencesUi; window.addSequenceToTab = addSequenceToTab; window.removeSequenceFromTab = removeSequenceFromTab; @@ -1200,6 +1936,14 @@ document.addEventListener('DOMContentLoaded', () => { const modal = document.getElementById('sequences-modal'); const closeBtn = document.getElementById('sequences-close-btn'); const addBtn = document.getElementById('sequence-add-btn'); + const addAnimBtn = document.getElementById('animation-add-btn'); + if (addAnimBtn) { + addAnimBtn.addEventListener('click', () => { + sequenceEditorId = null; + seqEditorNewKind = 'animation'; + openSequenceEditor(null, null); + }); + } if (btn && modal) { btn.addEventListener('click', () => { modal.classList.add('active'); @@ -1212,6 +1956,7 @@ document.addEventListener('DOMContentLoaded', () => { if (addBtn) { addBtn.addEventListener('click', () => { sequenceEditorId = null; + seqEditorNewKind = 'sequence'; openSequenceEditor(null, null); }); } @@ -1244,11 +1989,54 @@ document.addEventListener('DOMContentLoaded', () => { const presetsMap = presetsRes.ok ? await presetsRes.json() : {}; const groupsMap = await fetchGroupsMapSeq(); const idx = host.querySelectorAll('.sequence-lane').length; - host.appendChild(renderSequenceLane(idx, [], [], presetsMap, groupsMap)); + if (idx >= LANES_MAX_UI) { + alert(`A sequence can have up to ${LANES_MAX_UI} lanes.`); + return; + } + seqEd.presetsMap = presetsMap; + host.appendChild(renderSequenceLane(idx, [], [], presetsMap, groupsMap, seqEd.zoneGroupIds, {})); refreshSequenceEditorLaneTitles(); applySequenceEditorMode(); }); } const edMode = document.getElementById('sequence-editor-mode'); if (edMode) edMode.addEventListener('change', applySequenceEditorMode); + const edKind = document.getElementById('sequence-editor-kind'); + if (edKind) { + edKind.addEventListener('change', () => { + const loopCb = document.getElementById('sequence-editor-loop'); + if (loopCb) loopCb.checked = edKind.value !== 'animation'; + applySequenceKindUi(); + }); + } + const addTarget = document.getElementById('sequence-editor-add-target-btn'); + if (addTarget) { + addTarget.addEventListener('click', () => { + if (addSeqTarget({})) renderTargetsPanel(); + }); + } + const perDriver = document.getElementById('sequence-editor-per-driver-btn'); + if (perDriver) perDriver.addEventListener('click', addTargetsPerDriver); + const viewBtn = document.getElementById('sequence-editor-view-btn'); + if (viewBtn) { + viewBtn.addEventListener('click', () => { + const g = document.getElementById('sequence-editor-grid'); + setSequenceEditorView(!!g && g.hidden); + }); + } + const tryBtn = document.getElementById('sequence-editor-try-btn'); + if (tryBtn) { + tryBtn.addEventListener('click', async () => { + if (!sequenceEditorId) return; + await saveSequenceEditor(); + const zid = resolveZoneIdForPresetStripRefresh(); + const kind = document.getElementById('sequence-editor-kind'); + try { + if (kind && kind.value === 'animation') await fireAnimation(sequenceEditorId, zid); + else await requestBackendSequencePlay(sequenceEditorId, zid, null); + } catch (e) { + alert(e.message || 'Could not play it.'); + } + }); + } }); diff --git a/web/static/style.css b/web/static/style.css index 45c1e05..d45cc13 100644 --- a/web/static/style.css +++ b/web/static/style.css @@ -2583,3 +2583,240 @@ html.simulated-bpm-mode .seq-switch-toggle-wrap { .list-search-hidden { display: none !important; } + +/* Sequence editor: targets, lane options, grid (sequences.js) */ +.seq-targets-panel { + margin: 0 0 0.75rem 0; + padding: 0.5rem; + border: 1px solid #4a4a4a; + border-radius: 6px; +} + +.seq-targets-panel > summary { + cursor: pointer; +} + +.seq-target-row { + display: flex; + align-items: flex-start; + gap: 0.5rem; + flex-wrap: wrap; +} + +.seq-target-row input[type="text"] { + width: 10rem; +} + +.seq-target-row details { + flex: 1 1 12rem; + min-width: 0; +} + +.seq-target-row summary { + cursor: pointer; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.seq-target-num { + min-width: 1.5rem; + opacity: 0.7; + padding-top: 0.35rem; +} + +.seq-target-pick, +.seq-target-chips { + display: flex; + flex-wrap: wrap; + gap: 0.35rem 0.75rem; + align-items: center; + margin: 0.25rem 0; +} + +.seq-target-pick .muted-text { + flex-basis: 100%; +} + +.seq-target-btns { + display: flex; + gap: 0.25rem; +} + +.seq-chip { + display: inline-flex; + align-items: center; + gap: 0.25rem; + font-size: 0.9em; +} + +.seq-lane-opts { + display: flex; + flex-wrap: wrap; + gap: 0.35rem 0.75rem; + align-items: center; + margin-bottom: 0.5rem; +} + +.seq-lane-opts > .muted-text, +.seq-lane-opts > .seq-target-chips, +.seq-lane-chase { + flex-basis: 100%; +} + +.seq-lane-opt { + display: inline-flex; + align-items: center; + gap: 0.35rem; +} + +.seq-lane-chase, +.seq-lane-chase-opts { + display: flex; + flex-wrap: wrap; + gap: 0.5rem; + align-items: center; +} + +.seq-step-more > summary { + cursor: pointer; + font-size: 0.9em; + opacity: 0.8; +} + +.seq-grid { + margin: 0 0 0.75rem 0; + overflow-x: auto; +} + +.seq-grid-row { + display: flex; + align-items: stretch; + gap: 0.35rem; + margin-bottom: 0.25rem; +} + +.seq-grid-lane { + flex: 0 0 2rem; + display: flex; + align-items: center; + justify-content: center; + font-weight: bold; + opacity: 0.8; +} + +.seq-grid-track { + flex: 1 1 auto; + display: flex; + min-width: 18rem; +} + +.seq-grid-cell { + --cell-color: #6a5acd; + box-sizing: border-box; + min-width: 1.25rem; + height: 2.25rem; + padding: 0 0.25rem; + border: 1px solid #222; + border-left: 4px solid var(--cell-color); + background: #3a3a3a; + color: white; + font-size: 0.8em; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + cursor: pointer; + text-align: left; +} + +.seq-grid-cell:hover { + background: #474747; +} + +.seq-grid-gap { + background: repeating-linear-gradient(45deg, transparent 0 4px, rgba(255, 255, 255, 0.06) 4px 8px); + border-left: 1px solid #222; + cursor: default; +} + +.seq-grid-rest { + background: #262626; + color: #aaa; + border-left-color: #555; +} + +.seq-grid-anim { + border-left-color: #f5c542; +} + +.seq-grid-cell.playing { + outline: 2px solid #f5f5f5; + outline-offset: -2px; + background: #55507a; +} + +.seq-grid-pick { + height: 2.25rem; + min-width: 6rem; +} + +.seq-grid-axis { + margin-top: 0.25rem; + font-size: 0.85em; +} + +/* Animation tiles: one shot, with progress while playing */ +.animation-tile-row .sequence-tile-main { + position: relative; + overflow: hidden; + border-style: dashed; +} + +.animation-tile-progress { + position: absolute; + left: 0; + bottom: 0; + height: 3px; + width: calc(var(--anim-progress, 0) * 100%); + background: #f5c542; + transition: width 0.2s linear; +} + +.animation-tile-row.firing .sequence-tile-main { + border-color: #f5c542; +} + +/* The modal's block labels and full-width inputs don't suit the sequence editor's compact rows */ +#sequence-editor-modal .seq-chip, +#sequence-editor-modal .seq-lane-opt, +#sequence-editor-modal .seq-lane-chase label, +#sequence-editor-modal .seq-step-more label { + display: inline-flex; + align-items: center; + gap: 0.3rem; + margin: 0; + font-weight: normal; +} + +#sequence-editor-modal .seq-chip input[type="checkbox"], +#sequence-editor-modal .seq-lane-chase input[type="checkbox"], +#sequence-editor-modal .seq-step-more input[type="checkbox"] { + width: auto; + margin: 0; +} + +#sequence-editor-modal .seq-lane-opt input, +#sequence-editor-modal .seq-lane-chase-opts input { + width: 4.5rem; + margin: 0; +} + +#sequence-editor-modal .seq-target-row input[type="text"] { + width: 10rem; + margin: 0; +} + +#sequence-editor-modal .seq-lane-opts > .muted-text { + text-align: left; + margin-top: 0.25rem; +} diff --git a/web/templates/index.html b/web/templates/index.html index 23f0f28..fac4a67 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -349,13 +349,14 @@