An intelligent electronic chessboard that detects piece movements in real-time using Raspberry Pi and MCP23017 GPIO expanders, with integrated chess engine and game management features.
- Real-time Piece Detection: Hardware-based piece movement detection using MCP23017 GPIO expanders
- Chess Engine Integration: Full chess rule validation using the
python-chesslibrary - Game Management: Automatic PGN export with timestamps and game metadata
- LCD Display Support: Real-time game state display with menu system
- Move Parsing: Intelligent parsing of piece up/down events to determine valid chess moves
- Event Stack Processing: Advanced event processing with
RpiChessStackfor complex move sequences - Special Move Support: Handles castling, captures, promotions, and piece dragging
- Move Reversion: Automatic move reversion when pieces are returned to original positions
- Modular Design: Clean separation between chess engine and hardware interface
echess/
โโโ src/ # Source code directory
โ โโโ engine/ # Chess engine module
โ โ โโโ board.py # Core chess engine with electronic board support
โ โ โโโ stack.py # Event processing stack for piece movements
โ โโโ rpi/ # Raspberry Pi hardware interface
โ โโโ main.py # Main hardware interface and game loop
โ โโโ i2c_lcd.py # I2C LCD display driver
โ โโโ menu.py # Menu system for LCD
โโโ tests/ # Test suites
โ โโโ engine/ # Chess engine tests
โ โโโ rpi/ # Hardware interface tests
โโโ pgn/ # Sample PGN games
โโโ exports/ # Generated PGN game exports
โโโ pyproject.toml # Project configuration and dependencies
โโโ Makefile # Development and build commands
- Python 3.11+
- Raspberry Pi (for hardware interface)
- MCP23017 GPIO expanders
- I2C LCD display (optional)
-
Install uv (if not already installed)
curl -LsSf https://astral.sh/uv/install.sh | sh -
Clone the repository
git clone <repository-url> cd echess
-
Install dependencies
# Core dependencies only (works on any platform) make install # or directly: uv sync # With hardware support (Raspberry Pi only) make install-hardware # or directly: uv sync --extra hardware
-
Set up development environment (optional)
# Development tools only make setup-dev # or directly: uv sync --extra dev # Full environment with hardware support make setup-full # or directly: uv sync --extra hardware --extra dev
-
Install pre-commit hooks (recommended)
make pre-commit-install # or directly: uv run pre-commit install
- Test chess engine:
make run-chess - Run on Raspberry Pi:
make run-rpi(requires hardware) - Test LCD display:
make test-lcd
The project includes comprehensive tests for both the chess engine and hardware interface:
# Run all tests
make test
# Run specific test suites
make test-chess # Chess engine tests
make test-rpi # Raspberry Pi tests- Chess Engine Tests: Move validation, game state management, PGN export
- Event Stack Tests: Comprehensive testing of
RpiChessStackwith 20+ test cases covering:- Basic moves and captures
- Move reversion and intermediate events
- Complex move sequences and edge cases
- Castling, promotion, and special moves
- Hardware Interface Tests: GPIO parsing, event queue processing
- Integration Tests: End-to-end game scenarios including special moves
# Format code with ruff
make format
# Run linting with ruff and mypy
make lint
# Install pre-commit hooks
make pre-commit-install
# Run pre-commit on all files
make pre-commit-run
# Update pre-commit hooks
make pre-commit-update
# Full pre-commit checks
make pre-commitsrc/engine/board.py: Core chess engine with electronic board support (ChessBoardclass)src/engine/stack.py: Event processing stack for piece movements (RpiChessStackclass)src/rpi/main.py: Hardware interface and main game loopsrc/rpi/i2c_lcd.py: I2C LCD display driversrc/rpi/menu.py: Menu system for LCD navigation
- Hardware Interface: MCP23017 GPIO expanders monitor each square
- Event Processing: Piece up/down events are captured and processed by
RpiChessStack - Move Parsing: Events are intelligently parsed to determine valid chess moves
- Validation: Moves are validated against chess rules using
python-chess - Move Reversion: Automatic reversion when pieces are returned to original positions
- Initialization: Board state is read from hardware matrix and
ChessBoardis created - Event Loop: Continuous monitoring for piece movements via
RpiChessStack - Move Processing: Events are intelligently parsed and moves are executed on
ChessBoard - Game Management: Game state is tracked and displayed on LCD
- Export: Games are automatically saved as PGN files with timestamps
- Piece Dragging: Supports piece dragging with intermediate positions
- Capture Detection: Handles complex capture sequences
- Castling: Automatic castling detection and validation
- Promotion: Automatic queen promotion (configurable)
- Game Over Detection: Checkmate, stalemate, and draw detection
- Raspberry Pi (3B+ or 4 recommended)
- MCP23017 GPIO expander chips
- Electronic chessboard with piece detection
- I2C LCD display (16x2 or 20x4)
- MCP23017 chips connected via I2C
- Each square connected to GPIO pins
- Pull-up resistors for reliable detection
- I2C LCD display for game state
Edit src/rpi/main.py to configure:
- Number of MCP23017 chips (
NUM_MCP) - I2C device addresses (
DEVICE) - GPIO pin assignments
Edit src/engine/board.py to configure:
- Starting position (
STANDARD_STARTING_FEN) - Board matrix mapping (
STANDARD_STARTING_POS) - Promotion piece (default: Queen)
- Import Errors: Ensure all dependencies are installed with
make install - Hardware Detection: Check I2C connections and device addresses
- Move Validation: Verify piece detection matrix configuration
- LCD Display: Check I2C address and wiring
Enable debug mode in ChessBoard initialization:
board = ChessBoard(matrix=matrix, debug=True)This project is licensed under the MIT License - see the LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests and linting:
make pre-commit - Commit using conventional commits:
uv run cz commit - Submit a pull request
This project uses Conventional Commits for consistent commit messages:
# Use commitizen for guided commits
uv run cz commit
# Or manually follow the format:
# type(scope): description
#
# Examples:
# feat(engine): add castling support
# fix(rpi): resolve GPIO parsing issue
# docs: update README with installation steps
# test: add unit tests for event queueThe project includes comprehensive pre-commit hooks that run automatically on every commit:
- Ruff: Fast linting and formatting
- MyPy: Type checking
- Bandit: Security vulnerability scanning
- Pre-commit hooks: File validation and cleanup
- Commitizen: Commit message validation
ChessBoard: Extended chess board with electronic board supportRpiChessStack: Event processing stack for piece movementsEvent: Data class representing piece up/down eventspush(): Execute moves from hardware interfacesave_game(): Export game to PGN format
main(): Main game loop and hardware initializationparse_input(): Parse GPIO events to chess movesinit(): Initialize MCP23017 GPIO expandersrun(): Main event loop for piece detection
- Computer opponent integration
- Online game support
- Mobile app interface
- Advanced analytics
- Voice commands
- Tournament mode
Note: This project requires actual hardware to function. The chess engine can be tested independently, but the full electronic chessboard functionality requires the Raspberry Pi setup with MCP23017 GPIO expanders.