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):
- Default is SDL2 (
Sdl2Display+Sdl2Input). PYHMI_FB=1forces the Linux framebuffer (FramebufferDisplay+EvdevInput) with no SDL2 dependency.PYHMI_SDL2=1forces SDL2, overriding the auto-detect that prefers the framebuffer when/dev/fb0exists.- The
backendparameter accepts a name ("sdl2","framebuffer","circuitpython"), a custom display class, or a list of either as a fallback chain: the first backend whose display initialises wins.
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
- Linux kernel with framebuffer support (
CONFIG_FB) - Write access to
/dev/fb0(run as root or add the user to thevideogroup) - Python 3.8+
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
- Hold BOOT, press and release RESET, then release BOOT.
- Copy
firmware_<demo>.uf2to theRPI-RP2USB drive. - 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
- Touch: CST328 interrupt-driven queued input - the INT pin handler captures every touch report into a FIFO and the input backend (
src/pyhmi/backends/cst328_input.py) drains it, so quick taps and intermediate drag positions are never missed. - Clock: the demos start with
machine.freq(240_000_000)(board-guarded) and run the engine attarget_fps=30.
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).
- Build:
bash build_firmware.sh(ARM default) orbash build_firmware.sh riscv; outputbuild/arm/firmware.uf2(~1.39 MB). - pioasm required:
pio/pio_rgb.piois compiled by pioasm intobuild/$ARCH/pio_rgb.pio.h; pico-sdk 2.3 no longer ships a ready-made binary, so the build script builds pioasm from SDK sources when absent. - Debug console: UART0 on pins 0/1 at 115200 (unlike the 2.8" board these pins are not USB).
- Demo: a boot logo sequence followed by an interactive dashboard (labels, buttons, sliders, switches, radios, text field, spinbox, dropdown, list view, tabs, bar chart, gauge) driven by the GT911 touch panel.
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
- CircuitPython 9.0+ on a compatible microcontroller
- Supported SPI or RGB LCD display
- Bitmap fonts and BMP/PNG images only (no TrueType, no video)
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.
src/pyhmi/web_runner.py- non-blocking tick architecture: Pyodide'stime.sleepblocks the JS thread, so the runner patchesSdl2Display/Sdl2Inputto the web backends and replacesEngine.run()with a non-blocking start; a JSsetIntervaltimer (16 ms) callstick()to drive one engine frame per tick.src/pyhmi/backends/web_canvas.py-WebDisplay(renders into the shared frame buffer;flip()pushes the frame over a JS bridge) andWebInput(drains browser pointer/keyboard events into the widget tree).- Vendored PyYAML -
src/pyhmi/vendor/yaml/: Pyodide has noyamlpackage, so the pure-Python PyYAML is vendored and installed into the Pyodide filesystem by the loader.
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).
- Capability registry (
src/pyhmi/core/accelerator.py): accelerators advertise the operations they implement viaOP_*constants and are registered per pixel buffer. - Wrapper (
src/pyhmi/backends/fb_accelerator.py):FbAcceleratorwraps the compiled core for the Painter. - Automatic enable: the framebuffer backend attaches the accelerator when the compiled module is importable; MicroPython boards compile it into the firmware via
USER_C_MODULES. The Painter delegates each primitive and falls back to pure Python otherwise - pixel-identical, so acceleration only changes speed, never output.
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) |