Supporting multiple boards

This commit is contained in:
Eric Ratliff
2026-02-19 10:12:33 -06:00
parent 2739d83b99
commit b909da298e
16 changed files with 1554 additions and 94 deletions

View File

@@ -8,6 +8,7 @@
# ./monitor.sh Auto-detect port
# ./monitor.sh -p /dev/ttyUSB0 Specify port
# ./monitor.sh -b 9600 Override baud rate
# ./monitor.sh --board mega Use baud from a named board
# ./monitor.sh --watch Reconnect after reset/replug
#
# Prerequisites: arduino-cli in PATH
@@ -25,6 +26,7 @@ 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; }
@@ -35,6 +37,18 @@ toml_get() {
(grep "^$1 " "$CONFIG" 2>/dev/null || true) | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' '
}
toml_section_get() {
local section="$1" key="$2"
awk -v section="[$section]" -v key="$key" '
$0 == section { found=1; next }
/^\[/ { found=0 }
found && $1 == key && /=/ {
sub(/^[^=]*= *"?/, ""); sub(/"? *$/, ""); print; exit
}
' "$CONFIG"
}
DEFAULT_BOARD="$(toml_get 'default')"
BAUD="$(toml_get 'baud')"
BAUD="${BAUD:-115200}"
LOCAL_CONFIG="$SCRIPT_DIR/.anvil.local"
@@ -48,28 +62,41 @@ fi
# -- Parse arguments -------------------------------------------------------
PORT=""
DO_WATCH=0
BOARD_NAME=""
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--port) PORT="$2"; shift 2 ;;
-b|--baud) BAUD="$2"; shift 2 ;;
--board) BOARD_NAME="$2"; shift 2 ;;
--watch) DO_WATCH=1; shift ;;
-h|--help)
echo "Usage: ./monitor.sh [-p PORT] [-b BAUD] [--watch]"
echo "Usage: ./monitor.sh [-p PORT] [-b BAUD] [--board NAME] [--watch]"
echo " Opens serial monitor. Baud rate from .anvil.toml."
echo " --board NAME selects a board from [boards.NAME]."
exit 0
;;
*) die "Unknown option: $1" ;;
esac
done
# -- Resolve board ---------------------------------------------------------
ACTIVE_BOARD="${BOARD_NAME:-$DEFAULT_BOARD}"
BOARD_BAUD="$(toml_section_get "boards.$ACTIVE_BOARD" "baud")"
if [[ -n "$BOARD_BAUD" ]]; then
BAUD="$BOARD_BAUD"
fi
if [[ -n "$BOARD_NAME" ]]; then
ok "Using board: $BOARD_NAME (baud: $BAUD)"
fi
# -- 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" \
@@ -77,7 +104,6 @@ auto_detect() {
| 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" \
@@ -88,7 +114,6 @@ auto_detect() {
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:]')"
@@ -113,7 +138,6 @@ except: pass
}
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
@@ -125,13 +149,11 @@ if [[ -z "$PORT" ]]; then
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)"