@@ -51,6 +51,10 @@ import godice
5151async def main():
5252 mac = "00:00:00:00:00:00"
5353 client = bleak.BleakClient(mac, timeout=15)
54+
55+ # Python context manager (async with) is used for convenient connection handling
56+ # Device stays connected during `async with` block execution and auto-disconnected on block finish
57+ # Otherwise, dice.connect/dice.disconnect can be used instead
5458 async with godice.create(client, godice.DiceShell.D6) as dice:
5559 print("Connected")
5660 blue_rgb = (0, 0, 255)
@@ -64,13 +68,19 @@ async def main():
6468 print(f"Battery: {battery_lvl}")
6569
6670 print("Listening to position updates. Flip your dice")
67- await dice.subscribe_number_notification(print_upd )
71+ await dice.subscribe_number_notification(notification_callback )
6872 await asyncio.sleep(30)
6973 await dice.set_led(off_rgb, off_rgb)
7074
7175
72- async def print_upd(stability_descr, number):
73- print(f"Stability descriptor: {stability_descr}, number: {number}")
76+ async def notification_callback(number, stability_descr):
77+ """
78+ GoDice number notification callback.
79+ Called each time GoDice is flipped, receiving flip event data:
80+ :param number: a rolled number
81+ :param stability_descr: an additional value clarifying device movement state, ie stable, rolling...
82+ """
83+ print(f"Number: {number}, stability descriptor: {stability_descr}")
7484
7585
7686asyncio.run(main())
0 commit comments