Skip to content

Commit b38da91

Browse files
add a demo script
1 parent 253fa31 commit b38da91

File tree

3 files changed

+102
-36
lines changed

3 files changed

+102
-36
lines changed

README.md

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,46 @@ Use the GoDice Python API to integrate GoDice functionality into your own Python
1313
* Get notifications regarding the die state (Rolling or Stable and get the outcome number)
1414
* Use and configure different shells (D6, D20, D12 etc.)
1515

16+
## Installation
1617

17-
## Usage
18+
- once deployed to PyPI
1819

1920
```
20-
import asyncio
21-
import bleak
22-
import godice
21+
pip install godice
22+
```
2323

24+
- meanwhile, to install a local copy
25+
1. Clone the repo
26+
2. cd into the root dir
27+
3. Install a local copy
28+
```
29+
pip install .
30+
```
2431

25-
async def print_upd(stability_descr, number):
26-
print(f"Stability descriptor: {stability_descr}, number: {number}")
32+
## Demo
2733

34+
Package includes a demo script showing up API features. Command to run it:
35+
```
36+
python -m godice.demo
37+
```
2838

29-
def filter_godice_devices(devices):
30-
return [
31-
dev for dev in devices if dev.name.startswith("GoDice")
32-
]
39+
It will discover GoDice devices nearby and connect to a closest one.
40+
Then it setups a dice color and starts listening to dice position changes, outputting a new number each time a dice is flipped.
41+
42+
## Usage
43+
44+
One can import and use the API from any custom Python script like below
45+
```
46+
import asyncio
47+
import bleak
48+
import godice
3349
3450
3551
async def main():
36-
print("Discovering GoDice devices...")
37-
devices = await bleak.BleakScanner.discover(timeout=10)
38-
devices = filter_godice_devices(devices)
39-
40-
print("Connecting to a dice...")
41-
dev = devices[0]
42-
client = bleak.BleakClient(dev, timeout=15)
43-
52+
mac = "00:00:00:00:00:00"
53+
client = bleak.BleakClient(mac, timeout=15)
4454
async with godice.create(client, godice.DiceType.D6) as dice:
45-
print("Connected")
46-
55+
print("Connected")
4756
blue_rgb = (0, 0, 255)
4857
yellow_rgb = (255, 255, 0)
4958
off_rgb = (0, 0, 0)
@@ -54,27 +63,15 @@ async def main():
5463
print(f"Color: {color}")
5564
print(f"Battery: {battery_lvl}")
5665
57-
print("Listening to dice position updates. Flip your dice")
66+
print("Listening to position updates. Flip your dice")
5867
await dice.subscribe_number_notification(print_upd)
5968
await asyncio.sleep(30)
6069
await dice.set_led(off_rgb, off_rgb)
6170
6271
63-
asyncio.run(main())
64-
```
65-
66-
## Installation
67-
68-
- once deployed to PyPI
72+
async def print_upd(stability_descr, number):
73+
print(f"Stability descriptor: {stability_descr}, number: {number}")
6974
70-
```
71-
pip install godice
72-
```
7375
74-
- meanwhile, to install a local copy
75-
1. Clone the repo
76-
2. cd into the root dir
77-
3. Install a local copy
78-
```
79-
pip install .
76+
asyncio.run(main())
8077
```

godice/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
GoDice Python API
3+
"""
4+
__version__ = "0.0.1"
5+
16
from .factory import (
27
create,
38
DiceType

godice/demo.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import asyncio
2+
import bleak
3+
import godice
4+
5+
6+
async def main():
7+
print("Discovering GoDice devices...")
8+
discovery_res = await bleak.BleakScanner.discover(timeout=10, return_adv=True)
9+
dev_advdata_tuples = discovery_res.values()
10+
dev_advdata_tuples = filter_godice_devices(dev_advdata_tuples)
11+
12+
print("Discovered devices...")
13+
print_device_info(dev_advdata_tuples)
14+
15+
print("Connecting to a closest device...")
16+
dev, _adv_data = select_closest_device(dev_advdata_tuples)
17+
client = bleak.BleakClient(dev, timeout=15)
18+
19+
async with godice.create(client, godice.DiceType.D6) as dice:
20+
print(f"Connected to {dev.name}")
21+
22+
blue_rgb = (0, 0, 255)
23+
yellow_rgb = (255, 255, 0)
24+
off_rgb = (0, 0, 0)
25+
await dice.set_led(blue_rgb, yellow_rgb)
26+
27+
color = await dice.get_color()
28+
battery_lvl = await dice.get_battery_level()
29+
print(f"Color: {color}")
30+
print(f"Battery: {battery_lvl}")
31+
32+
print("Listening to position updates. Flip your dice")
33+
await dice.subscribe_number_notification(print_notification)
34+
await asyncio.sleep(30)
35+
await dice.set_led(off_rgb, off_rgb)
36+
37+
38+
async def print_notification(stability_descr, number):
39+
print(f"Stability descriptor: {stability_descr}, number: {number}")
40+
41+
42+
def filter_godice_devices(dev_advdata_tuples):
43+
return [
44+
(dev, adv_data)
45+
for dev, adv_data in dev_advdata_tuples
46+
if dev.name.startswith("GoDice")
47+
]
48+
49+
50+
def select_closest_device(dev_advdata_tuples):
51+
def _rssi_as_key(dev_advdata):
52+
_, adv_data = dev_advdata
53+
return adv_data.rssi
54+
55+
return max(dev_advdata_tuples, key=_rssi_as_key)
56+
57+
58+
def print_device_info(devices):
59+
for dev, adv_data in devices:
60+
print(f"Name: {dev.name}, address: {dev.address}, rssi: {adv_data.rssi}")
61+
62+
63+
if __name__ == "__main__":
64+
asyncio.run(main())

0 commit comments

Comments
 (0)