asyncdgt: Communicate asynchronously with DGT boards

asyncdgt uses asyncio to communicate asynchronously with a DGT electronic chess board. View reference documentation.

Example

Create an event loop and a connection to the DGT board.

import asyncio

loop = asyncio.get_event_loop()
dgt = asyncdgt.auto_connect(["/dev/ttyACM*"], loop)

Register some pyee event handlers. They will be called whenever a board gets connected, disconnected or the position changed.

@dgt.on("connected")
def on_connected(port):
    print("Board connected to {0}!".format(port))

@dgt.on("disconnected")
def on_disconnected():
    print("Board disconnected!")

@dgt.on("board")
def on_board(board):
    print("Position changed:")
    print(board)
    print()

Get some information outside of an event handler using the coroutine get_version().

print("Version:", loop.run_until_complete(dgt.get_version()))

Run the event loop.

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    dgt.close()
    loop.close()

See asyncdgt/__main__.py for the complete example. Run with python -m asyncdgt /dev/ttyACM0.

Hardware

Tested with the following boards:

  • DGT e-Board 3.1
  • DGT e-Board 3.1 Bluetooth

Clocks:

  • DGT Clock 3000

Dependencies

pip install -r requirements.txt

License

python-asyncdtg is licensed under the GPL3. See the LICENSE.txt file for the full license text.

Reference documentation

asyncdgt.connect(port_globs, loop)

Creates a asyncdgt.Connection.

Raises IOError when no board can be connected.

asyncdgt.auto_connect(port_globs, loop, max_backoff=10.0)

Creates a asyncdgt.Connection.

If no board is available or the board gets disconnected, reconnection attempts will be made with exponential backoff.

max_backoff is the maximum expontential backoff time in seconds. The exponential backoff will not be increased beyond this.

class asyncdgt.Connection(port_globs, loop)

Bases: pyee.EventEmitter

Manages a DGT board connection.

port_globs is a list of glob expressions like ["/dev/ttyACM*"]. When connecting the first successful match will be used.

loop is the asyncio event loop.

connect()

Try to connect. Returns the connected port or False.

close()

Close any open board connection.

get_version()

Get the board version.

get_board()

Get the current board position as a asyncdgt.Board.

get_serialnr()

Get the board serial number.

get_long_serialnr()

Get the long variant of the board serial number.

class asyncdgt.Board(board_fen=None)

A position on the board.

>>> board = asyncdgt.Board("rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR")
>>> print(board)
r n b k q b n r
p p p p p p p p
. . . . . . . .
. . . . . . . .
. . . P . . . .
. . . . . . . .
P P P . P P P .
R N B Q K B N R
board_fen()

Gets the FEN of the position.

>>> board = asyncdgt.Board()
>>> board.board_fen()
'8/8/8/8/8/8/8/8'
set_board_fen(fen)

Set a FEN.

clear()

Clear the board.

copy()

Get a copy of the board.