Skip to content

Commit ea38d46

Browse files
authored
feat: add support to take screenshot from simulator (#1)
1 parent 8d485cf commit ea38d46

8 files changed

Lines changed: 654 additions & 4 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ jobs:
1717
- run: uv run ruff format --check .
1818
- run: uv run ruff check .
1919
- run: uv run ty check .
20+
- run: uv run pytest

‎README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,31 @@ full brightness would be blinding in person; a color that's already
237237
it. Start around 5.0 for a similarly low "should read as white" value
238238
and adjust live with `[`/`]` for anything not listed.
239239

240+
## Screenshots
241+
242+
For CI, PR previews, or anywhere else a terminal isn't available:
243+
244+
```sh
245+
uv run matrixbox screenshot clock -o clock.png
246+
```
247+
248+
Boots one app headlessly (no terminal, no button listener), waits for it
249+
to draw, writes the result to a PNG, and exits — no second terminal or
250+
`--connect` needed. `--settings <name>` seeds it with a settings file
251+
before boot, resolved inside the app's own directory (e.g. `--settings
252+
ci.json` for `apps/clock/ci.json`); omit it to boot with plain defaults.
253+
Always starts from a clean, reset state, regardless of whatever an
254+
earlier `uv run matrixbox app` run against the same app may have saved.
255+
256+
By default it captures as soon as one frame is drawn, waiting up to 5
257+
seconds; `--after-frames <n>` and `--timeout <seconds>` adjust both,
258+
whichever is reached first. An app that never draws in time, or raises
259+
while starting up, exits non-zero with the error printed, so a CI job
260+
fails loudly instead of shipping a blank or stale image. `--scale <n>`
261+
sets the output PNG's pixel scale factor (default 8, so a 128x32 panel
262+
becomes a 1024x256 image). `--size` / `--width` / `--height` pick the
263+
panel size, same as `matrixbox app` (see "Panel sizes" above).
264+
240265
## Useful flags
241266

242267
`uv run matrixbox app`: `--size` or `--width` / `--height` for panel
@@ -248,6 +273,8 @@ and start fresh, `--refresh-fps` / `--gamma` (see above).
248273
`--device` or `--width` / `--height` for the demo/placeholder size (see
249274
"Panel sizes" above), `--fps` (demo mode only).
250275

276+
`uv run matrixbox screenshot`: see "Screenshots" above.
277+
251278
## Limitations
252279

253280
- `Group(scale=...)` is accepted but not honored: nothing renders

‎matrixbox_simulator/cli.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
"""Single `matrixbox` entrypoint, with `app` and `simulator` as subcommands."""
1+
"""Single `matrixbox` entrypoint, with `app`, `simulator`, and `screenshot`
2+
as subcommands."""
23

34
import argparse
45

5-
from matrixbox_simulator.device import run_app
6+
from matrixbox_simulator.device import run_app, run_screenshot
67
from matrixbox_simulator.term import run_simulator
78

89

@@ -24,12 +25,23 @@ def main() -> None:
2425
description=run_simulator.__doc__,
2526
)
2627
)
28+
run_screenshot.build_parser(
29+
subparsers.add_parser(
30+
"screenshot",
31+
help="boot an app headlessly and save one rendered frame to a PNG",
32+
description=run_screenshot.__doc__,
33+
)
34+
)
2735

2836
args = parser.parse_args()
2937
if args.command == "app":
3038
run_app.run(args)
31-
else:
39+
elif args.command == "screenshot":
40+
run_screenshot.run(args)
41+
elif args.command == "simulator":
3242
run_simulator.run(args)
43+
else:
44+
raise AssertionError(f"unhandled command: {args.command!r}")
3345

3446

3547
if __name__ == "__main__":

‎matrixbox_simulator/device/frame_bridge.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import struct
1010
import sys
1111
import time
12+
from collections.abc import Callable
1213

1314
try:
1415
import resource
@@ -40,6 +41,11 @@ def __init__(self) -> None:
4041
self._last_rgb: bytes | None = None
4142
self._smoothed_interval: float | None = None
4243

44+
# Set by headless callers (screenshot mode) that need a composited
45+
# frame directly, without standing up a real renderer to decode it
46+
# back off the wire.
47+
self.on_publish: Callable[[int, int, bytes], None] | None = None
48+
4349
def start(self, host: str = "127.0.0.1", port: int = 9191) -> wsserver.FrameServer:
4450
if self._server is None:
4551
self._server = wsserver.FrameServer(host, port)
@@ -97,6 +103,9 @@ def publish(self, width: int, height: int, rgb: bytes) -> None:
97103
header = struct.pack("<BBHHBB", 0xF3, 1, width, height, 0, self._tiles)
98104
self._server.broadcast(header + bytes(rgb), kind="frame")
99105

106+
if self.on_publish is not None:
107+
self.on_publish(width, height, rgb)
108+
100109
self._publish_stats()
101110

102111
def _publish_stats(self) -> None:

0 commit comments

Comments
 (0)