diff --git a/README.md b/README.md index fdebc329..8c2650e3 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ All examples use [uv](https://docs.astral.sh/uv/) for dependency management: | Script | Description | |--------|-------------| +| [`all_commands.py`](examples/all_commands.py) | Exercise all 8 commands (Ping, SetConfig, GetConfig, StartRx, StopRx, Transmit, DisplayOn/Off) | | [`simple_rx.py`](examples/simple_rx.py) | Configure radio, receive and print packets | | [`simple_tx.py`](examples/simple_tx.py) | Transmit a single packet | | [`ping_pong.py`](examples/ping_pong.py) | Two-dongle demo (`--role tx` / `--role rx`) | diff --git a/examples/all_commands.py b/examples/all_commands.py new file mode 100755 index 00000000..74e9cc33 --- /dev/null +++ b/examples/all_commands.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +"""Exercise every DongLoRa command to verify the API works. + +Usage: + uv run examples/all_commands.py [PORT] +""" +# /// script +# requires-python = ">=3.10" +# dependencies = ["cobs", "pyserial"] +# /// + +import serial +import sys + +sys.path.insert(0, __import__("pathlib").Path(__file__).parent.as_posix()) +import donglora as dl # noqa: E402 + +try: + ser = dl.connect(sys.argv[1] if len(sys.argv) > 1 else None) + + print("── Ping ──") + print(dl.send(ser, "Ping")) + + print("\n── SetConfig ──") + print(dl.send(ser, "SetConfig", config=dl.DEFAULT_CONFIG)) + + print("\n── GetConfig ──") + print(dl.send(ser, "GetConfig")) + + print("\n── StartRx ──") + print(dl.send(ser, "StartRx")) + + print("\n── StopRx ──") + print(dl.send(ser, "StopRx")) + + print("\n── Transmit ──") + print(dl.send(ser, "Transmit", payload=b"Hello from all_commands.py!")) + + print("\n── DisplayOff ──") + print(dl.send(ser, "DisplayOff")) + + print("\n── DisplayOn ──") + print(dl.send(ser, "DisplayOn")) + + print("\nAll 8 commands exercised successfully.") + +except KeyboardInterrupt: + print("\nInterrupted.") +except serial.SerialException as e: + print(f"\nSerial error: {e}", file=sys.stderr) + sys.exit(1) diff --git a/examples/donglora.py b/examples/donglora.py index 8ca43d73..5645eee8 100644 --- a/examples/donglora.py +++ b/examples/donglora.py @@ -125,6 +125,11 @@ def decode_response(data: bytes) -> dict: if tag == 0: return {"type": "Pong"} elif tag == 1: + if len(rest) >= 10: + freq_hz, bw, sf, cr, sync_word, pwr = struct.unpack_from("