feat: LCD kit (segment digits, bars, fonts)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
afd649acc5
commit
36e01c4b1c
11 files changed
+448
No files matched your search
@@ -0,0 +1,137 @@
|
||||
"""Casio-LCD drawing kit: slanted 7-segment digits, segmented bars, fonts, small labels."""
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
from typing import Sequence
|
||||
|
||||
from PIL import ImageDraw, ImageFont
|
||||
|
||||
W, H = 480, 320
|
||||
BG = (170, 184, 154)
|
||||
INK = (28, 36, 24)
|
||||
GHOST = (158, 172, 143) # unlit segments, barely darker than the background
|
||||
SLANT = 0.14
|
||||
|
||||
FONT_PATH = Path(__file__).parent / "assets" / "fonts" / "Roboto-Bold.ttf"
|
||||
SEGMENT_GAP = 1.5
|
||||
|
||||
DIGIT_SEGMENTS = {
|
||||
"0": "abcdef", "1": "bc", "2": "abdeg", "3": "abcdg", "4": "bcfg",
|
||||
"5": "acdfg", "6": "acdefg", "7": "abc", "8": "abcdefg", "9": "abcdfg",
|
||||
}
|
||||
|
||||
|
||||
def _slant(points: Sequence[tuple[float, float]], base_y: float) -> list[tuple[float, float]]:
|
||||
return [(x + SLANT * (base_y - y), y) for x, y in points]
|
||||
|
||||
|
||||
def _hexseg(horizontal: bool, c0: float, c1: float, c: float, t: float) -> list[tuple[float, float]]:
|
||||
"""Hexagonal segment along the centre line c, from c0 to c1, thickness t."""
|
||||
if horizontal:
|
||||
return [(c0, c), (c0 + t / 2, c - t / 2), (c1 - t / 2, c - t / 2),
|
||||
(c1, c), (c1 - t / 2, c + t / 2), (c0 + t / 2, c + t / 2)]
|
||||
return [(c, c0), (c + t / 2, c0 + t / 2), (c + t / 2, c1 - t / 2),
|
||||
(c, c1), (c - t / 2, c1 - t / 2), (c - t / 2, c0 + t / 2)]
|
||||
|
||||
|
||||
def _advance(ch: str, w: float, t: float, gap: float) -> float:
|
||||
if ch == ":":
|
||||
return t * 1.1 * 2 + gap
|
||||
if ch == ".":
|
||||
return t + gap
|
||||
return w + gap
|
||||
|
||||
|
||||
def _digit(d: ImageDraw.ImageDraw, ch: str, x: float, y: float, w: float, h: float, t: float, ghost: bool) -> None:
|
||||
g = SEGMENT_GAP
|
||||
top, mid, bot = y + t / 2, y + h / 2, y + h - t / 2
|
||||
left, right = x + t / 2, x + w - t / 2
|
||||
geometry = {
|
||||
"a": _hexseg(True, left + g, right - g, top, t),
|
||||
"g": _hexseg(True, left + g, right - g, mid, t),
|
||||
"d": _hexseg(True, left + g, right - g, bot, t),
|
||||
"f": _hexseg(False, top + g, mid - g, left, t),
|
||||
"b": _hexseg(False, top + g, mid - g, right, t),
|
||||
"e": _hexseg(False, mid + g, bot - g, left, t),
|
||||
"c": _hexseg(False, mid + g, bot - g, right, t),
|
||||
}
|
||||
lit = DIGIT_SEGMENTS.get(ch, "")
|
||||
for name, points in geometry.items():
|
||||
if name in lit or ghost:
|
||||
d.polygon(_slant(points, y + h), fill=INK if name in lit else GHOST)
|
||||
|
||||
|
||||
def number(d: ImageDraw.ImageDraw, text: str, x: float, y: float, w: float, h: float, t: float,
|
||||
gap: float, ghost: bool = True) -> float:
|
||||
"""Draw `0-9 : . -` as slanted 7-segment glyphs (a space is an empty cell).
|
||||
|
||||
Returns x after the last glyph, including the trailing gap.
|
||||
"""
|
||||
for ch in text:
|
||||
if ch == ":":
|
||||
s = t * 1.1
|
||||
for cy in (y + h * 0.32, y + h * 0.68):
|
||||
square = [(x + s / 2, cy - s / 2), (x + s * 1.5, cy - s / 2),
|
||||
(x + s * 1.5, cy + s / 2), (x + s / 2, cy + s / 2)]
|
||||
d.polygon(_slant(square, y + h), fill=INK)
|
||||
elif ch == ".":
|
||||
dot = [(x, y + h - t), (x + t, y + h - t), (x + t, y + h), (x, y + h)]
|
||||
d.polygon(_slant(dot, y + h), fill=INK)
|
||||
elif ch == "-":
|
||||
d.polygon(_slant(_hexseg(True, x + t, x + w - t, y + h / 2, t), y + h), fill=INK)
|
||||
elif ch != " ":
|
||||
_digit(d, ch, x, y, w, h, t, ghost)
|
||||
x += _advance(ch, w, t, gap)
|
||||
return x
|
||||
|
||||
|
||||
def number_width(text: str, w: float, t: float, gap: float) -> float:
|
||||
"""Horizontal space `number` uses for `text`."""
|
||||
return sum(_advance(ch, w, t, gap) for ch in text)
|
||||
|
||||
|
||||
def _lit_segments(pct: float | None, n_seg: int) -> int:
|
||||
if pct is None:
|
||||
return 0
|
||||
return max(0, min(n_seg, round(pct / 100 * n_seg)))
|
||||
|
||||
|
||||
def v_bar(d: ImageDraw.ImageDraw, x: float, y_bottom: float, pct: float | None, w: float,
|
||||
n_seg: int, seg_h: float, gap: float) -> None:
|
||||
"""Vertical segmented bar growing upwards from y_bottom; pct None draws it all unlit."""
|
||||
lit = _lit_segments(pct, n_seg)
|
||||
for k in range(n_seg):
|
||||
y1 = y_bottom - k * (seg_h + gap)
|
||||
d.rectangle((x, y1 - seg_h, x + w - 1, y1 - 1), fill=INK if k < lit else GHOST)
|
||||
|
||||
|
||||
def h_bar(d: ImageDraw.ImageDraw, x: float, y: float, pct: float | None, h: float,
|
||||
n_seg: int, seg_w: float, gap: float) -> None:
|
||||
"""Horizontal segmented bar growing to the right; pct None draws it all unlit."""
|
||||
lit = _lit_segments(pct, n_seg)
|
||||
for i in range(n_seg):
|
||||
x0 = x + i * (seg_w + gap)
|
||||
d.rectangle((x0, y, x0 + seg_w - 1, y + h - 1), fill=INK if i < lit else GHOST)
|
||||
|
||||
|
||||
@lru_cache(maxsize=None)
|
||||
def load_font(size: int) -> ImageFont.FreeTypeFont:
|
||||
return ImageFont.truetype(str(FONT_PATH), size)
|
||||
|
||||
|
||||
def tag(d: ImageDraw.ImageDraw, xy: tuple[int, int], text: str, font: ImageFont.FreeTypeFont) -> tuple[int, int, int, int]:
|
||||
"""Inverted label (background-coloured text on an ink box); returns the box."""
|
||||
pad = 3
|
||||
left, top, right, bottom = d.textbbox((0, 0), text, font=font)
|
||||
x, y = xy
|
||||
box = (x, y, x + (right - left) + 2 * pad, y + (bottom - top) + 2 * pad)
|
||||
d.rectangle(box, fill=INK)
|
||||
d.text((x + pad - left, y + pad - top), text, font=font, fill=BG)
|
||||
return box
|
||||
|
||||
|
||||
def fmt_temp(value: float | None) -> str:
|
||||
"""Whole-degree text; `--` when unknown; never `-0`."""
|
||||
if value is None:
|
||||
return "--"
|
||||
rounded = round(value)
|
||||
return "0" if rounded == 0 else str(int(rounded))
|
||||
Reference in new issue
Block a user