Skip to content

Commit 347052b

Browse files
authored
Merge pull request #4 from anton-ptashnik/adapt-for-async-env
Adapt API for Python async environment
2 parents 5fa7149 + 345ca1a commit 347052b

File tree

8 files changed

+636
-630
lines changed

8 files changed

+636
-630
lines changed

README.md

Lines changed: 62 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,147 +1,87 @@
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-
```
20-
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...)
23-
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.
27-
28-
You can install bleak by using this command:
29-
30-
$ pip install bleak
31-
or from sources ([Bleak installation docs](https://bleak.readthedocs.io/en/latest/installation.html))
32-
33-
Usage
34-
=====
35-
**See Main.py for more examples of usage**
16+
## Installation
3617

37-
Discovering and connecting
38-
----
39-
To discover dice using GoDice library:
40-
```python
41-
from godice import *
18+
- once deployed to PyPI
4219

43-
# Example how to discover GoDice Bluetooth devices
44-
def main():
45-
46-
# Discovering GoDice devices using BLE
47-
dice_devices = discover_dice()
4820
```
49-
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])
21+
pip install godice
5322
```
5423

55-
Once you have the GoDice instance ready (e.g. myDie from the example above) you can call the following class functions:
56-
57-
Messages
58-
-----------
59-
Activating LEDs:
60-
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
66-
67-
set_led(led1: list, led2: list)
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
6828
```
69-
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)
29+
pip install .
7830
```
7931

80-
Requests
81-
-----------
82-
(Instanced methods of GoDice object)
83-
```python
84-
# Sends request for color of die
85-
send_color_request()
86-
```
87-
88-
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)
32+
## Demo
10033

101-
set_die_type(new_die_type)
34+
Package includes a demo script showing up API features. Command to run it:
10235
```
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-
.
36+
python -m godice.demo
12237
```
12338

124-
**Possible 'Results In Queue' (tuples)**
125-
126-
("R" - On roll start, No value)
127-
128-
("S" - Stable event, Value of die)
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.
12941

130-
("TS" - Tilt stable event, Value of die)
42+
## Usage
13143

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
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
49+
50+
51+
async def main():
52+
mac = "00:00:00:00:00:00"
53+
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
58+
async with godice.create(client, godice.Shell.D6) as dice:
59+
print("Connected")
60+
blue_rgb = (0, 0, 255)
61+
yellow_rgb = (255, 255, 0)
62+
off_rgb = (0, 0, 0)
63+
await dice.set_led(blue_rgb, yellow_rgb)
64+
65+
color = await dice.get_color()
66+
battery_lvl = await dice.get_battery_level()
67+
print(f"Color: {color}")
68+
print(f"Battery: {battery_lvl}")
69+
70+
print("Listening to position updates. Flip your dice")
71+
await dice.subscribe_number_notification(notification_callback)
72+
await asyncio.sleep(30)
73+
await dice.set_led(off_rgb, off_rgb)
74+
75+
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}")
84+
85+
86+
asyncio.run(main())
14787
```

0 commit comments

Comments
 (0)