Placing scripts in the generated project
This commit is contained in:
107
templates/basic/monitor.sh
Normal file
107
templates/basic/monitor.sh
Normal file
@@ -0,0 +1,107 @@
|
||||
#!/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" | head -1 | sed 's/.*= *"\{0,1\}\([^"]*\)"\{0,1\}/\1/' | tr -d ' '
|
||||
}
|
||||
|
||||
BAUD="$(toml_get 'baud')"
|
||||
BAUD="${BAUD:-115200}"
|
||||
|
||||
# -- 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() {
|
||||
arduino-cli board list 2>/dev/null \
|
||||
| grep -i "serial" \
|
||||
| head -1 \
|
||||
| awk '{print $1}'
|
||||
}
|
||||
|
||||
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"
|
||||
fi
|
||||
warn "Auto-detected port: $PORT (use -p to override)"
|
||||
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
|
||||
Reference in New Issue
Block a user