Skip to content

Commit 87f29ee

Browse files
Dice shell-related changes:
- allow to change Dice shell without device reconnection - renaming DiceType -> DiceShell
1 parent 049bb8e commit 87f29ee

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import godice
5151
async def main():
5252
mac = "00:00:00:00:00:00"
5353
client = bleak.BleakClient(mac, timeout=15)
54-
async with godice.create(client, godice.DiceType.D6) as dice:
54+
async with godice.create(client, godice.DiceShell.D6) as dice:
5555
print("Connected")
5656
blue_rgb = (0, 0, 255)
5757
yellow_rgb = (255, 255, 0)

godice/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from .factory import (
77
create,
8-
DiceType
8+
DiceShell
99
)
1010
from .dice import (
1111
StabilityDescriptor,

godice/demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async def main():
1919
dev, _adv_data = select_closest_device(dev_advdata_tuples)
2020
client = bleak.BleakClient(dev, timeout=15)
2121

22-
async with godice.create(client, godice.DiceType.D6) as dice:
22+
async with godice.create(client, godice.DiceShell.D6) as dice:
2323
print(f"Connected to {dev.name}")
2424

2525
blue_rgb = (0, 0, 255)

godice/dice.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ class Dice:
3333
"""
3434
Represents a dice providing API to features
3535
"""
36-
def __init__(self, ble_client, xyz_interpret_fn) -> None:
36+
def __init__(self, ble_client) -> None:
3737
self._client = ble_client
3838
self._rx_char = None
3939
self._tx_char = None
4040
self._color = None
4141
self._color_upd_q = asyncio.Queue()
4242
self._battery_lvl_upd_q = asyncio.Queue()
43-
self._xyz_interpret_fn = xyz_interpret_fn
43+
self._xyz_interpret_fn = None
4444
self._nop_cb = lambda _: None
4545
self._position_upd_cb = self._nop_cb
4646

godice/factory.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
A factory for all dice types
2+
GoDice factory returning a proper Dice based on a shell
33
"""
44
import enum
55
import bleak
@@ -8,9 +8,9 @@
88
from . import xyzinterpret
99

1010

11-
class DiceType(enum.Enum):
11+
class DiceShell(enum.Enum):
1212
"""
13-
Available types of a dice (shells)
13+
Available shells a dice may be wrapped into
1414
"""
1515
D6 = 0
1616
D20 = 1
@@ -21,19 +21,29 @@ class DiceType(enum.Enum):
2121
D12 = 6
2222

2323
xyz_interpret_map = {
24-
DiceType.D6: xyzinterpret.get_rolled_number_d6,
25-
DiceType.D10: xyzinterpret.get_rolled_number_d10,
26-
DiceType.D10X: xyzinterpret.get_rolled_number_d10x,
27-
DiceType.D20: xyzinterpret.get_rolled_number_d20,
28-
DiceType.D4: xyzinterpret.get_rolled_number_d4,
29-
DiceType.D8: xyzinterpret.get_rolled_number_d8,
30-
DiceType.D12: xyzinterpret.get_rolled_number_d12,
24+
DiceShell.D6: xyzinterpret.get_rolled_number_d6,
25+
DiceShell.D10: xyzinterpret.get_rolled_number_d10,
26+
DiceShell.D10X: xyzinterpret.get_rolled_number_d10x,
27+
DiceShell.D20: xyzinterpret.get_rolled_number_d20,
28+
DiceShell.D4: xyzinterpret.get_rolled_number_d4,
29+
DiceShell.D8: xyzinterpret.get_rolled_number_d8,
30+
DiceShell.D12: xyzinterpret.get_rolled_number_d12,
3131
}
3232

3333

34-
def create(ble_client: bleak.BleakClient, dice_type: DiceType):
34+
def create(ble_client: bleak.BleakClient, dice_shell: DiceShell):
3535
"""
3636
Creates Dice API object representing the specified type of a dice
3737
"""
38-
_xyzinterpret_fn = xyz_interpret_map[dice_type]
39-
return dice.Dice(ble_client, _xyzinterpret_fn)
38+
_dice = dice.Dice(ble_client)
39+
set_shell(_dice, dice_shell)
40+
return _dice
41+
42+
43+
def set_shell(_dice: dice.Dice, dice_shell: DiceShell):
44+
"""
45+
Change a dice shell.
46+
Should be called in order to receive correct number notifications corresponding to a newly set shell
47+
"""
48+
_xyzinterpret_fn = xyz_interpret_map[dice_shell]
49+
_dice._xyz_interpret_fn = _xyzinterpret_fn

godice/xyzinterpret.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Tools needed to translate coords sent by a dice to a resulting number based on a dice type
2+
Tools needed to translate coords sent by a dice to a resulting number based on a dice shell
33
"""
44

55
import math

0 commit comments

Comments
 (0)