Skip to content

Commit 28f6c1a

Browse files
feat: add gpio_list method to retrieve all GPIO pins in WokwiClient (#6)
1 parent 22ef062 commit 28f6c1a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/wokwi_client/client.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
import base64
66
from pathlib import Path
7-
from typing import Any, Optional, Union
7+
from typing import Any, Optional, Union, cast
88

9+
from wokwi_client.exceptions import ProtocolError
910
from wokwi_client.framebuffer import (
1011
read_framebuffer_png_bytes,
1112
save_framebuffer_png,
@@ -16,7 +17,7 @@
1617
from .control import set_control
1718
from .event_queue import EventQueue
1819
from .file_ops import download, upload, upload_file
19-
from .pins import pin_listen, pin_read
20+
from .pins import gpio_list, pin_listen, pin_read
2021
from .protocol_types import EventMessage, ResponseMessage
2122
from .serial import monitor_lines, write_serial
2223
from .simulation import pause, restart, resume, start
@@ -256,6 +257,18 @@ async def listen_pin(self, part: str, pin: str, listen: bool = True) -> Response
256257
"""
257258
return await pin_listen(self._transport, part=part, pin=pin, listen=listen)
258259

260+
async def gpio_list(self) -> list[str]:
261+
"""Get a list of all GPIO pins available in the simulation.
262+
263+
Returns:
264+
list[str]: Example: ["esp32:GPIO0", "esp32:GPIO1", ...]
265+
"""
266+
resp = await gpio_list(self._transport)
267+
pins_val: Any = resp.get("result", {}).get("pins")
268+
if not isinstance(pins_val, list) or not all(isinstance(p, str) for p in pins_val):
269+
raise ProtocolError("Malformed gpio:list response: expected result.pins: list[str]")
270+
return cast(list[str], pins_val)
271+
259272
async def set_control(
260273
self, part: str, control: str, value: Union[int, bool, float]
261274
) -> ResponseMessage:

src/wokwi_client/pins.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,13 @@ async def pin_listen(
4343
"""
4444

4545
return await transport.request("pin:listen", {"part": part, "pin": pin, "listen": listen})
46+
47+
48+
async def gpio_list(transport: Transport) -> ResponseMessage:
49+
"""List all GPIO pins and their current states.
50+
51+
Args:
52+
transport: The active Transport instance.
53+
"""
54+
55+
return await transport.request("gpio:list", {})

0 commit comments

Comments
 (0)