Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**

Expand Down Expand Up @@ -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)
Expand Down
13 changes: 6 additions & 7 deletions godice/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions godice/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion godice/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down