|
1 | | -# GoDicePythonAPI |
| 1 | + |
| 2 | +# GoDice Python API (with demo) |
| 3 | + |
| 4 | +## Overview |
| 5 | + |
| 6 | +Use the GoDice Python API to integrate GoDice functionality into your own Python applications |
| 7 | + |
| 8 | +Here are some of the things that you can do with the GoDice Python API: |
| 9 | + |
| 10 | +* Turn ON/OFF GoDice RGB Leds |
| 11 | +* Ask for the die Color (dots color) |
| 12 | +* Ask for the die battery level |
| 13 | +* Get differernt notifications reagrding the die state (Rolling or Stable and get the outcome number) |
| 14 | +* Use and configure different shells (D6, D20, D12 etc.) |
| 15 | + |
| 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 | + Dependencies Installation |
| 22 | +------------ |
| 23 | +The GoDice Python API is dependent on the [Bleak](https://github.com/hbldh/bleak) library for bluetooth (BLE) communication with the dice |
| 24 | + |
| 25 | +You can install bleak by using this command: |
| 26 | + |
| 27 | + $ pip install bleak |
| 28 | + or from sources ([Bleak installation docs](https://bleak.readthedocs.io/en/latest/installation.html)) |
| 29 | + |
| 30 | +Usage |
| 31 | +===== |
| 32 | +**See Main.py for more examples of usage** |
| 33 | + |
| 34 | +Discovering and connecting |
| 35 | +---- |
| 36 | +To discover dice using GoDice library: |
| 37 | +```python |
| 38 | +from godice import * |
| 39 | + |
| 40 | +# Example how to discover GoDice bluetooth devices |
| 41 | +def main(): |
| 42 | + |
| 43 | + # Discovering GoDice devices using BLE |
| 44 | + dice_devices = discover_dice() |
| 45 | +``` |
| 46 | + |
| 47 | +Connecting to a die and creating a die object (use "create_dice" and pass it a die device): |
| 48 | +```python |
| 49 | +My_die = create_dice(dice_devices[0]) |
| 50 | +``` |
| 51 | + |
| 52 | +Messages |
| 53 | +----------- |
| 54 | +Activating LEDs: |
| 55 | + |
| 56 | +```python |
| 57 | +# Turn On/Off RGB LEDs, will turn off if led1 and led2 are None |
| 58 | +# led1 - a list to control the 1st LED in the following format '[R, G, B]' |
| 59 | +# where R, G, and B are numbers in the range of 0-255 |
| 60 | +# led2 - same as led1 fot the second led |
| 61 | + |
| 62 | +set_led(led1: list, led2: list) |
| 63 | +``` |
| 64 | + |
| 65 | +```python |
| 66 | +# Pulses the die's leds for set time |
| 67 | +# pulse_count - How many pulses |
| 68 | +# on_time - How much time to spend on (units of 10 ms) |
| 69 | +# off_time - How much time to spend off (units of 10 ms) |
| 70 | +# rgb - List of RGB values to set die to pulse to |
| 71 | + |
| 72 | +pulse_led(pulse_count, on_time, off_time, rgb) |
| 73 | +``` |
| 74 | + |
| 75 | +Requests |
| 76 | +----------- |
| 77 | +(Instanced methods of GoDice object) |
| 78 | +```python |
| 79 | +# Sends request for color of die |
| 80 | +send_color_request() |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | +```python |
| 85 | +# Changes die type (shell) |
| 86 | +# new_die_type - DieType Enum: |
| 87 | +# D6 = 0 (default) |
| 88 | +# D20 = 1, |
| 89 | +# D10 = 2, |
| 90 | +# D10x = 3, |
| 91 | +# D4 = 4, |
| 92 | +# D8 = 5, |
| 93 | +# D12 = 6 |
| 94 | +# For example: set_die_type(DieType.D20) |
| 95 | + |
| 96 | +set_die_type(new_die_type) |
| 97 | +``` |
| 98 | + |
| 99 | +Reading responses |
| 100 | +----------- |
| 101 | +Each die object has a "result_queue" attribute. |
| 102 | +Whenever a response/message is sent to the die, it's resposne code (if it has one) and it's value placed in the result queue as a tuple. |
| 103 | + |
| 104 | +Example for iterating throught the queue: |
| 105 | +```python |
| 106 | +while not die_object.result_queue.empty(): |
| 107 | + |
| 108 | + current_result = die_object.result_queue.get() |
| 109 | + |
| 110 | + result_code = current_result[0] |
| 111 | + if result_code == "S": |
| 112 | + value = current_result[1] |
| 113 | + # Do something |
| 114 | + . |
| 115 | + . |
| 116 | + . |
| 117 | +``` |
| 118 | + |
| 119 | +**Possible 'Results In Queue' (tuples)** |
| 120 | + |
| 121 | +("R" - On roll start, No value) |
| 122 | + |
| 123 | +("S" - Stable event, Value of die) |
| 124 | + |
| 125 | +("TS" - Tilt stable event, Value of die) |
| 126 | + |
| 127 | +("MS" - Move stable event, Value of die) |
| 128 | + |
| 129 | +("FS" - Fake stable event, Value of die) |
| 130 | + |
| 131 | +("B" - Battery response, Battery charge precent: 0-100) |
| 132 | + |
| 133 | +("C" - Color response, ID of color of die: 0-5) |
| 134 | + |
| 135 | +```python |
| 136 | +# Black 0 |
| 137 | +# Red 1 |
| 138 | +# Green 2 |
| 139 | +# Blue 3 |
| 140 | +# Yellow 4 |
| 141 | +# Orange 5 |
| 142 | +``` |
0 commit comments