Import led-driver app: pico/ and esp32/ layout
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
108
dev.py
108
dev.py
@@ -1,33 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import serial
|
||||
import sys
|
||||
|
||||
print(sys.argv)
|
||||
|
||||
port = sys.argv[1]
|
||||
|
||||
cmd = sys.argv[1]
|
||||
|
||||
for cmd in sys.argv[1:]:
|
||||
print(cmd)
|
||||
match cmd:
|
||||
case "src":
|
||||
subprocess.call(["mpremote", "connect", port, "fs", "cp", "-r", ".", ":" ], cwd="src")
|
||||
case "lib":
|
||||
subprocess.call(["mpremote", "connect", port, "fs", "cp", "-r", "lib", ":" ])
|
||||
case "ls":
|
||||
subprocess.call(["mpremote", "connect", port, "fs", "ls", ":" ])
|
||||
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: # Check if there is data in the buffer
|
||||
data = ser.readline().decode('utf-8').strip() # Read and decode the data
|
||||
print(data)
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user