#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" BOARD_DIR="$(cd "${ROOT_DIR}/.." && pwd)" DEMO_BASE="${DEMO_BASE:-${BOARD_DIR}/../../../../SKETCH/ESP32-S3-Touch-AMOLED-2.16/official-demo/examples/Arduino-v3.3.5}" MODE="${1:-widgets}" PORT="${PORT:-}" FQBN="${FQBN:-esp32:esp32:esp32s3:USBMode=hwcdc,CDCOnBoot=cdc,UploadSpeed=921600,CPUFreq=240,FlashMode=dio,FlashSize=16M,PartitionScheme=app3M_fat9M_16MB,PSRAM=opi}" BUILD_DIR="${BUILD_DIR:-${ROOT_DIR}/.arduino-build/build-${MODE}}" OUT_DIR="${OUT_DIR:-${ROOT_DIR}/.arduino-build/out-${MODE}}" detect_port() { local detected detected="$(arduino-cli board list 2>/dev/null | awk '/\/dev\/tty(ACM|USB)/ {print $1; exit}')" if [[ -n "${detected}" ]]; then echo "${detected}" return 0 fi for candidate in /dev/ttyACM* /dev/ttyUSB*; do if [[ -e "${candidate}" ]]; then echo "${candidate}" return 0 fi done return 1 } case "${MODE}" in hello) SKETCH_DIR="${DEMO_BASE}/examples/01_HelloWorld" ;; widgets) SKETCH_DIR="${DEMO_BASE}/examples/05_LVGL_Widgets" ;; audio) SKETCH_DIR="${DEMO_BASE}/examples/07_ES8311" ;; registration-signer) SKETCH_DIR="${ROOT_DIR}/shine_registration_signer" ;; *) echo "Unknown mode: ${MODE}" >&2 echo "Use one of: registration-signer, hello, widgets, audio" >&2 exit 2 ;; esac echo "== Mode: ${MODE}" echo "== Sketch: ${SKETCH_DIR}" echo "== FQBN: ${FQBN}" mkdir -p "${BUILD_DIR}" "${OUT_DIR}" compile_args=( --fqbn "${FQBN}" --build-path "${BUILD_DIR}" --output-dir "${OUT_DIR}" --library "${DEMO_BASE}/libraries/GFX_Library_for_Arduino" --library "${DEMO_BASE}/libraries/SensorLib" --library "${DEMO_BASE}/libraries/XPowersLib" --library "${DEMO_BASE}/libraries/lvgl" --library "${DEMO_BASE}/libraries/Mylibrary" "${SKETCH_DIR}" ) if [[ "${MODE}" == "registration-signer" && "${REGISTRAR_DEV_UNENCRYPTED:-0}" == "1" ]]; then echo "WARNING: test-only registrar build; plaintext NVS, never provision a production key" >&2 compile_args=(--build-property "compiler.cpp.extra_flags=-DSHINE_REGISTRAR_INSECURE_DEV" "${compile_args[@]}") fi echo "== Compile: fast incremental build" if ! arduino-cli compile "${compile_args[@]}"; then echo "== Compile: fast build failed, retrying clean build" arduino-cli compile --clean "${compile_args[@]}" fi if [[ "${NO_UPLOAD:-0}" == "1" ]]; then echo "== Done: compiled without upload (NO_UPLOAD=1)." exit 0 fi if [[ -z "${PORT}" ]]; then if ! PORT="$(detect_port)"; then echo "Failed to auto-detect ESP32 port. Set PORT=/dev/ttyACM0 ./burn.sh ${MODE}" >&2 exit 3 fi fi echo "== Port: ${PORT}" arduino-cli upload \ -p "${PORT}" \ --fqbn "${FQBN}" \ --input-dir "${OUT_DIR}" \ "${SKETCH_DIR}" echo echo "== Done."