-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add gpio_list method to retrieve all GPIO pins in WokwiClient #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dab255f
feat: add framebuffer command helpers for reading and saving PNGs
JakubAndrysek 321b103
feat: add download and download_file methods to WokwiClient and file_ops
JakubAndrysek 8a158b9
feat: add gpio_list method to retrieve all GPIO pins in WokwiClient
JakubAndrysek 296b502
feat: update gpio_list method to return a list of GPIO pins and handl…
JakubAndrysek 9c6c5da
Merge remote-tracking branch 'origin/main' into add-gpio-list
JakubAndrysek 1395379
Merge remote-tracking branch 'origin/main' into add-gpio-list
JakubAndrysek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| """Framebuffer command helpers. | ||
|
|
||
| Provides utilities to interact with devices exposing a framebuffer (e.g. LCD | ||
| modules) via the `framebuffer:read` command. | ||
|
|
||
| Exposed helpers: | ||
| * framebuffer_read -> raw response (contains base64 PNG at result.png) | ||
| * framebuffer_png_bytes -> decoded PNG bytes | ||
| * save_framebuffer_png -> save PNG to disk | ||
| * compare_framebuffer_png -> compare current framebuffer against reference | ||
| """ | ||
|
|
||
| # SPDX-FileCopyrightText: 2025-present CodeMagic LTD | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import base64 | ||
| from pathlib import Path | ||
|
|
||
| from .exceptions import WokwiError | ||
| from .protocol_types import ResponseMessage | ||
| from .transport import Transport | ||
|
|
||
| __all__ = [ | ||
| "framebuffer_read", | ||
| "framebuffer_png_bytes", | ||
| "save_framebuffer_png", | ||
| "compare_framebuffer_png", | ||
| ] | ||
|
|
||
|
|
||
| async def framebuffer_read(transport: Transport, *, id: str) -> ResponseMessage: | ||
| """Issue `framebuffer:read` for the given device id and return raw response.""" | ||
| return await transport.request("framebuffer:read", {"id": id}) | ||
|
|
||
|
|
||
| def _extract_png_b64(resp: ResponseMessage) -> str: | ||
| result = resp.get("result", {}) | ||
| png_b64 = result.get("png") | ||
| if not isinstance(png_b64, str): # pragma: no cover - defensive | ||
| raise WokwiError("Malformed framebuffer:read response: missing 'png' base64 string") | ||
| return png_b64 | ||
|
|
||
|
|
||
| async def framebuffer_png_bytes(transport: Transport, *, id: str) -> bytes: | ||
| """Return decoded PNG bytes for the framebuffer of device `id`.""" | ||
| resp = await framebuffer_read(transport, id=id) | ||
| return base64.b64decode(_extract_png_b64(resp)) | ||
|
|
||
|
|
||
| async def save_framebuffer_png( | ||
| transport: Transport, *, id: str, path: Path, overwrite: bool = True | ||
| ) -> Path: | ||
| """Save the framebuffer PNG to `path` and return the path. | ||
|
|
||
| Args: | ||
| transport: Active transport. | ||
| id: Device id (e.g. "lcd1"). | ||
| path: Destination file path. | ||
| overwrite: Overwrite existing file (default True). If False and file | ||
| exists, raises WokwiError. | ||
| """ | ||
| if path.exists() and not overwrite: | ||
| raise WokwiError(f"File already exists and overwrite=False: {path}") | ||
| data = await framebuffer_png_bytes(transport, id=id) | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| with open(path, "wb") as f: | ||
| f.write(data) | ||
| return path | ||
|
|
||
|
|
||
| async def compare_framebuffer_png( | ||
| transport: Transport, *, id: str, reference: Path, save_mismatch: Path | None = None | ||
| ) -> bool: | ||
| """Compare the current framebuffer PNG with a reference file. | ||
|
|
||
| Performs a byte-for-byte comparison. If different and `save_mismatch` is | ||
| provided, writes the current framebuffer PNG there. | ||
|
|
||
| Returns True if identical, False otherwise. | ||
| """ | ||
| if not reference.exists(): | ||
| raise WokwiError(f"Reference image does not exist: {reference}") | ||
| current = await framebuffer_png_bytes(transport, id=id) | ||
| ref_bytes = reference.read_bytes() | ||
| if current == ref_bytes: | ||
| return True | ||
| if save_mismatch: | ||
| save_mismatch.parent.mkdir(parents=True, exist_ok=True) | ||
| save_mismatch.write_bytes(current) | ||
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's return a useful type (ResponseMessage isn't really useful for someone who is using the API.
IMHO the client should check for error, and throw if there's one, otherwise just return the result with a proper type (so users know which fields to expect and what types they have)