#!/usr/bin/env bash set -euo pipefail PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" BUILD_DIR="${BUILD_DIR:-build}" SERIAL_DEV="${1:-${PICO_SERIAL:-/dev/ttyACM0}}" BAUD_RATE="${PICO_BAUD:-115200}" cmake -S "$PROJECT_ROOT" -B "$PROJECT_ROOT/$BUILD_DIR" cmake --build "$PROJECT_ROOT/$BUILD_DIR" UF2_PATH="$PROJECT_ROOT/$BUILD_DIR/eink_api.uf2" if [ ! -f "$UF2_PATH" ]; then echo "UF2 image not found at $UF2_PATH" >&2 exit 1 fi if command -v picotool >/dev/null 2>&1; then echo "Put the Pico into BOOTSEL mode (hold BOOTSEL and plug in USB), then press Enter to flash..." read -r if ! picotool load "$UF2_PATH" --verify --reset; then echo "picotool flashing failed." >&2 exit 1 fi else echo "picotool not found; copy $UF2_PATH to the Pico's mass-storage drive manually to flash." >&2 fi SERIAL_CLIENT="${SERIAL_CLIENT:-screen}" case "$SERIAL_CLIENT" in screen) if ! command -v screen >/dev/null 2>&1; then echo "screen not installed; set SERIAL_CLIENT to a different terminal (picocom, minicom)." >&2 exit 1 fi echo "Connecting to $SERIAL_DEV at $BAUD_RATE with screen (Ctrl-A, then k to exit)." exec screen "$SERIAL_DEV" "$BAUD_RATE" ;; picocom) if ! command -v picocom >/dev/null 2>&1; then echo "picocom not installed; install it or set SERIAL_CLIENT=screen." >&2 exit 1 fi echo "Connecting to $SERIAL_DEV at $BAUD_RATE with picocom (Ctrl-A, Ctrl-X to exit)." exec picocom -b "$BAUD_RATE" "$SERIAL_DEV" ;; minicom) if ! command -v minicom >/dev/null 2>&1; then echo "minicom not installed; install it or set SERIAL_CLIENT=screen." >&2 exit 1 fi echo "Connecting to $SERIAL_DEV at $BAUD_RATE with minicom (Ctrl-A, Z then X to exit)." exec minicom -D "$SERIAL_DEV" -b "$BAUD_RATE" ;; *) echo "Unsupported SERIAL_CLIENT '$SERIAL_CLIENT'." >&2 exit 1 ;; esac