#!/usr/bin/env python3 """Copy each PlatformIO build and write the ESP Web Tools manifests. Usage: package_firmware.py The image is flashed as 4 separate parts (bootloader, partition table, otadata, app) instead of one merged file: a merged file fills the gaps with 0xFF and would wipe the NVS partition (0x9000: PIN, WiFi, energy, berries). The LittleFS area (the Pokemon) is not touched either, so an update keeps the save unless the user ticks "erase". """ import json, re, shutil, subprocess, sys from pathlib import Path BOARDS = { "pockettrainer": "PocketTrainer - round 1.28\" (GC9A01 + touch)", "lcd147": "PocketTrainer - Waveshare ESP32-C3-LCD-1.47", } proj, out = Path(sys.argv[1]), Path(sys.argv[2]) out.mkdir(parents=True, exist_ok=True) pio = Path.home() / ".platformio" / "packages" esptool = pio / "tool-esptoolpy" / "esptool.py" boot_app0 = pio / "framework-arduinoespressif32" / "tools" / "partitions" / "boot_app0.bin" version = re.search(r'#define POCKET_FIRMWARE "([^"]+)"', (proj / "src" / "net.h").read_text()).group(1) for env, name in BOARDS.items(): b = proj / ".pio" / "build" / env d = out / env d.mkdir(exist_ok=True) parts = [] for offset, src in [(0x0, b / "bootloader.bin"), (0x8000, b / "partitions.bin"), (0xE000, boot_app0), (0x10000, b / "firmware.bin")]: shutil.copy(src, d / src.name) parts.append({"path": f"{env}/{src.name}", "offset": offset}) # the bootloader header must carry the flash settings PlatformIO uses for upload (qio -> dio, 80m, 4MB) subprocess.run([sys.executable, str(esptool), "--chip", "esp32c3", "merge_bin", "-o", str(d / "bootloader.bin"), "--flash_mode", "dio", "--flash_freq", "80m", "--flash_size", "4MB", "0x0", str(b / "bootloader.bin")], check=True) manifest = { "name": name, "version": version, "new_install_prompt_erase": True, "builds": [{"chipFamily": "ESP32-C3", "parts": parts}], } (out / f"{env}.json").write_text(json.dumps(manifest, indent=2)) size = sum((d / Path(p["path"]).name).stat().st_size for p in parts) print(f"{env}: {size} bytes in {len(parts)} parts, v{version}") (out / "version.json").write_text(json.dumps({"version": version}))