feat(cli): add --reset-device-name and WDT feed during uploads
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
76
device.py
76
device.py
@@ -23,12 +23,15 @@ if lib_path not in sys.path and os.path.exists(lib_path):
|
||||
sys.path.insert(0, lib_path)
|
||||
|
||||
from mpremote.transport_serial import SerialTransport
|
||||
from mpremote.transport import TransportError
|
||||
from mpremote.transport import TransportError, TransportExecError
|
||||
|
||||
|
||||
class DeviceConnection:
|
||||
"""Wrapper for device communication."""
|
||||
|
||||
|
||||
#: Feed interval during uploads (led-driver uses WDT(timeout=10000) ms).
|
||||
WDT_FEED_INTERVAL_SEC = 5.0
|
||||
|
||||
def __init__(self, device):
|
||||
"""Connect to a device."""
|
||||
self.device = device
|
||||
@@ -53,7 +56,39 @@ class DeviceConnection:
|
||||
except Exception:
|
||||
pass
|
||||
self.transport = None
|
||||
|
||||
|
||||
def _feed_wdt(self) -> None:
|
||||
"""Best-effort feed of the ESP task WDT between chunked FS writes."""
|
||||
if self.transport is None:
|
||||
return
|
||||
try:
|
||||
self.transport.exec(
|
||||
"try:\n import machine\n machine.WDT(timeout=10000).feed()\nexcept Exception:\n pass\n"
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _make_wdt_upload_progress_callback(self):
|
||||
"""Progress hook for Transport.fs_writefile: feed WDT every WDT_FEED_INTERVAL_SEC."""
|
||||
last_feed = [time.monotonic()]
|
||||
|
||||
def progress(written: int, total: int) -> None:
|
||||
now = time.monotonic()
|
||||
if now - last_feed[0] >= self.WDT_FEED_INTERVAL_SEC:
|
||||
self._feed_wdt()
|
||||
last_feed[0] = now
|
||||
|
||||
return progress
|
||||
|
||||
def _fs_writefile_with_wdt(self, remote_path: str, data: bytes) -> None:
|
||||
"""Write file to device with periodic WDT feeds during long transfers."""
|
||||
self._feed_wdt()
|
||||
self.transport.fs_writefile(
|
||||
remote_path,
|
||||
data,
|
||||
progress_callback=self._make_wdt_upload_progress_callback(),
|
||||
)
|
||||
|
||||
def copy_from_device(self, remote_path, local_path):
|
||||
"""Copy a file from device to local filesystem."""
|
||||
self.connect()
|
||||
@@ -70,7 +105,7 @@ class DeviceConnection:
|
||||
try:
|
||||
with open(local_path, 'rb') as f:
|
||||
data = f.read()
|
||||
self.transport.fs_writefile(remote_path, data)
|
||||
self._fs_writefile_with_wdt(remote_path, data)
|
||||
finally:
|
||||
self.disconnect()
|
||||
|
||||
@@ -133,7 +168,7 @@ class DeviceConnection:
|
||||
print(f"Uploading: {remote_file}", file=sys.stderr)
|
||||
with open(local_file, 'rb') as f:
|
||||
data = f.read()
|
||||
self.transport.fs_writefile(remote_file, data)
|
||||
self._fs_writefile_with_wdt(remote_file, data)
|
||||
files_copied += 1
|
||||
|
||||
return files_copied, dirs_created
|
||||
@@ -235,6 +270,37 @@ class DeviceConnection:
|
||||
raise TransportError(f"Failed to follow output: {e}") from e
|
||||
|
||||
|
||||
def firmware_default_device_name(device: str) -> str:
|
||||
"""
|
||||
Default logical device name on led-driver firmware: led-<sta_mac_hex>,
|
||||
matching Settings.set_defaults() in led-driver/src/settings.py.
|
||||
"""
|
||||
conn = DeviceConnection(device)
|
||||
conn.connect()
|
||||
try:
|
||||
code = (
|
||||
"import network, ubinascii\n"
|
||||
"sta = network.WLAN(network.STA_IF)\n"
|
||||
"sta.active(True)\n"
|
||||
"mac = ubinascii.hexlify(sta.config('mac')).decode().lower()\n"
|
||||
"print('led-' + mac)\n"
|
||||
)
|
||||
out = conn.transport.exec(code)
|
||||
except TransportExecError as e:
|
||||
raise RuntimeError(
|
||||
"Could not read STA MAC from device (is this MicroPython with network?): "
|
||||
+ (e.error_output or str(e)).strip()
|
||||
) from e
|
||||
finally:
|
||||
conn.disconnect()
|
||||
text = out.decode("utf-8", errors="replace").strip()
|
||||
for line in reversed(text.splitlines()):
|
||||
line = line.strip()
|
||||
if line.startswith("led-"):
|
||||
return line
|
||||
raise RuntimeError("Device did not return a led-<mac> default name")
|
||||
|
||||
|
||||
def copy_file(from_device, device, remote_path, local_path):
|
||||
"""
|
||||
Copy a file to/from device.
|
||||
|
||||
Reference in New Issue
Block a user