-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflash.py
More file actions
92 lines (73 loc) · 2.87 KB
/
flash.py
File metadata and controls
92 lines (73 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
import sys
import subprocess
import shutil
import glob
# Configuration
PORT = "/dev/esp32"
BUILD_DIR = "build"
def prepare_build():
if os.path.exists(BUILD_DIR):
shutil.rmtree(BUILD_DIR)
os.makedirs(BUILD_DIR)
# 0. Generate Keys if needed
if not os.path.exists("secret.key"):
print("Generating Keys...")
subprocess.run([sys.executable, "tools/keygen.py"], check=True)
# 0b. Generate Serial if needed
if not os.path.exists("serial.txt"):
print("Generating Serial...")
subprocess.run([sys.executable, "tools/factory_setup.py"], check=True)
# 1. Build Frontend
print("Building Frontend...")
subprocess.run(["npm", "run", "build"], cwd="frontend", check=True)
# 2. Copy Source Files
sources = glob.glob("*.py") + glob.glob("*.json")
if os.path.exists("serial.txt"):
sources.append("serial.txt")
for src in sources:
if src == os.path.basename(__file__): continue
shutil.copy(src, os.path.join(BUILD_DIR, src))
# 2b. Setup Keys
if os.path.exists("secret.key"):
keys_dir = os.path.join(BUILD_DIR, "keys")
if not os.path.exists(keys_dir):
os.makedirs(keys_dir)
shutil.copy("secret.key", os.path.join(keys_dir, "factory.key"))
# 3. Copy Microdot Lib
microdot_path = ".venv/lib/python3.13/site-packages/microdot/microdot.py"
if os.path.exists(microdot_path):
shutil.copy(microdot_path, os.path.join(BUILD_DIR, "microdot.py"))
# 4. Copy WWW
if os.path.exists("www"):
shutil.copytree("www", os.path.join(BUILD_DIR, "www"))
def main():
if not os.path.exists(PORT):
print(f"Error: Device {PORT} not found.")
return
print("Preparing Build Directory...")
import glob # Lazy import
prepare_build()
print("Compiling Fonts...")
subprocess.run([sys.executable, "compile_font.py"], cwd=BUILD_DIR, check=True)
print("Uploading with mpremote (Fast)...")
# Clean up compile_font from build before upload
if os.path.exists(os.path.join(BUILD_DIR, "compile_font.py")):
os.remove(os.path.join(BUILD_DIR, "compile_font.py"))
try:
# mpremote cp -r build/* :
# Note: mpremote syntax for recursive copy from local to remote root
cmd = ["mpremote", "connect", PORT, "cp", "-r", f"{BUILD_DIR}/.", ":"]
subprocess.run(cmd, check=True)
print("Upload complete.")
print("Resetting board...")
subprocess.run(["mpremote", "connect", PORT, "reset"], check=True)
# Monitor
print("Monitoring...")
subprocess.run(["mpremote", "connect", PORT, "repl"], check=True)
except KeyboardInterrupt:
print("\nExiting...")
except subprocess.CalledProcessError as e:
print(f"\nmpremote failed: {e}")
if __name__ == "__main__":
main()