92 lines
3.0 KiB
Python
Executable File
92 lines
3.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
import serial
|
|
|
|
|
|
def usage() -> None:
|
|
print("Usage:")
|
|
print(" dev.py <port> <device> [src] [lib] [reset] [follow]")
|
|
print(" e.g. dev.py /dev/ttyUSB0 pico src lib")
|
|
print(" e.g. dev.py /dev/ttyUSB0 esp32 src reset follow")
|
|
print(" device: pico | esp32. If no src/lib given, deploys both.")
|
|
|
|
|
|
def main() -> None:
|
|
if len(sys.argv) < 3:
|
|
usage()
|
|
return
|
|
|
|
port = sys.argv[1]
|
|
device = sys.argv[2].lower()
|
|
actions = [a.lower() for a in sys.argv[3:]]
|
|
|
|
if port.startswith("/") or (len(port) >= 3 and port.upper().startswith("COM")):
|
|
pass
|
|
else:
|
|
print("First argument must be serial port (e.g. /dev/ttyUSB0 or COM3).")
|
|
usage()
|
|
return
|
|
|
|
if device not in ("pico", "esp32"):
|
|
print("Device must be pico or esp32.")
|
|
usage()
|
|
return
|
|
|
|
if not actions:
|
|
actions = ["src", "lib"]
|
|
|
|
src_dir = f"{device}/src"
|
|
lib_dir = f"{device}/lib"
|
|
|
|
for a in actions:
|
|
print(a)
|
|
match a:
|
|
case "src":
|
|
if os.path.isdir(src_dir):
|
|
# Ensure remote directories exist before copying files
|
|
created_dirs: set[str] = set()
|
|
for dirpath, _, filenames in os.walk(src_dir):
|
|
for name in filenames:
|
|
path = os.path.join(dirpath, name)
|
|
rel = os.path.relpath(path, src_dir).replace(os.sep, "/")
|
|
remote_dir = ""
|
|
if "/" in rel:
|
|
remote_dir = rel.rsplit("/", 1)[0]
|
|
if remote_dir and remote_dir not in created_dirs:
|
|
subprocess.call(
|
|
["mpremote", "connect", port, "fs", "mkdir", ":" + remote_dir],
|
|
)
|
|
created_dirs.add(remote_dir)
|
|
subprocess.call(
|
|
["mpremote", "connect", port, "fs", "cp", path, ":" + rel],
|
|
)
|
|
else:
|
|
print(" (no src dir)")
|
|
case "lib":
|
|
if os.path.isdir(lib_dir):
|
|
subprocess.call(
|
|
["mpremote", "connect", port, "fs", "cp", "-r", lib_dir, ":"],
|
|
)
|
|
else:
|
|
print(" (no lib dir)")
|
|
case "reset":
|
|
with serial.Serial(port, baudrate=115200) as ser:
|
|
ser.write(b"\x03\x03\x04")
|
|
case "follow":
|
|
with serial.Serial(port, baudrate=115200) as ser:
|
|
while True:
|
|
if ser.in_waiting > 0:
|
|
data = ser.readline().decode("utf-8").strip()
|
|
print(data)
|
|
case _:
|
|
print("Unknown action:", a)
|
|
usage()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|