Files
MeshTerm/meshterm/emulator/raster.py
T
Jean-Pierre MartineauandClaude Opus 5.5 58909d1373 meshterm emulate shows either handheld's display in a window
`meshterm emulate cardputer-zero --mock` and `meshterm emulate picocalc-lyra --mock`
open a desktop window drawing MeshTerm as that device's own screen draws it. An
EmulatedDevice (emulator/devices.py) holds what differs per handheld: the panel, a
Linux console's top-left grid and bold-as-bright 4-bit palette for the PicoCalc, the
lane keys drawn under the Cardputer's panel, and how each keyboard spells Shift with
an F-key (the PicoCalc's F6-F10, the Cardputer's xterm-shifted F4-F8).

It is not a strict emulator, and the docs say so wherever they offer it: MeshTerm runs
on the desktop as itself, and the window mimics the display's and keyboard's
constraints, never the CPU or the OS. Inside it the platform resolves with own_display
(platforms.drawn_by_meshterm), so nothing offers to move to another terminal.

Global options typed anywhere (--mock, --port, --db...) reach the emulated MeshTerm.
The command refuses --json like specimen. The Terminus fetch moved into the package as
`meshterm emulate --fetch-fonts` (with --archive for a copy fetched another way),
replacing scripts/cardputer-zero/fetch-terminus.py. A build without Tk says to install
with pip or pipx instead of failing.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-04 14:19:22 -04:00

163 lines
6.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# SPDX-License-Identifier: Apache-2.0
"""Cells to pixels: the panel the emulator shows, drawn from a :class:`~.vt.Terminal`.
The Cardputer Zero's panel is 320×170; 53 columns of 6 pixels and 14 rows of 12 leave a
two-pixel margin right and bottom, which this splits evenly so the grid sits centred. A
Linux console (the PicoCalc's) instead starts its grid at the panel's top-left corner, and
draws bold as *bright* — a dim palette colour becomes its bright twin, in the same glyph —
which ``top_left`` and ``bold_is_bright`` reproduce. Pixels are packed
straight into the byte layout the destination wants — 16-bit RGB565 for the Cardputer's
framebuffer, 24-bit RGB for a desktop window — so nothing converts a whole frame on its
way out.
Only rows the terminal reports dirty are redrawn, and a glyph row is drawn by lookup, not
per pixel: every (6-bit row pattern, foreground, background) triple is packed once and
reused, which is what keeps a full repaint affordable on a 1 GHz core.
"""
from __future__ import annotations
from collections.abc import Callable
from .font import CELL_H, CELL_W, Font
from .vt import BOLD, DIM, HIDDEN, REVERSE, RGB, STRIKE, UNDERLINE, Terminal
#: The Cardputer Zero's panel, in pixels.
PANEL_W = 320
PANEL_H = 170
#: A packer: one colour to the destination's bytes for one pixel.
Packer = Callable[[RGB], bytes]
def rgb565(colour: RGB) -> bytes:
"""``colour`` as one little-endian RGB565 pixel (the Cardputer framebuffer's format)."""
r, g, b = colour
value = (r >> 3) << 11 | (g >> 2) << 5 | b >> 3
return value.to_bytes(2, "little")
def rgb888(colour: RGB) -> bytes:
"""``colour`` as one 24-bit RGB pixel (a desktop window's format)."""
return bytes(colour)
def _blend(a: RGB, b: RGB) -> RGB:
"""Halfway from ``a`` to ``b`` — how a dim cell's ink is drawn."""
return ((a[0] + b[0]) // 2, (a[1] + b[1]) // 2, (a[2] + b[2]) // 2)
class Raster:
"""The panel's pixels, kept in step with a terminal.
Args:
terminal: The grid to draw.
font: The cell font.
pack: How one pixel is laid out in :attr:`pixels`.
default_fg: The ink of a cell whose style names none.
default_bg: The paper of a cell whose style names none.
width: Panel width in pixels.
height: Panel height in pixels.
top_left: Start the grid at the panel's top-left corner, as a Linux console does,
rather than centring it.
bold_is_bright: Draw bold the Linux console's way: a foreground from the palette's
eight dim slots takes its bright twin (slot ``n + 8``) and the glyph stays
regular, since a console font has no bold face.
"""
def __init__(
self,
terminal: Terminal,
font: Font,
*,
pack: Packer = rgb888,
default_fg: RGB = (170, 170, 170),
default_bg: RGB = (0, 0, 0),
width: int = PANEL_W,
height: int = PANEL_H,
top_left: bool = False,
bold_is_bright: bool = False,
) -> None:
"""Lay the grid out on the panel and paint it blank."""
self.terminal = terminal
self.font = font
self.pack = pack
self.default_fg = default_fg
self.default_bg = default_bg
self.width = width
self.height = height
self.bpp = len(pack((0, 0, 0)))
self.stride = width * self.bpp
self.left = 0 if top_left else (width - terminal.cols * CELL_W) // 2
self.top = 0 if top_left else (height - terminal.rows * CELL_H) // 2
palette = terminal.palette
self._bright = {palette[n]: palette[n + 8] for n in range(8)} if bold_is_bright else None
self.pixels = bytearray(pack(default_bg) * (width * height))
self._spans: dict[tuple[int, RGB, RGB], bytes] = {}
def _span(self, bits: int, fg: RGB, bg: RGB) -> bytes:
"""One glyph row's six pixels, packed (cached by pattern and colours)."""
key = (bits, fg, bg)
span = self._spans.get(key)
if span is None:
ink, paper = self.pack(fg), self.pack(bg)
span = b"".join(ink if bits & (0x80 >> x) else paper for x in range(CELL_W))
if len(self._spans) > 65536:
self._spans.clear()
self._spans[key] = span
return span
def update(self) -> list[tuple[int, int]]:
"""Redraw the rows the terminal says changed.
Returns:
The pixel row bands ``(first_y, last_y_exclusive)`` that changed, in order,
for a destination that writes only what moved.
"""
rows = sorted(self.terminal.take_dirty())
for row in rows:
self._draw_row(row)
bands: list[tuple[int, int]] = []
for row in rows:
y0 = self.top + row * CELL_H
if bands and bands[-1][1] == y0:
bands[-1] = (bands[-1][0], y0 + CELL_H)
else:
bands.append((y0, y0 + CELL_H))
return bands
def redraw(self) -> None:
"""Mark every row dirty and redraw the whole panel."""
self.terminal.take_dirty()
for row in range(self.terminal.rows):
self._draw_row(row)
def _draw_row(self, row: int) -> None:
cells = self.terminal.screen[row]
y0 = self.top + row * CELL_H
stride, bpp = self.stride, self.bpp
pixels = self.pixels
for col, (char, style) in enumerate(cells):
fg = style.fg or self.default_fg
bg = style.bg or self.default_bg
flags = style.flags
bold = bool(flags & BOLD)
if bold and self._bright is not None:
fg, bold = self._bright.get(fg, fg), False
if flags & REVERSE:
fg, bg = bg, fg
if flags & DIM:
fg = _blend(fg, bg)
if flags & HIDDEN or char == "":
glyph = bytes(CELL_H)
else:
glyph = self.font.glyph(char, bold=bold)
if flags & UNDERLINE:
glyph = glyph[: CELL_H - 2] + b"\xfc" + glyph[CELL_H - 1 :]
if flags & STRIKE:
glyph = glyph[:6] + b"\xfc" + glyph[7:]
x0 = (self.left + col * CELL_W) * bpp
for line in range(CELL_H):
at = (y0 + line) * stride + x0
pixels[at : at + CELL_W * bpp] = self._span(glyph[line], fg, bg)