# dandon01/feargreed architecture

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

- Interactive diagram: https://gitdiagram.com/dandon01/feargreed
- Repository: https://github.com/dandon01/feargreed

## Overview

##### Main Components
- Hardware Interface: This includes the Raspberry Pi, the Display HAT Mini (ST7735 LCD), physical buttons for UI interaction, and optional LED indicators.
- Application Core: The main executable (“feargreeddisplay.py”) which orchestrates the display updates, API calls, and user input handling.
- API/Data Integration: The components responsible for fetching data from external services such as the Alternative.me API (for the Fear & Greed Index) and CoinGecko for price info. This includes the API cache system and error handling logic.
- GIF Processing Module: “gif_processor.py” is dedicated to monitoring incoming GIFs, splitting them into frames, and saving them in the proper directories.
- Configuration/Settings: “config.json” (and any config settings embedded in code) that define display time, brightness, enabled modes, and performance thresholds such as memory limits.
- Testing and CI/CD: The tests directory with multiple test scripts (unit tests, display capability tests) and the GitHub Actions workflow for continuous integration.
- Auxiliary Scripts and Resources: Setup scripts (setup.sh), resource folders (gifs with subfolders for animations and specific modes), and extra files (PiHatMiniPinout.txt, LICENSE).

##### Relationships and Interactions
- The main display script (feargreeddisplay.py) acts as the central controller:
- It initializes the hardware, reads configuration settings from config.json, and sets up the environment.
- It fetches the current data from external APIs, consulting a locally maintained API cache (with a 5-minute update interval) and manages error states.
- It selects and renders the appropriate animated GIFs (from directories like gifs/feargreed, gifs/money_flow, and gifs/animations) based on the current market sentiment.
- It handles user input through physical buttons to switch between display modes and settings in the config menu.
- The GIF processing module (gif_processor.py) runs, possibly as a separate process, listening for new GIF files in an “incoming” folder (as outlined in the README). It decomposes GIFs into individual frames and organizes them in an animations folder, which the main display script then accesses.
- The API integration component also contains mechanisms for error handling: if data fetching fails or the API returns an error (e.g., for network issues or rate limits), an “error.gif” is displayed and cached data is used as fallback.
- The tests and CI/CD pipelines (GitHub Actions and pytest configurations) ensure that changes to the source code, including hardware interfacing, maintain expected behavior.

##### Architectural Patterns & Design Principles
- Modular Design: Separate individual responsibilities into different scripts (for display logic, configuration, GIF processing) makes the system easier to maintain.
- Event-Driven Interaction: Hardware button inputs and network events (API responses) trigger changes in display mode and data refreshing.
- Caching Mechanism: Implements a basic API cache system to reduce latency and handle temporary outages.
- Resource Management: With dedicated modules for memory management (like limiting frames per GIF and monitoring RAM usage), the system adapts to low-memory scenarios.
- Continuous Integration: Automated testing and deployment pipelines (via GitHub Actions) ensure stable operation on the target hardware.

##### Key Technologies and Frameworks
- Python 3.7+ – Primary programming language.
- Libraries: psutil (for memory monitoring), requests (for HTTP API calls), Pillow (image processing for GIF frames), watchdog (for monitoring file system changes), and ST7735 for display handling.
- Hardware: Raspberry Pi (Zero 2W), Display HAT Mini (ST7735 LCD), and optionally GPIO libraries for button and LED control.

##### Elements
- Clear Labels: Name each component (e.g., “Display Controller”, “API Cache Manager”, “GIF Processing Worker”, “Hardware Interface”).
- Annotations: Include brief notes next to components outlining key responsibilities like “Handles error states and retries” or “Processes incoming GIFs and caches frames.”

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Hardware Interface
    subgraph "Hardware Interface"
        H1["Raspberry Pi"]:::hardware
        H2["Display HAT Mini (ST7735)"]:::hardware
        H3["Physical Buttons"]:::hardware
        H4["LED Indicators (optional)"]:::hardware
        H5["Hardware Pin Mapping"]:::docs
        H6["Display Config Settings"]:::docs
    end

    %% Application Core
    subgraph "Application Core"
        AC1["feargreeddisplay.py (Controller)"]:::app
        AC2["API Cache & Data Fetching"]:::api
        AC3["Configuration Settings"]:::config
    end

    %% GIF Processing Module
    subgraph "GIF Processing Module"
        G1["gif_processor.py (GIF Worker)"]:::gif
        G2["Processed GIF Frames Directory"]:::gif
    end

    %% GIF Directories
    subgraph "GIF Directories"
        G3["gifs/feargreed"]:::gif
        G4["gifs/money_flow"]:::gif
        G5["gifs/animations"]:::gif
    end

    %% External APIs
    subgraph "External APIs"
        API1["Alternative.me API"]:::external
        API2["CoinGecko API"]:::external
    end

    %% Testing and CI/CD
    subgraph "Testing & CI/CD"
        T1["Test Scripts"]:::test
        T2["GitHub Actions Workflow"]:::test
    end

    %% Auxiliary Scripts and Resources
    subgraph "Auxiliary & Resources"
        A1["Setup Script"]:::aux
        A2["LICENSE"]:::aux
        A3["README.md"]:::aux
    end

    %% Interconnections
    AC1 -->|"Initializes"| H1
    AC1 -->|"Reads"| AC3
    AC1 -->|"Fetches Data"| AC2
    AC2 -->|"Calls"| API1
    AC2 -->|"Calls"| API2
    AC1 -->|"Renders to"| H2
    AC1 -->|"Processes Input"| H3
    AC1 -->|"Triggers"| G1
    G1 -->|"Sends Frames to"| AC1
    G1 -->|"Stores Frames in"| G2
    G1 -->|"Accesses"| G3
    G1 -->|"Accesses"| G4
    G1 -->|"Accesses"| G5
    T1 -.-> AC1
    T2 -.-> AC1
    T2 -.-> T1

    %% Click Events
    click H5 "https://github.com/dandon01/feargreed/blob/main/PiHatMiniPinout.txt"
    click H6 "https://github.com/dandon01/feargreed/blob/main/display_config.py"
    click AC1 "https://github.com/dandon01/feargreed/blob/main/feargreeddisplay.py"
    click AC3 "https://github.com/dandon01/feargreed/blob/main/config.json"
    click G1 "https://github.com/dandon01/feargreed/blob/main/gif_processor.py"
    click G5 "https://github.com/dandon01/feargreed/tree/main/gifs/animations"
    click G3 "https://github.com/dandon01/feargreed/tree/main/gifs/feargreed"
    click G4 "https://github.com/dandon01/feargreed/tree/main/gifs/money_flow"
    click T1 "https://github.com/dandon01/feargreed/tree/main/tests"
    click T2 "https://github.com/dandon01/feargreed/blob/main/.github/workflows/python-app.yml"
    click A1 "https://github.com/dandon01/feargreed/blob/main/setup.sh"
    click A2 "https://github.com/dandon01/feargreed/tree/main/LICENSE"
    click A3 "https://github.com/dandon01/feargreed/blob/main/README.md"

    %% Styles
    classDef hardware fill:#ffcc00,stroke:#333,stroke-width:2px;
    classDef app fill:#9fdf9f,stroke:#333,stroke-width:2px;
    classDef api fill:#9fc5e8,stroke:#333,stroke-width:2px;
    classDef config fill:#fdd49e,stroke:#333,stroke-width:2px;
    classDef gif fill:#d9ead3,stroke:#333,stroke-width:2px;
    classDef docs fill:#eed3ff,stroke:#333,stroke-width:2px;
    classDef test fill:#f4cccc,stroke:#333,stroke-width:2px;
    classDef aux fill:#c9daf8,stroke:#333,stroke-width:2px;
    classDef external fill:#f9cb9c,stroke:#333,stroke-width:2px;
```
