Skip to content

Commit 01d59f3

Browse files
refactor: change upload methods to return None and update pin command helpers (#8)
1 parent 6bf1c23 commit 01d59f3

File tree

2 files changed

+49
-53
lines changed

2 files changed

+49
-53
lines changed

src/wokwi_client/client.py

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
from pathlib import Path
77
from typing import Any, Optional, Union, cast
88

9-
from wokwi_client.exceptions import ProtocolError
10-
from wokwi_client.framebuffer import (
11-
read_framebuffer_png_bytes,
12-
save_framebuffer_png,
13-
)
14-
159
from .__version__ import get_version
1610
from .constants import DEFAULT_WS_URL
1711
from .control import set_control
1812
from .event_queue import EventQueue
13+
from .exceptions import ProtocolError
1914
from .file_ops import download, upload, upload_file
20-
from .pins import gpio_list, pin_listen, pin_read
21-
from .protocol_types import EventMessage, ResponseMessage
15+
from .framebuffer import (
16+
read_framebuffer_png_bytes,
17+
save_framebuffer_png,
18+
)
19+
from .pins import PinReadMessage, gpio_list, pin_listen, pin_read
20+
from .protocol_types import EventMessage
2221
from .serial import monitor_lines, write_serial
2322
from .simulation import pause, restart, resume, start
2423
from .transport import Transport
@@ -65,33 +64,25 @@ async def disconnect(self) -> None:
6564
"""
6665
await self._transport.close()
6766

68-
async def upload(self, name: str, content: bytes) -> ResponseMessage:
67+
async def upload(self, name: str, content: bytes) -> None:
6968
"""
7069
Upload a file to the simulator from bytes content.
7170
7271
Args:
7372
name: The name to use for the uploaded file.
7473
content: The file content as bytes.
75-
76-
Returns:
77-
The response message from the server.
7874
"""
79-
return await upload(self._transport, name, content)
75+
await upload(self._transport, name, content)
8076

81-
async def upload_file(
82-
self, filename: str, local_path: Optional[Path] = None
83-
) -> ResponseMessage:
77+
async def upload_file(self, filename: str, local_path: Optional[Path] = None) -> None:
8478
"""
8579
Upload a local file to the simulator.
8680
8781
Args:
8882
filename: The name to use for the uploaded file.
8983
local_path: Optional path to the local file. If not provided, uses filename as the path.
90-
91-
Returns:
92-
The response message from the server.
9384
"""
94-
return await upload_file(self._transport, filename, local_path)
85+
await upload_file(self._transport, filename, local_path)
9586

9687
async def download(self, name: str) -> bytes:
9788
"""
@@ -127,7 +118,7 @@ async def start_simulation(
127118
elf: Optional[str] = None,
128119
pause: bool = False,
129120
chips: list[str] = [],
130-
) -> ResponseMessage:
121+
) -> None:
131122
"""
132123
Start a new simulation with the given parameters.
133124
@@ -150,38 +141,29 @@ async def start_simulation(
150141
elf: The ELF file filename (optional).
151142
pause: Whether to start the simulation paused (default: False).
152143
chips: List of custom chips to load into the simulation (default: empty list).
153-
154-
Returns:
155-
The response message from the server.
156144
"""
157-
return await start(
145+
await start(
158146
self._transport,
159147
firmware=firmware,
160148
elf=elf,
161149
pause=pause,
162150
chips=chips,
163151
)
164152

165-
async def pause_simulation(self) -> ResponseMessage:
153+
async def pause_simulation(self) -> None:
166154
"""
167155
Pause the running simulation.
168-
169-
Returns:
170-
The response message from the server.
171156
"""
172-
return await pause(self._transport)
157+
await pause(self._transport)
173158

174-
async def resume_simulation(self, pause_after: Optional[int] = None) -> ResponseMessage:
159+
async def resume_simulation(self, pause_after: Optional[int] = None) -> None:
175160
"""
176161
Resume the simulation, optionally pausing after a given number of nanoseconds.
177162
178163
Args:
179164
pause_after: Number of nanoseconds to run before pausing again (optional).
180-
181-
Returns:
182-
The response message from the server.
183165
"""
184-
return await resume(self._transport, pause_after)
166+
await resume(self._transport, pause_after)
185167

186168
async def wait_until_simulation_time(self, seconds: float) -> None:
187169
"""
@@ -197,17 +179,14 @@ async def wait_until_simulation_time(self, seconds: float) -> None:
197179
await resume(self._transport, int(remaining_nanos))
198180
await self._pause_queue.get()
199181

200-
async def restart_simulation(self, pause: bool = False) -> ResponseMessage:
182+
async def restart_simulation(self, pause: bool = False) -> None:
201183
"""
202184
Restart the simulation, optionally starting paused.
203185
204186
Args:
205187
pause: Whether to start the simulation paused (default: False).
206-
207-
Returns:
208-
The response message from the server.
209188
"""
210-
return await restart(self._transport, pause)
189+
await restart(self._transport, pause)
211190

212191
async def serial_monitor_cat(self, decode_utf8: bool = True, errors: str = "replace") -> None:
213192
"""
@@ -235,16 +214,17 @@ async def serial_write(self, data: Union[bytes, str, list[int]]) -> None:
235214
def _on_pause(self, event: EventMessage) -> None:
236215
self.last_pause_nanos = int(event["nanos"])
237216

238-
async def read_pin(self, part: str, pin: str) -> ResponseMessage:
217+
async def read_pin(self, part: str, pin: str) -> PinReadMessage:
239218
"""Read the current state of a pin.
240219
241220
Args:
242221
part: The part id (e.g. "uno").
243222
pin: The pin name (e.g. "A2").
244223
"""
245-
return await pin_read(self._transport, part=part, pin=pin)
224+
pin_data = await pin_read(self._transport, part=part, pin=pin)
225+
return cast(PinReadMessage, pin_data["result"])
246226

247-
async def listen_pin(self, part: str, pin: str, listen: bool = True) -> ResponseMessage:
227+
async def listen_pin(self, part: str, pin: str, listen: bool = True) -> None:
248228
"""Start or stop listening for changes on a pin.
249229
250230
When enabled, "pin:change" events will be delivered via the transport's
@@ -255,7 +235,7 @@ async def listen_pin(self, part: str, pin: str, listen: bool = True) -> Response
255235
pin: The pin name.
256236
listen: True to start listening, False to stop.
257237
"""
258-
return await pin_listen(self._transport, part=part, pin=pin, listen=listen)
238+
await pin_listen(self._transport, part=part, pin=pin, listen=listen)
259239

260240
async def gpio_list(self) -> list[str]:
261241
"""Get a list of all GPIO pins available in the simulation.
@@ -269,17 +249,15 @@ async def gpio_list(self) -> list[str]:
269249
raise ProtocolError("Malformed gpio:list response: expected result.pins: list[str]")
270250
return cast(list[str], pins_val)
271251

272-
async def set_control(
273-
self, part: str, control: str, value: Union[int, bool, float]
274-
) -> ResponseMessage:
252+
async def set_control(self, part: str, control: str, value: Union[int, bool, float]) -> None:
275253
"""Set a control value (e.g. simulate button press).
276254
277255
Args:
278256
part: Part id (e.g. "btn1").
279257
control: Control name (e.g. "pressed").
280258
value: Control value to set (float).
281259
"""
282-
return await set_control(self._transport, part=part, control=control, value=value)
260+
await set_control(self._transport, part=part, control=control, value=value)
283261

284262
async def read_framebuffer_png_bytes(self, id: str) -> bytes:
285263
"""Return the current framebuffer as PNG bytes."""

src/wokwi_client/pins.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,30 @@
1111
#
1212
# SPDX-License-Identifier: MIT
1313

14-
from .protocol_types import ResponseMessage
14+
from typing import TypedDict, Union
15+
16+
from wokwi_client.protocol_types import ResponseMessage
17+
1518
from .transport import Transport
1619

1720

21+
class PinReadMessage(TypedDict):
22+
pin: str
23+
direction: str
24+
value: Union[float, int, bool]
25+
pullUp: bool
26+
pullDown: bool
27+
28+
29+
class PinListenEvent(TypedDict):
30+
part: str
31+
pin: str
32+
direction: str
33+
value: Union[float, int, bool]
34+
pullUp: bool
35+
pullDown: bool
36+
37+
1838
async def pin_read(transport: Transport, *, part: str, pin: str) -> ResponseMessage:
1939
"""Read the state of a pin.
2040
@@ -27,9 +47,7 @@ async def pin_read(transport: Transport, *, part: str, pin: str) -> ResponseMess
2747
return await transport.request("pin:read", {"part": part, "pin": pin})
2848

2949

30-
async def pin_listen(
31-
transport: Transport, *, part: str, pin: str, listen: bool = True
32-
) -> ResponseMessage:
50+
async def pin_listen(transport: Transport, *, part: str, pin: str, listen: bool = True) -> None:
3351
"""Enable or disable listening for changes on a pin.
3452
3553
When listening is enabled, "pin:change" events will be emitted with the
@@ -42,7 +60,7 @@ async def pin_listen(
4260
listen: True to start listening, False to stop.
4361
"""
4462

45-
return await transport.request("pin:listen", {"part": part, "pin": pin, "listen": listen})
63+
await transport.request("pin:listen", {"part": part, "pin": pin, "listen": listen})
4664

4765

4866
async def gpio_list(transport: Transport) -> ResponseMessage:

0 commit comments

Comments
 (0)