# doudar/smartspin2k architecture

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

- Interactive diagram: https://gitdiagram.com/doudar/smartspin2k
- Repository: https://github.com/doudar/smartspin2k

## Overview

##### Main Components Identification

a. Hardware Layer:
- Physical Components (PCB, Case, Mechanical Parts)
- Stepper Motor System
- Resistance Control Mechanism
- Bike Mount Interface
- Shifter Controls

b. Firmware/Software Layer:
- ESP32 Microcontroller Core
- BLE Services (Multiple services identified in /include/BLE_*)
- Web Server Components
- Sensor Collection System
- ERG Mode Controller

c. Communication Interfaces:
- Bluetooth Low Energy (BLE)
- WiFi Connectivity
- HTTP/WebSocket Services
- UDP Communication

**[User Interface Layer]**
- Mobile App Interface
- Web Interface (data/*.html files)
- Physical Controls (Shifters)

**[Application Layer]**
- BLE Services Manager
- HTTP Server
- WebSocket Server
- ERG Mode Controller
- Sensor Collector

**[Hardware Control Layer]**
- Motor Controller
- Sensor Interfaces
- Power Management
- Physical Feedback Systems

**[Physical Layer]**
- PCB Components
- Mechanical Assembly
- Bike Integration

##### Key Relationships

- Data flow between BLE services and external apps (Zwift, TrainerRoad)
- Sensor data collection and processing pipeline
- Control signals from user inputs to motor actions
- Power and feedback loops in the ERG mode system

##### Implementation Details

- The modular nature of the BLE services (/include/BLE_*)
- The web interface components (/data/*.html)
- The sensor collection system (SensorCollector.cpp)
- The physical integration with various bike models (Hardware/Common Assets)

##### Documentation Elements

Include:
- Version information
- Key component specifications
- Interface descriptions
- Power requirements

Remember to include notes about the different hardware versions (V1-V3) and their specific characteristics, as the project has evolved through multiple iterations.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "User Interface Layer"
        UI_Mobile["Mobile Apps\n(Zwift/TrainerRoad)"]
        UI_Web["Web Interface"]:::web
        UI_Physical["Physical Controls\n(Shifters)"]
    end

    subgraph "Application Layer"
        BLE_Manager["BLE Services Manager"]:::ble
        HTTP_Server["HTTP Server"]:::server
        WS_Server["WebSocket Server"]:::server
        ERG_Controller["ERG Mode Controller"]:::control
        Sensor_Collector["Sensor Collector"]:::sensor
        UDP_Service["UDP Service"]:::comm
        Main["Main Application"]:::core
        Settings["System Settings"]:::core
        Logger["Logging System"]:::core
    end

    subgraph "Hardware Control Layer"
        Motor_Control["Motor Controller"]:::hw
        Sensor_Interface["Sensor Interfaces"]:::hw
        Power_Mgmt["Power Management"]:::hw
        Feedback["Physical Feedback Systems"]:::hw
    end

    subgraph "Physical Layer"
        PCB["PCB Components"]:::hw
        Mech["Mechanical Assembly"]:::hw
        Bike["Bike Integration"]:::hw
    end

    %% Relationships
    UI_Mobile <--> BLE_Manager
    UI_Web <--> |HTTP/WS| HTTP_Server
    UI_Web <--> |WebSocket| WS_Server
    UI_Physical --> Sensor_Interface

    BLE_Manager --> Main
    HTTP_Server --> Main
    WS_Server --> Main
    UDP_Service --> Main
    Main --> Settings
    Main --> Logger

    Main --> ERG_Controller
    Main --> Sensor_Collector
    
    Sensor_Collector --> Sensor_Interface
    ERG_Controller --> Motor_Control
    
    Motor_Control --> PCB
    Sensor_Interface --> PCB
    Power_Mgmt --> PCB
    
    PCB --> Mech
    Mech --> Bike
    Feedback --> Bike

    %% Click Events
    click UI_Web "https://github.com/doudar/SmartSpin2k/blob/develop/data/*.html"
    click HTTP_Server "https://github.com/doudar/SmartSpin2k/blob/develop/src/HTTP_Server_Basic.cpp"
    click WS_Server "https://github.com/doudar/SmartSpin2k/blob/develop/src/WebsocketAppender.cpp"
    click ERG_Controller "https://github.com/doudar/SmartSpin2k/blob/develop/src/ERG_Mode.cpp"
    click Sensor_Collector "https://github.com/doudar/SmartSpin2k/blob/develop/src/SensorCollector.cpp"
    click UDP_Service "https://github.com/doudar/SmartSpin2k/blob/develop/src/UdpAppender.cpp"
    click Main "https://github.com/doudar/SmartSpin2k/blob/develop/src/Main.cpp"
    click Settings "https://github.com/doudar/SmartSpin2k/blob/develop/include/settings.h"
    click Logger "https://github.com/doudar/SmartSpin2k/blob/develop/src/SS2KLog.cpp"
    click BLE_Manager "https://github.com/doudar/SmartSpin2k/blob/develop/include/BLE_*.h"
    click PCB "https://github.com/doudar/SmartSpin2k/tree/develop/Hardware/V3 - Integrated PCB/PCB/SS2K_KiCad_Files"
    click Mech "https://github.com/doudar/SmartSpin2k/tree/develop/Hardware/Common Assets"

    %% Styles
    classDef hw fill:#B0BEC5,stroke:#455A64
    classDef comm fill:#2196F3,stroke:#1976D2
    classDef control fill:#4CAF50,stroke:#388E3C
    classDef web fill:#FF9800,stroke:#F57C00
    classDef sensor fill:#9C27B0,stroke:#7B1FA2
    classDef server fill:#00BCD4,stroke:#0097A7
    classDef ble fill:#3F51B5,stroke:#303F9F
    classDef core fill:#FFC107,stroke:#FFA000
```
