# chasecee/loop architecture

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

- Interactive diagram: https://gitdiagram.com/chasecee/loop
- Repository: https://github.com/chasecee/loop

## Overview

##### Project Type and Purpose
- LOOP is a full-stack, embedded IoT application for Raspberry Pi that plays GIFs, videos, and images on a small SPI-driven LCD.
- It combines a Python-based backend (device control, media pipeline, REST API) with a Next.js SPA frontend for browser-based media management.

##### High-Level Architectural Overview
- Hardware Layer: Raspberry Pi OS, SPI display, WiFi chipset.
- Backend Layer (Python 3.9+):
- Boot/Networking (boot/wifi.py, hotspot.sh)
- Configuration (config/config.json + schema.py)
- Media Index (media_index.py): canonical state store
- Conversion Engine (convert.py): raw→RGB565 frames
- Player (player.py + spiout/framebuf): reads state, writes SPI
- Web Server (FastAPI in web/server.py + SPA assets)
- Sync & Updater (sync/, deployment/updater.py)
- Frontend Layer (Next.js SPA under backend/web/spa & frontend/loop-frontend): UI for uploads, queue management, playback controls.

##### Key Components & Responsibilities
- MEDIA_INDEX (central authority): holds loop queue, active media, config parameters; serves both server and player.
- SERVER (FastAPI): exposes REST & WebSocket endpoints; applies all state changes via MEDIA_INDEX; persists uploads to backend/media/raw.
- CONVERT ENGINE: picks up raw uploads, processes into per-frame data in backend/media/processed, strips RGB565 payload. Reports progress via WebSocket.
- PLAYER: polls MEDIA_INDEX to fetch “current media,” renders frames from processed cache to the LCD (via SPI). Never modifies state.
- WIFI MANAGER (boot module): auto-connects to WiFi or launches “LOOP-Setup” AP for configuration.
- UPDATER/DEPLOYMENT: shell scripts and Python updater that install dependencies, configure systemd, apply Git pull updates.
- FRONTEND SPA: static assets served by SERVER; makes API calls for upload/progress, loop reordering, playback controls.

##### Relationships & Data Flows
- Browser → (HTTP/WS) → SERVER → MEDIA_INDEX → CONVERT & PLAYER
- Upload path: Browser POST → /api/media → raw media dir → CONVERT → processed dir → MEDIA_INDEX updated → notify frontend
- Playback path: PLAYER polls MEDIA_INDEX → reads processed frames → writes to SPI → updates display
- Configuration path: Browser edits settings via API → SERVER updates MEDIA_INDEX and config file
- WiFi path: On boot, WIFI MANAGER configures network interfaces; once online, SERVER listens on port 8000

##### Architectural Patterns & Principles
- Single Source of Truth (media_index.py) to avoid race conditions
- Separation of Concerns:
- Conversion only reports progress
- Player only reads state
- Server only mutates state via controlled API
- Modular drivers: Python SPI driver in display/spiout abstracts Waveshare C/Python code
- Systemd service orchestrates lifecycle; deployment scripts automate provisioning

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Hardware Layer
    subgraph "Hardware Layer"
        OS["Raspberry Pi OS"]:::hardware
        WiFiChip["WiFi Chipset"]:::hardware
        SPI["SPI-driven LCD"]:::hardware
        Systemd["systemd Service\n(loop)"]:::hardware
    end

    %% Backend Layer
    subgraph "Backend Layer"
        BootMgr["WiFi Manager\n(boot/wifi.py)"]:::backend
        ConfigLoader["Config Loader\n(schema.py)"]:::backend
        MediaIdx["MEDIA_INDEX\n(media_index.py)"]:::backend
        Server["FastAPI Server\n(server.py + routes/)"]:::backend
        Convert["Convert Engine\n(convert.py)"]:::backend
        Player["Player Renderer\n(player.py)"]:::backend
        Sync["Sync Module"]:::backend
        Updater["Updater\n(updater.py)"]:::backend
        Main["Main Entrypoint\n(main.py)"]:::backend
    end

    %% Storage Layer
    subgraph "Storage"
        Raw["RAW Media\n(backend/media/raw/)"]:::storage
        Processed["PROCESSED Media\n(backend/media/processed/)"]:::storage
        ConfigFile["config.json"]:::storage
        IndexFile["index.json"]:::storage
    end

    %% Frontend Layer
    subgraph "Frontend Layer"
        Client["Browser Client"]:::external
        SPA["SPA Assets\n(backend/web/spa/)"]:::frontend
        DevUI["Next.js Dev UI\n(frontend/loop-frontend/)"]:::frontend
    end

    %% Connections
    Client -->|"HTTP/WS"| Server
    Client -->|"loads static"| SPA
    SPA -->|"served by"| Server

    Server <-->|"reads/writes state"| MediaIdx
    Server -->|"POST /api/media"| Raw
    Raw -->|"triggers"| Convert
    Convert -->|"writes frames"| Processed
    Convert -->|WS_notify| Client
    Convert -->|"updates index"| MediaIdx

    Player -->|"polls state"| MediaIdx
    Player -->|"reads frames"| Processed
    Player -->|"SPI"| SPI

    BootMgr -->|"configures"| WiFiChip
    BootMgr -->|"on boot starts"| Server
    Systemd -->|"launches"| Main
    Main -->|"initializes"| Server

    ConfigLoader -->|"loads/saves"| ConfigFile
    MediaIdx -->|"persists"| IndexFile

    Sync -->|"updates repo"| Updater
    Updater -->|"provisions"| Systemd

    %% Click Events
    click BootMgr "https://github.com/chasecee/loop/blob/main/backend/boot/wifi.py"
    click ConfigLoader "https://github.com/chasecee/loop/blob/main/backend/config/schema.py"
    click ConfigFile "https://github.com/chasecee/loop/blob/main/backend/config/config.json"
    click MediaIdx "https://github.com/chasecee/loop/blob/main/backend/utils/media_index.py"
    click IndexFile "https://github.com/chasecee/loop/blob/main/backend/media/index.json"
    click Server "https://github.com/chasecee/loop/blob/main/backend/web/server.py"
    click Convert "https://github.com/chasecee/loop/blob/main/backend/utils/convert.py"
    click Player "https://github.com/chasecee/loop/blob/main/backend/display/player.py"
    click Raw "https://github.com/chasecee/loop/tree/main/backend/media/raw/"
    click Processed "https://github.com/chasecee/loop/tree/main/backend/media/processed/"
    click Sync "https://github.com/chasecee/loop/tree/main/backend/sync/"
    click Updater "https://github.com/chasecee/loop/blob/main/backend/deployment/updater.py"
    click Systemd "https://github.com/chasecee/loop/blob/main/backend/loop.service"
    click Main "https://github.com/chasecee/loop/blob/main/backend/main.py"
    click SPA "https://github.com/chasecee/loop/tree/main/backend/web/spa/"
    click DevUI "https://github.com/chasecee/loop/tree/main/frontend/loop-frontend/"

    %% Styles
    classDef hardware fill:#cccccc,stroke:#333,stroke-width:1px
    classDef backend fill:#add8e6,stroke:#333,stroke-width:1px
    classDef storage fill:#ffa500,stroke:#333,stroke-width:1px
    classDef frontend fill:#90ee90,stroke:#333,stroke-width:1px
    classDef external fill:#c0c0c0,stroke:#333,stroke-width:1px
```
