Skip to content

Commit 6a0a1b1

Browse files
Merge pull request #8 from anton-ptashnik/fix-issue-7-exc-on-flip
fix #7 - Exception is raised when the Dice is flipped
2 parents 133a3f7 + b1deb05 commit 6a0a1b1

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
Use the GoDice Python API to integrate GoDice functionality into your own Python applications
77

8-
![PyPI - Version](https://img.shields.io/pypi/v/godice)
8+
[![PyPI version](https://badge.fury.io/py/godice.svg)](https://pypi.org/project/godice)
99

1010
**Supported features:**
1111

@@ -42,12 +42,11 @@ import godice
4242
4343
async def main():
4444
mac = "00:00:00:00:00:00"
45-
client = bleak.BleakClient(mac, timeout=15)
4645
4746
# Python context manager (async with) is used for convenient connection handling
4847
# Device stays connected during `async with` block execution and auto-disconnected on block finish
4948
# Otherwise, dice.connect/dice.disconnect can be used instead
50-
async with godice.create(client, godice.Shell.D6) as dice:
49+
async with godice.create(mac, godice.Shell.D6, timeout=30) as dice:
5150
print("Connected")
5251
blue_rgb = (0, 0, 255)
5352
yellow_rgb = (255, 255, 0)

godice/demo.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,20 @@
99
async def main():
1010
print("Discovering GoDice devices...")
1111
discovery_res = await bleak.BleakScanner.discover(timeout=10, return_adv=True)
12-
dev_advdata_tuples = discovery_res.values()
13-
dev_advdata_tuples = filter_godice_devices(dev_advdata_tuples)
12+
device_advdata_tuples = discovery_res.values()
13+
device_advdata_tuples = filter_godice_devices(device_advdata_tuples)
1414

1515
print("Discovered devices...")
16-
print_device_info(dev_advdata_tuples)
16+
print_device_info(device_advdata_tuples)
1717

1818
print("Connecting to a closest device...")
19-
dev, _adv_data = select_closest_device(dev_advdata_tuples)
20-
client = bleak.BleakClient(dev, timeout=15)
19+
device, _adv_data = select_closest_device(device_advdata_tuples)
2120

2221
# Python context manager (async with) is used for convenient connection handling
2322
# Device stays connected during `async with` block execution and auto-disconnected on block finish
2423
# Otherwise, dice.connect/dice.disconnect can be used instead
25-
async with godice.create(client, godice.Shell.D6) as dice:
26-
print(f"Connected to {dev.name}")
24+
async with godice.create(device.address, godice.Shell.D6) as dice:
25+
print(f"Connected to {device.name}")
2726

2827
blue_rgb = (0, 0, 255)
2928
yellow_rgb = (255, 255, 0)

godice/dice.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ def __init__(self, ble_client) -> None:
4141
self._color_upd_q = asyncio.Queue()
4242
self._battery_lvl_upd_q = asyncio.Queue()
4343
self._xyz_interpret_fn = None
44-
self._nop_cb = lambda _: None
45-
self._position_upd_cb = self._nop_cb
44+
async def _noop_cb(_num, _stab_descr):
45+
pass
46+
self._position_upd_cb = _noop_cb
4647

4748
async def connect(self):
4849
await self._client.connect()

godice/factory.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,13 @@ class Shell(enum.Enum):
3131
}
3232

3333

34-
def create(ble_client: bleak.BleakClient, dice_shell: Shell):
34+
def create(ble_address: str, dice_shell: Shell, timeout: int=15, disconnect_cb=None):
3535
"""
3636
Creates Dice API object representing the specified type of a dice
3737
:param ble_client: BleakClient
3838
:param dice_shell: Shell
3939
"""
40+
ble_client = bleak.BleakClient(ble_address, timeout=timeout, disconnected_callback=disconnect_cb)
4041
_dice = dice.Dice(ble_client)
4142
set_shell(_dice, dice_shell)
4243
return _dice

0 commit comments

Comments
 (0)