Tutorial: Adaptive UI, Themes, RLE Fonts & Semantic Icons (pylotos.ui)
pylotos.ui lets you declare your user interface once in clean Python while rendering natively onto physical ESP32 screens (via dirty-band RGB565 DMA blits or 1-bit SSD1306 thresholding) and streaming compact JSON state to Core 0's native C :80/:443 WebServer.
1. Adaptive Display & Input Matrix
DisplayProfile.detect() and InputProfile.detect() automatically inspect hal.display_info() and hal.input_info() at startup:
| Display Archetype | Resolution Examples | Automatic Layout Adaptation |
|---|---|---|
| Headless | 0×0 (m5stack_nanoc6, seeed_xiao_*) | Skips framebuffer allocation (0 B RAM); serves full Web UI on :80/:443. |
| Micro 1-bit OLED | 72×40 (generic_esp32c3_oled_042) | Single-widget paged view with high-contrast monochrome PALETTE_MONO. |
| Standard 1-bit OLED | 128×64 (heltec_wifi_lora_32_v3) | Compact 1-column stack, crisp 1-bit borders, zero gray dither blur. |
| Square Compact TFT | 128×128 (m5stack_atoms3, atoms3r) | 1-Button navigation (1× next, Hold OK/edit, 2× back, 3× home). |
| Portrait TFT | 135×240 (m5stack_stickc_plus2) | Collapses Grid(cols=2) cleanly into stacked vertical cards. |
| Round TFT | 240×240 (m5stack_dial GC9A01) | Chord-safe horizontal inset padding so headers/footers never clip on circular bezels; rotary encoder + touch support. |
| Wide Compact TFT | 240×135..320×172 (cardputer_adv, t_embed) | 2-column metrics, QWERTY keyboard shortcuts (shortcut="b"), inline text editing. |
| Standard Landscape TFT | 320×240 (core2, cores3, t_deck, cyd) | Full 2–3 column grids, touch drag sliders, swipe tabs, and on-screen TextInputModal. |
2. Reactive State & Two-Way Binding
Mutating any attribute on ui.State automatically increments app.rev and schedules both a dirty-band LCD repaint and a Core 0 sys_web.publish_ui() push:
import pylotos.ui as ui
st = ui.State(temp=22.5, target=23.0, mode="Auto", armed=True)
# Read/write directly in timers or callbacks:
st.temp = 22.8
# Bind to interactive or display widgets:
slider = ui.Slider("Target", st.bind("target"), min=16.0, max=30.0, step=0.5, unit="C")
badge = ui.StatusBadge(
"System",
lambda: "ARMED" if st.armed else "STANDBY",
tone=lambda: "ok" if st.armed else "muted",
)
3. Runtime ThemeManager (dark, light, high_contrast)
Switch themes globally at runtime — both the physical RGB565 display palette and the browser CSS tokens (ui_renderer.js) update in lockstep:
import pylotos.ui as ui
# Switch between "dark" (2B-2 Aviator default), "light", and "high_contrast":
ui.theme_manager.set_theme("high_contrast")
# Read active RGB565 palette tokens inside custom Canvas drawing:
pal = ui.theme_manager.palette_for(mono=False)
bg_rgb565 = pal["bg"]
accent_rgb565 = pal["cyan"]
# Read CSS hex tokens exported to the Web Dashboard:
css_vars = ui.theme_manager.web_tokens()
Note: On 1-bit monochrome OLEDs (SSD1306), palette_for(mono=True) always preserves PALETTE_MONO so pixel legibility is never compromised.
4. Priority-Queued NotificationManager
app.notify(...) backs every toast and alert with a bounded priority queue (err > warn > ok > info). Higher-priority alerts immediately preempt lower-priority ones while preserving interrupted alerts in the queue and tracking unread_count:
# Standard informational toast (priority 1):
app.notify("Synced 4 sensors", level="info", icon="cloud")
# Warning banner (priority 3):
app.notify("Battery below 15%", level="warn", icon="battery", duration_ms=4000)
# Critical error (priority 4 — immediately preempts active info/warn banners!):
app.notify("LoRa TX duty-cycle limit reached", level="err", icon="error")
# Inspect history or clear unread indicator:
history = app.notifications.history()
app.notifications.mark_all_read()
5. Proportional RLE Typography (pylotos.ui.fonts)
pylotos.ui.fonts packs a crisp proportional 5×7 ASCII table in just 475 bytes of ROM with four built-in integer-scaled presets ("sm", "md", "lg", "xl"), plus a 1-bit Run-Length Encoded (RLE) custom font registry for custom sizes or Unicode glyphs (such as Hungarian ÁÉÍÓÖŐÚÜŰ):
from pylotos.ui import fonts
import pylotos.ui as ui
# Measure & draw built-in proportional presets inside a CanvasBox:
w_px, h_px = ui.measure_text("23.4 C", font="xl")
ui.draw_text(cv, "23.4 C", x=10, y=12, col=ui.theme.CYAN, font="xl")
# Register a custom RLE-compressed glyph font (e.g. custom symbols or accented glyphs):
glyph_A_acute = fonts.encode_rle_bitmap(
width=5,
height=9,
rows=[
0b00010, # acute accent
0b00100,
0b01110,
0b10001,
0b10001,
0b11111,
0b10001,
0b10001,
0b00000,
],
)
fonts.register_font("hu_9", height=9, glyphs={ord("Á"): (5, glyph_A_acute)}, spacing=1)
6. Semantic 16×16 / 24×24 Icon Atlas (pylotos.ui.icons)
20 built-in 1-bit MSB-first icons occupy only 640 bytes total and scale cleanly (size=8, 12, 16, 24, 32) across dirty-band render slices:
- Available icon names:
home,wifi,ble,battery,bolt,cpu,temp,settings,apps,terminal,shield,bell,cloud,sd,play,check,warn,error,chart,lock.
import pylotos.ui as ui
def draw_header_strip(cv, x, y, w, h):
ui.draw_icon(cv, "wifi", x + 4, y + 4, col=ui.theme.CYAN, size=16)
ui.draw_icon(cv, "shield", x + 26, y + 4, col=ui.theme.GOLD, size=16)
ui.draw_text(cv, "SECURED", x + 48, y + 8, col=ui.theme.WHITE, font="md")
box = ui.CanvasBox(height=26, draw=draw_header_strip, label="Security Strip")
Try the complete interactive showcase right now:
pylotos-sim examples/hmi_themes_and_alerts.py --board m5stack_core2 --port 8080