Files
esphome-samsung-vfd/tools/gen_charmaps.py
T
mangavalkandClaude Opus 5.5 95686a3608 Support every datasheet command and map text to the CG-ROM fonts
- Brightness, blink, screen on/off, annunciators, cursor control, display
  modes, user-defined characters, code table, international charset,
  peripheral select, self-test, raw writes and reinit
- UTF-8 text is encoded with the selected code table (tables generated
  from Python codecs by tools/gen_charmaps.py), falling back to
  accent-stripping
- Settings survive reinit and the self-test
- Example gains brightness select and display switch, and uses the git
  source on mangavalk.com

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 17:55:36 +01:00

79 lines
2.3 KiB
Python

"""Generate components/samsung_vfd/charmaps.h from Python's code page codecs.
Each table lists the Unicode code point shown for bytes 0x80..0xFF in one of
the display's CG-ROM fonts (datasheet section 6), or 0 where there is none.
CP10 (for DP86-20) has no standard codec, so it gets no table.
Run from the repo root: python tools/gen_charmaps.py
"""
from pathlib import Path
# Same order as the ESC t n argument (datasheet 5.4.18).
TABLES = [
("PC437", "cp437"),
("PC850", "cp850"),
("PC866", "cp866"),
("PC737", "cp737"),
("PC775", "cp775"),
("CP10", None),
("PC864", "cp864"),
("PC852", "cp852"),
]
OUT = Path(__file__).resolve().parent.parent / "components" / "samsung_vfd" / "charmaps.h"
def high_half(codec):
points = []
for b in range(0x80, 0x100):
try:
ch = bytes([b]).decode(codec)
except UnicodeDecodeError:
ch = ""
points.append(ord(ch) if len(ch) == 1 else 0)
return points
def main():
lines = [
"// Generated by tools/gen_charmaps.py -- do not edit.",
"#pragma once",
"",
"#include <cstdint>",
"",
"namespace esphome {",
"namespace samsung_vfd {",
"",
]
names = []
for name, codec in TABLES:
if codec is None:
names.append("nullptr")
continue
ident = f"CHARMAP_{name}"
names.append(ident)
pts = high_half(codec)
lines.append(f"// {name} ({codec}), bytes 0x80..0xFF")
lines.append(f"static const uint16_t {ident}[128] = {{")
for i in range(0, 128, 8):
row = ", ".join(f"0x{p:04X}" for p in pts[i : i + 8])
lines.append(f" {row}, // 0x{0x80 + i:02X}")
lines.append("};")
lines.append("")
lines.append("// Indexed by CodeTable (the ESC t argument); nullptr = no mapping known.")
lines.append(f"static const uint16_t *const CHARMAPS[{len(TABLES)}] = {{")
for (name, _), ident in zip(TABLES, names):
lines.append(f" {ident}, // {name}")
lines.append("};")
lines.append("")
lines.append("} // namespace samsung_vfd")
lines.append("} // namespace esphome")
lines.append("")
OUT.write_text("\n".join(lines), encoding="utf-8", newline="\n")
print(f"wrote {OUT}")
if __name__ == "__main__":
main()