Files
PlatformIO_Esp32C3_BedCooler/scripts/build_upload_device.py
2026-06-28 13:15:53 -05:00

58 lines
1.9 KiB
Python

#!/usr/bin/env python3
import argparse
import shutil
import subprocess
import sys
from pathlib import Path
DEFAULT_ENV = "seeed_xiao_esp32c3"
def find_pio():
pio = shutil.which("pio")
if pio:
return pio
candidate = Path.home() / ".platformio" / "penv" / "Scripts" / "pio.exe"
if candidate.exists():
return str(candidate)
candidate = Path.home() / ".platformio" / "penv" / "bin" / "pio"
if candidate.exists():
return str(candidate)
return "pio"
def run(command, cwd):
print("+ " + " ".join(command), flush=True)
subprocess.run(command, check=True, cwd=cwd)
def pio_command(args, pio, environment, upload_port):
command = [pio, "run", "-e", environment] + args
if upload_port:
command += ["--upload-port", upload_port]
return command
def main():
parser = argparse.ArgumentParser(description="Build firmware and LittleFS, then upload both to a connected device.")
parser.add_argument("-e", "--environment", default=DEFAULT_ENV, help="PlatformIO environment to build and upload.")
parser.add_argument("-p", "--upload-port", help="Upload port, for example COM3.")
parser.add_argument("--pio", default=find_pio(), help="Path to the PlatformIO executable.")
args = parser.parse_args()
root = Path(__file__).resolve().parent.parent
try:
run(pio_command(["-t", "buildfs"], args.pio, args.environment, None), root)
run(pio_command([], args.pio, args.environment, None), root)
run(pio_command(["-t", "uploadfs"], args.pio, args.environment, args.upload_port), root)
run(pio_command(["-t", "upload"], args.pio, args.environment, args.upload_port), root)
except subprocess.CalledProcessError as error:
sys.exit(error.returncode)
print(f"Uploaded filesystem and firmware for {args.environment} from {root}")
if __name__ == "__main__":
main()