diff --git a/.claude/skills/nextion-hmi/SKILL.md b/.claude/skills/nextion-hmi/SKILL.md index 74ecb7f..839b13d 100644 --- a/.claude/skills/nextion-hmi/SKILL.md +++ b/.claude/skills/nextion-hmi/SKILL.md @@ -29,6 +29,8 @@ Back up the user's file first (copy it) unless they said not to; edits write in - `schema ` lists every attribute of a component kind with defaults and allowed values; `list_kinds` lists the kinds (`button`, `text`, `number`, `slider`, `progress_bar`, ...). - Coordinates: `x`, `y`, `w`, `h` in pixels; `endx`/`endy` are updated automatically. +- `new_project` is landscape by default (`portrait: true` for a tall screen); `info` shows + `direction` and `encoding`. - Text needs a font: `font` is a font id. A new project has **no fonts** -- call `add_font` first (height in px; no TrueType path = bundled CC0 font). `txt_maxl` must be >= the text length. - Pictures: `add_picture` returns an id for `pic` / `picc` (crop) / `pic2` etc. Basic-series diff --git a/docs/COVERAGE.md b/docs/COVERAGE.md index 5bc63ae..31d1b8c 100644 --- a/docs/COVERAGE.md +++ b/docs/COVERAGE.md @@ -89,7 +89,7 @@ byte for byte, or a file written by nxhmi was opened in the Editor and showed th | Intelligent picture format 4 | block codec with 8-bit alpha; first findings in FORMAT.md | | Anti-aliased fonts, multi-byte encodings (GB2312, Big5, ...) | only B/W single-byte fonts seen | | Other display series | Discovery / Enhanced / Edge not sampled; only Basic + Intelligent templates | -| Display direction | picture/glyph order verified for one direction (main.HMI byte 13 = 3) only | +| Display direction | byte 12 decoded and written (`new_project(portrait=...)`); picture/glyph order verified for one direction (90° Basic) only | | Some header fields | main.HMI bytes 8, 13, 36, 0x41; page header 21/23; entry bytes 25-27 | | Font name shown in the Editor's Fonts panel | cosmetic, generated fonts show an odd label | | Media resources | video / audio / Gmov files themselves (not just the components) | diff --git a/docs/FORMAT.md b/docs/FORMAT.md index 0b4a241..23d6a2a 100644 --- a/docs/FORMAT.md +++ b/docs/FORMAT.md @@ -62,8 +62,8 @@ primary table from the backup, among other things) -- never reuse a test file af | 9 | 1 | 0x41 in a 2019 file, 0x44 after saving with Editor 1.68 (Editor/format revision?) | | 10 | 1 | 0x21 | | 11 | 1 | display series: 0 = Basic (T), 3 = Intelligent (P); others **OPEN** | -| 12 | 1 | text encoding index (1 = iso-8859-1; an unknown value makes the Editor fail with "'null' is not a supported encoding name") | -| 13 | 1 | 3 (**OPEN**; not the model index) | +| 12 | 1 | display direction in 90° steps (Setting > Display: 0 = 0°, 1 = 90°, 2 = 180°, 3 = 270°). Basic panels are portrait at 0°, so a 480×320 Basic project has 1; Intelligent panels are landscape at 0°. Page sizes follow (the Editor shows a 480×272 Intelligent project with 1 as 272×480). An unsupported value made the Editor fail to load ("'null' is not a supported encoding name") | +| 13 | 1 | character encoding (Setting > Display): 1 ascii, 2 gb2312, 3 iso-8859-1, 4-11 iso-8859-2..9, 12 iso-8859-13, 13 iso-8859-15, 14 iso-8859-11, 15 ks_c_5601-1987, 16 big5, 17-20 windows-1255..1258, 21 windows-874, 22 koi8-r, 23 shift-jis, 24 utf-8 (all measured; `models.ENCODINGS`) | | 14 | 1 | 0x4F | | 16 | 4 | model id (e.g. 0xB23BC018 NX4832T035_011, 0x181169DA NX4827P043_011); must match the series byte, otherwise the Editor falls back to its first model | | 24 | 4 | 0x60 | @@ -205,7 +205,7 @@ Format 0: RGB565 little-endian, converted by plain truncation (`r>>3, g>>2, b>>3 panel's native scan order -- column by column, each column bottom-to-top: `offset = 2 * (x * height + (height - 1 - y))`. Verified pixel-exact (480×320 and 64×64, Editor output byte-identical to ours). **OPEN**: whether the order changes with the project's display -direction (all samples: main.HMI byte 13 = 3). +direction (all samples: main.HMI byte 12 = 1, i.e. 90° on a Basic panel). Format 4 (Intelligent series, **OPEN**): header field at 20 is non-zero for small pictures (e.g. 2640 for a 3307-byte payload); payload starts with u32 1, u32 height, u32 width (note: diff --git a/src/nxhmi/mcp_server.py b/src/nxhmi/mcp_server.py index 6fd12f7..cb1b599 100644 --- a/src/nxhmi/mcp_server.py +++ b/src/nxhmi/mcp_server.py @@ -143,10 +143,10 @@ def t_add_page(path, name, attributes=None): return {"page": name, "entry": entry} -def t_new_project(path, model="NX4832T035_011", first_page="page0"): +def t_new_project(path, model="NX4832T035_011", first_page="page0", portrait=False, encoding="iso-8859-1"): if os.path.exists(path): raise ValueError("%s exists; refusing to overwrite" % path) - project.new_project(model, first_page).save(path) + project.new_project(model, first_page, portrait=portrait, encoding=encoding).save(path) return t_info(path) @@ -237,8 +237,9 @@ TOOLS = { {"path": S, "page": S, "component": S}, ["path", "page", "component"]), "add_page": (t_add_page, "Add an empty page (display size of the project).", {"path": S, "name": S, "attributes": OBJ}, ["path", "name"]), - "new_project": (t_new_project, "Create a new project file (refuses to overwrite). Models: see nxhmi.models.", - {"path": S, "model": S, "first_page": S}, ["path"]), + "new_project": (t_new_project, "Create a new project file (refuses to overwrite). Models: see nxhmi.models. " + "Landscape unless portrait is true; encoding as in the Editor (iso-8859-1, utf-8, gb2312, ...).", + {"path": S, "model": S, "first_page": S, "portrait": {"type": "boolean"}, "encoding": S}, ["path"]), "add_picture": (t_add_picture, "Add a picture from an image file (png/jpg/bmp); returns its picture id.", {"path": S, "image_path": S}, ["path", "image_path"]), "add_font": (t_add_font, "Add a font rasterized from a TrueType file (omit ttf_path for the bundled CC0 font).", diff --git a/src/nxhmi/models.py b/src/nxhmi/models.py index e4fc47a..8450086 100644 --- a/src/nxhmi/models.py +++ b/src/nxhmi/models.py @@ -23,8 +23,34 @@ MODELS = { BY_ID = {v[0]: k for k, v in MODELS.items()} SERIES = {0: "Basic", 3: "Intelligent"} -# main.HMI text-encoding index (offset 12). Only the value seen in real projects is listed. -ENCODINGS = {"iso-8859-1": 1} +# main.HMI offset 12: display direction, in 90-degree steps as chosen in the Editor +# (Setting > Display: 0 / 90 / 180 / 270). Basic panels are portrait at 0 degrees, so the +# landscape sizes listed above need 90 or 270; Intelligent panels are landscape at 0 and 180. +NATIVE_PORTRAIT = {0: True, 3: False} + + +def direction_byte(model: str, landscape: bool = True, flipped: bool = False) -> int: + """main.HMI byte 12 for a landscape/portrait project on this model (flipped = +180 degrees).""" + portrait_native = NATIVE_PORTRAIT[lookup(model)[1]] + return (1 if landscape == portrait_native else 0) + (2 if flipped else 0) + + +def page_size(model: str, direction: int): + """(width, height) of the pages for main.HMI byte 12 = direction.""" + _, series, w, h = lookup(model) + landscape = (direction % 2 == 1) == NATIVE_PORTRAIT[series] + return (w, h) if landscape else (h, w) + + +# main.HMI offset 13: character encoding, value -> name as in the Editor's Setting > Display list. +# Measured by choosing each entry in Nextion Editor 1.68 and saving. +ENCODINGS = { + "ascii": 1, "gb2312": 2, "iso-8859-1": 3, "iso-8859-2": 4, "iso-8859-3": 5, "iso-8859-4": 6, + "iso-8859-5": 7, "iso-8859-6": 8, "iso-8859-7": 9, "iso-8859-8": 10, "iso-8859-9": 11, + "iso-8859-13": 12, "iso-8859-15": 13, "iso-8859-11": 14, "ks_c_5601-1987": 15, "big5": 16, + "windows-1255": 17, "windows-1256": 18, "windows-1257": 19, "windows-1258": 20, + "windows-874": 21, "koi8-r": 22, "shift-jis": 23, "utf-8": 24, +} def lookup(model: str): diff --git a/src/nxhmi/project.py b/src/nxhmi/project.py index 9c983f9..0faed7a 100644 --- a/src/nxhmi/project.py +++ b/src/nxhmi/project.py @@ -44,6 +44,10 @@ def parse_main(blob: bytes) -> dict: mid = struct.unpack_from(" Display + names = {v: k for k, v in models.ENCODINGS.items()} + d["encoding"] = names.get(blob[13], blob[13]) # resource list: header size at +4 (= list start), count at +28, entries of type[8] + name[8] start, = struct.unpack_from(" bytes: +def make_main(model: str, resources: list, encoding: str = "iso-8859-1", direction: int = None) -> bytes: """Build a main.HMI project header from scratch. Field values follow Nextion Editor 1.68 output (see docs/FORMAT.md); ``resources`` is a list of - (type, name) such as ("pa", "0.pa"). + (type, name) such as ("pa", "0.pa"). ``direction`` is the Editor's display direction in + 90-degree steps (byte 12); the default is landscape for the model. """ from . import models mid, series, _, _ = models.lookup(model) + if direction is None: + direction = models.direction_byte(model) + if encoding not in models.ENCODINGS: + raise ValueError("unknown encoding %r; known: %s" % (encoding, ", ".join(models.ENCODINGS))) b = bytearray(0x60) struct.pack_into(" byte return bytes(b) -def new_project(model: str = "NX4832T035_011", first_page: str = "page0", program: str = DEFAULT_PROGRAM) -> HMIFile: - """A new, empty project with one page -- no Nextion files needed.""" +def new_project(model: str = "NX4832T035_011", first_page: str = "page0", program: str = DEFAULT_PROGRAM, + portrait: bool = False, encoding: str = "iso-8859-1") -> HMIFile: + """A new, empty project with one page -- no Nextion files needed. Landscape unless ``portrait``.""" from . import models from .components import new_page - _, _, w, h = models.lookup(model) + direction = models.direction_byte(model, landscape=not portrait) + w, h = models.page_size(model, direction) page = new_page(first_page, w=w, h=h) - items = [("main.HMI", make_main(model, [("pa", "0.pa")])), + items = [("main.HMI", make_main(model, [("pa", "0.pa")], encoding, direction)), ("Program.s", program.encode("latin-1")), ("0.pa", page.pack())] return build(items) def project_size(h: HMIFile): - """(width, height) of the project's display, from main.HMI.""" + """(width, height) of the project's pages, from main.HMI (model and display direction).""" from . import models - mid = struct.unpack_from("