# e2002/yoradio architecture

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

- Interactive diagram: https://gitdiagram.com/e2002/yoradio
- Repository: https://github.com/e2002/yoradio

## Overview

##### Project type and purpose
- This is an embedded IoT application running on an ESP32 device. It implements a web-radio that can play Internet streams or SD-card audio via either an I2S DAC or a VS1053 audio decoder, presents a local web-UI (SPIFFS), supports hardware controls (buttons, encoder, IR, touchscreen), publishes status/control via MQTT (for Home Assistant and other clients), and exposes a Telnet/CLI interface.
##### File structure
- Top-level:
- HA/custom_components/yoradio  → Home Assistant integration
- examples/                      → Arduino example option and plugin sketches
- nextion/                       → Nextion HMI project files
- yoRadio/                       → Main Arduino project directory
- broken204/                     → Patches for Adafruit display libraries
- yoRadio/data/www/…            → Compressed HTML/CSS/JS UI files stored in SPIFFS
- yoRadio/src/                   → Device firmware source, subdivided into:
- core/    → Business logic: audio, network, config, controls, display, MQTT, telnet, SD, RTC
- displays/→ Display-driver abstractions, configurations, fonts, widget code
- AsyncWebServer, AsyncTCP, async-mqtt-client → Embedded HTTP, WebSocket, MQTT client stack
- audioI2S, audioVS1053, IRremoteESP8266, OneButton, etc. → Embedded drivers for hardware modules
- pluginsManager/ → Event-hook infrastructure for extendible plugins
- main.cpp + yoRadio.ino → Entry point and Arduino setup/loop
##### README insights
- Describes hardware variants (I2S vs VS1053, dozens of display models, buttons, encoder, IR, touchscreen, RTC)
- Describes workflow: generate myoptions.h, upload SPIFFS, flash firmware, configure WiFi via captive-portal, browse web UI, control via MQTT/HA.
- Emphasizes embedded HTTP server, SPIFFS data store, MQTT topics, Telnet/CLI, plugin architecture.
##### Main components
a. Hardware layer
- ESP32 SoC
- Audio output: I2S DAC or VS1053 module
- Device (one of many SPI/I2C LCD/OLED or Nextion touch panel)
- Controls: Buttons, rotary encoder (+ switch), IR receiver, optional touchscreen
- Storage: SD card (SPI) and SPIFFS (on-chip flash)
- RTC module (I2C DS3231/DS1307)
- LED_BUILTIN, light sensor, backlight control
b. Firmware / Software layer (running on ESP32 FreeRTOS)
- Startup & configuration loader (options.h / myoptions.h)
- Plugin manager (lifecycle & event dispatch)
- Core logic:
- AudioHandler: stream fetch, decode (I2S or VS1053), buffer management
- DisplayManager: update screen, widgets, screensaver
- NetServer: HTTP + WebSocket server (AsyncWebServer / AsyncTCP) serving SPIFFS UI
- MQTT client (async-mqtt-client) publishing and subscribing to control/status
- Telnet/CLI interface for commands
- SDCard manager (playlist import/export, file indexing)
- RTC support: NTP sync, display clock
- Control input handlers: buttons, encoder, IR, touchscreen
- File system: SPIFFS partition (web UI, playlists, WiFi list)
c. External systems
- Web browser (UI) connecting over WiFi HTTP to device’s captive-portal
- MQTT broker (local or cloud)
- Home Assistant (custom component) subscribes/publishes via MQTT
- WiFi network, captive-portal AP
##### Relationships and interactions
- On boot: core reads configuration, mounts SPIFFS, initializes hardware drivers, registers HTTP/MQTT/Telnet services, loads plugins.
- HTTP server → serves compressed UI pages from SPIFFS; WebSocket pushes status updates; REST endpoints accept config commands.
- MQTT client ↔ broker: publishes status topics, subscribes to command topic. Home Assistant custom component uses it.
- Audio pipeline: HTTP stream or SD read → AudioHandler → I2S or VS1053 → speaker
- Pipeline: DisplayManager receives events (track/volume/time) → renders via driver
- Input pipeline: button/encoder/IR/touch events → core action or plugin
- Plugins subscribe to core events via pluginsManager
- Telnet server listens on TCP port, routes commands to core
##### Architectural patterns and design principles
- Layered architecture: hardware abstraction → core services → network interfaces → plugins
- Event-driven: plugins hook into lifecycle events; WebSocket pushes updates; input triggers events
- Modular drivers: each display, audio, input driver lives in its own folder, following consistent interfaces
- Single source of configuration: myoptions.h influences hardware wiring & feature flags
- Async I/O: HTTP, WebSocket, MQTT run on separate FreeRTOS tasks/cores, non-blocking
##### Technologies, frameworks, libraries
- Platform: Arduino-ESP32 / FreeRTOS
- Networking: AsyncWebServer, AsyncTCP
- MQTT: async-mqtt-client
- Audio: ESP32-audioI2S / ESP32-vs1053_ext
- Displays: Adafruit_GFX + model-specific drivers (integrated into project), Nextion
- Input: IRremoteESP8266, OneButton, custom encoder code
- File System: SPIFFS (Sketch data) + SD (SdFat)
- RTC: RTCLib
##### Final advice
- Be exhaustive: include every peripheral, every major firmware component, and every external integration
- Event-driven nature: indicate plugin hooks, WebSocket push, MQTT pub/sub
- Validate with README: ensure each feature (web UI, remote control, HA integration, SD mode) is represented

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Systems
    subgraph "External Systems"
        style ExternalSystems fill:#FAD7A0,stroke:#E67E22
        Browser["Web Browser UI"]:::external
        Broker["MQTT Broker"]:::external
        HA["Home Assistant"]:::external
        TelClient["Telnet Client"]:::external
    end

    %% Hardware Layer
    subgraph "Hardware Layer"
        style HardwareLayer fill:#AED6F1,stroke:#3498DB
        ESP["ESP32 SoC"]:::hardware
        I2S["I2S DAC"]:::hardware
        VS1053["VS1053 Decoder"]:::hardware
        DisplayDev["Display Device"]:::hardware
        ControlsHW["Buttons/Encoder/IR"]:::hardware
        TouchHW["Touchscreen"]:::hardware
        SD_HW["SD Card"]:::hardware
        RTC_HW["RTC Module"]:::hardware
        LED_HW["LED & Sensors"]:::hardware
    end

    %% File Systems
    subgraph "Storage"
        style Storage fill:#F9E79F,stroke:#F1C40F
        SPIFFS["SPIFFS Partition"]:::fs
        SD["SD Card FS"]:::fs
    end

    %% Firmware Abstraction Layer
    subgraph "Firmware - Abstraction Layer"
        style Abstraction fill:#D6EAF8,stroke:#5DADE2
        AudioI2SDrv["AudioI2S Driver"]:::abstraction
        VS1053Drv["VS1053 Driver"]:::abstraction
        DisplayDrv["Display Drivers"]:::abstraction
        NetLibs["AsyncWebServer/AsyncTCP"]:::abstraction
        MQTTLib["async-mqtt-client"]:::abstraction
    end

    %% Firmware Core Services
    subgraph "Firmware - Core Services"
        style CoreServices fill:#ABEBC6,stroke:#27AE60
        Config["Configuration Loader"]:::core
        PluginMgr["Plugin Manager"]:::plugin
        AudioH["AudioHandler"]:::core
        DisplayM["DisplayManager"]:::core
        SDMgr["SDCard Manager"]:::core
        RTC["RTC Support"]:::core
        Controls["Control Input Handlers"]:::core
    end

    %% Firmware Network Interfaces
    subgraph "Firmware - Network Interfaces"
        style NetServices fill:#A9DFBF,stroke:#1ABC9C
        NetServer["NetServer (HTTP/WebSocket)"]:::network
        MQTTc["MQTT Client"]:::network
        Telnet["Telnet/CLI Interface"]:::network
    end

    %% Entry Point
    Entry["Main (setup/loop)"]:::core

    %% Relationships
    Browser -->|HTTP/WebSocket| NetServer
    NetServer -->|serves UI| SPIFFS
    SPIFFS -->|assets| SPIFFS
    NetServer -->|WebSocket events| DisplayM
    Broker <-->|MQTT pub/sub| MQTTc
    HA <-->|uses MQTT topics| Broker
    TelClient -->|Telnet commands| Telnet

    Entry -->|init| Config
    Entry -->|init| PluginMgr
    Entry -->|init| NetServer
    Entry -->|init| MQTTc
    Entry -->|init| Telnet
    Entry -->|init| AudioH
    Entry -->|init| DisplayM
    Entry -->|init| SDMgr
    Entry -->|init| RTC
    Entry -->|init| Controls

    Config -->|loads options| CoreServices
    PluginMgr -->|hooks into| AudioH
    PluginMgr -->|hooks into| DisplayM
    PluginMgr -->|hooks into| NetServer
    PluginMgr -->|hooks into| MQTTc
    PluginMgr -->|hooks into| Telnet
    PluginMgr -->|hooks into| SDMgr
    PluginMgr -->|hooks into| RTC
    PluginMgr -->|hooks into| Controls

    AudioH -->|decode| AudioI2SDrv
    AudioH -->|decode| VS1053Drv
    SDMgr -->|reads files| SD
    SDMgr -->|feeds| AudioH
    AudioH -->|audio out| I2S
    AudioH -->|audio out| VS1053

    DisplayM -->|render| DisplayDrv
    DisplayDrv -->|to device| DisplayDev
    DisplayM -->|update time| RTC

    Controls -->|GPIO events| ControlsHW
    Controls -->|touch events| TouchHW
    Controls -->|control actions| AudioH
    Controls -->|control actions| DisplayM

    RTC -->|I2C sync| RTC_HW

    NetLibs -->|HTTP stack| NetServer
    MQTTLib -->|MQTT stack| MQTTc

    Entry -->|uses| AudioI2SDrv
    Entry -->|uses| VS1053Drv
    Entry -->|uses| DisplayDrv
    Entry -->|uses| NetLibs
    Entry -->|uses| MQTTLib

    %% Click Events
    click Config "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/options.h"
    click Config "https://github.com/e2002/yoradio/blob/main/examples/myoptions.h"
    click PluginMgr "https://github.com/e2002/yoradio/blob/main/yoRadio/src/pluginsManager/pluginsManager.cpp"
    click PluginMgr "https://github.com/e2002/yoradio/blob/main/yoRadio/src/pluginsManager/pluginsManager.h"
    click AudioH "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/player.cpp"
    click AudioH "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/player.h"
    click DisplayM "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/display.cpp"
    click DisplayM "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/display.h"
    click NetServer "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/netserver.cpp"
    click NetServer "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/netserver.h"
    click MQTTc "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/mqtt.cpp"
    click MQTTc "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/mqtt.h"
    click Telnet "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/telnet.cpp"
    click Telnet "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/telnet.h"
    click SDMgr "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/sdmanager.cpp"
    click SDMgr "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/sdmanager.h"
    click RTC "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/rtcsupport.cpp"
    click RTC "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/rtcsupport.h"
    click Controls "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/controls.cpp"
    click Controls "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/controls.h"
    click TouchHW "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/touchscreen.cpp"
    click TouchHW "https://github.com/e2002/yoradio/blob/main/yoRadio/src/core/touchscreen.h"
    click AudioI2SDrv "https://github.com/e2002/yoradio/blob/main/yoRadio/src/audioI2S/Audio.cpp"
    click AudioI2SDrv "https://github.com/e2002/yoradio/blob/main/yoRadio/src/audioI2S/AudioEx.h"
    click VS1053Drv "https://github.com/e2002/yoradio/blob/main/yoRadio/src/audioVS1053/audioVS1053Ex.cpp"
    click VS1053Drv "https://github.com/e2002/yoradio/blob/main/yoRadio/src/audioVS1053/audioVS1053Ex.h"
    click DisplayDrv "https://github.com/e2002/yoradio/blob/main/yoRadio/src/displays/display*.cpp"
    click DisplayDrv "https://github.com/e2002/yoradio/blob/main/yoRadio/src/displays/display*.h"
    click DisplayDrv "https://github.com/e2002/yoradio/blob/main/yoRadio/src/displays/widgets/widgets.cpp"
    click DisplayDrv "https://github.com/e2002/yoradio/blob/main/yoRadio/src/displays/widgets/widgets.h"
    click DisplayDrv "https://github.com/e2002/yoradio/blob/main/yoRadio/src/displays/conf/*.h"
    click NetLibs "https://github.com/e2002/yoradio/tree/main/yoRadio/src/AsyncWebServer"
    click NetLibs "https://github.com/e2002/yoradio/tree/main/yoRadio/src/AsyncTCP"
    click MQTTLib "https://github.com/e2002/yoradio/tree/main/yoRadio/src/async-mqtt-client"
    click SPIFFS "https://github.com/e2002/yoradio/blob/main/yoRadio/data/www/index.html"
    click Entry "https://github.com/e2002/yoradio/blob/main/yoRadio/src/main.cpp"
    click Entry "https://github.com/e2002/yoradio/blob/main/yoRadio/yoRadio.ino"
    click HA "https://github.com/e2002/yoradio/blob/main/HA/custom_components/yoradio/media_player.py"
    click HA "https://github.com/e2002/yoradio/blob/main/HA/custom_components/yoradio/__init__.py"

    %% Styles
    classDef hardware fill:#AED6F1,stroke:#3498DB
    classDef core fill:#ABEBC6,stroke:#27AE60
    classDef abstraction fill:#D6EAF8,stroke:#5DADE2
    classDef network fill:#A9DFBF,stroke:#1ABC9C
    classDef external fill:#FAD7A0,stroke:#E67E22
    classDef fs fill:#F9E79F,stroke:#F1C40F
    classDef plugin fill:#D2B4DE,stroke:#8E44AD
```
