Files
anvil/templates/basic/monitor.bat
Eric Ratliff 8fe1ef0e27 Refactor CLI, add refresh command, fix port detection, add device tracking
- Remove build/upload/monitor subcommands (projects are self-contained)
- Remove ctrlc dependency (only used by removed monitor watch mode)
- Update next-steps messaging to reference project scripts directly

- Add 'anvil refresh [DIR] [--force]' to update project scripts
  to latest templates without touching user code

- Fix Windows port detection: replace fragile findstr/batch TOML
  parsing with proper comment-skipping logic; add _detect_port.ps1
  helper for reliable JSON-based port detection via PowerShell

- Add .anvil.local for machine-specific config (gitignored)
  - 'anvil devices --set [PORT] [-d DIR]' saves port + VID:PID
  - 'anvil devices --get [-d DIR]' shows saved port status
  - VID:PID tracks USB devices across COM port reassignment
  - Port resolution: -p flag > VID:PID > saved port > auto-detect
  - Uppercase normalization for Windows COM port names

- Update all .bat/.sh templates to read from .anvil.local
- Remove port entries from .anvil.toml (no machine-specific config in git)
- Add .anvil.local to .gitignore template
- Expand 'anvil devices' output with VID:PID, serial number, and
  usage instructions
2026-02-18 20:32:42 -06:00

113 lines
3.5 KiB
Batchfile

@echo off
setlocal enabledelayedexpansion
:: monitor.bat -- Open the serial monitor
::
:: Reads baud rate from .anvil.toml. No Anvil binary required.
::
:: Usage:
:: monitor.bat Open monitor (auto-detect port)
:: monitor.bat -p COM3 Specify port
:: monitor.bat -b 9600 Override baud rate
set "SCRIPT_DIR=%~dp0"
set "CONFIG=%SCRIPT_DIR%.anvil.toml"
set "LOCAL_CONFIG=%SCRIPT_DIR%.anvil.local"
if not exist "%CONFIG%" (
echo FAIL: No .anvil.toml found in %SCRIPT_DIR%
exit /b 1
)
:: -- Parse .anvil.toml ----------------------------------------------------
for /f "usebackq tokens=1,* delims==" %%a in ("%CONFIG%") do (
set "_K=%%a"
if not "!_K:~0,1!"=="#" if not "!_K:~0,1!"=="[" (
set "_K=!_K: =!"
set "_V=%%b"
if defined _V (
set "_V=!_V: =!"
set "_V=!_V:"=!"
)
if "!_K!"=="baud" set "BAUD=!_V!"
)
)
:: -- Parse .anvil.local (machine-specific, not in git) --------------------
set "LOCAL_PORT="
set "LOCAL_VID_PID="
if exist "%LOCAL_CONFIG%" (
for /f "usebackq tokens=1,* delims==" %%a in ("%LOCAL_CONFIG%") do (
set "_K=%%a"
if not "!_K:~0,1!"=="#" (
set "_K=!_K: =!"
set "_V=%%b"
if defined _V (
set "_V=!_V: =!"
set "_V=!_V:"=!"
)
if "!_K!"=="port" set "LOCAL_PORT=!_V!"
if "!_K!"=="vid_pid" set "LOCAL_VID_PID=!_V!"
)
)
)
if "%BAUD%"=="" set "BAUD=115200"
:: -- Parse arguments ------------------------------------------------------
set "PORT="
:parse_args
if "%~1"=="" goto done_args
if "%~1"=="-p" set "PORT=%~2" & shift & shift & goto parse_args
if "%~1"=="--port" set "PORT=%~2" & shift & shift & goto parse_args
if "%~1"=="-b" set "BAUD=%~2" & shift & shift & goto parse_args
if "%~1"=="--baud" set "BAUD=%~2" & shift & shift & goto parse_args
if "%~1"=="--help" goto show_help
if "%~1"=="-h" goto show_help
echo FAIL: Unknown option: %~1
exit /b 1
:show_help
echo Usage: monitor.bat [-p PORT] [-b BAUD]
echo Opens serial monitor. Baud rate from .anvil.toml.
exit /b 0
:done_args
:: -- Preflight ------------------------------------------------------------
where arduino-cli >nul 2>nul
if errorlevel 1 (
echo FAIL: arduino-cli not found in PATH.
exit /b 1
)
:: -- Resolve port ---------------------------------------------------------
:: Priority: -p flag > VID:PID resolve > saved port > auto-detect
if "%PORT%"=="" (
for /f "delims=" %%p in ('powershell -NoProfile -ExecutionPolicy Bypass -File "%SCRIPT_DIR%_detect_port.ps1" -VidPid "%LOCAL_VID_PID%" -SavedPort "%LOCAL_PORT%"') do (
if "!PORT!"=="" set "PORT=%%p"
)
if "!PORT!"=="" (
echo FAIL: No serial port detected. Specify with: monitor.bat -p COM3
echo Or save a default: anvil devices --set
exit /b 1
)
if not "%LOCAL_VID_PID%"=="" (
if not "!PORT!"=="%LOCAL_PORT%" (
echo info Device %LOCAL_VID_PID% found on !PORT! ^(moved from %LOCAL_PORT%^)
) else (
echo info Using port !PORT! ^(from .anvil.local^)
)
) else if not "%LOCAL_PORT%"=="" (
echo info Using port !PORT! ^(from .anvil.local^)
) else (
echo warn Auto-detected port: !PORT! ^(use -p to override, or: anvil devices --set^)
)
)
:: -- Monitor --------------------------------------------------------------
echo Opening serial monitor on %PORT% at %BAUD% baud...
echo Press Ctrl+C to exit.
echo.
arduino-cli monitor -p %PORT% -c "baudrate=%BAUD%"