This commit is contained in:
Eric Ratliff
2026-02-18 20:15:46 -06:00
parent 833ea44748
commit 60ba7c7bed
11 changed files with 559 additions and 81 deletions

View File

@@ -0,0 +1,29 @@
# _detect_port.ps1 -- Detect the best serial port via arduino-cli
#
# Called by upload.bat and monitor.bat. Outputs a single port name
# (e.g. COM3) or nothing if no port is found.
# Prefers USB serial ports over legacy motherboard COM ports.
$ErrorActionPreference = 'SilentlyContinue'
$raw = arduino-cli board list --format json 2>$null
if (-not $raw) { exit }
$data = $raw | ConvertFrom-Json
if (-not $data.detected_ports) { exit }
$serial = $data.detected_ports | Where-Object { $_.port.protocol -eq 'serial' }
if (-not $serial) { exit }
# Prefer USB serial ports (skip legacy COM1-style ports)
$usb = $serial | Where-Object { $_.port.protocol_label -like '*USB*' } | Select-Object -First 1
if ($usb) {
Write-Output $usb.port.address
exit
}
# Fall back to any serial port
$any = $serial | Select-Object -First 1
if ($any) {
Write-Output $any.port.address
}

View File

@@ -10,4 +10,3 @@ extra_flags = ["-Werror"]
[monitor]
baud = 115200
# port = "/dev/ttyUSB0" # Uncomment to skip auto-detect

View File

@@ -2,6 +2,9 @@
.build/
test/build/
# Machine-specific config (created by: anvil devices --set)
.anvil.local
# IDE
.vscode/.browse*
.vscode/*.log

View File

@@ -19,23 +19,21 @@ if not exist "%CONFIG%" (
)
:: -- Parse .anvil.toml ----------------------------------------------------
for /f "tokens=1,* delims==" %%a in ('findstr /b "name " "%CONFIG%"') do (
set "SKETCH_NAME=%%b"
:: Read file directly, skip comments and section headers
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!"=="name" set "SKETCH_NAME=!_V!"
if "!_K!"=="fqbn" set "FQBN=!_V!"
if "!_K!"=="warnings" set "WARNINGS=!_V!"
)
)
for /f "tokens=1,* delims==" %%a in ('findstr /b "fqbn " "%CONFIG%"') do (
set "FQBN=%%b"
)
for /f "tokens=1,* delims==" %%a in ('findstr /b "warnings " "%CONFIG%"') do (
set "WARNINGS=%%b"
)
:: Strip quotes and whitespace
set "SKETCH_NAME=%SKETCH_NAME: =%"
set "SKETCH_NAME=%SKETCH_NAME:"=%"
set "FQBN=%FQBN: =%"
set "FQBN=%FQBN:"=%"
set "WARNINGS=%WARNINGS: =%"
set "WARNINGS=%WARNINGS:"=%"
if "%SKETCH_NAME%"=="" (
echo FAIL: Could not read project name from .anvil.toml
@@ -102,19 +100,7 @@ echo.
if not exist "%BUILD_DIR%" mkdir "%BUILD_DIR%"
set "COMPILE_CMD=arduino-cli compile --fqbn %FQBN% --build-path "%BUILD_DIR%" --warnings %WARNINGS%"
if not "%BUILD_FLAGS%"=="" (
set "COMPILE_CMD=%COMPILE_CMD% --build-property "build.extra_flags=%BUILD_FLAGS%""
)
if not "%VERBOSE%"=="" (
set "COMPILE_CMD=%COMPILE_CMD% %VERBOSE%"
)
set "COMPILE_CMD=%COMPILE_CMD% "%SKETCH_DIR%""
%COMPILE_CMD%
arduino-cli compile --fqbn %FQBN% --build-path "%BUILD_DIR%" --warnings %WARNINGS% --build-property "build.extra_flags=%BUILD_FLAGS%" %VERBOSE% "%SKETCH_DIR%"
if errorlevel 1 (
echo.
echo FAIL: Compilation failed.
@@ -123,4 +109,4 @@ if errorlevel 1 (
echo.
echo ok Compile succeeded.
echo.
echo.

View File

@@ -12,6 +12,7 @@ setlocal enabledelayedexpansion
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%
@@ -19,11 +20,37 @@ if not exist "%CONFIG%" (
)
:: -- Parse .anvil.toml ----------------------------------------------------
for /f "tokens=1,* delims==" %%a in ('findstr /b "baud " "%CONFIG%"') do (
set "BAUD=%%b"
:: Read file directly, skip comments and section headers
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!"
)
)
set "BAUD=%BAUD: =%"
set "BAUD=%BAUD:"=%"
:: -- Parse .anvil.local (machine-specific, not in git) --------------------
set "LOCAL_PORT="
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 "%BAUD%"=="" set "BAUD=115200"
:: -- Parse arguments ------------------------------------------------------
@@ -54,21 +81,28 @@ if errorlevel 1 (
exit /b 1
)
:: -- Auto-detect port -----------------------------------------------------
:: -- Resolve port ---------------------------------------------------------
:: Priority: -p flag > .anvil.local > auto-detect
if "%PORT%"=="" (
for /f "tokens=1" %%p in ('arduino-cli board list 2^>nul ^| findstr /i "serial" ^| findstr /n "." ^| findstr "^1:"') do (
set "PORT=%%p"
if not "%LOCAL_PORT%"=="" (
set "PORT=!LOCAL_PORT!"
echo info Using port !PORT! ^(from .anvil.local^)
) else (
:: Use PowerShell helper for reliable JSON-based detection
for /f "delims=" %%p in ('powershell -NoProfile -ExecutionPolicy Bypass -File "%SCRIPT_DIR%_detect_port.ps1"') 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 COM3
exit /b 1
)
echo warn Auto-detected port: !PORT! ^(use -p to override, or: anvil devices --set^)
)
set "PORT=!PORT:1:=!"
if "!PORT!"=="" (
echo FAIL: No serial port detected. Specify with: monitor.bat -p COM3
exit /b 1
)
echo warn Auto-detected port: !PORT! (use -p to override)
)
:: -- 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%"
arduino-cli monitor -p %PORT% -c "baudrate=%BAUD%"

View File

@@ -14,6 +14,7 @@ setlocal enabledelayedexpansion
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%
@@ -21,27 +22,39 @@ if not exist "%CONFIG%" (
)
:: -- Parse .anvil.toml ----------------------------------------------------
for /f "tokens=1,* delims==" %%a in ('findstr /b "name " "%CONFIG%"') do (
set "SKETCH_NAME=%%b"
)
for /f "tokens=1,* delims==" %%a in ('findstr /b "fqbn " "%CONFIG%"') do (
set "FQBN=%%b"
)
for /f "tokens=1,* delims==" %%a in ('findstr /b "warnings " "%CONFIG%"') do (
set "WARNINGS=%%b"
)
for /f "tokens=1,* delims==" %%a in ('findstr /b "baud " "%CONFIG%"') do (
set "BAUD=%%b"
:: Read file directly, skip comments and section headers
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!"=="name" set "SKETCH_NAME=!_V!"
if "!_K!"=="fqbn" set "FQBN=!_V!"
if "!_K!"=="warnings" set "WARNINGS=!_V!"
if "!_K!"=="baud" set "BAUD=!_V!"
)
)
set "SKETCH_NAME=%SKETCH_NAME: =%"
set "SKETCH_NAME=%SKETCH_NAME:"=%"
set "FQBN=%FQBN: =%"
set "FQBN=%FQBN:"=%"
set "WARNINGS=%WARNINGS: =%"
set "WARNINGS=%WARNINGS:"=%"
set "BAUD=%BAUD: =%"
set "BAUD=%BAUD:"=%"
:: -- Parse .anvil.local (machine-specific, not in git) --------------------
set "LOCAL_PORT="
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 "%SKETCH_NAME%"=="" (
echo FAIL: Could not read project name from .anvil.toml
@@ -84,18 +97,25 @@ if errorlevel 1 (
exit /b 1
)
:: -- Auto-detect port -----------------------------------------------------
:: -- Resolve port ---------------------------------------------------------
:: Priority: -p flag > .anvil.local > auto-detect
if "%PORT%"=="" (
for /f "tokens=1" %%p in ('arduino-cli board list 2^>nul ^| findstr /i "serial" ^| findstr /n "." ^| findstr "^1:"') do (
set "PORT=%%p"
if not "%LOCAL_PORT%"=="" (
set "PORT=!LOCAL_PORT!"
echo info Using port !PORT! ^(from .anvil.local^)
) else (
:: Use PowerShell helper for reliable JSON-based detection
for /f "delims=" %%p in ('powershell -NoProfile -ExecutionPolicy Bypass -File "%SCRIPT_DIR%_detect_port.ps1"') do (
if "!PORT!"=="" set "PORT=%%p"
)
if "!PORT!"=="" (
echo FAIL: No serial port detected. Is the board plugged in?
echo Specify manually: upload.bat -p COM3
echo Or save a default: anvil devices --set COM3
exit /b 1
)
echo warn Auto-detected port: !PORT! ^(use -p to override, or: anvil devices --set^)
)
:: Strip the line number prefix
set "PORT=!PORT:1:=!"
if "!PORT!"=="" (
echo FAIL: No serial port detected. Specify with: upload.bat -p COM3
exit /b 1
)
echo warn Auto-detected port: !PORT! (use -p to override)
)
:: -- Clean ----------------------------------------------------------------
@@ -141,4 +161,4 @@ if "%DO_MONITOR%"=="1" (
echo Press Ctrl+C to exit.
echo.
arduino-cli monitor -p %PORT% -c "baudrate=%BAUD%"
)
)