From 0287cf21e458a362e1bc1096f0401404b6c06ece Mon Sep 17 00:00:00 2001 From: MangaValk Date: Mon, 16 Nov 2026 23:31:12 +0100 Subject: [PATCH] tft: picture, crop, progress bar, checkbox, radio; page RAM header fix; README sub-goals - five more Basic types in tft_types (default variants; others refused) - the page RAM image starts with 4 x FF + one FF per component (padded to 4) - README: .tft status split into sub-goals with a stated basis each - sample generator: display direction and gradient pictures; regression test walks sub-folders (84 Editor samples reproduced) Co-Authored-By: Claude Fable 5.2 --- README.md | 21 +++++++++++++++- docs/COVERAGE.md | 2 +- docs/FORMAT.md | 3 ++- docs/NOTES.md | 11 +++++++++ src/nxhmi/tft_build.py | 8 +++--- src/nxhmi/tft_types.py | 52 +++++++++++++++++++++++++++++++++++++-- tests/test_tft_build.py | 3 ++- tools/make_tft_samples.py | 11 ++++++++- 8 files changed, 101 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c11fb76..c127b35 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,26 @@ Every percentage is a count with a stated basis, so it can be checked (details i | Readable attribute names in JSON | **100 %** (31/31 types) | every value list the Editor documents, incl. the Intelligent-only types and load effects | | Rendering, Basic components | **100 %** (20/20 pixel-exact) | incl. gauges and QR codes (own encoder, no dependencies) | | Rendering, all components | **94 %** (29/31 pixel-exact) | Intelligent ComboBox and Switch are drawn, but their anti-aliased edges are approximate (~99 % / ~92 % of pixels exact) | -| `.tft` compilation | **81 %** (6.5/8 milestones) | `nxhmi build-tft`: byte-identical to the Editor for NX4832T035 projects with pages, hotspots, plain text, int variables, timers, touch caps, pictures, fonts and script (assignments, arithmetic, 27 commands, if/else/while/for, global variables); the other component types not yet; never tried on a display | + +### `.tft` compilation (`nxhmi build-tft`) + +Research stage. The goal is output byte-identical to Nextion Editor's; anything not measured is +refused with the reason instead of guessed. Checked against 84 Editor-compiled sample projects. +The firmware files inside every `.tft` are read from your own Editor installation at build time. + +| Sub-goal | Status | Basis | +|---|---|---| +| File container | **done** (4/4) | header, layout table, the four checksums, resource area | +| Pictures and fonts in the `.tft` | **done** (2/2) | any number of each (Basic picture format) | +| Page section structure | **done** | code blob, name hashes, RAM image, attribute records, page table, object records | +| Script: statements | **mostly** | assignments, arithmetic, numbers, global `int` variables, attribute references, `if` / `else if` / `else` / `while` / `for`. Not yet: `>=` (inferred only), mixed `&&`/`||`, string attributes and string functions, negative global initial values | +| Script: commands | **39 %** (25/64) | commands whose token was read from a compiled sample | +| Script: system variables and colours | **40 %** (29/72) | same basis | +| Component types (Basic) | **65 %** (13/20) | page, hotspot, text, number, button, picture, crop, progress bar, checkbox, radio, int variable, timer, touch cap. Missing: slider, dual-state button, float, scrolling text, gauge, waveform, QR code | +| Component variants | **partly** | number: all backgrounds and border styles. Button: all 5 styles, solid background only. Text: flat on solid colour only. Progress bar: horizontal, solid colours only. Checkbox: default style only. Page: solid colour only. No global (`vscope`) components, no drag / effects | +| Display directions | **25 %** (1/4) | 90 degrees (landscape) only | +| Display models | **4 %** (1/27) | NX4832T035 only | +| Tested on a real display | **no** | byte-identity with the Editor is the only evidence so far | ## Compatibility diff --git a/docs/COVERAGE.md b/docs/COVERAGE.md index a3f4f67..653c6aa 100644 --- a/docs/COVERAGE.md +++ b/docs/COVERAGE.md @@ -113,7 +113,7 @@ byte for byte, or a file written by nxhmi was opened in the Editor and showed th | Trailer checksum | solved for any length (`tft.trailer`); one model measured | | 3 header checksums (0x44, 0xC4, 0x18C) | solved (`tft.header_checksums`, `tft.seal`) | | Pictures and fonts inside the TFT | solved (FORMAT.md 7.2) | -| Compiled page format | structure solved (code blob, name hashes, RAM image, attribute records, page table, object records -- FORMAT.md 7.3-7.5); per-type tables done for page, hotspot, text (solid background, flat style), int variable, timer, touch cap; the other types need their slot tables and generated draw scripts | +| Compiled page format | structure solved (code blob, name hashes, RAM image, attribute records, page table, object records -- FORMAT.md 7.3-7.5); 13 of 20 Basic types in `nxhmi.tft_types` (page, hotspot, text, number, button, picture, crop, progress bar, checkbox, radio, int variable, timer, touch cap; variants and limits in the README table); missing: slider, dual-state button, float, scrolling text, gauge, waveform, QR code | | Event-code tokens | solved for what was sampled (FORMAT.md 7.6): 27 commands, 33 system variables/colours, numbers, global variables, attribute references, if/else/while/for. Not sampled: string functions and attributes, the remaining commands and system variables, `>=` op code | | Builder (`nxhmi build-tft`) | byte-identical to the Editor for the supported subset (one model: NX4832T035); refuses anything else. **Never tested on a real display** | | Licensing | every `.tft` embeds Nextion's firmware files; the repo must never ship them -- a builder would read them from the user's own Editor installation | diff --git a/docs/FORMAT.md b/docs/FORMAT.md index a0ff9fa..f0d7d0e 100644 --- a/docs/FORMAT.md +++ b/docs/FORMAT.md @@ -418,7 +418,8 @@ tokenised script line; an empty chunk ends a section. (timer: 7.6; slider, touch cap -- **OPEN**); the page's Preinitialize lines; the chunk `09 21 08`; the page's Postinitialize lines; empty chunk. -RAM image: 8 x FF, then every RAM-stored attribute of every component in component order, +RAM image: 4 x FF, one FF per component of the page (the page included) padded with FF to a +multiple of 4, then every RAM-stored attribute of every component in component order, each aligned to its size, each component padded to 4. For a page: `sta` (u8), pad, `bco` (u16). ### 7.4 Attribute records (24 bytes) diff --git a/docs/NOTES.md b/docs/NOTES.md index 0ae4977..6127717 100644 --- a/docs/NOTES.md +++ b/docs/NOTES.md @@ -221,3 +221,14 @@ The generated scripts are ordinary Nextion script compiled by the same compiler attributes written as literals and RAM attributes as record references. `nxhmi.tft_types` therefore describes a type as: visible attributes, record kinds, RAM order, script templates. The RAM order and the unnamed drawing commands still have to be read from samples per type. + +State on 2026-11-17 (paused by the owner mid-way): 13 Basic types compile, 84 samples reproduced. +Page RAM image starts with 4 x FF plus one FF per component padded to 4 (found when a page +with five components failed). Read from samples but not yet in the builder: vertical progress +bar (`sys05_4=val*h/100`, fill from `endy+1-sys05_4`), text with border / crop / picture +background (same `_back` / `_border` rules as number, but `sta`, `style`, `borderc`, `borderw`, +`spax`, `spay` are constants there and are written into the script as literals). +Samples waiting to be compiled: `g3.json` in the session scratchpad (directions 0/180/270, +`>=`, mixed `&&`/`||`, string attributes, negative globals, more variants) -- regenerate with +`tools/make_tft_samples.py` (`direction` and `gradient` keys) if the scratchpad is gone. +New sample pairs live in `D:\Claude\nextion-tft-samples\2026-11-17` (outside the repo). diff --git a/src/nxhmi/tft_build.py b/src/nxhmi/tft_build.py index 234c52a..c70e4b9 100644 --- a/src/nxhmi/tft_build.py +++ b/src/nxhmi/tft_build.py @@ -25,7 +25,7 @@ BLOCK = 0x10000 _RECT = ("x", "y", "w", "h", "endx", "endy") _RECT_REC = {"x": (9, 0x0B, 6000, -6000), "y": (10, 0x0C, 6000, -6000), "w": (11, 0x0D, 2000, -2000), "h": (12, 0x0E, 2000, -2000), "endx": (13, 0x08, 32767, -32768), "endy": (14, 0x08, 32767, -32768)} -_CONST_KINDS = (0x01, 0x02) +_CONST_KINDS = (0x01, 0x02, 0x08) def _chunk(b: bytes) -> bytes: @@ -184,7 +184,9 @@ def compile_pages(pages: list[Page], program: str, nfonts: int = 0, npics: int = section(program_lines + ["page 0"]) records, objects, table = bytearray(), bytearray(), [] for pi, row in enumerate(comps): - ram = bytearray(b"\xff" * 8) + # RAM image: 4 x FF, one FF per component (padded to 4), then the attributes + ram = bytearray(b"\xff" * (4 + 4 * ((len(row) + 3) // 4))) + head = len(ram) prec, first_obj, comp_names, init = bytearray(), len(objects) // tft.OBJ_SIZE, [], [] pre, post = [], [] current[0] = row[0].name @@ -192,7 +194,7 @@ def compile_pages(pages: list[Page], program: str, nfonts: int = 0, npics: int = d, tid, cid = k.d, k.tid, k.v["id"] current[1] = k owner = struct.pack(" position in the object record's map (u16 table at +0x38) MAP = {"vscope": 1, "sta": 2, "bco": 4, "picc": 5, "pic": 6, "pco2": 12, "pco": 16, "val": 20, "bco2": 24, - "font": 25, "xcen": 26, "ycen": 27, "txt": 28, "txt_maxl": 29, "tim": 33, "en": 34, "lenth": 41, "x": 42, - "y": 43, "endx": 44, "endy": 45, "w": 46, "h": 47, "id": 49, "isbr": 54, "spax": 56, "spay": 57, "pw": 58, + "font": 25, "xcen": 26, "dez": 30, "dis": 55, "ycen": 27, "txt": 28, "txt_maxl": 29, "tim": 33, "en": 34, "lenth": 41, "x": 42, + "y": 43, "endx": 44, "endy": 45, "w": 46, "h": 47, "id": 49, "vvs0": 50, "vvs1": 51, "isbr": 54, "spax": 56, "spay": 57, "pw": 58, "style": 59, "borderc": 60, "borderw": 61, "type": 62, "format": 64} U8, U16, COLOR = (0x31, 1, 255, 0), (0x32, 2, 65535, 0), (0x32, 2, 65535, 0) TOUCH = (("down", "codesdown", ()), ("up", "codesup", ())) @@ -143,6 +143,49 @@ def _button(v, s, env): events=(("down", "codesdown", ("@val=1", refresh)), ("up", "codesup", ("@val=0", refresh)))) +def _picture(v, s, env): + return dict(vis=["pic"], rec={"pic": (0x32, 2, "pics", 0)}, ram=["pic"], flag=0x37, events=TOUCH, + draw=["cmd01_4 @x,@y,@pic"]) + + +def _crop(v, s, env): + offset = (0x08, 2, 2147483647, -2147483648) + _need("crop", v, vvs0=0, vvs1=0) # the 6 RAM bytes after picc may hold them + return dict(vis=["picc", "vvs0", "vvs1"], rec={"picc": (0x32, 2, "pics", 0), "vvs0": offset, "vvs1": offset}, + ram=["picc", 6], flag=0x37, events=TOUCH, draw=["cmd0A_4 @x,@y,@w,@h,@x+@vvs0,@y+@vvs1,@picc"]) + + +TOGGLE = ("if(@val==1)", "{", "@val=0", "}else", "{", "@val=1", "}") + + +def _progress_bar(v, s, env): + _need("progress bar", v, sta=0, dez=0) + return dict(vis=["sta", "dez", "val", "dis", "bco", "pco"], + rec={"sta": (0x21, 1, 1, 0), "dez": (0x21, 1, 1, 0), "val": (0x31, 1, 100, 0), "dis": (0x31, 1, 100, 0), + "bco": COLOR, "pco": COLOR}, + ram=["sta", "dez", "val", "dis", "bco", "pco"], flag=0x37, events=TOUCH, + draw=["sys05_4=@val*@w/100", "fill @x,@y,sys05_4,@h,@pco", "fill @x+sys05_4,@y,@w-sys05_4,@h,@bco"]) + + +def _checkbox(v, s, env): + _need("checkbox", v, style=2) + return dict(vis=["style", "bco", "pco", "val"], + rec={"style": (0x21, 1, 3, 0), "bco": COLOR, "pco": COLOR, "val": (0x31, 1, 1, 0)}, + ram=["style", 3, "bco", "pco", "val"], flag=0x37, + draw=["sys05_4=@w/4", "sys07_4=@w-sys05_4-sys05_4", "fill @x,@y,@w,@h,@bco", "if(@val==1)", "{", + "fill @x+sys05_4,@y+sys05_4,sys07_4,sys07_4,@pco", "}", "cmd07_8 @x,@y,@w,@h,16936,59164,1"], + events=(("down", "codesdown", TOGGLE), ("up", "codesup", ()))) + + +def _radio(v, s, env): + return dict(vis=["bco", "pco", "val"], rec={"bco": COLOR, "pco": COLOR, "val": (0x31, 1, 1, 0)}, + ram=["bco", "pco", "val"], flag=0x37, + draw=["sys05_4=@w&1", "if(sys05_4==0)", "{", "sys05_4=@w/2-1", "}else", "{", "sys05_4=@w/2", "}", + "cirs @x+sys05_4,@y+sys05_4,sys05_4,@bco", "cir @x+sys05_4,@y+sys05_4,sys05_4,16936", + "if(@val==1)", "{", "cirs @x+sys05_4,@y+sys05_4,sys05_4/2,@pco", "}"], + events=(("down", "codesdown", TOGGLE), ("up", "codesup", ()))) + + # type id -> (name, has a rectangle and the 19 common attributes, describe) TYPES = { 121: ("page", True, _page), @@ -153,4 +196,9 @@ TYPES = { 116: ("text", True, _text), 54: ("number", True, _number), 98: ("button", True, _button), + 112: ("picture", True, _picture), + 113: ("crop", True, _crop), + 106: ("progress_bar", True, _progress_bar), + 56: ("checkbox", True, _checkbox), + 57: ("radio", True, _radio), } diff --git a/tests/test_tft_build.py b/tests/test_tft_build.py index 30e0c74..1f2b5af 100644 --- a/tests/test_tft_build.py +++ b/tests/test_tft_build.py @@ -138,7 +138,7 @@ class Build(unittest.TestCase): def test_against_editor_samples(self): from nxhmi.container import HMIFile done = 0 - for ref in sorted(glob.glob(os.path.join(SAMPLES, "*.tft"))): + for ref in sorted(glob.glob(os.path.join(SAMPLES, "**", "*.tft"), recursive=True)): hmi = ref[:-4] + ".HMI" if not os.path.exists(hmi): continue @@ -149,6 +149,7 @@ class Build(unittest.TestCase): with open(ref, "rb") as fh: self.assertEqual(out, fh.read(), os.path.basename(ref)) done += 1 + print("%d samples reproduced byte for byte" % done, end=" ") self.assertGreater(done, 0) diff --git a/tools/make_tft_samples.py b/tools/make_tft_samples.py index 30adbd1..6ebf7fd 100644 --- a/tools/make_tft_samples.py +++ b/tools/make_tft_samples.py @@ -25,12 +25,21 @@ from nxhmi.page import Page # noqa: E402 def make(spec): kw = {"program": spec["program"]} if "program" in spec else {} h = project.new_project("NX4832T035_011", **kw) + if "direction" in spec: # 0..3 = 0/90/180/270 degrees (main.HMI byte 12) + from nxhmi import models + from nxhmi.container import build + w, hh = models.page_size("NX4832T035_011", spec["direction"]) + h = build([("main.HMI", project.make_main("NX4832T035_011", [("pa", "0.pa")], "iso-8859-1", spec["direction"])), + ("Program.s", h.read("Program.s")), ("0.pa", C.new_page("page0", w=w, h=hh).pack())]) for height in spec.get("fonts", []): project.add_font(h, fonts.from_ttf(None, height)) for i, (w, hh) in enumerate(spec.get("pics", [])): from PIL import Image b = io.BytesIO() - Image.new("RGB", (w, hh), (200, 40 + 30 * i, 40)).save(b, "PNG") + im = Image.new("RGB", (w, hh), (200, 40 + 30 * i, 40)) + if spec.get("gradient"): # every pixel different: shows how pictures are rotated + im.putdata([((x * 8) & 255, (y * 8) & 255, (x + y) & 255) for y in range(hh) for x in range(w)]) + im.save(b, "PNG") project.add_picture(h, b.getvalue()) for pi, ps in enumerate(spec.get("pages", [spec])): if pi: