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
This commit is contained in:
@@ -1,29 +1,38 @@
|
||||
# {{PROJECT_NAME}}
|
||||
|
||||
Arduino project generated by Anvil v{{ANVIL_VERSION}}.
|
||||
Arduino project generated by [Anvil](https://github.com/nexusworkshops/anvil) v{{ANVIL_VERSION}}.
|
||||
|
||||
This project is self-contained. After creation, it only needs `arduino-cli`
|
||||
in PATH -- the Anvil binary is not required for day-to-day work.
|
||||
|
||||
## Quick Start
|
||||
|
||||
```bash
|
||||
# Check your system
|
||||
anvil doctor
|
||||
# Compile only (verify)
|
||||
./build.sh
|
||||
|
||||
# Find connected boards
|
||||
anvil devices
|
||||
|
||||
# Compile only (no upload)
|
||||
anvil build --verify {{PROJECT_NAME}}
|
||||
|
||||
# Compile and upload
|
||||
anvil build {{PROJECT_NAME}}
|
||||
# Compile and upload to board
|
||||
./upload.sh
|
||||
|
||||
# Compile, upload, and open serial monitor
|
||||
anvil build --monitor {{PROJECT_NAME}}
|
||||
./upload.sh --monitor
|
||||
|
||||
# Open serial monitor (no compile)
|
||||
./monitor.sh
|
||||
|
||||
# Persistent monitor (reconnects after reset/replug)
|
||||
./monitor.sh --watch
|
||||
|
||||
# Run host-side unit tests (no board needed)
|
||||
cd test && ./run_tests.sh
|
||||
./test/run_tests.sh
|
||||
```
|
||||
|
||||
On Windows, use `build.bat`, `upload.bat`, `monitor.bat`, and
|
||||
`test\run_tests.bat` instead.
|
||||
|
||||
All scripts read settings from `.anvil.toml` -- edit it to change
|
||||
the board, baud rate, include paths, or compiler flags.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
@@ -44,6 +53,9 @@ cd test && ./run_tests.sh
|
||||
CMakeLists.txt Test build system
|
||||
run_tests.sh Test runner (Linux/Mac)
|
||||
run_tests.bat Test runner (Windows)
|
||||
build.sh / build.bat Compile sketch
|
||||
upload.sh / upload.bat Compile + upload to board
|
||||
monitor.sh / monitor.bat Serial monitor
|
||||
.anvil.toml Project configuration
|
||||
```
|
||||
|
||||
@@ -52,7 +64,7 @@ cd test && ./run_tests.sh
|
||||
All hardware access goes through the `Hal` interface. The app code
|
||||
(`lib/app/`) depends only on `Hal`, never on `Arduino.h` directly.
|
||||
This means the app can be compiled and tested on the host without
|
||||
any Arduino SDK.
|
||||
any Arduino hardware.
|
||||
|
||||
Two HAL implementations:
|
||||
- `ArduinoHal` -- passthroughs to real hardware (used in the .ino)
|
||||
@@ -72,3 +84,9 @@ extra_flags = ["-Werror"]
|
||||
[monitor]
|
||||
baud = 115200
|
||||
```
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- `arduino-cli` in PATH with `arduino:avr` core installed
|
||||
- For host tests: `cmake`, `g++` (or `clang++`), `git`
|
||||
- Install everything at once: `anvil setup`
|
||||
|
||||
78
templates/basic/_detect_port.ps1
Normal file
78
templates/basic/_detect_port.ps1
Normal file
@@ -0,0 +1,78 @@
|
||||
# _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.
|
||||
#
|
||||
# If .anvil.local contains a vid_pid, resolves the device to its
|
||||
# current COM port (handles reassignment after replug).
|
||||
# Falls back to saved port, then auto-detect.
|
||||
|
||||
param(
|
||||
[string]$VidPid = "",
|
||||
[string]$SavedPort = ""
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'SilentlyContinue'
|
||||
|
||||
$raw = arduino-cli board list --format json 2>$null
|
||||
if (-not $raw) {
|
||||
# No arduino-cli or no output; fall back to saved port
|
||||
if ($SavedPort) { Write-Output $SavedPort }
|
||||
exit
|
||||
}
|
||||
|
||||
$data = $raw | ConvertFrom-Json
|
||||
if (-not $data.detected_ports) {
|
||||
if ($SavedPort) { Write-Output $SavedPort }
|
||||
exit
|
||||
}
|
||||
|
||||
$serial = $data.detected_ports | Where-Object { $_.port.protocol -eq 'serial' }
|
||||
if (-not $serial) {
|
||||
if ($SavedPort) { Write-Output $SavedPort }
|
||||
exit
|
||||
}
|
||||
|
||||
# -- Try VID:PID resolution first ------------------------------------------
|
||||
if ($VidPid) {
|
||||
$parts = $VidPid -split ':'
|
||||
if ($parts.Count -eq 2) {
|
||||
$targetVid = $parts[0].ToLower()
|
||||
$targetPid = $parts[1].ToLower()
|
||||
|
||||
foreach ($p in $serial) {
|
||||
$pVid = ($p.port.properties.vid -replace '^0x','').ToLower()
|
||||
$pPid = ($p.port.properties.pid -replace '^0x','').ToLower()
|
||||
if ($pVid -eq $targetVid -and $pPid -eq $targetPid) {
|
||||
Write-Output $p.port.address
|
||||
exit
|
||||
}
|
||||
}
|
||||
|
||||
# VID:PID not found -- device not connected
|
||||
# Fall through to saved port / auto-detect
|
||||
}
|
||||
}
|
||||
|
||||
# -- Fall back to saved port if it exists on the system --------------------
|
||||
if ($SavedPort) {
|
||||
$found = $serial | Where-Object { $_.port.address -eq $SavedPort } | Select-Object -First 1
|
||||
if ($found) {
|
||||
Write-Output $SavedPort
|
||||
exit
|
||||
}
|
||||
# Saved port not present either; fall through to auto-detect
|
||||
}
|
||||
|
||||
# -- Auto-detect: prefer USB serial 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
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
# Build artifacts
|
||||
.build/
|
||||
test/build/
|
||||
|
||||
# Machine-specific config (created by: anvil devices --set)
|
||||
.anvil.local
|
||||
|
||||
# IDE
|
||||
.vscode/.browse*
|
||||
.vscode/*.log
|
||||
|
||||
112
templates/basic/build.bat
Normal file
112
templates/basic/build.bat
Normal file
@@ -0,0 +1,112 @@
|
||||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
|
||||
:: build.bat -- Compile the sketch using arduino-cli
|
||||
::
|
||||
:: Reads all settings from .anvil.toml. No Anvil binary required.
|
||||
::
|
||||
:: Usage:
|
||||
:: build.bat Compile (verify only)
|
||||
:: build.bat --clean Delete build cache first
|
||||
:: build.bat --verbose Show full compiler 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 ----------------------------------------------------
|
||||
:: 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 "%SKETCH_NAME%"=="" (
|
||||
echo FAIL: Could not read project name from .anvil.toml
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set "SKETCH_DIR=%SCRIPT_DIR%%SKETCH_NAME%"
|
||||
set "BUILD_DIR=%SCRIPT_DIR%.build"
|
||||
|
||||
:: -- Parse arguments ------------------------------------------------------
|
||||
set "DO_CLEAN=0"
|
||||
set "VERBOSE="
|
||||
|
||||
:parse_args
|
||||
if "%~1"=="" goto done_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: build.bat [--clean] [--verbose]
|
||||
echo Compiles 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
|
||||
)
|
||||
|
||||
if not exist "%SKETCH_DIR%" (
|
||||
echo FAIL: Sketch directory not found: %SKETCH_DIR%
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
:: -- Clean ----------------------------------------------------------------
|
||||
if "%DO_CLEAN%"=="1" (
|
||||
if exist "%BUILD_DIR%" (
|
||||
echo Cleaning build cache...
|
||||
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%...
|
||||
echo Board: %FQBN%
|
||||
echo Sketch: %SKETCH_DIR%
|
||||
echo.
|
||||
|
||||
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.
|
||||
echo FAIL: Compilation failed.
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo.
|
||||
echo ok Compile succeeded.
|
||||
echo.
|
||||
145
templates/basic/build.sh
Normal file
145
templates/basic/build.sh
Normal file
@@ -0,0 +1,145 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# build.sh -- Compile the sketch using arduino-cli
|
||||
#
|
||||
# Reads all settings from .anvil.toml. No Anvil binary required.
|
||||
#
|
||||
# Usage:
|
||||
# ./build.sh Compile (verify only)
|
||||
# ./build.sh --clean Delete build cache first
|
||||
# ./build.sh --verbose Show full compiler output
|
||||
#
|
||||
# Prerequisites: arduino-cli in PATH, arduino:avr core installed
|
||||
# Install: anvil setup (or manually: arduino-cli core install arduino:avr)
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
CONFIG="$SCRIPT_DIR/.anvil.toml"
|
||||
|
||||
# -- Colors ----------------------------------------------------------------
|
||||
if [[ -t 1 ]]; then
|
||||
RED=$'\033[0;31m'; GRN=$'\033[0;32m'; YLW=$'\033[0;33m'
|
||||
CYN=$'\033[0;36m'; BLD=$'\033[1m'; RST=$'\033[0m'
|
||||
else
|
||||
RED=''; GRN=''; YLW=''; CYN=''; BLD=''; RST=''
|
||||
fi
|
||||
|
||||
ok() { echo "${GRN}ok${RST} $*"; }
|
||||
warn() { echo "${YLW}warn${RST} $*"; }
|
||||
die() { echo "${RED}FAIL${RST} $*" >&2; exit 1; }
|
||||
|
||||
# -- Parse .anvil.toml -----------------------------------------------------
|
||||
[[ -f "$CONFIG" ]] || die "No .anvil.toml found in $SCRIPT_DIR"
|
||||
|
||||
# Extract a simple string value: toml_get "key"
|
||||
# Searches the whole file; for sectioned keys, grep is specific enough
|
||||
# given our small, flat schema.
|
||||
toml_get() {
|
||||
(grep "^$1 " "$CONFIG" 2>/dev/null || true) | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' '
|
||||
}
|
||||
|
||||
# Extract a TOML array as space-separated values: toml_array "key"
|
||||
toml_array() {
|
||||
(grep "^$1 " "$CONFIG" 2>/dev/null || true) | head -1 \
|
||||
| sed 's/.*\[//; s/\].*//; s/"//g; s/,/ /g' | tr -s ' '
|
||||
}
|
||||
|
||||
SKETCH_NAME="$(toml_get 'name')"
|
||||
FQBN="$(toml_get 'fqbn')"
|
||||
WARNINGS="$(toml_get 'warnings')"
|
||||
INCLUDE_DIRS="$(toml_array 'include_dirs')"
|
||||
EXTRA_FLAGS="$(toml_array 'extra_flags')"
|
||||
|
||||
[[ -n "$SKETCH_NAME" ]] || die "Could not read project name from .anvil.toml"
|
||||
[[ -n "$FQBN" ]] || die "Could not read fqbn from .anvil.toml"
|
||||
|
||||
SKETCH_DIR="$SCRIPT_DIR/$SKETCH_NAME"
|
||||
BUILD_DIR="$SCRIPT_DIR/.build"
|
||||
|
||||
# -- Parse arguments -------------------------------------------------------
|
||||
DO_CLEAN=0
|
||||
VERBOSE=""
|
||||
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--clean) DO_CLEAN=1 ;;
|
||||
--verbose) VERBOSE="--verbose" ;;
|
||||
-h|--help)
|
||||
echo "Usage: ./build.sh [--clean] [--verbose]"
|
||||
echo " Compiles the sketch. Settings from .anvil.toml."
|
||||
exit 0
|
||||
;;
|
||||
*) die "Unknown option: $arg" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# -- Preflight -------------------------------------------------------------
|
||||
command -v arduino-cli &>/dev/null \
|
||||
|| die "arduino-cli not found in PATH. Install it first."
|
||||
|
||||
[[ -d "$SKETCH_DIR" ]] \
|
||||
|| die "Sketch directory not found: $SKETCH_DIR"
|
||||
|
||||
[[ -f "$SKETCH_DIR/$SKETCH_NAME.ino" ]] \
|
||||
|| die "Sketch file not found: $SKETCH_DIR/$SKETCH_NAME.ino"
|
||||
|
||||
# -- Clean -----------------------------------------------------------------
|
||||
if [[ $DO_CLEAN -eq 1 ]] && [[ -d "$BUILD_DIR" ]]; then
|
||||
echo "${YLW}Cleaning build cache...${RST}"
|
||||
rm -rf "$BUILD_DIR"
|
||||
ok "Cache cleared."
|
||||
fi
|
||||
|
||||
# -- Build include flags ---------------------------------------------------
|
||||
BUILD_FLAGS=""
|
||||
for dir in $INCLUDE_DIRS; do
|
||||
abs="$SCRIPT_DIR/$dir"
|
||||
if [[ -d "$abs" ]]; then
|
||||
BUILD_FLAGS="$BUILD_FLAGS -I$abs"
|
||||
else
|
||||
warn "Include directory not found: $dir"
|
||||
fi
|
||||
done
|
||||
for flag in $EXTRA_FLAGS; do
|
||||
BUILD_FLAGS="$BUILD_FLAGS $flag"
|
||||
done
|
||||
|
||||
# -- Compile ---------------------------------------------------------------
|
||||
echo "${CYN}${BLD}Compiling ${SKETCH_NAME}...${RST}"
|
||||
echo " Board: $FQBN"
|
||||
echo " Sketch: $SKETCH_DIR"
|
||||
echo ""
|
||||
|
||||
mkdir -p "$BUILD_DIR"
|
||||
|
||||
COMPILE_ARGS=(
|
||||
compile
|
||||
--fqbn "$FQBN"
|
||||
--build-path "$BUILD_DIR"
|
||||
--warnings "$WARNINGS"
|
||||
)
|
||||
|
||||
if [[ -n "$BUILD_FLAGS" ]]; then
|
||||
COMPILE_ARGS+=(--build-property "build.extra_flags=$BUILD_FLAGS")
|
||||
fi
|
||||
|
||||
if [[ -n "$VERBOSE" ]]; then
|
||||
COMPILE_ARGS+=("$VERBOSE")
|
||||
fi
|
||||
|
||||
COMPILE_ARGS+=("$SKETCH_DIR")
|
||||
|
||||
arduino-cli "${COMPILE_ARGS[@]}" || die "Compilation failed."
|
||||
|
||||
echo ""
|
||||
ok "Compile succeeded."
|
||||
|
||||
# -- Binary size -----------------------------------------------------------
|
||||
ELF="$BUILD_DIR/$SKETCH_NAME.ino.elf"
|
||||
if [[ -f "$ELF" ]] && command -v avr-size &>/dev/null; then
|
||||
echo ""
|
||||
avr-size --mcu=atmega328p -C "$ELF"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
113
templates/basic/monitor.bat
Normal file
113
templates/basic/monitor.bat
Normal file
@@ -0,0 +1,113 @@
|
||||
@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%"
|
||||
173
templates/basic/monitor.sh
Normal file
173
templates/basic/monitor.sh
Normal file
@@ -0,0 +1,173 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# monitor.sh -- Open the serial monitor
|
||||
#
|
||||
# Reads baud rate from .anvil.toml. No Anvil binary required.
|
||||
#
|
||||
# Usage:
|
||||
# ./monitor.sh Auto-detect port
|
||||
# ./monitor.sh -p /dev/ttyUSB0 Specify port
|
||||
# ./monitor.sh -b 9600 Override baud rate
|
||||
# ./monitor.sh --watch Reconnect after reset/replug
|
||||
#
|
||||
# Prerequisites: arduino-cli in PATH
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
CONFIG="$SCRIPT_DIR/.anvil.toml"
|
||||
|
||||
# -- Colors ----------------------------------------------------------------
|
||||
if [[ -t 1 ]]; then
|
||||
RED=$'\033[0;31m'; GRN=$'\033[0;32m'; YLW=$'\033[0;33m'
|
||||
CYN=$'\033[0;36m'; BLD=$'\033[1m'; RST=$'\033[0m'
|
||||
else
|
||||
RED=''; GRN=''; YLW=''; CYN=''; BLD=''; RST=''
|
||||
fi
|
||||
|
||||
warn() { echo "${YLW}warn${RST} $*"; }
|
||||
die() { echo "${RED}FAIL${RST} $*" >&2; exit 1; }
|
||||
|
||||
# -- Parse .anvil.toml -----------------------------------------------------
|
||||
[[ -f "$CONFIG" ]] || die "No .anvil.toml found in $SCRIPT_DIR"
|
||||
|
||||
toml_get() {
|
||||
(grep "^$1 " "$CONFIG" 2>/dev/null || true) | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' '
|
||||
}
|
||||
|
||||
BAUD="$(toml_get 'baud')"
|
||||
BAUD="${BAUD:-115200}"
|
||||
LOCAL_CONFIG="$SCRIPT_DIR/.anvil.local"
|
||||
LOCAL_PORT=""
|
||||
LOCAL_VID_PID=""
|
||||
if [[ -f "$LOCAL_CONFIG" ]]; then
|
||||
LOCAL_PORT="$(grep '^port ' "$LOCAL_CONFIG" 2>/dev/null | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' ')"
|
||||
LOCAL_VID_PID="$(grep '^vid_pid ' "$LOCAL_CONFIG" 2>/dev/null | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' ')"
|
||||
fi
|
||||
|
||||
# -- Parse arguments -------------------------------------------------------
|
||||
PORT=""
|
||||
DO_WATCH=0
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-p|--port) PORT="$2"; shift 2 ;;
|
||||
-b|--baud) BAUD="$2"; shift 2 ;;
|
||||
--watch) DO_WATCH=1; shift ;;
|
||||
-h|--help)
|
||||
echo "Usage: ./monitor.sh [-p PORT] [-b BAUD] [--watch]"
|
||||
echo " Opens serial monitor. Baud rate from .anvil.toml."
|
||||
exit 0
|
||||
;;
|
||||
*) die "Unknown option: $1" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# -- Preflight -------------------------------------------------------------
|
||||
command -v arduino-cli &>/dev/null \
|
||||
|| die "arduino-cli not found in PATH."
|
||||
|
||||
# -- Auto-detect port ------------------------------------------------------
|
||||
auto_detect() {
|
||||
# Prefer ttyUSB/ttyACM (real USB devices) over ttyS (hardware UART)
|
||||
local port
|
||||
port=$(arduino-cli board list 2>/dev/null \
|
||||
| grep -i "serial" \
|
||||
| awk '{print $1}' \
|
||||
| grep -E 'ttyUSB|ttyACM|COM' \
|
||||
| head -1)
|
||||
|
||||
# Fallback: any serial port
|
||||
if [[ -z "$port" ]]; then
|
||||
port=$(arduino-cli board list 2>/dev/null \
|
||||
| grep -i "serial" \
|
||||
| head -1 \
|
||||
| awk '{print $1}')
|
||||
fi
|
||||
|
||||
echo "$port"
|
||||
}
|
||||
|
||||
# resolve_vid_pid VID:PID -- search arduino-cli JSON for matching device
|
||||
resolve_vid_pid() {
|
||||
local target_vid target_pid json
|
||||
target_vid="$(echo "$1" | cut -d: -f1 | tr '[:upper:]' '[:lower:]')"
|
||||
target_pid="$(echo "$1" | cut -d: -f2 | tr '[:upper:]' '[:lower:]')"
|
||||
json="$(arduino-cli board list --format json 2>/dev/null)" || return
|
||||
echo "$json" | python3 -c "
|
||||
import sys, json
|
||||
try:
|
||||
data = json.load(sys.stdin)
|
||||
for dp in data.get('detected_ports', []):
|
||||
port = dp.get('port', {})
|
||||
if port.get('protocol') != 'serial':
|
||||
continue
|
||||
props = port.get('properties', {})
|
||||
vid = props.get('vid', '').lower().replace('0x', '')
|
||||
pid = props.get('pid', '').lower().replace('0x', '')
|
||||
if vid == '$target_vid' and pid == '$target_pid':
|
||||
print(port.get('address', ''))
|
||||
break
|
||||
except: pass
|
||||
" 2>/dev/null
|
||||
}
|
||||
|
||||
if [[ -z "$PORT" ]]; then
|
||||
# Try VID:PID resolution first
|
||||
if [[ -n "$LOCAL_VID_PID" ]]; then
|
||||
PORT="$(resolve_vid_pid "$LOCAL_VID_PID")"
|
||||
if [[ -n "$PORT" ]]; then
|
||||
if [[ "$PORT" != "$LOCAL_PORT" ]] && [[ -n "$LOCAL_PORT" ]]; then
|
||||
warn "Device $LOCAL_VID_PID found on $PORT (moved from $LOCAL_PORT)"
|
||||
else
|
||||
warn "Using port $PORT (from .anvil.local)"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Fall back to saved port
|
||||
if [[ -z "$PORT" ]] && [[ -n "$LOCAL_PORT" ]]; then
|
||||
PORT="$LOCAL_PORT"
|
||||
warn "Using port $PORT (from .anvil.local)"
|
||||
fi
|
||||
|
||||
# Fall back to auto-detect
|
||||
if [[ -z "$PORT" ]]; then
|
||||
PORT="$(auto_detect)"
|
||||
|
||||
if [[ -z "$PORT" ]]; then
|
||||
die "No serial port detected. Is the board plugged in?\n Specify manually: ./monitor.sh -p /dev/ttyUSB0\n Or save a default: anvil devices --set"
|
||||
fi
|
||||
warn "Auto-detected port: $PORT (use -p to override, or: anvil devices --set)"
|
||||
fi
|
||||
fi
|
||||
|
||||
# -- Watch mode ------------------------------------------------------------
|
||||
if [[ $DO_WATCH -eq 1 ]]; then
|
||||
echo "${CYN}${BLD}Persistent monitor on ${PORT} at ${BAUD} baud${RST}"
|
||||
echo "Reconnects after upload / reset / replug."
|
||||
echo "Press Ctrl+C to exit."
|
||||
echo ""
|
||||
|
||||
trap "echo ''; echo 'Monitor stopped.'; exit 0" INT
|
||||
|
||||
while true; do
|
||||
if [[ -e "$PORT" ]]; then
|
||||
arduino-cli monitor -p "$PORT" -c "baudrate=$BAUD" 2>/dev/null || true
|
||||
echo "${YLW}--- ${PORT} disconnected ---${RST}"
|
||||
else
|
||||
echo "${CYN}--- Waiting for ${PORT} ...${RST}"
|
||||
while [[ ! -e "$PORT" ]]; do
|
||||
sleep 0.5
|
||||
done
|
||||
sleep 1
|
||||
echo "${GRN}--- ${PORT} connected ---${RST}"
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
else
|
||||
echo "Opening serial monitor on $PORT at $BAUD baud..."
|
||||
echo "Press Ctrl+C to exit."
|
||||
echo ""
|
||||
arduino-cli monitor -p "$PORT" -c "baudrate=$BAUD"
|
||||
fi
|
||||
169
templates/basic/upload.bat
Normal file
169
templates/basic/upload.bat
Normal file
@@ -0,0 +1,169 @@
|
||||
@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"
|
||||
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!"=="name" set "SKETCH_NAME=!_V!"
|
||||
if "!_K!"=="fqbn" set "FQBN=!_V!"
|
||||
if "!_K!"=="warnings" set "WARNINGS=!_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 "%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
|
||||
)
|
||||
|
||||
:: -- 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. Is the board plugged in?
|
||||
echo Specify manually: upload.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^)
|
||||
)
|
||||
)
|
||||
|
||||
:: -- 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%"
|
||||
)
|
||||
226
templates/basic/upload.sh
Normal file
226
templates/basic/upload.sh
Normal file
@@ -0,0 +1,226 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# upload.sh -- Compile and upload the sketch to the board
|
||||
#
|
||||
# Reads all settings from .anvil.toml. No Anvil binary required.
|
||||
#
|
||||
# Usage:
|
||||
# ./upload.sh Auto-detect port, compile + upload
|
||||
# ./upload.sh -p /dev/ttyUSB0 Specify port
|
||||
# ./upload.sh --monitor Open serial monitor after upload
|
||||
# ./upload.sh --clean Clean build cache first
|
||||
# ./upload.sh --verbose Full compiler + avrdude output
|
||||
#
|
||||
# Prerequisites: arduino-cli in PATH, arduino:avr core installed
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
CONFIG="$SCRIPT_DIR/.anvil.toml"
|
||||
|
||||
# -- Colors ----------------------------------------------------------------
|
||||
if [[ -t 1 ]]; then
|
||||
RED=$'\033[0;31m'; GRN=$'\033[0;32m'; YLW=$'\033[0;33m'
|
||||
CYN=$'\033[0;36m'; BLD=$'\033[1m'; RST=$'\033[0m'
|
||||
else
|
||||
RED=''; GRN=''; YLW=''; CYN=''; BLD=''; RST=''
|
||||
fi
|
||||
|
||||
ok() { echo "${GRN}ok${RST} $*"; }
|
||||
warn() { echo "${YLW}warn${RST} $*"; }
|
||||
die() { echo "${RED}FAIL${RST} $*" >&2; exit 1; }
|
||||
|
||||
# -- Parse .anvil.toml -----------------------------------------------------
|
||||
[[ -f "$CONFIG" ]] || die "No .anvil.toml found in $SCRIPT_DIR"
|
||||
|
||||
toml_get() {
|
||||
(grep "^$1 " "$CONFIG" 2>/dev/null || true) | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' '
|
||||
}
|
||||
|
||||
toml_array() {
|
||||
(grep "^$1 " "$CONFIG" 2>/dev/null || true) | head -1 \
|
||||
| sed 's/.*\[//; s/\].*//; s/"//g; s/,/ /g' | tr -s ' '
|
||||
}
|
||||
|
||||
SKETCH_NAME="$(toml_get 'name')"
|
||||
FQBN="$(toml_get 'fqbn')"
|
||||
WARNINGS="$(toml_get 'warnings')"
|
||||
INCLUDE_DIRS="$(toml_array 'include_dirs')"
|
||||
EXTRA_FLAGS="$(toml_array 'extra_flags')"
|
||||
BAUD="$(toml_get 'baud')"
|
||||
|
||||
[[ -n "$SKETCH_NAME" ]] || die "Could not read project name from .anvil.toml"
|
||||
[[ -n "$FQBN" ]] || die "Could not read fqbn from .anvil.toml"
|
||||
|
||||
BAUD="${BAUD:-115200}"
|
||||
LOCAL_CONFIG="$SCRIPT_DIR/.anvil.local"
|
||||
LOCAL_PORT=""
|
||||
LOCAL_VID_PID=""
|
||||
if [[ -f "$LOCAL_CONFIG" ]]; then
|
||||
LOCAL_PORT="$(grep '^port ' "$LOCAL_CONFIG" 2>/dev/null | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' ')"
|
||||
LOCAL_VID_PID="$(grep '^vid_pid ' "$LOCAL_CONFIG" 2>/dev/null | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' ')"
|
||||
fi
|
||||
SKETCH_DIR="$SCRIPT_DIR/$SKETCH_NAME"
|
||||
BUILD_DIR="$SCRIPT_DIR/.build"
|
||||
|
||||
# -- Parse arguments -------------------------------------------------------
|
||||
PORT=""
|
||||
DO_MONITOR=0
|
||||
DO_CLEAN=0
|
||||
VERBOSE=""
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
-p|--port) PORT="$2"; shift 2 ;;
|
||||
--monitor) DO_MONITOR=1; shift ;;
|
||||
--clean) DO_CLEAN=1; shift ;;
|
||||
--verbose) VERBOSE="--verbose"; shift ;;
|
||||
-h|--help)
|
||||
echo "Usage: ./upload.sh [-p PORT] [--monitor] [--clean] [--verbose]"
|
||||
echo " Compiles and uploads the sketch. Settings from .anvil.toml."
|
||||
exit 0
|
||||
;;
|
||||
*) die "Unknown option: $1" ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# -- Preflight -------------------------------------------------------------
|
||||
command -v arduino-cli &>/dev/null \
|
||||
|| die "arduino-cli not found in PATH."
|
||||
|
||||
[[ -d "$SKETCH_DIR" ]] \
|
||||
|| die "Sketch directory not found: $SKETCH_DIR"
|
||||
|
||||
# -- Resolve port ----------------------------------------------------------
|
||||
# Priority: -p flag > VID:PID resolve > saved port > auto-detect
|
||||
|
||||
# resolve_vid_pid VID:PID -- search arduino-cli JSON for matching device
|
||||
resolve_vid_pid() {
|
||||
local target_vid target_pid json
|
||||
target_vid="$(echo "$1" | cut -d: -f1 | tr '[:upper:]' '[:lower:]')"
|
||||
target_pid="$(echo "$1" | cut -d: -f2 | tr '[:upper:]' '[:lower:]')"
|
||||
json="$(arduino-cli board list --format json 2>/dev/null)" || return
|
||||
# Walk through JSON looking for matching vid/pid on serial ports
|
||||
echo "$json" | python3 -c "
|
||||
import sys, json
|
||||
try:
|
||||
data = json.load(sys.stdin)
|
||||
for dp in data.get('detected_ports', []):
|
||||
port = dp.get('port', {})
|
||||
if port.get('protocol') != 'serial':
|
||||
continue
|
||||
props = port.get('properties', {})
|
||||
vid = props.get('vid', '').lower().replace('0x', '')
|
||||
pid = props.get('pid', '').lower().replace('0x', '')
|
||||
if vid == '$target_vid' and pid == '$target_pid':
|
||||
print(port.get('address', ''))
|
||||
break
|
||||
except: pass
|
||||
" 2>/dev/null
|
||||
}
|
||||
|
||||
if [[ -z "$PORT" ]]; then
|
||||
# Try VID:PID resolution first
|
||||
if [[ -n "$LOCAL_VID_PID" ]]; then
|
||||
PORT="$(resolve_vid_pid "$LOCAL_VID_PID")"
|
||||
if [[ -n "$PORT" ]]; then
|
||||
if [[ "$PORT" != "$LOCAL_PORT" ]] && [[ -n "$LOCAL_PORT" ]]; then
|
||||
warn "Device $LOCAL_VID_PID found on $PORT (moved from $LOCAL_PORT)"
|
||||
else
|
||||
warn "Using port $PORT (from .anvil.local)"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Fall back to saved port
|
||||
if [[ -z "$PORT" ]] && [[ -n "$LOCAL_PORT" ]]; then
|
||||
PORT="$LOCAL_PORT"
|
||||
warn "Using port $PORT (from .anvil.local)"
|
||||
fi
|
||||
|
||||
# Fall back to auto-detect
|
||||
if [[ -z "$PORT" ]]; then
|
||||
PORT=$(arduino-cli board list 2>/dev/null \
|
||||
| grep -i "serial" \
|
||||
| awk '{print $1}' \
|
||||
| grep -E 'ttyUSB|ttyACM|COM' \
|
||||
| head -1)
|
||||
|
||||
if [[ -z "$PORT" ]]; then
|
||||
PORT=$(arduino-cli board list 2>/dev/null \
|
||||
| grep -i "serial" \
|
||||
| head -1 \
|
||||
| awk '{print $1}')
|
||||
fi
|
||||
|
||||
if [[ -z "$PORT" ]]; then
|
||||
die "No serial port detected. Is the board plugged in?\n Specify manually: ./upload.sh -p /dev/ttyUSB0\n Or save a default: anvil devices --set"
|
||||
fi
|
||||
|
||||
warn "Auto-detected port: $PORT (use -p to override, or: anvil devices --set)"
|
||||
fi
|
||||
fi
|
||||
|
||||
# -- Clean -----------------------------------------------------------------
|
||||
if [[ $DO_CLEAN -eq 1 ]] && [[ -d "$BUILD_DIR" ]]; then
|
||||
echo "${YLW}Cleaning build cache...${RST}"
|
||||
rm -rf "$BUILD_DIR"
|
||||
fi
|
||||
|
||||
# -- Build include flags ---------------------------------------------------
|
||||
BUILD_FLAGS=""
|
||||
for dir in $INCLUDE_DIRS; do
|
||||
abs="$SCRIPT_DIR/$dir"
|
||||
if [[ -d "$abs" ]]; then
|
||||
BUILD_FLAGS="$BUILD_FLAGS -I$abs"
|
||||
fi
|
||||
done
|
||||
for flag in $EXTRA_FLAGS; do
|
||||
BUILD_FLAGS="$BUILD_FLAGS $flag"
|
||||
done
|
||||
|
||||
# -- Compile ---------------------------------------------------------------
|
||||
echo "${CYN}${BLD}Compiling ${SKETCH_NAME}...${RST}"
|
||||
mkdir -p "$BUILD_DIR"
|
||||
|
||||
COMPILE_ARGS=(
|
||||
compile
|
||||
--fqbn "$FQBN"
|
||||
--build-path "$BUILD_DIR"
|
||||
--warnings "$WARNINGS"
|
||||
)
|
||||
|
||||
if [[ -n "$BUILD_FLAGS" ]]; then
|
||||
COMPILE_ARGS+=(--build-property "build.extra_flags=$BUILD_FLAGS")
|
||||
fi
|
||||
|
||||
[[ -n "$VERBOSE" ]] && COMPILE_ARGS+=("$VERBOSE")
|
||||
COMPILE_ARGS+=("$SKETCH_DIR")
|
||||
|
||||
arduino-cli "${COMPILE_ARGS[@]}" || die "Compilation failed."
|
||||
ok "Compile succeeded."
|
||||
|
||||
# -- Upload ----------------------------------------------------------------
|
||||
echo ""
|
||||
echo "${CYN}${BLD}Uploading to ${PORT}...${RST}"
|
||||
|
||||
UPLOAD_ARGS=(
|
||||
upload
|
||||
--fqbn "$FQBN"
|
||||
--port "$PORT"
|
||||
--input-dir "$BUILD_DIR"
|
||||
)
|
||||
|
||||
[[ -n "$VERBOSE" ]] && UPLOAD_ARGS+=("$VERBOSE")
|
||||
|
||||
arduino-cli "${UPLOAD_ARGS[@]}" || die "Upload failed."
|
||||
ok "Upload complete!"
|
||||
|
||||
# -- Monitor ---------------------------------------------------------------
|
||||
if [[ $DO_MONITOR -eq 1 ]]; then
|
||||
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"
|
||||
fi
|
||||
Reference in New Issue
Block a user