Files
anvil/templates/basic/upload.sh
2026-02-16 08:53:27 -06:00

180 lines
5.4 KiB
Bash

#!/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}"
DEFAULT_PORT="$(toml_get 'port')"
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"
# -- Auto-detect port ------------------------------------------------------
if [[ -z "$PORT" ]]; then
# Check .anvil.toml for configured port
if [[ -n "$DEFAULT_PORT" ]]; then
PORT="$DEFAULT_PORT"
else
# Prefer ttyUSB/ttyACM (real USB devices) over ttyS (hardware UART)
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 no USB ports found
if [[ -z "$PORT" ]]; then
PORT=$(arduino-cli board list 2>/dev/null \
| grep -i "serial" \
| head -1 \
| awk '{print $1}')
fi
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 set port in .anvil.toml"
fi
warn "Auto-detected port: $PORT (use -p to override)"
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