diff --git a/MAP.md b/MAP.md index f624dff..13553c4 100644 --- a/MAP.md +++ b/MAP.md @@ -112,6 +112,7 @@ the "BUILD DELTAS" block at the top of `vcv/SPEC.md`). `src/MEMLNaut.cpp` (modul ### `scripts/` — build + verify entry points - `build-firmware.sh`, `flash-firmware.sh`, `build-and-flash-firmware.sh`, `firmware-common.sh` — Arduino-CLI wrapper for RP2350 target with C++20 flag. +- `flash-useq-main.sh`, `flash-useq-expander.sh` — build + flash the uSEQ-CV firmware (`firmware/useq-celium/{main,expander}`) via PlatformIO (nix-shell fallback for `pio`). - `build-wasm.sh` — Emscripten compile producing `playground/public/nisps.{wasm,js}`. - `build-cpp-tests.sh` — CMake configure + build + ctest (Ninja). - `parity-check.sh` — runs native + WASM and diffs binary outputs. diff --git a/firmware/useq-celium/README.md b/firmware/useq-celium/README.md index a6b526f..5fc1793 100644 --- a/firmware/useq-celium/README.md +++ b/firmware/useq-celium/README.md @@ -27,12 +27,22 @@ expander/ uSEQ expander: I2C slave → CV4..11 (8× PWM) The main board forwards CV4–CV11 to the expander over I2C (addr `0x10`, 400 kHz) and rescans every 2 s until the expander is found (hot-attach friendly). -## Build / flash (PlatformIO) +## Build / flash + +Convenience scripts (resolve PlatformIO via `nix-shell` if `pio` isn't on PATH; +extra args pass through to `pio`, e.g. `--upload-port /dev/ttyACM0`): ```bash -# main board -cd firmware/useq-celium/main && pio run -e main -t upload -# expander board +scripts/flash-useq-main.sh # build + flash the main module +scripts/flash-useq-expander.sh # build + flash the expander +``` + +If an upload can't auto-reset the board, hold **BOOTSEL** while plugging in USB. + +Raw PlatformIO, equivalently: + +```bash +cd firmware/useq-celium/main && pio run -e main -t upload cd firmware/useq-celium/expander && pio run -e expander -t upload ``` diff --git a/manifold/src/backends/cv-backend.ts b/manifold/src/backends/cv-backend.ts index e9a37a0..3388ec3 100644 --- a/manifold/src/backends/cv-backend.ts +++ b/manifold/src/backends/cv-backend.ts @@ -35,6 +35,9 @@ import { const SEND_INTERVAL_MS = 1000 / STREAM_HZ; // ~10 ms const CV_DEAD_ZONE = 8; // on the 12-bit value (~0.2%) const IDENTIFY_TIMEOUT_MS = 5000; +// The firmware streams INPUT frames at 20 Hz, so a live module proves itself +// within ~50 ms. Allow for a USB-CDC (DTR) reset on open before giving up. +const HANDSHAKE_TIMEOUT_MS = 2500; /** Resolve a channel id into a fast {kind, index} target (or null for 'none'). */ type Target = { kind: 'cv'; idx: number } | { kind: 'gate'; idx: number } | null; @@ -57,10 +60,14 @@ export class UseqCvBackend implements OutputBackend { private writer: WritableStreamDefaultWriter | null = null; private reader: ReadableStreamDefaultReader | null = null; private parser = new UseqRxParser( - () => {}, + () => this.onDeviceFrame(), // any INPUT frame proves the module is alive (ok) => this.onAck(ok), ); + /** True once a valid device→host frame has been received (handshake OK). */ + private handshook = false; + private handshakeTimer: ReturnType | null = null; + private cvVals = new Uint16Array(NUM_CV); // last computed 12-bit CV per channel private gateBits = 0; private lastCv = new Int32Array(NUM_CV).fill(-1); @@ -84,7 +91,11 @@ export class UseqCvBackend implements OutputBackend { return; } if (this.port) { - this.setStatus({ state: 'ready', message: 'uSEQ connected' }); + this.setStatus( + this.handshook + ? { state: 'ready', message: 'uSEQ handshake OK' } + : { state: 'connecting', message: 'uSEQ port open — waiting for module…' }, + ); return; } // Try to silently re-open a previously-granted port (no user gesture needed). @@ -97,7 +108,7 @@ export class UseqCvBackend implements OutputBackend { } catch { /* ignore — fall through to the idle "connect" prompt */ } - this.setStatus({ state: 'ready', message: 'uSEQ ready — click Connect device' }); + this.setStatus({ state: 'idle', message: 'No uSEQ — click Connect device' }); } setContext(ctx: BackendContext): void { @@ -123,7 +134,7 @@ export class UseqCvBackend implements OutputBackend { try { port = await navigator.serial.requestPort(); } catch { - this.setStatus({ state: 'ready', message: 'uSEQ ready — click Connect device' }); + this.setStatus({ state: 'idle', message: 'No uSEQ — click Connect device' }); return; // user dismissed the picker } await this.open(port); @@ -140,11 +151,43 @@ export class UseqCvBackend implements OutputBackend { this.writer = port.writable?.getWriter() ?? null; this.lastCv.fill(-1); this.lastGateBits = -1; + this.handshook = false; if (typeof port.addEventListener === 'function') { port.addEventListener('disconnect', () => this.handleDisconnect()); } this.startReadLoop(); - this.setStatus({ state: 'ready', message: 'uSEQ connected' }); + // Port is open, but a handshake isn't confirmed until the module talks back. + this.setStatus({ state: 'connecting', message: 'uSEQ port open — waiting for module…' }); + this.armHandshakeWatchdog(); + } + + /** If no device frame arrives within the timeout, surface a clear error. */ + private armHandshakeWatchdog(): void { + this.clearHandshakeWatchdog(); + this.handshakeTimer = setTimeout(() => { + this.handshakeTimer = null; + if (this.port && !this.handshook) { + this.setStatus({ + state: 'error', + message: 'No response from module — check firmware / cable, then Identify', + }); + } + }, HANDSHAKE_TIMEOUT_MS); + } + + private clearHandshakeWatchdog(): void { + if (this.handshakeTimer !== null) { + clearTimeout(this.handshakeTimer); + this.handshakeTimer = null; + } + } + + /** A valid device→host frame arrived → the module is alive (handshake OK). */ + private onDeviceFrame(): void { + this.clearHandshakeWatchdog(); + if (this.handshook) return; + this.handshook = true; + if (this.port) this.setStatus({ state: 'ready', message: 'uSEQ handshake OK' }); } private async startReadLoop(): Promise { @@ -175,22 +218,30 @@ export class UseqCvBackend implements OutputBackend { if (!w) return; const frame = new Uint8Array(FRAME_IDENTIFY_LEN); encodeIdentify(frame); - this.setStatus({ state: 'connecting', message: 'Identify… (watch the LEDs)' }); + this.setStatus({ state: 'connecting', message: 'Identify sent — watch the LEDs…' }); let settled = false; - const done = (msg: string) => { + const done = (s: BackendStatus) => { if (settled) return; settled = true; this.pendingIdentify = null; - if (this.port) this.setStatus({ state: 'ready', message: msg }); + if (this.port) this.setStatus(s); }; - this.pendingIdentify = () => done('uSEQ connected — identify ack'); - w.write(frame).catch(() => done('uSEQ connected')); - setTimeout(() => done('uSEQ connected'), IDENTIFY_TIMEOUT_MS); + this.pendingIdentify = () => done({ state: 'ready', message: 'uSEQ handshake OK — LEDs flashed' }); + w.write(frame).catch(() => done({ state: 'error', message: 'Identify failed — module not responding' })); + setTimeout( + () => + done( + this.handshook + ? { state: 'ready', message: 'uSEQ handshake OK' } + : { state: 'error', message: 'No identify ack — check firmware / cable' }, + ), + IDENTIFY_TIMEOUT_MS, + ); } - private onAck(ok: boolean): void { + private onAck(_ok: boolean): void { + this.onDeviceFrame(); // an ack is also proof the module is alive if (this.pendingIdentify) this.pendingIdentify(); - else if (ok && this.port) this.setStatus({ state: 'ready', message: 'uSEQ connected' }); } send(routed: Float32Array): void { @@ -245,6 +296,8 @@ export class UseqCvBackend implements OutputBackend { private handleDisconnect(): void { if (!this.port) return; + this.clearHandshakeWatchdog(); + this.handshook = false; this.port = null; this.writer = null; this.setStatus({ state: 'error', message: 'uSEQ disconnected — click Connect device' }); @@ -252,10 +305,12 @@ export class UseqCvBackend implements OutputBackend { async disconnect(): Promise { await this.closePort(); - this.setStatus({ state: 'ready', message: 'uSEQ ready — click Connect device' }); + this.setStatus({ state: 'idle', message: 'No uSEQ — click Connect device' }); } private async closePort(): Promise { + this.clearHandshakeWatchdog(); + this.handshook = false; try { this.reader?.cancel().catch(() => {}); this.writer?.releaseLock(); diff --git a/manifold/src/dock/OutputsBackendConfig.tsx b/manifold/src/dock/OutputsBackendConfig.tsx index 63cde6a..2ad824d 100644 --- a/manifold/src/dock/OutputsBackendConfig.tsx +++ b/manifold/src/dock/OutputsBackendConfig.tsx @@ -587,8 +587,22 @@ function VcvConfig({ ctx }: { ctx: ConsoleCtx }) { function CvConfig({ ctx }: { ctx: ConsoleCtx }) { const s = ctx.backendStatus; - const statusColor = - s.state === 'ready' ? 'var(--good)' : s.state === 'connecting' ? 'var(--warn)' : 'var(--danger)'; + // Green ONLY when the module has actually talked back (handshake confirmed); + // amber while waiting, red on error, grey when nothing is connected. + const dotColor = + s.state === 'ready' + ? 'var(--good)' + : s.state === 'connecting' + ? 'var(--warn)' + : s.state === 'idle' + ? 'var(--fg-mute)' + : 'var(--danger)'; + const handshakeLabel = + s.state === 'ready' + ? 'Module handshake OK' + : s.state === 'idle' + ? 'Not connected' + : s.message; return ( <> uSEQ device (USB serial) @@ -602,7 +616,32 @@ function CvConfig({ ctx }: { ctx: ConsoleCtx }) { - {s.message} + +
+ + + {handshakeLabel} +

Flash the uSEQ main + expander with firmware/useq-celium, connect over USB, then assign each diff --git a/scripts/flash-useq-expander.sh b/scripts/flash-useq-expander.sh new file mode 100755 index 0000000..2a91ecf --- /dev/null +++ b/scripts/flash-useq-expander.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# Build + flash the uSEQ-CV **expander** firmware (firmware/useq-celium/expander). +# Target: uSEQ USEQHARDWARE_EXPANDER_OUT_0_1 (RP2040). Any extra args are passed +# to pio (e.g. --upload-port /dev/ttyACM1). +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +BOARD_DIR="$SCRIPT_DIR/../firmware/useq-celium/expander" +ENV="expander" + +if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then + echo "Usage: scripts/flash-useq-expander.sh [extra pio args]" + echo " Builds firmware/useq-celium/expander and uploads it to the uSEQ EXPANDER board." + exit 0 +fi + +# Resolve PlatformIO (nix-friendly: fall back to an ephemeral nix-shell). +run_pio() { + if command -v pio >/dev/null 2>&1; then + pio "$@" + elif command -v platformio >/dev/null 2>&1; then + platformio "$@" + elif command -v nix-shell >/dev/null 2>&1; then + nix-shell -p platformio --run "pio $*" + else + echo "error: PlatformIO not found. Install it, e.g.:" >&2 + echo " nix-shell -p platformio # or: pipx install platformio" >&2 + exit 1 + fi +} + +echo "==> Build + flash uSEQ EXPANDER (env: $ENV)" +echo " If the upload can't auto-reset the board, hold BOOTSEL while plugging in USB." +cd "$BOARD_DIR" +run_pio run -e "$ENV" -t upload "$@" +echo "==> Done. The expander LEDs sweep when the main board forwards an Identify." diff --git a/scripts/flash-useq-main.sh b/scripts/flash-useq-main.sh new file mode 100755 index 0000000..12aefe2 --- /dev/null +++ b/scripts/flash-useq-main.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# Build + flash the uSEQ-CV **main** module firmware (firmware/useq-celium/main). +# Target: uSEQ USEQHARDWARE_1_0 (RP2040). Any extra args are passed to pio +# (e.g. --upload-port /dev/ttyACM0). +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +BOARD_DIR="$SCRIPT_DIR/../firmware/useq-celium/main" +ENV="main" + +if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then + echo "Usage: scripts/flash-useq-main.sh [extra pio args]" + echo " Builds firmware/useq-celium/main and uploads it to the uSEQ MAIN board." + exit 0 +fi + +# Resolve PlatformIO (nix-friendly: fall back to an ephemeral nix-shell). +run_pio() { + if command -v pio >/dev/null 2>&1; then + pio "$@" + elif command -v platformio >/dev/null 2>&1; then + platformio "$@" + elif command -v nix-shell >/dev/null 2>&1; then + nix-shell -p platformio --run "pio $*" + else + echo "error: PlatformIO not found. Install it, e.g.:" >&2 + echo " nix-shell -p platformio # or: pipx install platformio" >&2 + exit 1 + fi +} + +echo "==> Build + flash uSEQ MAIN module (env: $ENV)" +echo " If the upload can't auto-reset the board, hold BOOTSEL while plugging in USB." +cd "$BOARD_DIR" +run_pio run -e "$ENV" -t upload "$@" +echo "==> Done. The main board's LEDs sweep on Identify from the Manifold CV panel."