Skip to main content

GoDice Python API

Overview

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

PyPI version

Supported features:

  • Turn ON/OFF GoDice RGB LEDs
  • Read color (dots color)
  • Read battery level
  • Get notifications regarding the die state (Rolling or Stable and get the outcome number)
  • Use and configure different shells (D6, D20, D12 etc.)

Installation

pip install godice

Demo

Package includes a demo script showing up API features. Command to run it:

python -m godice.demo

It will discover GoDice devices nearby and connect to a closest one. Then it setups a dice color and starts listening to dice position changes, outputting a new number each time a dice is flipped.

Usage

One can import and use the API from any custom Python script like below

import asyncio
import bleak
import godice


async def main():
    mac = "00:00:00:00:00:00"

    # Python context manager (async with) is used for convenient connection handling
    # Device stays connected during `async with` block execution and auto-disconnected on block finish
    # Otherwise, dice.connect/dice.disconnect can be used instead 
    async with godice.create(mac, godice.Shell.D6, timeout=30) as dice:
		print("Connected")
        blue_rgb = (0, 0, 255)
        yellow_rgb = (255, 255, 0)
        off_rgb = (0, 0, 0)
        await dice.set_led(blue_rgb, yellow_rgb)

        color = await dice.get_color()
        battery_lvl = await dice.get_battery_level()
        print(f"Color: {color}")
        print(f"Battery: {battery_lvl}")
        
        print("Listening to position updates. Flip your dice")
        await dice.subscribe_number_notification(notification_callback)
        await asyncio.sleep(30)
        await dice.set_led(off_rgb, off_rgb)


async def notification_callback(number, stability_descr):
    """
    GoDice number notification callback.
    Called each time GoDice is flipped, receiving flip event data:
    :param number: a rolled number
    :param stability_descr: an additional value clarifying device movement state, ie stable, rolling...
    """
    print(f"Number: {number}, stability descriptor: {stability_descr}")


asyncio.run(main())

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

godice-0.1.2.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

godice-0.1.2-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file godice-0.1.2.tar.gz.

File metadata

  • Download URL: godice-0.1.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.2

File hashes

Hashes for godice-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f5f41b5984afe0e9faae520dec3a76a2ec461e8d401eb538259c631c401d4e86
MD5 fbf7c651e58bb1a91c219bd8721bd270
BLAKE2b-256 372f88b827f58744e0e5c2b05b061861ab7bb92671fa290a6f0cd10da50e31a9

See more details on using hashes here.

File details

Details for the file godice-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: godice-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.2

File hashes

Hashes for godice-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f2dd406fa9fdecf6f0449c108bbb3b878e3b96dddbb29760b66513d3dc37133c
MD5 a79a2eea0d69e8d612b47e8950804ee3
BLAKE2b-256 1a480f5e4cbd5f4f28267750a782949ecc9eb5cd0816f94a03ece38b77e25c20

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page