#!/bin/bash # ============================================================================ # flash.sh — Flash Pico firmware for a given project # ============================================================================ # Runs on the HOST macOS (not inside Docker) because it needs access to # /Volumes/RPI-RP2, the USB mass storage mount that appears when the Pico # is held in BOOTSEL mode. # # Usage: # ./flash.sh # # Example: # ./flash.sh color_switcher # # The script finds the .uf2 file in the project's build/ directory # automatically — no hardcoded firmware name needed. # ============================================================================ set -e # --- Validate arguments --- PROJECT="${1:?Usage: ./flash.sh (e.g., ./flash.sh color_switcher)}" # Resolve paths relative to this script's location (the repo root) SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" BUILD_DIR="$SCRIPT_DIR/$PROJECT/build" PICO_MOUNT="/Volumes/RPI-RP2" # --- Find the .uf2 file --- UF2_FILE=$(find "$BUILD_DIR" -maxdepth 1 -name "*.uf2" 2>/dev/null | head -1) if [ -z "$UF2_FILE" ]; then echo "Error: No .uf2 file found in $BUILD_DIR" echo " Run the build first:" echo " cd $PROJECT && docker compose run --rm pico-build bash /scripts/build.sh" exit 1 fi echo "Firmware: $UF2_FILE" # --- Wait for the Pico in BOOTSEL mode --- echo "Waiting for Pico in BOOTSEL mode ($PICO_MOUNT)..." echo " -> Hold BOOTSEL and plug in the Pico via USB" while [ ! -d "$PICO_MOUNT" ]; do sleep 0.5 done # --- Copy the firmware --- echo "Pico detected. Copying $(basename "$UF2_FILE")..." cp "$UF2_FILE" "$PICO_MOUNT/" # --- Wait for reboot --- echo "Waiting for Pico to reboot..." while [ -d "$PICO_MOUNT" ]; do sleep 0.5 done echo "Done! Pico rebooted with new firmware." echo " -> Open a serial monitor: screen /dev/tty.usbmodem* 115200"