@echo off setlocal enabledelayedexpansion :: upload.bat -- Compile and upload the sketch to the board :: :: Reads all settings from .anvil.toml. No Anvil binary required. :: :: Usage: :: upload.bat Auto-detect port, compile + upload :: upload.bat -p COM3 Specify port :: upload.bat --monitor Open serial monitor after upload :: upload.bat --clean Clean build cache first :: upload.bat --verbose Full compiler + avrdude output set "SCRIPT_DIR=%~dp0" set "CONFIG=%SCRIPT_DIR%.anvil.toml" if not exist "%CONFIG%" ( echo FAIL: No .anvil.toml found in %SCRIPT_DIR% exit /b 1 ) :: -- 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" ) 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:"=%" if "%SKETCH_NAME%"=="" ( echo FAIL: Could not read project name from .anvil.toml exit /b 1 ) if "%BAUD%"=="" set "BAUD=115200" set "SKETCH_DIR=%SCRIPT_DIR%%SKETCH_NAME%" set "BUILD_DIR=%SCRIPT_DIR%.build" :: -- Parse arguments ------------------------------------------------------ set "PORT=" set "DO_MONITOR=0" set "DO_CLEAN=0" set "VERBOSE=" :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"=="--monitor" set "DO_MONITOR=1" & shift & goto parse_args if "%~1"=="--clean" set "DO_CLEAN=1" & shift & goto parse_args if "%~1"=="--verbose" set "VERBOSE=--verbose" & 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: upload.bat [-p PORT] [--monitor] [--clean] [--verbose] echo Compiles and uploads the sketch. Settings 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 ) :: -- Auto-detect port ----------------------------------------------------- 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" ) :: 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 ---------------------------------------------------------------- if "%DO_CLEAN%"=="1" ( if exist "%BUILD_DIR%" rmdir /s /q "%BUILD_DIR%" ) :: -- Build include flags -------------------------------------------------- set "BUILD_FLAGS=" for %%d in (lib\hal lib\app) do ( if exist "%SCRIPT_DIR%%%d" ( set "BUILD_FLAGS=!BUILD_FLAGS! -I%SCRIPT_DIR%%%d" ) ) set "BUILD_FLAGS=!BUILD_FLAGS! -Werror" :: -- Compile -------------------------------------------------------------- echo Compiling %SKETCH_NAME%... if not exist "%BUILD_DIR%" mkdir "%BUILD_DIR%" 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 FAIL: Compilation failed. exit /b 1 ) echo ok Compile succeeded. :: -- Upload --------------------------------------------------------------- echo. echo Uploading to %PORT%... arduino-cli upload --fqbn %FQBN% --port %PORT% --input-dir "%BUILD_DIR%" %VERBOSE% if errorlevel 1 ( echo FAIL: Upload failed. exit /b 1 ) echo ok Upload complete! :: -- Monitor -------------------------------------------------------------- if "%DO_MONITOR%"=="1" ( echo. echo Opening serial monitor on %PORT% at %BAUD% baud... echo Press Ctrl+C to exit. echo. arduino-cli monitor -p %PORT% -c "baudrate=%BAUD%" )