Hardware package

The hardware package contains all software required to interface with the hardware

Hardware.HardwareInterface

class Hardware.HardwareInterface.Offer(value)[source]

Bases: enum.Enum

An enumeration.

CONTINUE = 0
DRAW = 1
RESIGN = 2
class Hardware.HardwareInterface.HardwareInterface[source]

Bases: abc.ABC

Defines interface to the hardware

abstract mark_squares(squares: List[List[bool]])None[source]

Marks squares on the chessboard where squares is an 8x8 matrix implemented as a 2s list

Note that squares are mapped as squares[file][rank] ie. a1 = squares[0][0], a2 = squares[0][1], b1 = squares[1][0] and h8 = squares[7][7]

Parameters

squares – 8x8 matrix of squares to mark on the chessboard where square [file][rank] is marked if square[file][rank] == TRUE

abstract get_occupancy()List[List[bool]][source]

Returns all occupied squares as 8x8 matrix implemented as a 2d list

Note that squares are mapped as squares[file][rank] so if square a2 is occupied then get_occupancy[0][1] equals TRUE :return: 8x8 matrix with all occupied squares on the chessboard

promotion_piece()chess.Piece[source]

Which piece to promote to

Returns

Piece to promote to if not reimplemented returns queen

game_end_offers()Hardware.HardwareInterface.Offer[source]

Returns continue, draw or return offers

Returns

Always returns continue

display(txt: str)[source]

Displays text string on hardware

Parameters

txt – text to display on hardware

Hardware.HardwareImplementation

class Hardware.HardwareImplementation.HardwareImplementation[source]

Bases: Hardware.HardwareInterface.HardwareInterface

Interface to Hardware chessboard

mark_squares(squares: List[List[bool]])None[source]

Marks squares on the chessboard where squares is an 8x8 matrix implemented as a 2s list

Note that squares are mapped as squares[file][rank] ie. a1 = squares[0][0], a2 = squares[0][1], b1 = squares[1][0] and h8 is squares[7][7]

Parameters

squares – 8x8 matrix of squares to mark on the chessboard where square [file][rank] is marked if square[file][rank] == TRUE

get_occupancy()List[List[bool]][source]

Returns all occupied squares as 8x8 matrix implemented as a 2d list

Note that squares are mapped as squares[file][rank] so if square a2 is occupied then get_occupancy[0][1] is TRUE

Returns

8x8 matrix with all occupied squares on the chessboard

promotion_piece()chess.Piece[source]

Which piece to promote to

Reads input button to select promotion piece, writes selected piece to display and waits for confirmation

Returns

Piece to promote to

game_end_offers()Hardware.HardwareInterface.Offer[source]

Returns continue, draw or return offers

If no button pressed returns continue, otherwise wait for confirmation

Returns

Always returns continue

display(txt: str)[source]

Displays text string on hardware

Parameters

txt – text to display on hardware

class Hardware.HardwareImplementation.LedWrapper(ht16k33: adafruit_ht16k33.matrix.MatrixBackpack16x8, mcp: adafruit_mcp230xx.mcp23017.MCP23017, perform_safe)[source]

Bases: object

” Wraps LED hardware

clear()[source]

Clears all square on the chessboard

set_squares(squares: List[List[bool]])[source]

Marks squares on the chessboard

Improvement on calling clear() then marking all squires since this prevents a flickering of the LED

Parameters

squares – 8x8 matrix with the squares to be marked

Hardware.SafeDecorator

Hardware.SafeDecorator.perform_safe_factory(reset: Optional[Callable[[None], None]] = None, max_tries: int = 5)Callable[[Callable[[Any], Any]], Callable[[Any], Any]][source]

Factory to create a decorator to perform an i2c operation safely

This factory returns the perform_safe decorator. This decorator will try to execute a function until it succeeds or until max_tries have failed. If a tca is supplied then the factory will also reset the tca lock after every SOError.

Parameters
  • reset – Any other function to execute when recovering from an error

  • max_tries – Maximum amount of times that an operation will be attempted

Returns

The decorator perform safe

Hardware.SafeDecorator.perform_safe(func: Callable[[Any], Any])Callable[[Any], Any]

” Tries to execute func until no OSError is thrown or TRIES attempts have failed

The i2c buss is sensitive to noise. Corrupted messages can trigger an OSError on the buss device. We can recover from this error by simply resending the message until it arrives correctly. This method accepts a function func which could trigger an OSError which would cause the program to fail. We can easily recover from this error by retrying ‘func’ if the error is cause by noise. The perform safe decorator makes sure that an error is only trow if the operation fails max_tries times. Because of an error in the adafruit libraries the tca can soft lock after an exception. Resetting the lock to false after an exception prevents the errors

Parameters

func – function to be executed

Returns

function which executes func until it either succeeds or max_tries attempts have failed

Return type

Same as func