# pre-tesh/ps4-esp32 architecture

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

- Interactive diagram: https://gitdiagram.com/pre-tesh/ps4-esp32
- Repository: https://github.com/pre-tesh/ps4-esp32

## Overview

##### Main Components
- Examples / Application Layer: The “examples” directory contains multiple Arduino sketches (e.g., PS4ReceiveData, PS4SendData, Ps4Connect, PS4ViewIncomingBits) that act as the user interface for demonstrating the library.
- Library API (PS4Controller): The C++ wrapper in PS4Controller.cpp/PS4Controller.h provides a friendly API for Arduino sketches to interact with the underlying functionalities.
- PS4 Protocol/Communication Layer: In the “src” directory, the core functionality for handling PS4 controller communication resides. This includes:
- General PS4 protocol logic (files like ps4.c, ps4.h, ps4_int.h)
- Bluetooth channel-specific modules such as ps4_l2cap.c (for L2CAP channel communication) and ps4_spp.c (for SPP handling)
- Parsing and data interpretation as seen in ps4_parser.c.
- OSI & Stack Utilities:
- The “src/osi” folder (allocator.h) provides OS abstraction functions, probably for dynamic memory management or other low-level operations.
- The “src/stack” folder includes several header files defining data types and APIs related to the Bluetooth stack (e.g., bt_types.h, btm_api.h, gap_api.h, l2c_api.h, etc.). These files underpin the interaction with ESP32’s Bluetooth hardware.

##### Relationships and Interactions
- The Arduino sketches from the “examples” directory interface with the PS4Controller API. This API abstracts and orchestrates communication between the application layer and the lower-level protocol management layers.
- PS4Controller internally calls functions implemented in the PS4 protocol modules (in C and C++ in the “src” folder) to manage connection establishment, protocol parsing, and command/data flow.
- The Bluetooth stack and OSI utilities (in “src/stack” and “src/osi”) provide necessary infrastructure, offering standard APIs, memory allocation, and data type definitions to manage Bluetooth connectivity and lower-level protocol operations.
- A directional data flow exists: From user application (Arduino sketch) → PS4Controller API → PS4 protocol logic (which in turn interacts with the Bluetooth stack and OSI-level utilities) → Communication with the PS4 Controller.

##### Architectural Patterns & Design Principles
- Modularity & Abstraction: The separation between high-level API (PS4Controller) and low-level protocol handling (ps4.c, ps4_l2cap.c, etc.) demonstrates a layered design.
- Reusability: By structuring the code into well-defined modules (API vs. core protocol vs. OS/stack utilities), the library can be easily integrated and possibly extended.
- Clear Separation of Concerns: The project cleanly divides the Arduino-friendly interface (used in sketches and user code) from the underlying Bluetooth communication and protocol management.
- Integration With Existing Bluetooth Stacks: The “src/stack” directory clearly shows integration with ESP32’s Bluetooth stack through specific header files.

##### Including Relevant Technologies & Frameworks
- Arduino Platform: The examples and library integration with Arduino IDE are crucial (indicated by the .ino files and installation instructions).
- ESP32: The entire library is built specifically for the ESP32’s hardware and Bluetooth capabilities.
- Bluetooth Protocols: The project handles specific Bluetooth protocols (e.g., L2CAP, SPP) in its internal modules.
- C and C++: The file extensions and separation indicate a mixed use of C (for performance-critical and lower-level functions) and C++ (for API abstraction).

- Directly below, show the PS4Controller module as the API gateway.
- Below this, lay out the PS4 protocol modules handling different channels:

## Diagram (Mermaid)

```mermaid
flowchart TD
    A1("Application Layer (Arduino Sketch Examples)"):::application
    B1("PS4 Controller API (PS4Controller)"):::api

    subgraph "Protocol Modules"
        C1("PS4 Protocol - General Logic"):::protocol
        C2("PS4 Protocol - L2CAP Handling"):::protocol
        C3("PS4 Protocol - SPP Handling"):::protocol
        C4("PS4 Protocol - Data Parsing"):::protocol
    end

    subgraph "OS and Bluetooth Stack"
        D1("OSI Utilities"):::protocol
        D2("Bluetooth Stack Integration"):::protocol
    end

    E1("ESP32 Hardware (Bluetooth Interface)"):::hardware

    %% Connections
    A1 -->|"calls"| B1
    B1 -->|"invokes"| C1
    B1 -->|"invokes"| C2
    B1 -->|"invokes"| C3
    B1 -->|"invokes"| C4
    C1 -->|"uses"| D1
    C1 -->|"uses"| D2
    C2 -->|"uses"| D1
    C2 -->|"uses"| D2
    C3 -->|"uses"| D1
    C3 -->|"uses"| D2
    C4 -->|"uses"| D1
    C4 -->|"uses"| D2
    D1 -->|"supports"| E1
    D2 -->|"interfaces"| E1

    %% Click Events
    click A1 "https://github.com/pre-tesh/ps4-esp32/tree/master/examples/"
    click B1 "https://github.com/pre-tesh/ps4-esp32/blob/master/src/PS4Controller.cpp"
    click B1 "https://github.com/pre-tesh/ps4-esp32/blob/master/src/PS4Controller.h"
    click C1 "https://github.com/pre-tesh/ps4-esp32/blob/master/src/ps4.c"
    click C1 "https://github.com/pre-tesh/ps4-esp32/blob/master/src/ps4.h"
    click C1 "https://github.com/pre-tesh/ps4-esp32/blob/master/src/ps4_int.h"
    click C2 "https://github.com/pre-tesh/ps4-esp32/blob/master/src/ps4_l2cap.c"
    click C3 "https://github.com/pre-tesh/ps4-esp32/blob/master/src/ps4_spp.c"
    click C4 "https://github.com/pre-tesh/ps4-esp32/blob/master/src/ps4_parser.c"
    click D1 "https://github.com/pre-tesh/ps4-esp32/blob/master/src/osi/allocator.h"
    click D2 "https://github.com/pre-tesh/ps4-esp32/tree/master/src/stack/"

    %% Styles
    classDef application fill:#a8e6cf,stroke:#56ab2f,stroke-width:2px;
    classDef api fill:#a2d5f2,stroke:#0173b2,stroke-width:2px;
    classDef protocol fill:#ffd3b6,stroke:#ff8c00,stroke-width:2px;
    classDef hardware fill:#d3d3d3,stroke:#808080,stroke-width:2px;
```
