11 Commits
Author SHA1 Message Date
jimmyandClaude Haiku 5.5 20a9b60d5f feat: device configuration over HTTP and OTA updates for the CLI
The CLI reaches a device over HTTP as well as USB: http_device.py holds the
request helpers, driver_ota.py uploads driver firmware, and cli.py and
ota.py use them. The README documents the new options.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-17 00:34:22 +13:00
jimmyandClaude Opus 5.5 f5ab256b4a feat: configure the ESP-IDF led-driver over USB
idf_device.py talks to the driver's USB API (led-driver main/usb_cfg.cpp).
--driver auto (default) asks the port first and falls back to mpremote.
Existing flags map to the driver's API (-s, --pin, -l, -o, -b, --preset,
--default, --ssid/--wifi-password, -n, -r, -f); new ESP-IDF flags cover
the rest of the network settings (--ap, --ap-ssid, --ap-password, --ddp,
--dhcp, --static, --dns), --max-brightness and --scan. Opening the port
keeps DTR high so connecting doesn't reset the C3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 22:34:36 +13:00
jimmyandClaude Haiku 5.5 c9dea6e142 chore: in-progress CLI and device changes
Work in progress from the same tree as the controller's sequences-v2 changes.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-11 14:10:55 +13:00
jimmyandClaude Sonnet 5.5 892ccaec71 feat(cli): add --ota to update a v2 controller over Ethernet
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 00:22:55 +13:00
piandCursor d6331a105c feat(cli): add --reset-device-name and WDT feed during uploads
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-03 21:27:29 +12:00
jimmyandCursor 2f3db9272b feat(cli): extend upload flags for src, patterns, lib, and --all
Support --patterns/--paterns, --all, --erase, and src upload excluding patterns/.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-03 14:54:13 +12:00
jimmy 713cd6e9a1 feat(cli): add ls flag and default lib upload
Made-with: Cursor
2026-04-15 00:46:19 +12:00
jimmy 9e72c62481 fix(led-tool): build cli without spec file dependency 2026-04-14 23:13:21 +12:00
pi eee9327e15 docs: fix readme layout and cli flags
Made-with: Cursor
2026-04-12 00:13:49 +12:00
pi 5f7acf38f0 fix(cli): remove --server-ip flag and settings edit path
Made-with: Cursor
2026-04-11 15:20:22 +12:00
pi e86312437c feat(cli): create presets on device; flags for led-driver transport
Made-with: Cursor
2026-04-05 21:12:58 +12:00
9 changed files with 1477 additions and 76 deletions

No files matched your search

+1 -1
View File
@@ -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"
+162 -5
View File
@@ -1,8 +1,165 @@
# led-tool # led-tool
- `-s, --show`: Display current settings from device
- `--no-download`: Don't download settings first (use empty settings)
**Default behavior:** Downloads settings and prints them. If any edit flags are provided, settings are modified and uploaded automatically (unless `--no-upload` is specified).
## Device Connection Command-line tool for the LED drivers. It configures both firmwares found in this repo, and updates their firmware:
The tools use an integrated mpremote transport to communicate with MicroPython devices. Make sure your device is connected and accessible via the specified serial port.## LicenseSee LICENSE file for details. - **ESP-IDF led-driver** (ESP32-C3): settings, patterns and presets over **USB** or **Wi-Fi**. Changes apply live, so no reset is needed.
- **MicroPython driver**: `settings.json`, presets and files over USB (via mpremote). Changes need a reset to apply.
The tool detects the firmware on the serial port. Use `--driver idf` or `--driver mpy` to skip detection.
Run `python cli.py -h` for every flag, grouped by firmware, with examples at the end.
## Connecting
| Connection | Flag | Notes |
|---|---|---|
| USB serial (default) | `-p`, `--port` | Default `/dev/ttyACM0`. Opening the port keeps DTR high and RTS low, so the C3 is not reset on connect. |
| Wi-Fi | `--host ADDR` | IP or name of an ESP-IDF driver, for example `--host 192.168.4.20` or `--host bar1.local`. Port 80. |
Over `--host` every settings, pattern and preset flag works the same as over USB. Two flags need USB: `-r` (reboot) and `-f` (follow the serial log). MicroPython is USB only.
## ESP-IDF led-driver
### Settings (USB or Wi-Fi)
| Flag | Setting |
|---|---|
| `--pin`, `-l`, `-o`, `--max-brightness` | LED GPIO, LED count, colour order, global ceiling |
| `-b` | Brightness |
| `-n NAME`, `--reset-device-name` | Hostname (mDNS `NAME.local`); reset gives `led` |
| `--ap on\|off`, `--ap-ssid NAME`, `--ap-password PASS` | Access point (empty name: `led-XXXXXX`; empty password: open) |
| `--ddp on\|off` | Receive DDP pixels and frame sync |
| `--dhcp`, `--static IP NETMASK GATEWAY [DNS]`, `--dns ADDR` | Wi-Fi client address |
| `--ssid SSID`, `--wifi-password PASS` | Wi-Fi network to join; the device reconnects |
| `--default NAME` | Startup preset (runs at boot) |
| `--scan` | List the Wi-Fi networks the device can see |
`-s` shows the settings: hardware, brightness, network, Wi-Fi SSID and the startup preset. It is printed after any change.
Network changes can move the device to a new address. If you change `--static` or `--dhcp` over `--host`, the next command needs the new address.
### Patterns and presets (USB or Wi-Fi)
Presets are saved on the device. Patterns named with `--play` or `--pattern` are the driver's own names; see `--list-patterns`.
| Flag | Does |
|---|---|
| `--list-patterns` | Print the pattern names the driver has |
| `--list-presets` | Print the saved preset names, with the startup one marked |
| `--preset NAME [--pattern P] [options]` | Create or replace a saved preset. Only the fields given are changed. |
| `--play PATTERN [options]` | Show a pattern now, without saving it |
| `--select NAME` | Show a saved preset now |
| `--delete NAME` | Delete a saved preset |
| `--preset-order NAME [NAME ...]` | Set the order of the saved presets |
| `--presets-defaults` | Add the default presets that are missing |
Options for `--preset` and `--play`:
| Option | Meaning |
|---|---|
| `--colors HEX [HEX ...]` | Colours, for example `#ff0000 #00ff00` |
| `--background HEX` | Background colour |
| `--delay MS` | Delay between steps |
| `--preset-brightness 0-255` | Brightness of this preset (not the global level) |
| `--fade MS` | Fade time |
| `--reverse on\|off` | Run in reverse |
| `--auto on\|off` | Autorun flag |
| `--beat-time on\|off` | Time the pattern to the beat |
`--preset` and `--play` cannot be used together. Options given without one of them are an error.
Not yet exposed: the preset's numeric parameters `n1`–`n4` and `mode`.
```bash
python cli.py --list-presets
python cli.py --preset warm --pattern chase --colors '#ff0000' '#00ff00' --delay 50 --default warm
python cli.py --select warm
python cli.py --play rainbow --fade 200
python cli.py --host 192.168.4.20 --preset warm --pattern solid --colors '#ffa000'
```
### Reboot and logs (USB only)
```bash
python cli.py -r # reboot
python cli.py -f # follow the serial log, Ctrl+C to stop
python cli.py -r --pause 2 -f
```
## Firmware updates
Both update over Wi-Fi with the driver's `/ota` endpoint. The image is always given explicitly for LED drivers.
| Flag | Does |
|---|---|
| `--ota HOST [IMAGE]` | Update one **v2 controller** (ESP32-S3, Ethernet). Image defaults to `led_controller_v2.bin` under `./v2/build`, `./build` or `../v2/build`. |
| `--ota-drivers [TARGET ...]` | Find ESP-IDF LED drivers in the given targets and update them in parallel. Default `auto` is this machine's `/24`. See the target forms below. |
| `--ota-image PATH` | Driver firmware for `--ota-drivers`. **Required**, for example `../led-driver/build/led_driver.bin`. |
| `--ota-jobs N` | Simultaneous uploads (default 4). |
| `--ota-list` | Only find and list the drivers; do not update. |
Targets:
| Form | Scans |
|---|---|
| `auto` | This machine's `/24` (default) |
| `192.168.4` | The `/24`, the same as `192.168.4.0/24` |
| `10.0.0.0/23` | A CIDR network (up to 4096 addresses per run) |
| `192.168.4.10-40` or `192.168.4.10-192.168.4.40` | An inclusive address range |
| `192.168.4.25` | One address |
Give several targets separated by spaces or commas. Duplicates are scanned once. A run can probe at most 4096 addresses; larger targets are refused, so narrow them.
```bash
cd led-tool
python cli.py --ota-drivers --ota-image ../led-driver/build/led_driver.bin
python cli.py --ota-drivers 192.168.4.10-40 --ota-list
python cli.py --ota-drivers 192.168.4.0/24 10.0.0.0/25 --ota-jobs 2 --ota-image ../led-driver/build/led_driver.bin
python cli.py --ota-drivers 192.168.4.10-40,192.168.5.7 --ota-image ../led-driver/build/led_driver.bin
```
`--ota-drivers` probes `GET /ota` on each address in the targets, so it only finds devices that run the driver firmware. Drivers are reported with their partition and build; a driver that comes back on the same partition is reported as a failure.
Build the driver image first with `make build` in `led-driver`.
## MicroPython driver
These flags apply to the MicroPython driver only, and are rejected when an ESP-IDF driver answers.
| Flag | Does |
|---|---|
| `-n NAME`, `-l N`, `-b`, `-o`, `--pin` | Settings written to `settings.json` |
| `--preset NAME --pattern P` | Create or replace a preset in `presets.json` (patterns: off, on, blink, rainbow, pulse, transition, chase, circle) |
| `--default NAME` | Startup preset |
| `--transport espnow\|wifi`, `--ssid`, `--wifi-password`, `--wifi-channel`, `--id`, `-d` | Transport and radio settings |
| `-u SRC [DEST]`, `--src [DIR]`, `--lib [DIR]`, `--patterns [DIR]`, `--all` | Upload directories to the device |
| `--ls`, `--rm PATH`, `-e` | List, delete or erase files on the device |
| `--flash BINARY` | Flash a firmware binary with esptool (path, or a filename in `bin/`) |
| `--reset-device-name` | Name becomes `led-` + the STA MAC |
Without setting edits, the tool downloads `settings.json` and prints the merged view. It uploads again only when you pass edits, `--preset`, or an upload, flash or erase action. Actions run in the order given on the command line, for example `--reset --pause 2 --follow`.
## Installing
`install.sh` installs the Python dependencies with pipenv, builds `dist/led-cli` with PyInstaller, and copies it to `~/.local/bin`.
```bash
./install.sh
```
Run from source with `python cli.py` after `pipenv install`.
## Files
| File | Purpose |
|---|---|
| `cli.py` | Argument parsing and the commands for both firmwares |
| `idf_device.py` | ESP-IDF API over USB (`@api` lines on the serial port) |
| `http_device.py` | The same ESP-IDF API over Wi-Fi (`--host`) |
| `device.py` | MicroPython device access via mpremote |
| `ota.py` | Firmware upload to one device, used by `--ota` and `--ota-drivers` |
| `driver_ota.py` | Discovery of ESP-IDF drivers on a subnet and parallel update |
## License
See **LICENSE** in this directory.
+754 -64
View File
File diff suppressed because it is too large. Load diff
+80 -5
View File
@@ -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
@@ -53,7 +56,39 @@ class DeviceConnection:
except Exception: except Exception:
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()
@@ -87,6 +122,13 @@ class DeviceConnection:
if not os.path.isdir(local_dir): if not os.path.isdir(local_dir):
raise ValueError(f"{local_dir} is not a directory") raise ValueError(f"{local_dir} is not a directory")
# Host bytecode must never reach the device: remove it before uploading.
import shutil
for root, dirs, _files in os.walk(local_dir):
for d in [d for d in dirs if d == "__pycache__"]:
shutil.rmtree(os.path.join(root, d), ignore_errors=True)
dirs.remove(d)
self.connect() self.connect()
try: try:
# Determine remote directory name # Determine remote directory name
@@ -123,6 +165,8 @@ class DeviceConnection:
# Copy files # Copy files
for file in files: for file in files:
if file.endswith((".pyc", ".pyo")):
continue
local_file = os.path.join(root, file) local_file = os.path.join(root, file)
# Handle root directory case properly # Handle root directory case properly
if remote_base == '/': if remote_base == '/':
@@ -133,7 +177,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 +279,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.
+148
View File
@@ -0,0 +1,148 @@
"""
Find every LED driver on a Wi-Fi network or address range, and update them in parallel.
A driver answers GET /ota with its partition and build info, so discovery is a parallel probe
of each address in the targets. Each driver found then gets the same image, `jobs` at a time.
Uses ota.py for the per-device upload.
Targets (one or more, comma-separated or as separate arguments):
auto this machine's /24
192.168.4 a /24 (same as 192.168.4.0/24)
192.168.4.0/24 a CIDR network
192.168.4.10-192.168.4.40 an inclusive range
192.168.4.10-40 the same, short form
192.168.4.25 one address
"""
import concurrent.futures as cf
import ipaddress
import os
import socket
import sys
from typing import List, Optional, Tuple
from ota import _info, ota_update
# Largest number of addresses one run will probe, so a typo like 10.0.0.0/8 is refused
MAX_TARGETS = 4096
def local_subnet() -> str:
"""First three octets of the address this machine uses to reach the network."""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(("192.0.2.1", 9)) # UDP connect sends no packet
ip = s.getsockname()[0]
finally:
s.close()
return ".".join(ip.split(".")[:3])
def _ip(text: str) -> ipaddress.IPv4Address:
try:
return ipaddress.IPv4Address(text.strip())
except ValueError:
raise ValueError(f"not an IPv4 address: {text}") from None
def _expand(part: str) -> List[ipaddress.IPv4Address]:
"""One target (see the module docstring) -> the addresses it covers."""
if part == "auto":
part = local_subnet()
if "/" in part:
try:
net = ipaddress.IPv4Network(part, strict=False)
except ValueError:
raise ValueError(f"not a network: {part}") from None
if net.num_addresses > MAX_TARGETS:
raise ValueError(f"network too large (max {MAX_TARGETS} addresses): {part}")
return list(net.hosts()) if net.num_addresses > 2 else list(net)
if "-" in part:
first, last = part.split("-", 1)
start = _ip(first)
end = _ip(last) if last.count(".") == 3 else _ip(".".join(first.split(".")[:3] + [last]))
if int(end) < int(start):
raise ValueError(f"range ends before it starts: {part}")
if int(end) - int(start) >= MAX_TARGETS:
raise ValueError(f"range too large (max {MAX_TARGETS} addresses): {part}")
return [ipaddress.IPv4Address(i) for i in range(int(start), int(end) + 1)]
if part.count(".") == 2:
# Three octets: the whole /24, as before
return list(ipaddress.IPv4Network(part + ".0/24").hosts())
return [_ip(part)]
def parse_targets(specs: List[str]) -> List[str]:
"""All addresses named by the targets, in order, without duplicates."""
seen = set()
out: List[str] = []
for spec in specs:
for part in spec.split(","):
part = part.strip()
if not part:
continue
for addr in _expand(part):
if addr not in seen:
seen.add(addr)
out.append(str(addr))
if len(out) > MAX_TARGETS:
raise ValueError(f"too many addresses ({len(out)}; max {MAX_TARGETS}). Narrow the targets.")
return out
def discover(hosts: List[str], port: int = 80, workers: int = 64) -> List[Tuple[str, dict]]:
"""Probe each host for a driver. Returns (host, info) sorted by address."""
found = []
with cf.ThreadPoolExecutor(max_workers=workers) as pool:
futures = {pool.submit(_info, h, port): h for h in hosts}
for fut in cf.as_completed(futures):
info = fut.result()
if info:
found.append((futures[fut], info))
return sorted(found, key=lambda t: int(ipaddress.IPv4Address(t[0])))
def ota_drivers(specs: List[str], image: Optional[str], jobs: int, list_only: bool,
port: int = 80) -> int:
"""Discover drivers in the targets and update them. Returns a process exit code."""
if not list_only:
if not image or not os.path.isfile(image):
print(f"Error: image not found: {image}", file=sys.stderr)
return 1
try:
hosts = parse_targets(specs or ["auto"])
except ValueError as e:
print(f"Error: {e}", file=sys.stderr)
return 1
if not hosts:
print("Error: no addresses to scan", file=sys.stderr)
return 1
print(f"Scanning {len(hosts)} address(es) on port {port}: {hosts[0]} .. {hosts[-1]}", file=sys.stderr)
drivers = discover(hosts, port)
if not drivers:
print("No drivers found.", file=sys.stderr)
return 1
print(f"Found {len(drivers)} driver(s):", file=sys.stderr)
for host, info in drivers:
print(f" {host:<16} {info.get('partition', '?'):<8} built {info.get('built', '?')}", file=sys.stderr)
if list_only:
return 0
print(f"\nUpdating {len(drivers)} driver(s) with {image}, {jobs} at a time...", file=sys.stderr)
failed = []
with cf.ThreadPoolExecutor(max_workers=jobs) as pool:
futures = {pool.submit(ota_update, h, image, port): h for h, _ in drivers}
for fut in cf.as_completed(futures):
host = futures[fut]
try:
fut.result()
print(f"OK {host}", file=sys.stderr)
except Exception as e:
failed.append(host)
print(f"FAILED {host}: {e}", file=sys.stderr)
ok = len(drivers) - len(failed)
print(f"\n{ok} updated, {len(failed)} failed" + (f": {' '.join(failed)}" if failed else ""), file=sys.stderr)
return 1 if failed else 0
+82
View File
@@ -0,0 +1,82 @@
"""
Wi-Fi connection to the ESP-IDF led-driver.
The driver serves the same JSON API over HTTP on its address (led-driver main/web.cpp) that it
serves over USB (main/usb_cfg.cpp). HttpDevice has the same get/post/call interface as
idf_device.IdfDevice, so the settings code in cli.py works for both transports.
Not available over Wi-Fi: reboot (no HTTP route) and following the serial log.
"""
import http.client
import json
import socket
from typing import Any, Optional, Tuple
from idf_device import IdfError
class HttpDevice:
def __init__(self, host: str, port: int = 80, timeout: float = 5.0):
self.host = host
self.port = port
self.timeout = timeout
def open(self) -> "HttpDevice":
return self
def close(self) -> None:
pass
def __enter__(self) -> "HttpDevice":
return self
def __exit__(self, *exc) -> None:
pass
def call_raw(self, method: str, path: str, body: Any = None, host: Optional[str] = None,
timeout: Optional[float] = None) -> Tuple[int, Any]:
"""Send one request; return (status, parsed body). Does not raise on an error status."""
if host is not None:
raise IdfError(0, "relay through the driver is USB-only; use --port for that")
conn = http.client.HTTPConnection(self.host, self.port, timeout=timeout or self.timeout)
headers = {}
data = None
if body is not None:
data = json.dumps(body, separators=(",", ":")).encode()
headers["Content-Type"] = "application/json"
try:
conn.request(method, path, body=data, headers=headers)
r = conn.getresponse()
text = r.read().decode("utf-8", "replace")
except socket.timeout as e:
raise TimeoutError(f"no reply from {self.host} to {method} {path}") from e
except OSError as e:
raise TimeoutError(f"cannot reach {self.host}: {e}") from e
finally:
conn.close()
try:
parsed = json.loads(text) if text else {}
except ValueError:
parsed = text
return r.status, parsed
def call(self, method: str, path: str, body: Any = None, **kw) -> Any:
"""Send one request; return the parsed body. Raises IdfError on status >= 400."""
status, parsed = self.call_raw(method, path, body, **kw)
if status >= 400:
msg = parsed.get("error") if isinstance(parsed, dict) else None
raise IdfError(status, msg or str(parsed))
return parsed
def get(self, path: str, **kw) -> Any:
return self.call("GET", path, **kw)
def post(self, path: str, body: Any, **kw) -> Any:
return self.call("POST", path, body, **kw)
def reboot(self) -> None:
raise IdfError(0, "reboot is not available over Wi-Fi; use -r with --port (USB)")
def follow(self, seconds: Optional[float] = None) -> None:
raise IdfError(0, "following the log is not available over Wi-Fi; use -f with --port (USB)")
+172
View File
@@ -0,0 +1,172 @@
"""
USB connection to the ESP-IDF led-driver (ESP32-C3).
The driver serves its JSON API on the USB serial port (led-driver main/usb_cfg.cpp):
@api {"id": n, "method": "GET" or "POST", "path": "/api/...", "body": {...}}
-> @api {"id": n, "status": 200, "body": "<JSON text>"}
@reboot -> @ok, then the device restarts
Every other line on the port is log output.
"""
import json
import sys
import time
from typing import Any, Dict, Optional, Tuple
import serial
API = "@api "
class IdfError(RuntimeError):
"""The device answered with an error status."""
def __init__(self, status: int, message: str):
super().__init__(f"{status}: {message}")
self.status = status
self.message = message
class IdfDevice:
def __init__(self, port: str, verbose: bool = False):
self.port = port
self.verbose = verbose
self._id = 0
self._ser: Optional[serial.Serial] = None
def open(self) -> "IdfDevice":
if self._ser is None:
ser = serial.Serial()
ser.port = self.port
ser.baudrate = 115200
ser.timeout = 0.1
# On the C3's USB-Serial-JTAG, RTS high with DTR low resets the chip. Linux raises both
# on open and pyserial then sets DTR before RTS, so DTR must stay high (DTR low first
# would pass through RTS high, DTR low and reset it). RTS low keeps it running.
ser.dtr = True
ser.rts = False
ser.open()
self._ser = ser
return self
def close(self) -> None:
if self._ser is not None:
self._ser.close()
self._ser = None
def __enter__(self) -> "IdfDevice":
return self.open()
def __exit__(self, *exc) -> None:
self.close()
def _log(self, line: str) -> None:
if self.verbose and line:
print(f"[device] {line}", file=sys.stderr)
def _readline(self, deadline: float) -> Optional[str]:
"""One line from the device, or None at the deadline."""
buf = b""
while time.monotonic() < deadline:
chunk = self._ser.readline()
if not chunk:
continue
buf += chunk
if buf.endswith(b"\n"):
return buf.decode("utf-8", "replace").rstrip("\r\n")
return None
def call_raw(self, method: str, path: str, body: Any = None, host: Optional[str] = None,
timeout: float = 3.0) -> Tuple[int, Any]:
"""Send one request; return (status, parsed body). Does not raise on an error status."""
self.open()
self._id += 1
msg: Dict[str, Any] = {"id": self._id, "method": method, "path": path}
if body is not None:
msg["body"] = body
if host:
msg["host"] = host
self._ser.reset_input_buffer()
self._ser.write(("\n" + API + json.dumps(msg, separators=(",", ":")) + "\n").encode())
self._ser.flush()
deadline = time.monotonic() + timeout
while True:
line = self._readline(deadline)
if line is None:
raise TimeoutError(f"no reply from {self.port} to {method} {path}")
at = line.find(API + "{")
if at < 0:
self._log(line)
continue
if at > 0:
self._log(line[:at])
try:
env = json.loads(line[at + len(API):])
except ValueError:
continue
if env.get("id") != self._id:
continue
text = env.get("body", "")
try:
parsed = json.loads(text) if text else {}
except ValueError:
parsed = text
return int(env.get("status", 0)), parsed
def call(self, method: str, path: str, body: Any = None, **kw) -> Any:
"""Send one request; return the parsed body. Raises IdfError on status >= 400."""
status, parsed = self.call_raw(method, path, body, **kw)
if status >= 400:
msg = parsed.get("error") if isinstance(parsed, dict) else None
raise IdfError(status, msg or str(parsed))
return parsed
def get(self, path: str, **kw) -> Any:
return self.call("GET", path, **kw)
def post(self, path: str, body: Any, **kw) -> Any:
return self.call("POST", path, body, **kw)
def reboot(self) -> None:
"""Restart the device: the @reboot command, or an RTS reset pulse if it doesn't answer."""
self.open()
self._ser.reset_input_buffer()
self._ser.write(b"\n@reboot\n")
self._ser.flush()
deadline = time.monotonic() + 1.5
while True:
line = self._readline(deadline)
if line is None:
break
if line.strip() == "@ok":
return
self._log(line)
# Hardware reset: RTS high with DTR low holds the chip in reset (DTR low alone would
# mean "boot mode" at release, so RTS goes low first)
self._ser.rts = True
self._ser.dtr = False
time.sleep(0.1)
self._ser.rts = False
self._ser.dtr = True
def follow(self, seconds: Optional[float] = None) -> None:
"""Print the device's output until Ctrl+C, or for SECONDS."""
self.open()
end = time.monotonic() + seconds if seconds is not None else None
while end is None or time.monotonic() < end:
chunk = self._ser.read(256)
if chunk:
sys.stdout.write(chunk.decode("utf-8", "replace"))
sys.stdout.flush()
def probe(port: str, timeout: float = 1.0) -> bool:
"""True if an ESP-IDF led-driver answers the USB API on PORT."""
try:
with IdfDevice(port) as dev:
dev.call_raw("GET", "/api/status", timeout=timeout)
return True
except (TimeoutError, serial.SerialException, OSError):
return False
+1 -1
View File
@@ -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
+77
View File
@@ -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 an LED device?)")
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")