Compare commits
5
Commits
eee9327e15
...
ota
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
892ccaec71 | ||
|
|
d6331a105c | ||
|
|
2f3db9272b | ||
|
|
713cd6e9a1 | ||
|
|
9e72c62481 |
No files matched your search
@@ -16,5 +16,5 @@ python_version = "3"
|
|||||||
[scripts]
|
[scripts]
|
||||||
web = "python web.py"
|
web = "python web.py"
|
||||||
cli = "python cli.py"
|
cli = "python cli.py"
|
||||||
build = "pyinstaller --clean led-cli.spec"
|
build = "pyinstaller --clean --onefile --name led-cli --paths lib cli.py"
|
||||||
install = "pipenv install"
|
install = "pipenv install"
|
||||||
@@ -11,6 +11,7 @@ Connection is always via **`-p` / `--port`** (default `/dev/ttyACM0`). There is
|
|||||||
| `-p`, `--port` | Serial device (default `/dev/ttyACM0`) |
|
| `-p`, `--port` | Serial device (default `/dev/ttyACM0`) |
|
||||||
| `-s`, `--show` | Print current settings from the device |
|
| `-s`, `--show` | Print current settings from the device |
|
||||||
| `-n`, `--name` | Device **name** |
|
| `-n`, `--name` | Device **name** |
|
||||||
|
| `--reset-device-name` | Set **name** to firmware default (`led-` + STA MAC hex, same as `Settings.set_defaults` on led-driver) |
|
||||||
| `--id` | Numeric device id (ESP-NOW, 0–255) |
|
| `--id` | Numeric device id (ESP-NOW, 0–255) |
|
||||||
| `--pin` | LED GPIO (`led_pin`) |
|
| `--pin` | LED GPIO (`led_pin`) |
|
||||||
| `-b`, `--brightness` | Brightness 0–255 |
|
| `-b`, `--brightness` | Brightness 0–255 |
|
||||||
@@ -28,6 +29,7 @@ Connection is always via **`-p` / `--port`** (default `/dev/ttyACM0`). There is
|
|||||||
| `--src`, `--lib` | Upload `src/` or a tree to `/lib` |
|
| `--src`, `--lib` | Upload `src/` or a tree to `/lib` |
|
||||||
| `-e` | Erase device code (keeps `settings.json`) |
|
| `-e` | Erase device code (keeps `settings.json`) |
|
||||||
| `--rm` | Remove a path on the device |
|
| `--rm` | Remove a path on the device |
|
||||||
|
| `--ota` | Update a v2 controller over Ethernet: `--ota HOST [IMAGE]` (no serial port; image defaults to `led_controller_v2.bin` in `./v2/build`, `./build` or `../v2/build`) |
|
||||||
| `--flash` | Flash a firmware binary (uses **esptool** on the host) |
|
| `--flash` | Flash a firmware binary (uses **esptool** on the host) |
|
||||||
|
|
||||||
**Default behaviour:** the tool always downloads `settings.json`, prints the merged view, and uploads again **only** when you pass setting edits (`--name`, `--leds`, …), **`--preset`**, or the relevant upload/flash/erase actions in order.
|
**Default behaviour:** the tool always downloads `settings.json`, prints the merged view, and uploads again **only** when you pass setting edits (`--name`, `--leds`, …), **`--preset`**, or the relevant upload/flash/erase actions in order.
|
||||||
|
|||||||
@@ -11,11 +11,13 @@ import argparse
|
|||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import shutil
|
||||||
from typing import Dict, Any, List, Optional
|
from typing import Dict, Any, List, Optional
|
||||||
|
|
||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
from device import copy_file, DeviceConnection
|
from ota import ota_update
|
||||||
|
from device import copy_file, DeviceConnection, firmware_default_device_name
|
||||||
|
|
||||||
|
|
||||||
def resolve_flash_binary(path: str) -> Optional[str]:
|
def resolve_flash_binary(path: str) -> Optional[str]:
|
||||||
@@ -147,14 +149,14 @@ _FLAGS_WITH_VALUE = frozenset({
|
|||||||
'-p', '--port', '-n', '--name', '--pin', '-b', '--brightness',
|
'-p', '--port', '-n', '--name', '--pin', '-b', '--brightness',
|
||||||
'-l', '--leds', '-d', '-debug', '--debug', '-o', '--order',
|
'-l', '--leds', '-d', '-debug', '--debug', '-o', '--order',
|
||||||
'--preset', '--pattern', '--default', '--transport', '--ssid',
|
'--preset', '--pattern', '--default', '--transport', '--ssid',
|
||||||
'--wifi-password', '--wifi-channel',
|
'--wifi-password', '--wifi-channel', '--ota', '--src', '--lib', '--patterns', '--paterns',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
def _get_ordered_actions(argv: List[str]) -> List[tuple]:
|
def _get_ordered_actions(argv: List[str]) -> List[tuple]:
|
||||||
"""
|
"""
|
||||||
Scan argv and return list of (action_name, value) in order of appearance.
|
Scan argv and return list of (action_name, value) in order of appearance.
|
||||||
Actions: flash, pause, reset, upload, erase_all, rm, follow.
|
Actions: flash, ota, pause, reset, upload, ls, erase_all, rm, follow.
|
||||||
"""
|
"""
|
||||||
actions = []
|
actions = []
|
||||||
i = 1
|
i = 1
|
||||||
@@ -178,6 +180,16 @@ def _get_ordered_actions(argv: List[str]) -> List[tuple]:
|
|||||||
else:
|
else:
|
||||||
raise ValueError("--flash requires an argument")
|
raise ValueError("--flash requires an argument")
|
||||||
continue
|
continue
|
||||||
|
if arg == '--ota':
|
||||||
|
vals = []
|
||||||
|
i += 1
|
||||||
|
while i < len(argv) and not argv[i].startswith('-') and len(vals) < 2:
|
||||||
|
vals.append(argv[i])
|
||||||
|
i += 1
|
||||||
|
if not vals:
|
||||||
|
raise ValueError("--ota requires a host (and optionally an image path)")
|
||||||
|
actions.append(('ota', vals))
|
||||||
|
continue
|
||||||
if arg in ('-r', '--reset'):
|
if arg in ('-r', '--reset'):
|
||||||
actions.append(('reset', None))
|
actions.append(('reset', None))
|
||||||
i += 1
|
i += 1
|
||||||
@@ -201,22 +213,47 @@ def _get_ordered_actions(argv: List[str]) -> List[tuple]:
|
|||||||
i += 2
|
i += 2
|
||||||
else:
|
else:
|
||||||
i += 1
|
i += 1
|
||||||
# Use empty string as remote_dir to map to root on device
|
# Upload source tree excluding patterns/ (handled by --patterns).
|
||||||
actions.append(('upload', [local_dir, ""]))
|
actions.append(('upload_src_no_patterns', local_dir))
|
||||||
continue
|
continue
|
||||||
if arg == '--lib':
|
if arg in ('--patterns', '--paterns'):
|
||||||
# Upload local DIR to /lib on device
|
# Upload local patterns DIR (default: ./src/patterns) to /patterns.
|
||||||
if i + 1 < len(argv):
|
local_dir = os.path.join("src", "patterns")
|
||||||
|
if i + 1 < len(argv) and not argv[i + 1].startswith('-'):
|
||||||
local_dir = argv[i + 1]
|
local_dir = argv[i + 1]
|
||||||
actions.append(('upload', [local_dir, "lib"]))
|
|
||||||
i += 2
|
i += 2
|
||||||
else:
|
else:
|
||||||
raise ValueError("--lib requires a directory argument")
|
i += 1
|
||||||
|
actions.append(('upload', [local_dir, "patterns"]))
|
||||||
|
continue
|
||||||
|
if arg == '--all':
|
||||||
|
actions.append(('upload_src_no_patterns', "src"))
|
||||||
|
actions.append(('upload', [os.path.join("src", "patterns"), "patterns"]))
|
||||||
|
actions.append(('upload', ["lib", "lib"]))
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
|
if arg == '--lib':
|
||||||
|
# Upload local DIR (default: ./lib) to /lib on device
|
||||||
|
local_dir = "lib"
|
||||||
|
if i + 1 < len(argv) and not argv[i + 1].startswith('-'):
|
||||||
|
local_dir = argv[i + 1]
|
||||||
|
i += 2
|
||||||
|
else:
|
||||||
|
i += 1
|
||||||
|
actions.append(('upload', [local_dir, "lib"]))
|
||||||
|
continue
|
||||||
|
if arg == '--ls':
|
||||||
|
actions.append(('ls', None))
|
||||||
|
i += 1
|
||||||
continue
|
continue
|
||||||
if arg == '-e':
|
if arg == '-e':
|
||||||
actions.append(('erase_all', None))
|
actions.append(('erase_all', None))
|
||||||
i += 1
|
i += 1
|
||||||
continue
|
continue
|
||||||
|
if arg == '--erase':
|
||||||
|
actions.append(('erase_all', None))
|
||||||
|
i += 1
|
||||||
|
continue
|
||||||
if arg == '--rm':
|
if arg == '--rm':
|
||||||
if i + 1 < len(argv):
|
if i + 1 < len(argv):
|
||||||
actions.append(('rm', argv[i + 1]))
|
actions.append(('rm', argv[i + 1]))
|
||||||
@@ -278,11 +315,18 @@ Examples:
|
|||||||
%(prog)s --flash firmware.bin
|
%(prog)s --flash firmware.bin
|
||||||
%(prog)s -p /dev/ttyUSB0 --flash /path/to/firmware.bin
|
%(prog)s -p /dev/ttyUSB0 --flash /path/to/firmware.bin
|
||||||
|
|
||||||
|
# Update a v2 controller over Ethernet (image defaults to led_controller_v2.bin under ./v2/build, ./build or ../v2/build)
|
||||||
|
%(prog)s --ota 10.1.1.180
|
||||||
|
%(prog)s --ota 10.1.1.180 /path/to/led_controller_v2.bin
|
||||||
|
|
||||||
# Reset, pause 2 seconds, then follow output
|
# Reset, pause 2 seconds, then follow output
|
||||||
%(prog)s --reset --pause 2 --follow
|
%(prog)s --reset --pause 2 --follow
|
||||||
|
|
||||||
# Set name, num_leds, default pattern, and upload
|
# Set name, num_leds, default pattern, and upload
|
||||||
%(prog)s --name "MyStrip" -l 60 --default rainbow
|
%(prog)s --name "MyStrip" -l 60 --default rainbow
|
||||||
|
|
||||||
|
# Reset logical device name to firmware default (STA MAC based)
|
||||||
|
%(prog)s --reset-device-name
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -297,6 +341,12 @@ Examples:
|
|||||||
help="Device name"
|
help="Device name"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--reset-device-name",
|
||||||
|
action="store_true",
|
||||||
|
help="Set name to firmware default (led-<STA MAC hex>, same as fresh settings.json on led-driver)",
|
||||||
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--id",
|
"--id",
|
||||||
dest="device_id",
|
dest="device_id",
|
||||||
@@ -429,17 +479,40 @@ Examples:
|
|||||||
nargs="?",
|
nargs="?",
|
||||||
const="src",
|
const="src",
|
||||||
metavar="DIR",
|
metavar="DIR",
|
||||||
help="Upload DIR recursively to device root (:/, no leading directory). If DIR is omitted, uses local ./src."
|
help="Upload DIR recursively to device root (:/, no leading directory), excluding patterns/. If DIR is omitted, uses local ./src."
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--lib",
|
"--lib",
|
||||||
|
nargs="?",
|
||||||
|
const="lib",
|
||||||
metavar="DIR",
|
metavar="DIR",
|
||||||
help="Upload DIR recursively to /lib on device"
|
help="Upload DIR recursively to /lib on device. If DIR is omitted, uses local ./lib."
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-e",
|
"--all",
|
||||||
|
action="store_true",
|
||||||
|
help="Upload ./src (excluding patterns), ./src/patterns, and ./lib."
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--patterns", "--paterns",
|
||||||
|
dest="patterns_dir",
|
||||||
|
nargs="?",
|
||||||
|
const=os.path.join("src", "patterns"),
|
||||||
|
metavar="DIR",
|
||||||
|
help="Upload DIR recursively to /patterns on device. If DIR is omitted, uses local ./src/patterns."
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--ls",
|
||||||
|
action="store_true",
|
||||||
|
help="List files on the device root (:/)"
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"-e", "--erase",
|
||||||
dest="erase_all",
|
dest="erase_all",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Erase all code on the device (delete all files except settings.json)"
|
help="Erase all code on the device (delete all files except settings.json)"
|
||||||
@@ -451,6 +524,13 @@ Examples:
|
|||||||
help="Delete a file or directory on the device (recursive for directories)"
|
help="Delete a file or directory on the device (recursive for directories)"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"--ota",
|
||||||
|
nargs="+",
|
||||||
|
metavar=("HOST", "IMAGE"),
|
||||||
|
help="Update a v2 controller over Ethernet: HOST [IMAGE]. No serial port is used and the tool exits afterwards"
|
||||||
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--flash",
|
"--flash",
|
||||||
metavar="BINARY",
|
metavar="BINARY",
|
||||||
@@ -492,6 +572,15 @@ Examples:
|
|||||||
print("Error: esptool not found. Install it with: pip install esptool", file=sys.stderr)
|
print("Error: esptool not found. Install it with: pip install esptool", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
elif action_name == 'ota':
|
||||||
|
try:
|
||||||
|
ota_update(value[0], value[1] if len(value) > 1 else None)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: OTA failed: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
if all(a[0] in ('ota', 'pause') for a in ordered_actions):
|
||||||
|
return # network-only: nothing to download over serial
|
||||||
|
|
||||||
elif action_name == 'reset':
|
elif action_name == 'reset':
|
||||||
ran_reset = True
|
ran_reset = True
|
||||||
try:
|
try:
|
||||||
@@ -526,6 +615,50 @@ Examples:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error uploading directory: {e}", file=sys.stderr)
|
print(f"Error uploading directory: {e}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
elif action_name == 'upload_src_no_patterns':
|
||||||
|
src_dir = value
|
||||||
|
if not os.path.exists(src_dir):
|
||||||
|
print(f"Error: Directory does not exist: {src_dir}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
if not os.path.isdir(src_dir):
|
||||||
|
print(f"Error: Not a directory: {src_dir}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
try:
|
||||||
|
with tempfile.TemporaryDirectory() as temp_src:
|
||||||
|
for entry in sorted(os.listdir(src_dir)):
|
||||||
|
if entry == "patterns":
|
||||||
|
continue
|
||||||
|
src_entry = os.path.join(src_dir, entry)
|
||||||
|
dst_entry = os.path.join(temp_src, entry)
|
||||||
|
if os.path.isdir(src_entry):
|
||||||
|
shutil.copytree(src_entry, dst_entry)
|
||||||
|
else:
|
||||||
|
shutil.copy2(src_entry, dst_entry)
|
||||||
|
print(
|
||||||
|
f"Uploading {src_dir} (excluding patterns/) to device root on {port}...",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
conn = DeviceConnection(port)
|
||||||
|
files_copied, dirs_created = conn.upload_directory(temp_src, "")
|
||||||
|
print(
|
||||||
|
f"Upload complete: {files_copied} files, {dirs_created} directories created.",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error uploading src (excluding patterns): {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
elif action_name == 'ls':
|
||||||
|
try:
|
||||||
|
print(f"Listing files on device {port}...", file=sys.stderr)
|
||||||
|
conn = DeviceConnection(port)
|
||||||
|
items = conn.list_files('')
|
||||||
|
for name, is_dir, size in items:
|
||||||
|
marker = "d" if is_dir else "-"
|
||||||
|
print(f"{marker} {size:>8} {name}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error listing files: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
elif action_name == 'erase_all':
|
elif action_name == 'erase_all':
|
||||||
try:
|
try:
|
||||||
@@ -569,11 +702,35 @@ Examples:
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
return # follow blocks; when interrupted we're done
|
return # follow blocks; when interrupted we're done
|
||||||
|
|
||||||
|
default_name_from_device: Optional[str] = None
|
||||||
|
if args.reset_device_name:
|
||||||
|
if args.name is not None:
|
||||||
|
print(
|
||||||
|
"Error: use either --name or --reset-device-name, not both.",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
sys.exit(1)
|
||||||
|
try:
|
||||||
|
print(
|
||||||
|
f"Reading firmware default device name (STA MAC) on {port}...",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
default_name_from_device = firmware_default_device_name(port)
|
||||||
|
print(
|
||||||
|
f"Default name will be {default_name_from_device!r}.",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# Collect all edit parameters
|
# Collect all edit parameters
|
||||||
edits: Dict[str, Any] = {}
|
edits: Dict[str, Any] = {}
|
||||||
|
|
||||||
if args.name is not None:
|
if args.name is not None:
|
||||||
edits["name"] = args.name
|
edits["name"] = args.name
|
||||||
|
elif default_name_from_device is not None:
|
||||||
|
edits["name"] = default_name_from_device
|
||||||
|
|
||||||
if args.pin is not None:
|
if args.pin is not None:
|
||||||
edits["led_pin"] = args.pin
|
edits["led_pin"] = args.pin
|
||||||
|
|||||||
@@ -23,12 +23,15 @@ if lib_path not in sys.path and os.path.exists(lib_path):
|
|||||||
sys.path.insert(0, lib_path)
|
sys.path.insert(0, lib_path)
|
||||||
|
|
||||||
from mpremote.transport_serial import SerialTransport
|
from mpremote.transport_serial import SerialTransport
|
||||||
from mpremote.transport import TransportError
|
from mpremote.transport import TransportError, TransportExecError
|
||||||
|
|
||||||
|
|
||||||
class DeviceConnection:
|
class DeviceConnection:
|
||||||
"""Wrapper for device communication."""
|
"""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):
|
def __init__(self, device):
|
||||||
"""Connect to a device."""
|
"""Connect to a device."""
|
||||||
self.device = device
|
self.device = device
|
||||||
@@ -54,6 +57,38 @@ class DeviceConnection:
|
|||||||
pass
|
pass
|
||||||
self.transport = None
|
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):
|
def copy_from_device(self, remote_path, local_path):
|
||||||
"""Copy a file from device to local filesystem."""
|
"""Copy a file from device to local filesystem."""
|
||||||
self.connect()
|
self.connect()
|
||||||
@@ -70,7 +105,7 @@ class DeviceConnection:
|
|||||||
try:
|
try:
|
||||||
with open(local_path, 'rb') as f:
|
with open(local_path, 'rb') as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
self.transport.fs_writefile(remote_path, data)
|
self._fs_writefile_with_wdt(remote_path, data)
|
||||||
finally:
|
finally:
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
||||||
@@ -133,7 +168,7 @@ class DeviceConnection:
|
|||||||
print(f"Uploading: {remote_file}", file=sys.stderr)
|
print(f"Uploading: {remote_file}", file=sys.stderr)
|
||||||
with open(local_file, 'rb') as f:
|
with open(local_file, 'rb') as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
self.transport.fs_writefile(remote_file, data)
|
self._fs_writefile_with_wdt(remote_file, data)
|
||||||
files_copied += 1
|
files_copied += 1
|
||||||
|
|
||||||
return files_copied, dirs_created
|
return files_copied, dirs_created
|
||||||
@@ -235,6 +270,37 @@ class DeviceConnection:
|
|||||||
raise TransportError(f"Failed to follow output: {e}") from e
|
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):
|
def copy_file(from_device, device, remote_path, local_path):
|
||||||
"""
|
"""
|
||||||
Copy a file to/from device.
|
Copy a file to/from device.
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ pipenv install "$@"
|
|||||||
if [ ! -f "dist/led-cli" ] || [ "cli.py" -nt "dist/led-cli" ] || [ "device.py" -nt "dist/led-cli" ]; then
|
if [ ! -f "dist/led-cli" ] || [ "cli.py" -nt "dist/led-cli" ] || [ "device.py" -nt "dist/led-cli" ]; then
|
||||||
echo ""
|
echo ""
|
||||||
echo "Building binary..."
|
echo "Building binary..."
|
||||||
pipenv run pyinstaller --clean led-cli.spec
|
pipenv run pyinstaller --clean --onefile --name led-cli --paths lib cli.py
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Ensure ~/.local/bin exists
|
# Ensure ~/.local/bin exists
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
"""
|
||||||
|
Over-the-air firmware update for the v2 ESP32-S3 controller (Ethernet, plain HTTP).
|
||||||
|
|
||||||
|
The controller takes the raw app image on POST /ota, writes it to the idle slot and reboots.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import http.client
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
IMAGE_NAME = "led_controller_v2.bin"
|
||||||
|
# Searched in order when no image is given (relative to where the tool is run, since the
|
||||||
|
# PyInstaller binary has no useful location of its own)
|
||||||
|
DEFAULT_IMAGES = [
|
||||||
|
os.path.join("v2", "build", IMAGE_NAME),
|
||||||
|
os.path.join("build", IMAGE_NAME),
|
||||||
|
os.path.join("..", "v2", "build", IMAGE_NAME),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def _default_image() -> Optional[str]:
|
||||||
|
return next((p for p in DEFAULT_IMAGES if os.path.isfile(p)), None)
|
||||||
|
|
||||||
|
|
||||||
|
def _info(host: str, port: int) -> Optional[dict]:
|
||||||
|
try:
|
||||||
|
conn = http.client.HTTPConnection(host, port, timeout=5)
|
||||||
|
conn.request("GET", "/ota")
|
||||||
|
r = conn.getresponse()
|
||||||
|
return json.loads(r.read().decode()) if r.status == 200 else None
|
||||||
|
except (OSError, ValueError):
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def ota_update(host: str, image: Optional[str] = None, port: int = 80) -> None:
|
||||||
|
"""Upload image to host and wait for it to come back. Raises RuntimeError on failure."""
|
||||||
|
image = image or _default_image()
|
||||||
|
if not image:
|
||||||
|
raise RuntimeError(f"no image given and {IMAGE_NAME} not found under ./v2/build, ./build or ../v2/build")
|
||||||
|
if not os.path.isfile(image):
|
||||||
|
raise RuntimeError(f"image not found: {image}")
|
||||||
|
size = os.path.getsize(image)
|
||||||
|
|
||||||
|
before = _info(host, port)
|
||||||
|
if before is None:
|
||||||
|
raise RuntimeError(f"no OTA endpoint at http://{host}:{port}/ota (is this the v2 controller?)")
|
||||||
|
print(f"Running {before['partition']} (built {before['built']})")
|
||||||
|
|
||||||
|
print(f"Uploading {image} ({size} bytes)...")
|
||||||
|
conn = http.client.HTTPConnection(host, port, timeout=60)
|
||||||
|
with open(image, "rb") as f:
|
||||||
|
conn.request("POST", "/ota", body=f, headers={
|
||||||
|
"Content-Length": str(size),
|
||||||
|
"Content-Type": "application/octet-stream",
|
||||||
|
})
|
||||||
|
r = conn.getresponse()
|
||||||
|
body = r.read().decode()
|
||||||
|
if r.status != 200:
|
||||||
|
try:
|
||||||
|
body = json.loads(body).get("error", body)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
raise RuntimeError(f"update rejected ({r.status}): {body}")
|
||||||
|
|
||||||
|
print("Written, waiting for reboot...")
|
||||||
|
time.sleep(3)
|
||||||
|
for _ in range(30):
|
||||||
|
after = _info(host, port)
|
||||||
|
if after:
|
||||||
|
print(f"Back up on {after['partition']} (built {after['built']})")
|
||||||
|
if after["partition"] == before["partition"]:
|
||||||
|
raise RuntimeError("controller came back on the same slot: new image was rejected or rolled back")
|
||||||
|
return
|
||||||
|
time.sleep(1)
|
||||||
|
raise RuntimeError("controller did not come back within 30 s")
|
||||||
Reference in new issue
Block a user