# imaginus02/microfmu-library architecture

> Architecture diagram of the GitHub repository imaginus02/microfmu-library, 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/imaginus02/microfmu-library
- Repository: https://github.com/imaginus02/microfmu-library

## Overview

##### Project Type and Purpose
- This project is a specialized library that integrates FMU (Functional Mock-up Unit) simulation capabilities into MicroPython. Essentially, it provides a simulation engine written in C (the simulator in main.c) and wraps it as a module that can be compiled into MicroPython.
- The README indicates two primary target build environments: Unix (via a Makefile based on micropython.mk) and ESP32 (via a CMake file, micropython.cmake). Also, a helper script (parseFMU.sh) is used to unpack and process FMU files.

##### File Structure
- Top-level files: .gitignore, Makefile, README.md, micropython.cmake, and parseFMU.sh—these serve as general project configuration and build support.
- The “library” directory houses the core simulation code:
– “library/main.c” is the primary implementation of the simulator and MicroPython interfacing functions.
– “library/headers/” includes standard header files (fmi2FunctionTypes.h, fmi2Functions.h, fmi2TypesPlatform.h) that likely define FMU-related types and functions.
– “library/micropython.mk” is a build script module tailored for Unix compilation of MicroPython integration.
- The presence of both a Makefile and distinct build scripts (micropython.mk for Unix, micropython.cmake for ESP32) highlights a design intended to support multiple target platforms using different build systems.

##### Main Components to Illustrate
a. Build & Compilation Subsystem:
- Makefile: The main orchestrator for compilation and cleaning.
- Build Modules:
- micropython.mk for Unix builds.
- micropython.cmake for ESP32 builds.
- FMU Parsing: The parseFMU.sh script, which prepares the FMU files (by decompressing them and extracting content).

b. Core Simulation Engine:
- main.c: Implements the simulation logic and integrates with MicroPython to expose FMU simulation functionalities.
- FMU Standard Headers: The header files under library/headers defining FMU types and functions (crucial for standard compliance and interoperability).

c. MicroPython Integration:
- The mechanism by which the simulation engine is built into MicroPython (via the USER_C_MODULES mechanism in the build commands) to enable running simulations within the MicroPython environment.

##### Relationships and Data Flow
- Data Flow between the FMU Parser and the Simulation Engine: The parseFMU.sh script processes FMU files, which are then used by the simulation engine (main.c) to perform simulation tasks.
- Build Chain: The Makefile calls the appropriate build modules (either micropython.mk or micropython.cmake depending on target) to compile the simulation engine as a MicroPython module.
- Integration with MicroPython: The compiled simulator (including FMU functionality) is injected as a C module into the MicroPython environment, meaning there is a clear interface boundary between the core C simulation code and the MicroPython runtime.

##### Architectural Patterns and Design Principles
- Modular Design: The separation between FMU parsing, simulation engine, and MicroPython integration supports the modular design principle.
- Cross-Platform Compilation: By having separate build scripts and configuration files for Unix and ESP32, the design emphasizes portability and reusability.
- Standard Compliance: Including dedicated FMU header files indicates adherence to FMU standard interfaces.

##### Additional Details
- Optionally, add an external link or annotation noting that parts of the simulation engine are inspired or derived from the fmuSDK project.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Build System Subgraph
    subgraph "Build System"
        subgraph "Unix Build"
            A("Makefile"):::build
            B("micropython.mk"):::build
        end
        subgraph "ESP32 Build"
            C("micropython.cmake"):::build
        end
    end

    %% FMU Parser
    D("FMU Parser (parseFMU.sh)"):::parser

    %% Core Simulation Engine Subgraph
    subgraph "Core Simulation Engine"
        E("Simulator Engine (library/main.c)"):::sim
        F("FMU Headers (library/headers)"):::headers
    end

    %% MicroPython Integration
    G("MicroPython Integration"):::integration

    %% Connections
    A -->|"builds"| E
    B -->|"builds"| E
    C -->|"builds"| E
    D -->|"provides_FMU_data_to"| E
    F -->|"provides_FMU_types_to"| E
    E -->|"integrates_to"| G

    %% Click Events
    click A "https://github.com/imaginus02/microfmu-library/tree/main/Makefile"
    click B "https://github.com/imaginus02/microfmu-library/blob/main/library/micropython.mk"
    click C "https://github.com/imaginus02/microfmu-library/blob/main/micropython.cmake"
    click D "https://github.com/imaginus02/microfmu-library/blob/main/parseFMU.sh"
    click E "https://github.com/imaginus02/microfmu-library/blob/main/library/main.c"
    click F "https://github.com/imaginus02/microfmu-library/tree/main/library/headers"

    %% Styles
    classDef build fill:#aqua,stroke:#000,stroke-width:2px;
    classDef parser fill:#ffdead,stroke:#000,stroke-width:2px;
    classDef sim fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef headers fill:#98fb98,stroke:#000,stroke-width:2px;
    classDef integration fill:#dda0dd,stroke:#000,stroke-width:2px;
```
