# nazo6/rktk architecture

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

- Interactive diagram: https://gitdiagram.com/nazo6/rktk
- Repository: https://github.com/nazo6/rktk

## Overview

##### Main Architectural Components
- Core Firmware (rktk crate): This is the heart of the system where configuration, task scheduling, hooks, interfaces, and the main loop reside. It wires up all functionality and provides the abstraction layer over the hardware.
- Hardware Driver Modules:
– rktk-drivers-common: Contains shared drivers for key scan, mouse, display, USB, etc.
– rktk-drivers-nrf and rktk-drivers-rp: Provide platform-specific implementations (for nRF52840 and RP2040, respectively), handling things like split communication, native display support, and so on.
- Key Manager (rktk-keymanager): Implements the key mapping features including tap-hold, tap dance, oneshot keys, and combo keys. It is independent of the core firmware so that the key mapping logic is isolated.
- Communication and Logging Modules:
– rktk-rrp: Handles host communication and remote procedure calls.
– rktk-log: Manages logging and debugging outputs.
- External Tools and Clients:
– The tools directory includes both a CLI tool (for configuration, testing, releasing) and a graphical client (rktk-client) that likely provides a UI for remapping and interacting with the firmware.

##### Relationships and Data Flows
- The core rktk component uses these inputs to drive the main loop (see the tasks in lib/rktk/src/task) and to trigger processing across the firmware.
- The key manager converts raw input events into higher-level actions (e.g., keycodes, modifier actions, layer controls) and feeds these into the system.
- Communication components (rktk-rrp) and logging (rktk-log) handle sending data to external hosts or for diagnostics.
- External tools (rktk-client and CLI tools) interact with the firmware via interfaces like USB or Bluetooth, allowing live reconfiguration or debugging.

##### Architectural Patterns
- Layered/Modular Architecture: Each major facet of the system (hardware abstraction, firmware core, key mapping, communication) is implemented as its own crate/module, ensuring separation of concerns.
- Abstraction and Extensibility: The division between core functionality and drivers makes it straightforward to extend the support to additional hardware platforms.
- Task-based Concurrency: The project uses dedicated task modules (for display, logging, key scanning, etc.) that likely communicate via channels, which is important for real-time responsiveness on keyboard firmware.

##### Technologies, Frameworks, and Libraries
- Rust: The entire project is built in Rust with heavy usage of Cargo workspaces and crates for modularization.
- Embedded Rust Toolchains: Considering the embedded targets (nRF52840 and RP2040), expect use of platform-specific HALs and features optimized for embedded development.
- USB, Bluetooth, and low-level hardware interfaces are supported through these drivers, using appropriate libraries and language idioms for embedded systems.

- Each primary component (rktk core, individual driver crates, key manager, communication/logging, external tools).
– Data/event flow from hardware (input) through drivers to core logic.
– Processing flow from core logic to key mapping and out to communication interfaces.
- Annotations or notes capturing the purpose of each module (e.g., “platform-specific driver for RP2040”, “key mapping and combo resolution”).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Hardware Drivers"
        Common["rktk-drivers-common (Shared Drivers)"]
        nRF["rktk-drivers-nrf (nRF52840 Platform)"]
        RP["rktk-drivers-rp (RP2040 Platform)"]
    end

    subgraph "Core Firmware"
        CF["rktk (Core Firmware)"]
    end

    subgraph "Key Mapping"
        KM["rktk-keymanager (Key Manager)"]
    end

    subgraph "Communication & Logging"
        RRP["rktk-rrp (Communication Module)"]
        LOG["rktk-log (Logging Module)"]
    end

    subgraph "External Tools"
        CLI["CLI Tool"]
        Client["rktk-client (Graphical Client)"]
    end

    %% Connections from Hardware Drivers to Core Firmware
    Common --> CF
    nRF    --> CF
    RP     --> CF

    %% Core Firmware to Key Mapping and Communication/Logging
    CF --> KM
    CF --> RRP
    CF --> LOG

    %% Communication modules interacting with External Tools
    RRP -->|"host_comm"| CLI
    RRP -->|"host_comm"| Client
    LOG -->|"debug_logs"| CLI

    %% External Tools also interact with Core Firmware for configuration and remapping
    CLI -->|"configure"| CF
    Client -->|"UI_remap"| CF

    %% Styles and Colors
    classDef drivers fill:#cfe2f3,stroke:#333,stroke-width:2px;
    classDef core fill:#d9ead3,stroke:#333,stroke-width:2px;
    classDef key fill:#fff2cc,stroke:#333,stroke-width:2px;
    classDef comm fill:#fce5cd,stroke:#333,stroke-width:2px;
    classDef external fill:#ead1dc,stroke:#333,stroke-width:2px;

    class Common,nRF,RP drivers;
    class CF core;
    class KM key;
    class RRP,LOG comm;
    class CLI,Client external;

    %% Click Events
    click CF "https://github.com/nazo6/rktk/tree/master/lib/rktk"
    click Common "https://github.com/nazo6/rktk/tree/master/lib/rktk-drivers-common"
    click nRF "https://github.com/nazo6/rktk/tree/master/lib/rktk-drivers-nrf"
    click RP "https://github.com/nazo6/rktk/tree/master/lib/rktk-drivers-rp"
    click KM "https://github.com/nazo6/rktk/tree/master/lib/rktk-keymanager"
    click RRP "https://github.com/nazo6/rktk/tree/master/lib/rktk-rrp"
    click LOG "https://github.com/nazo6/rktk/tree/master/lib/rktk-log"
    click CLI "https://github.com/nazo6/rktk/tree/master/tools/cli"
    click Client "https://github.com/nazo6/rktk/tree/master/tools/rktk-client"
```
