Display Backends

PyHMI uses a pluggable backend architecture. The same UI description (PyHML) and the same Python controller code run on every platform. The backend determines how pixels reach the display and how input is captured.

The canonical entry point is the factory pyhmi.init() (alias PyHmi.init()), which returns a (display, engine) tuple with the input backend already wired:

import pyhmi

display, engine = pyhmi.init(width=320, height=240, title="My HMI")
engine.run()
display.quit()

Backend auto-selection rules (see src/pyhmi/factory.py):

SDL2 Emulation

Cross-platform window on macOS, Linux, and Windows. The development default; used by the examples, the editor, and the emulator.

Linux Framebuffer

Direct /dev/fb0 access via mmap. No X11/Wayland required. Used in production on the Fischertechnik TXT 4.0.

RP2350 MicroPython Boards

Per-demo firmware images for the Waveshare RP2350-Touch-LCD-2.8 (ST7789 SPI + CST328 touch) and RP2350-Touch-LCD-7 (ST7262 RGB + GT911).

CircuitPython SPI LCD

CircuitPythonDisplay via displayio FourWire for ST7735, ST7789, ILI9341, HX8357, and ST7262 controllers.

Web (Pyodide)

The library runs in the browser: a loader page plus a non-blocking tick runner and web display/input backends.

C Accelerator

The pyhmi_fb C core accelerates every drawing primitive. Enabled automatically when compiled in; output stays pixel-identical.

SDL2 Emulation (Desktop, Development Default)

The SDL2 backend renders to a window using PySDL2 and runs on macOS, Linux, and Windows. The engine paints an offscreen frame at the design resolution; flip() copies it into the window surface. It is the backend used by every example and by the UI Scene Editor.

Setup

# Install with the SDL2 extra
pip install "pyhmi[sdl2]"
# The short form: factory with auto-selection (SDL2 by default)
import pyhmi

display, engine = pyhmi.init(width=320, height=240, title="PyHMI Demo")
engine.run()
display.quit()

Explicit backend usage (as in src/pyhmi/examples/backends/81_sdl2_basic.py):

from pyhmi import Engine, Sdl2Display, Sdl2Input, Window, Label

display = Sdl2Display(width=320, height=240, title="PyHMI Demo")
display.initialize()
engine = Engine(display)
input_backend = Sdl2Input(engine=engine)
input_backend.set_tree(engine.tree)
engine.input = input_backend

window = Window()
window.add_child(Label(x=20, y=100, width=280, height=40, text="Hello"))
engine.tree.set_root(window)
engine.tree.compute_layout()

engine.run()
display.quit()

Running Scenes and Examples

# Run any example from src/pyhmi/examples/ in an SDL2 window
python3 src/pyhmi/examples/backends/81_sdl2_basic.py

# Emulate a PyHML scene file
python3 -m pyhmi.cli.editor.emulator scene.pyhml --width 320 --height 240

Console Scripts

Installing the package registers these commands (see pyproject.toml [project.scripts]):

Command Entry point Purpose
pyhmi-editor pyhmi.cli.editor.main:main UI Scene Editor (visual design, preview, PyHML generation)
pyhmi-run pyhmi.cli.runner:main Runner app: drag & drop a folder, .py, or .pyhml file to run it
pyhmi-emulator pyhmi.cli.emulator:main Standalone scene emulator window
pyhmi-gen pyhmi.cli.generator:main Project generator (simple and advanced wizard modes)

Input

Sdl2Input captures keyboard and mouse events natively and dispatches them to the widget tree; touch gestures are driven through the same event path.

Linux Framebuffer

The framebuffer backend renders directly to the Linux framebuffer device using mmap for zero-copy pixel access. No X11, Wayland, or graphical desktop environment is required. The resolution and color format are read from the device (fb_var_screeninfo); the C accelerator attaches automatically when the compiled pyhmi_fb core is available.

Setup

Use the factory (recommended):

import pyhmi

# Force the framebuffer backend, or run with PYHMI_FB=1 in the environment
display, engine = pyhmi.init(backend="framebuffer", fb_device="/dev/fb0")
engine.run()
display.quit()

Or wire the backends directly. Note that FramebufferDisplay is imported from its module (it is not exported from the top-level pyhmi package):

from pyhmi.backends.framebuffer import FramebufferDisplay
from pyhmi.backends.evdev_input import EvdevInput
from pyhmi.core import Engine

display = FramebufferDisplay("/dev/fb0")   # 0,0 = native framebuffer size
display.initialize()
engine = Engine(display)
inp = EvdevInput(engine=engine)
inp.set_tree(engine.tree)
engine.input = inp

engine.run()
display.quit()

Input

EvdevInput (src/pyhmi/backends/evdev_input.py) polls /dev/input/eventX devices directly, with no python-evdev dependency. It supports keyboards, mice, and (multi-)touch touchscreens.

Requirements

Real-World Deployment: Fischertechnik TXT 4.0

About txt40: the txt40 demo is a feasibility study demonstrating that PyHMI can port Qt-based applications - such as the application of the Fischertechnik TXT 4.0 Controller - to Linux and MCU platforms. TXT 4.0 is a product and brand of Fischertechnik.

The Fischertechnik TXT 4.0 controller (ARM Linux, 240x320 rgb565 framebuffer, IP 192.168.7.2) runs the full PyHMI HMI demo from platform/txt40/. Deployment is two scripts:

# platform/txt40/deploy.sh - copy the demo to the controller
scp txt40_demo.py root@192.168.7.2:/opt/ft/workspaces/
scp start-demo.sh  root@192.168.7.2:/opt/ft/workspaces/

# platform/txt40/start-demo.sh - free the framebuffer, then run
systemctl stop qtmenu
python3 txt40_demo.py

On the controller the display is an rgb565 framebuffer, the compiled pyhmi_fb C accelerator is active, and the measured render time median is approximately 71 ms per frame (~14 FPS) for the full demo scene. The demo loads its palette from the fischertechnik.pyhmt theme file through the factory:

display, engine = pyhmi.init(
    width=240, height=320, title="PyHMI TXT 4.0",
    theme="fischertechnik.pyhmt",
)

RP2350 MicroPython Boards (Waveshare RP2350-Touch-LCD-2.8)

MicroPython firmware for the Waveshare RP2350-Touch-LCD-2.8: RP2350A, 240x320 portrait ST7789 SPI LCD (SPI1, 80 MHz), CST328 capacitive touch on I2C. Three demos ship, each built as its own flashable firmware - there is no boot-time demo-selection flag.

Building (one firmware per demo)

cd platform/waveshare-rp2350-touch-lcd-2.8
bash build_firmware.sh              # build ALL demos (default)
bash build_firmware.sh standard     # or: watch | txt | all

Each demo produces build/arm_<demo>/firmware_<demo>.uf2 from its own frozen manifest; the boot entry main.py is generated per build to launch that demo.

Flashing

  1. Hold BOOT, press and release RESET, then release BOOT.
  2. Copy firmware_<demo>.uf2 to the RPI-RP2 USB drive.
  3. The board auto-reboots into the demo. USB serial console (115200) shows [boot] progress lines; the UI shows a live FPS label (stdio is USB CDC only - this board has no debug UART).

Flush Performance Tiers

The ST7789 pixel flush is selectable at build time. Measured full-frame flush times:

Tier Build flag Mechanism Full frame
Default (none) HW SPI, blocking CPU write, byte-wise (80 MHz configured, ~20 MHz effective - per-byte DREQ handshake-bound) ~61 ms
DMA USE_DMA_FLUSH=true HW SPI + byte-wise DMA (DMA_SIZE_8): removes the CPU from the feed, but the per-byte handshake cap stays ~61 ms (same)
SPI16 USE_SPI16=true HW SPI + 16-bit DMA frames (DMA_SIZE_16): one frame per RGB565 pixel, half the handshakes - the RP2350 PL022 speedup ~30 ms expected
PIO USE_PIO_FLUSH=true PIO state machine + DMA bit-bang, gapless clk_sys/4 = 60 MHz at 240 MHz (panel max ~66 MHz); takes precedence over the other flags ~20 ms

Variant builds land in their own directories, e.g. USE_SPI16=true bash build_firmware.sh standard produces build/arm_standard_spi16/firmware_standard_spi16.uf2.

Partial Update

PARTIAL_UPDATE=true bash build_firmware.sh generates frozen/partial_cfg.py with PARTIAL_UPDATE = True. The driver then diffs the back buffer against the last pushed frame using the C core's per-row checksum diff (diff_checksums, a 1280-byte table) and transfers only the changed row bands over SPI. The first flip after boot is always full; an unchanged frame sends nothing.

Touch and Clock

Desktop Development for the Same Demos

The identical demo files run in an SDL2 window on a workstation (mouse replaces the CST328):

python3 run_example.py       # cross-platform entry point
                             # or double-click run_example.command (macOS),
                             # run_example.sh (Linux), run_example.bat (Windows)

See platform/waveshare-rp2350-touch-lcd-2.8/README.md for the full board pin map, build prerequisites, and frozen-manifest layout.

RP2350-Touch-LCD-7 (Waveshare)

MicroPython firmware for the Waveshare RP2350-Touch-LCD-7: 800x480 ST7262 RGB parallel LCD driven by PIO + DMA through the custom lcd C module, with GT911 capacitive touch on I2C (input backend Gt911Input, driver frozen/touch.py).

See platform/waveshare-rp2350-7/README.md for board details and build steps.

CircuitPython SPI LCD

CircuitPythonDisplay (src/pyhmi/backends/circuitpython.py) renders to SPI LCD displays via CircuitPython's displayio protocol (displayio.FourWire bus). On desktop platforms it falls back to a software-rendered buffer that can be saved as a PNG for verification.

Supported Controllers

Controller Interface Common Sizes
ST7735 SPI (displayio FourWire) 0.96", 1.8" (128x160)
ST7789 SPI (displayio FourWire) 1.3", 1.54", 2.0", 2.8" (240x240, 240x320)
ILI9341 SPI (displayio FourWire) 2.2", 2.4", 2.8" (240x320)
HX8357 SPI (displayio FourWire) 3.2", 3.5" (480x320)
ST7262 RGB parallel (WaveShare RP2350-Touch-LCD-7) 7" (800x480)

Setup

import board
import displayio
from pyhmi.backends.circuitpython import CircuitPythonDisplay

displayio.release_displays()

display = CircuitPythonDisplay(
    width=240, height=240,
    controller="st7789",
    spi=spi_bus,
    command=pin_cmd,
    chip_select=pin_cs,
    reset=pin_reset,
)
display.initialize()

Requirements

Web Runtime (Pyodide)

The library runs unmodified in the browser on Pyodide. The web/ loader page installs the pyhmi sources into the Pyodide filesystem and runs curated examples live.

C Accelerator Layer

The pyhmi_fb portable C99 core (src/pyhmi_fb/) implements the fast memory-graphics primitives: set/get pixel, fill rect, line, circle, arc, glyph blit (4-bit alpha LUT), image blit, and flush, across all color formats (RGB565, RGB565_SWAPPED, RGB888, BGR888, GRAY1/2/4/8, RGB332) and rotations (0/90/180/270).

Input Backends

Input handling is pluggable. Each display backend pairs with an appropriate input backend:

SDL2 Input

Sdl2Input: keyboard and mouse via SDL2. Paired with the SDL2 emulation backend.

evdev

EvdevInput: raw /dev/input/eventX polling - touchscreen, keyboard, mouse. Paired with the framebuffer backend.

CST328

Cst328Input: I2C capacitive touch with interrupt-driven queued reports (RP2350-Touch-LCD-2.8).

GT911

Gt911Input: I2C capacitive touch (RP2350-Touch-LCD-7).

Touch / SPI

SPI/I2C touch controllers on microcontroller targets (e.g. XPT2046-class panels via CircuitPython drivers).

Web Input

WebInput: browser pointer and keyboard events drained into the widget tree (Pyodide runtime).

Backend Selection Guide

Scenario Recommended Backend
Design and prototyping (macOS / Linux / Windows) SDL2 Emulation (pyhmi-editor, pyhmi-run, examples)
Desktop Linux HMI / headless SBC Linux Framebuffer (PYHMI_FB=1 or backend="framebuffer")
Industrial controller (Fischertechnik TXT 4.0) Linux Framebuffer + evdev, C accelerator (platform/txt40/)
RP2350 board + SPI LCD (2.8") MicroPython firmware, per-demo uf2 (platform/waveshare-rp2350-touch-lcd-2.8/)
RP2350 board + 7" RGB LCD MicroPython firmware with lcd + gt911 C modules (platform/waveshare-rp2350-7/)
CircuitPython board + SPI LCD CircuitPythonDisplay (displayio FourWire)
Browser demos / docs Web runtime (Pyodide, web/ loader)