Skip to content

Commit 29f5105

Browse files
adapt API for Python async env
1 parent 5fa7149 commit 29f5105

File tree

6 files changed

+495
-624
lines changed

6 files changed

+495
-624
lines changed

README.md

Lines changed: 49 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,80 @@
11

2-
# GoDice Python API (with demo)
2+
# GoDice Python API
33

44
## Overview
55

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

8-
Here are some of the things that you can do with the GoDice Python API:
8+
**Supported features:**
99

1010
* Turn ON/OFF GoDice RGB LEDs
11-
* Ask for the die Color (dots color)
12-
* Ask for the die battery level
13-
* Get different notifications regarding the die state (Rolling or Stable and get the outcome number)
11+
* Read color (dots color)
12+
* Read battery level
13+
* 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-
To run the demo (that uses the API) make sure you have the Bleak library installed and run main.py as following:
17-
``` console
18-
python main.py
19-
```
2016

21-
Then enter the index of the GoDice you want to connect (or click Enter to connect to all available dice).
22-
Once connected you will start get notification from the connected dice (Rolling, Stable, etc...)
17+
## Usage
2318

24-
Dependencies Installation
25-
------------
26-
The GoDice Python API is dependent on the [Bleak](https://github.com/hbldh/bleak) library for Bluetooth (BLE) communication with the dice.
19+
```
20+
import asyncio
21+
import bleak
22+
import godice
2723
28-
You can install bleak by using this command:
2924
30-
$ pip install bleak
31-
or from sources ([Bleak installation docs](https://bleak.readthedocs.io/en/latest/installation.html))
25+
async def print_upd(stability_descr, number):
26+
print(f"Stability descriptor: {stability_descr}, number: {number}")
3227
33-
Usage
34-
=====
35-
**See Main.py for more examples of usage**
3628
37-
Discovering and connecting
38-
----
39-
To discover dice using GoDice library:
40-
```python
41-
from godice import *
29+
def filter_godice_devices(devices):
30+
return [
31+
dev for dev in devices if dev.name.startswith("GoDice")
32+
]
4233
43-
# Example how to discover GoDice Bluetooth devices
44-
def main():
45-
46-
# Discovering GoDice devices using BLE
47-
dice_devices = discover_dice()
48-
```
4934
50-
Connecting to a die and creating a die object (use "create_dice" and pass it a die device):
51-
```python
52-
myDie = create_dice(dice_devices[0])
53-
```
35+
async def main():
36+
print("Discovering GoDice devices...")
37+
devices = await bleak.BleakScanner.discover(timeout=10)
38+
devices = filter_godice_devices(devices)
5439
55-
Once you have the GoDice instance ready (e.g. myDie from the example above) you can call the following class functions:
40+
print("Connecting to a dice...")
41+
dev = devices[0]
42+
client = bleak.BleakClient(dev, timeout=15)
43+
44+
async with godice.create(client, godice.DiceType.D6) as dice:
45+
print("Connected")
5646
57-
Messages
58-
-----------
59-
Activating LEDs:
47+
blue_rgb = (0, 0, 255)
48+
yellow_rgb = (255, 255, 0)
49+
off_rgb = (0, 0, 0)
50+
await dice.set_led(blue_rgb, yellow_rgb)
6051
61-
```python
62-
# Turn On/Off RGB LEDs, will turn off if led1 and led2 are None
63-
# led1 - a list to control the 1st LED in the following format '[R, G, B]'
64-
# where R, G, and B are numbers in the range of 0-255
65-
# led2 - same as led1 for the second led
52+
color = await dice.get_color()
53+
battery_lvl = await dice.get_battery_level()
54+
print(f"Color: {color}")
55+
print(f"Battery: {battery_lvl}")
56+
57+
print("Listening to dice position updates. Flip your dice")
58+
await dice.subscribe_number_notification(print_upd)
59+
await asyncio.sleep(30)
60+
await dice.set_led(off_rgb, off_rgb)
6661
67-
set_led(led1: list, led2: list)
68-
```
6962
70-
```python
71-
# Pulses the die's leds for set time
72-
# pulse_count - How many pulses
73-
# on_time - How much time to spend on (units of 10 ms)
74-
# off_time - How much time to spend off (units of 10 ms)
75-
# rgb - List of RGB values to set die to pulse to
76-
77-
pulse_led(pulse_count, on_time, off_time, rgb)
78-
```
79-
80-
Requests
81-
-----------
82-
(Instanced methods of GoDice object)
83-
```python
84-
# Sends request for color of die
85-
send_color_request()
63+
asyncio.run(main())
8664
```
8765

66+
## Installation
8867

89-
```python
90-
# Changes die type (shell)
91-
# new_die_type - DieType Enum:
92-
# D6 = 0 (default)
93-
# D20 = 1,
94-
# D10 = 2,
95-
# D10x = 3,
96-
# D4 = 4,
97-
# D8 = 5,
98-
# D12 = 6
99-
# For example: set_die_type(DieType.D20)
68+
- once deployed to PyPI
10069

101-
set_die_type(new_die_type)
10270
```
103-
104-
Reading responses
105-
-----------
106-
Each die object has a "result_queue" attribute.
107-
Whenever a response/message is sent to the die, it's response code (if it has one) and it's value placed in the result queue as a tuple.
108-
109-
Example for iterating through the queue:
110-
```python
111-
while not die_object.result_queue.empty():
112-
113-
current_result = die_object.result_queue.get()
114-
115-
result_code = current_result[0]
116-
if result_code == "S":
117-
value = current_result[1]
118-
# Do something
119-
.
120-
.
121-
.
71+
pip install godice
12272
```
12373

124-
**Possible 'Results In Queue' (tuples)**
125-
126-
("R" - On roll start, No value)
127-
128-
("S" - Stable event, Value of die)
129-
130-
("TS" - Tilt stable event, Value of die)
131-
132-
("MS" - Move stable event, Value of die)
133-
134-
("FS" - Fake stable event, Value of die)
135-
136-
("B" - Battery response, Battery charge percent: 0-100)
137-
138-
("C" - Color response, ID of color of die: 0-5)
139-
140-
```python
141-
# Black 0
142-
# Red 1
143-
# Green 2
144-
# Blue 3
145-
# Yellow 4
146-
# Orange 5
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 .
14780
```

0 commit comments

Comments
 (0)