# mab5vot9us9a/mfrc522-python architecture

> Architecture diagram of the GitHub repository mab5vot9us9a/mfrc522-python, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mab5vot9us9a/mfrc522-python
- Repository: https://github.com/mab5vot9us9a/mfrc522-python

## Overview

##### Project type and purpose
- This is an open-source Python hardware-interface library and example suite.
- Purpose: expose a clean Python API (class MFRC522) on top of low-level SPI to read/write NFC tags.

##### Main components
a. Hardware Layer
- MFRC522 RFID/NFC reader module (external SPI hardware)
- Raspberry Pi (physical board hosting Linux and /dev/spidev)
b. OS and Drivers
- Linux Kernel SPI driver (exposes /dev/spidev0.0, /dev/spidev0.1)
c. Native Python SPI Binding
- SPI-Py (C extension installed with setup.py)
d. MFRC522-python Library
- MFRC522.py (core class implementing protocol)
- errors.py (error definitions)
e. Application Layer (Example Scripts)
- Dump.py, Read.py, Write.py, playground.py (demo and usage examples)
f. Development / Repo Metadata
- README.md, .gitignore (repo description and config)

##### Relationships and data flow
- Demo Script → MFRC522-python API → SPI-Py → /dev/spidev → MFRC522 hardware
- MFRC522 hardware → SPI bus → /dev/spidev → SPI-Py → MFRC522-python → Demo Script
- MFRC522-python uses errors.py for exception handling and xterm256_Colors.py to colorize console output.

##### Architectural patterns and design principles
- Layered architecture:
1. Hardware
2. Kernel driver
3. Native binding (SPI-Py)
4. Pure-Python protocol implementation (MFRC522 class)
5. Application scripts
- Single Responsibility: MFRC522.py only handles protocol logic; SPI-Py only handles raw SPI transfer; example scripts only demo usage.
- Clean dependency flow: each layer only depends on the one directly below.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Application Layer
    subgraph "Application Layer"
        direction TB
        Dump["Dump.py"]:::apps
        Read["Read.py"]:::apps
        Write["Write.py"]:::apps
        Playground["playground.py"]:::apps
    end

    %% Library Layer
    subgraph "MFRC522-python Library"
        direction TB
        MFRC522["MFRC522.py"]:::library
        Errors["errors.py"]:::library
        Colors["xterm256_Colors.py"]:::library
    end

    %% Native Binding Layer
    SPI["SPI-Py (C Extension)"]:::cext

    %% Kernel Driver Layer
    Dev["/dev/spidev0.0"]:::kernel

    %% Hardware Layer
    HW["MFRC522 RFID Reader"]:::hardware

    %% Metadata
    subgraph "Repository Metadata"
        direction TB
        Readme["README.md"]:::metadata
        Gitignore[".gitignore"]:::metadata
    end

    %% Data Flow
    Dump -->|"calls API"| MFRC522
    Read -->|"calls API"| MFRC522
    Write -->|"calls API"| MFRC522
    Playground -->|"calls API"| MFRC522

    MFRC522 -->|"uses Errors"| Errors
    MFRC522 -->|"uses Colors"| Colors

    MFRC522 -->|"calls SPI transfer"| SPI
    SPI -->|"ioctl read/write"| Dev
    Dev -->|"SPI signals"| HW

    HW -->|"data response"| Dev
    Dev -->|"raw data"| SPI
    SPI -->|"returns bytes"| MFRC522
    MFRC522 -->|"returns objects"| Dump
    MFRC522 -->|"returns objects"| Read
    MFRC522 -->|"returns objects"| Write
    MFRC522 -->|"returns objects"| Playground

    %% Click Events
    click MFRC522 "https://github.com/mab5vot9us9a/mfrc522-python/blob/master/MFRC522.py"
    click Errors "https://github.com/mab5vot9us9a/mfrc522-python/blob/master/errors.py"
    click Colors "https://github.com/mab5vot9us9a/mfrc522-python/blob/master/xterm256_Colors.py"
    click Dump "https://github.com/mab5vot9us9a/mfrc522-python/blob/master/Dump.py"
    click Read "https://github.com/mab5vot9us9a/mfrc522-python/blob/master/Read.py"
    click Write "https://github.com/mab5vot9us9a/mfrc522-python/blob/master/Write.py"
    click Playground "https://github.com/mab5vot9us9a/mfrc522-python/blob/master/playground.py"
    click Readme "https://github.com/mab5vot9us9a/mfrc522-python/blob/master/README.md"
    click Gitignore "https://github.com/mab5vot9us9a/mfrc522-python/blob/master/.gitignore"

    %% Styles
    classDef hardware fill:#blue,stroke:#000,color:#fff;
    classDef kernel fill:#bbb,stroke:#333,color:#000;
    classDef cext fill:#6f9,stroke:#070,color:#000;
    classDef library fill:#f96,stroke:#963,color:#000;
    classDef apps fill:#ffec99,stroke:#aa7,color:#000;
    classDef metadata fill:#ddd,stroke:#999,color:#000;
```
