Architecture

Overview

PyHMI implements a single codebase, multi-runtime architecture. The same Python UI description, widget logic, and controller code runs on multiple platforms:

Layered Design

PyHMI is organized into six horizontal layers. Each layer depends only on the layer below it, enabling independent testing and platform-specific substitution.

Six-layer architecture: Application, Widget, Core Engine, Graphics Abstraction, Display Backend, Input Backend.

1. Application Layer

User-facing entry points. Contains the PyHML parser, QML importer, HTML converter, data model, Python controllers, and scene manager.

  • PyHML Parser — Converts .pyhml files into widget trees at runtime
  • QML Importer — Loads Qt QML designs for reuse
  • HTML Converter — Transforms HTML files into PyHML
  • Data Model — Observable properties for reactive binding
  • Scene Manager — Load, unload, and navigate between scenes with data segue

2. Widget Layer

Reusable UI components. Each widget is a Python class inheriting from WidgetBase. Widgets manage their own state, rendering, and event handling.

  • 50+ built-in widgets — covering all elements from uGUI, LVGL, TouchGFX, emWin, and Qt/Quick
  • Custom widgets — user-defined via class inheritance, nestable in PyHML
  • Canvas — pixel-level drawing surface
  • Video — MP4, MJPEG stream playback (desktop only)
  • VirtualKeyboard — on-screen keyboard for small displays

3. Core Engine

The heart of PyHMI. Provides the infrastructure that powers all widgets and enables the declarative UI paradigm.

  • Widget Tree — Hierarchical model with parent-child relationships
  • Layout Engine — Flex (row/column) and Grid layouts
  • Event Loop — Single-threaded, polls input and schedules redraws
  • Animation Engine — Timeline-based with easing curves
  • Style Templates — Cascading properties (widget → class → theme)
  • Data Binding — Observable properties with dependency tracking
  • Gesture Recognizer — Touch gesture detection (swipe, pinch, tap, long-press)
  • Dialog Manager — Modal dialog system with segue support
  • Occlusion Culling — WidgetBase.render_recursive skips siblings beneath an opaque full-screen child that declares _occludes_siblings (e.g. a modal keyboard dialog), so the hidden scene is never rendered

4. Graphics Abstraction

Platform-agnostic drawing API. Shields the widget layer from platform-specific rendering details.

  • Canvas — Double-buffered render target with pixel-level operations
  • Painter — Drawing primitives (line, rect, circle, arc, text, image). Pure-Python reference implementation, always correct.
  • C Graphics Core — pyhmi_fb (portable C99, src/pyhmi_fb/): pixel, rect, line, circle, arc, glyph blit, image blit, flush. All colour formats (RGB565, RGB565_SWAPPED, RGB888, BGR888, GRAY1/2/4/8, RGB332) and rotations. The Painter delegates per-operation to a registered accelerator and falls back to Python — pixel-identical, so acceleration only changes speed.
  • Accelerator Registry — core/accelerator.py: OP_* capability constants (set_pixel, fill_rect, line, circle, arc, blit_glyph, blit_image, blit_alpha8, clear); accelerators advertise their operations and are registered per pixel buffer.
  • FbAccelerator — backends/fb_accelerator.py: Python wrapper around the compiled pyhmi_fb core, attached automatically by capable display backends.
  • Weak Flush Hook — the C core exposes a weak pyhmi_fb_draw() hook; board C flush drivers (e.g. the ST7789 SPI flush in frozen_c/) override it with a strong definition at link time.
  • Font Renderer — Bitmap fonts (built-in Arimo, 4-bit grayscale) + TrueType (desktop)
  • Image Decoder — PNG, BMP, GIF (desktop); BMP, PNG (MCU)
  • Video Decoder — MP4, MJPEG stream (desktop only)

5. Display Backend

Pluggable rendering target. Three implementations, selected at startup.

  • Framebuffer — Direct /dev/fb0 access via mmap (Linux)
  • SPI/LCD — SPI LCD over MicroPython/CircuitPython (ST7789, ST7735, ILI9341, HX8357); full-frame flush in the C core on the MCU
  • SDL2 — Cross-platform emulation window (macOS, Linux, Windows)

6. Input Backend

Pluggable input handling. Events are normalized and dispatched to the event loop.

  • evdev — Linux input devices (touchscreen, keyboard, mouse)
  • tinyusb — USB HID keyboard/mouse on CircuitPython (MCU)
  • Touch Controller — SPI/I2C touch screen (MCU)

Data Flow

User input is captured by the Input Backend, normalized by the Event Loop, and dispatched to the appropriate widget. Widgets update their state, triggering layout recalculation and redraw. The Graphics Abstraction layer translates draw calls to the active Display Backend.

Input → Event Loop → Widget Tree → Layout → Graphics Abstraction → Display Backend.

Runtime Environments

Desktop Linux (Framebuffer)

Runs on Raspberry Pi, BeagleBone, or any Linux SBC with framebuffer support. No X11/Wayland required. Direct pixel access via mmap for maximum performance.

  • Python 3.8+
  • /dev/fb0 via mmap
  • evdev for input (touchscreen, keyboard, mouse)
  • TrueType fonts, full image/video support
  • Target: 30+ FPS on Raspberry Pi 3

Microcontroller (MicroPython, RP2350)

Runs on RP2350 boards (Raspberry Pi Pico 2 and compatibles) with MicroPython and an SPI LCD. Verified on the Waveshare RP2350-Touch-LCD-2.8 (ST7789, 240x320, CST328 touch). The pyhmi_fb C module renders into the panel buffer natively; the full-frame SPI transfer runs in C. A C-only runtime variant (pyhmi_cpp, C++11) provides the same widget set for deployments without Python.

  • MicroPython with frozen pyhmi + pyhmi_fb C module
  • Row-span painting + C primitives keep RAM well under 500 KB
  • I2C touch controllers (CST328, GT911), USB HID via tinyusb
  • Bitmap fonts (compact Arimo tables), BMP/PNG images only
  • Target: 15+ FPS; board measurement: ~140 ms render, ~61 ms flip at 80 MHz SPI

Emulation (SDL2)

Runs on macOS, Linux, and Windows. Provides a windowed environment for design and testing. Renders identically to the framebuffer backend.

  • Python 3.8+ with PySDL2
  • SDL2 window with configurable resolution
  • Standard keyboard/mouse input
  • Simulated touch input for gesture testing

C/C++ Runtime Variant (pyhmi_cpp)

A secondary, best-effort runtime for deployments without Python (src/pyhmi_cpp/). C++11, built with CMake, renders through SDL2. Provides parity of the widget set, core engine (tree, layout, events, bindings, theme), and the PyHML parser with the Python implementation.

  • C++11, CMake build, SDL2 backend
  • Widgets, core modules, and parsers at Python parity
  • Same PyHML scene files render on both runtimes
  • Examples ported and verified headless alongside the Python set

Web Runtime (Pyodide)

The library runs unmodified in the browser. The web/ loader page installs the pyhmi sources into the Pyodide filesystem and runs curated examples live. web_runner.py patches the SDL2 aliases to the web backends and converts the engine loop into a non-blocking tick (a JS timer drives one frame per tick, since Pyodide's time.sleep would block the JS thread).

  • backends/web_canvas.py: WebDisplay + WebInput
  • web_runner.py: non-blocking tick architecture
  • Vendored pure-Python PyYAML (Pyodide ships no yaml package)
  • Loader page at web/ with curated runnable examples

Design Principles

Single Codebase, Multiple Runtimes

The same Python code runs on all three platforms. Platform-specific code is isolated in backend modules. UI descriptions (PyHML) are portable across runtimes.

Declarative UI

PyHML uses a YAML-compatible, QML-inspired syntax. UI hierarchy is defined by indentation. Properties, bindings, and signals are declared inline. The parser constructs a widget tree at runtime.

Composability

Custom widgets are Python classes. They can be composed, nested, and reused. The widget tree is a standard tree data structure — no special frameworks required.

Memory Efficiency

Desktop runtime targets <5 MB RAM for a typical HMI with 15 widgets. MCU runtime targets <500 KB RAM including display buffer. Achieved through lazy loading, selective widget compilation, and minimal abstractions.

Progressive Enhancement

Features are available based on platform capability. TrueType fonts, video, and MJPEG are desktop-only. The same UI description degrades gracefully on MCU targets.

Weak Implementation, Strong Acceleration

The pure-Python Painter is the reference implementation; compiled accelerators (the pyhmi_fb C core) advertise the operations they implement natively and are registered per pixel buffer. Fallbacks are pixel-identical — enabling an accelerator never changes rendering output, only speed.