From cb4ceb022fd45f22b92d92aae885c35ba0a1c348 Mon Sep 17 00:00:00 2001 From: Anton Ptashnik Date: Mon, 20 May 2024 18:30:16 +0300 Subject: [PATCH 1/2] fix #7 - Exception is raised when the Dice is flipped but no custom flip handler attached --- README.md | 2 +- godice/dice.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 699bee5..df570a5 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Use the GoDice Python API to integrate GoDice functionality into your own Python applications -![PyPI - Version](https://img.shields.io/pypi/v/godice) +[![PyPI version](https://badge.fury.io/py/godice.svg)](https://pypi.org/project/godice) **Supported features:** diff --git a/godice/dice.py b/godice/dice.py index 782cf6a..cf0d1a7 100644 --- a/godice/dice.py +++ b/godice/dice.py @@ -41,8 +41,9 @@ def __init__(self, ble_client) -> None: self._color_upd_q = asyncio.Queue() self._battery_lvl_upd_q = asyncio.Queue() self._xyz_interpret_fn = None - self._nop_cb = lambda _: None - self._position_upd_cb = self._nop_cb + async def _noop_cb(_num, _stab_descr): + pass + self._position_upd_cb = _noop_cb async def connect(self): await self._client.connect() From b1deb0525d1a3b3e14f84ea76a3453af4a7ffce0 Mon Sep 17 00:00:00 2001 From: Anton Ptashnik Date: Tue, 21 May 2024 09:17:05 +0300 Subject: [PATCH 2/2] encapsulate connection details inside a Dice constructor keeping client code unaware about an intermediate lib used for Dice communication --- README.md | 3 +-- godice/demo.py | 13 ++++++------- godice/factory.py | 3 ++- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index df570a5..63b6b5f 100644 --- a/README.md +++ b/README.md @@ -42,12 +42,11 @@ import godice async def main(): mac = "00:00:00:00:00:00" - client = bleak.BleakClient(mac, timeout=15) # Python context manager (async with) is used for convenient connection handling # Device stays connected during `async with` block execution and auto-disconnected on block finish # Otherwise, dice.connect/dice.disconnect can be used instead - async with godice.create(client, godice.Shell.D6) as dice: + async with godice.create(mac, godice.Shell.D6, timeout=30) as dice: print("Connected") blue_rgb = (0, 0, 255) yellow_rgb = (255, 255, 0) diff --git a/godice/demo.py b/godice/demo.py index f3f7732..6b7c9bc 100644 --- a/godice/demo.py +++ b/godice/demo.py @@ -9,21 +9,20 @@ async def main(): print("Discovering GoDice devices...") discovery_res = await bleak.BleakScanner.discover(timeout=10, return_adv=True) - dev_advdata_tuples = discovery_res.values() - dev_advdata_tuples = filter_godice_devices(dev_advdata_tuples) + device_advdata_tuples = discovery_res.values() + device_advdata_tuples = filter_godice_devices(device_advdata_tuples) print("Discovered devices...") - print_device_info(dev_advdata_tuples) + print_device_info(device_advdata_tuples) print("Connecting to a closest device...") - dev, _adv_data = select_closest_device(dev_advdata_tuples) - client = bleak.BleakClient(dev, timeout=15) + device, _adv_data = select_closest_device(device_advdata_tuples) # Python context manager (async with) is used for convenient connection handling # Device stays connected during `async with` block execution and auto-disconnected on block finish # Otherwise, dice.connect/dice.disconnect can be used instead - async with godice.create(client, godice.Shell.D6) as dice: - print(f"Connected to {dev.name}") + async with godice.create(device.address, godice.Shell.D6) as dice: + print(f"Connected to {device.name}") blue_rgb = (0, 0, 255) yellow_rgb = (255, 255, 0) diff --git a/godice/factory.py b/godice/factory.py index f33a1fd..a78028e 100644 --- a/godice/factory.py +++ b/godice/factory.py @@ -31,12 +31,13 @@ class Shell(enum.Enum): } -def create(ble_client: bleak.BleakClient, dice_shell: Shell): +def create(ble_address: str, dice_shell: Shell, timeout: int=15, disconnect_cb=None): """ Creates Dice API object representing the specified type of a dice :param ble_client: BleakClient :param dice_shell: Shell """ + ble_client = bleak.BleakClient(ble_address, timeout=timeout, disconnected_callback=disconnect_cb) _dice = dice.Dice(ble_client) set_shell(_dice, dice_shell) return _dice