tft: text (plain) and touch cap; 55 Editor samples reproduced byte for byte
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
0532a59120
commit
b5eaa68298
7 files changed
+102
-16
No files matched your search
@@ -260,7 +260,7 @@ TOOLS = {
|
||||
"should match).",
|
||||
{"path": S, "height": {"type": "integer"}, "ttf_path": S, "encoding": S, "chars": S}, ["path", "height"]),
|
||||
"build_tft": (t_build_tft, "Compile the project to a .tft firmware file. Research stage: model NX4832T035 only; "
|
||||
"pages, hotspots, int variables, timers, pictures, fonts and plain script; anything else is "
|
||||
"pages, hotspots, plain text, int variables, timers, touch caps, pictures, fonts and plain script; anything else is "
|
||||
"refused with the reason. Needs a Nextion Editor installation (editor_dir) for the firmware "
|
||||
"files. The output has never been tried on a real display.",
|
||||
{"path": S, "tft_path": S, "editor_dir": S}, ["path", "tft_path"]),
|
||||
|
||||
+53
-6
@@ -23,12 +23,22 @@ SUPPORTED_MODEL = 0xB23BC018 # NX4832T035_011, the only model measured
|
||||
BLOCK = 0x10000
|
||||
|
||||
# attribute map positions in the object record (u16 table at +0x38)
|
||||
MAP = {"vscope": 1, "sta": 2, "bco": 4, "val": 20, "tim": 33, "en": 34, "x": 42, "y": 43, "endx": 44, "endy": 45, "w": 46, "h": 47,
|
||||
MAP = {"vscope": 1, "sta": 2, "bco": 4, "pco": 16, "val": 20, "font": 25, "xcen": 26, "ycen": 27, "txt": 28,
|
||||
"txt_maxl": 29, "tim": 33, "en": 34, "isbr": 54, "spax": 56, "spay": 57, "pw": 58, "style": 59, "x": 42, "y": 43, "endx": 44, "endy": 45, "w": 46, "h": 47,
|
||||
"id": 49, "type": 62}
|
||||
# attribute record templates: name -> (slot, kind, size, max, min)
|
||||
_COMMON = {"type": (0, 0x01, 1, 255, 0), "id": (1, 0x01, 1, 255, 0), "vscope": (3, 0x01, 1, 1, 0)}
|
||||
_RECT = {"x": (9, 0x0B, 2, 6000, -6000), "y": (10, 0x0C, 2, 6000, -6000), "w": (11, 0x0D, 2, 2000, -2000),
|
||||
"h": (12, 0x0E, 2, 2000, -2000), "endx": (13, 0x08, 2, 32767, -32768), "endy": (14, 0x08, 2, 32767, -32768)}
|
||||
|
||||
|
||||
def _text_draw(rect, ref):
|
||||
"""Draw section of a text component (solid background, no border)."""
|
||||
line = (b"\x09\x1c\x08" + ("%d,%d,%d,%d," % tuple(rect[:4])).encode() + ref("font") + b"," + ref("pco") + b","
|
||||
+ ref("bco") + b"," + ref("xcen") + b"," + ref("ycen") + b",1," + ref("isbr") + b",0,0," + ref("pw") + b",0")
|
||||
return [line, b"\x09\x13\x04" + b"\x03\xff\x7f\x00\x00," * 4 + ref("txt")]
|
||||
|
||||
|
||||
# type id -> description. ``ram`` attributes live in the page's RAM image (in this order);
|
||||
# ``owner``: whether records name the component to refresh when the attribute changes.
|
||||
TYPES = {
|
||||
@@ -40,6 +50,17 @@ TYPES = {
|
||||
ram={"val": (8, 0x39, 4, 2147483647, -2147483648)}, owner=False),
|
||||
51: dict(name="timer", slots=11, flag=0x27, group=16, const=dict(_COMMON),
|
||||
ram={"tim": (7, 0x32, 2, 65534, 50), "en": (8, 0x31, 1, 1, 0)}, owner=True, action={"tim": 1, "en": 2}),
|
||||
# text: only the plain form (sta = solid colour, style = flat, no extra spacing) was measured
|
||||
116: dict(name="text", slots=41, flag=0x37, group=16,
|
||||
const={**_COMMON, **_RECT, "sta": (19, 0x01, 1, 2, 0), "style": (20, 0x01, 1, 3, 0),
|
||||
"txt_maxl": (29, 0x02, 2, 65534, 0), "spax": (31, 0x01, 1, 255, 0), "spay": (32, 0x01, 1, 255, 0)},
|
||||
ram={"font": (22, 0x31, 1, "fonts", 0), "isbr": (30, 0x31, 1, 1, 0), "pco": (24, 0x32, 2, 65535, 0),
|
||||
"bco": (23, 0x32, 2, 65535, 0), "xcen": (25, 0x31, 1, 2, 0), "ycen": (26, 0x31, 1, 2, 0),
|
||||
"pw": (27, 0x31, 1, 1, 0), "txt": (28, 0x3F, 0, 0, 0)},
|
||||
owner=True, need={"sta": 1, "style": 0, "spax": 0, "spay": 0}, draw=_text_draw),
|
||||
# touch cap: down/up events without a rectangle; one init line (only one per page was measured)
|
||||
5: dict(name="touch_cap", slots=10, flag=0x27, group=16, const=dict(_COMMON),
|
||||
ram={"val": (7, 0x21, 1, 65535, 0)}, owner=True, events=True, init=b"\x09\x16\x041"),
|
||||
}
|
||||
|
||||
|
||||
@@ -95,7 +116,7 @@ def resource_area(hmi: HMIFile, editor_dir: str) -> tuple[bytes, dict]:
|
||||
return area, info
|
||||
|
||||
|
||||
def compile_pages(pages: list[Page], program: str):
|
||||
def compile_pages(pages: list[Page], program: str, nfonts: int = 0):
|
||||
"""-> (code blob, global variable descriptors, attribute records, page table, object records,
|
||||
program offset)."""
|
||||
names = sorted((tft.name_hash(p.components[0].get("objname")), i) for i, p in enumerate(pages))
|
||||
@@ -135,7 +156,9 @@ def compile_pages(pages: list[Page], program: str):
|
||||
return None
|
||||
first, t = where[key]
|
||||
slot = {**t["const"], **t["ram"]}.get(name)
|
||||
return None if slot is None else first + slot[0]
|
||||
if slot is None or slot[1] == 0x3F: # string attributes in script: not measured
|
||||
return None
|
||||
return first + slot[0]
|
||||
|
||||
ctx = tft_code.Context(offsets, {k[1] for k in where}, attr)
|
||||
|
||||
@@ -166,6 +189,9 @@ def compile_pages(pages: list[Page], program: str):
|
||||
raise Unsupported("string variables")
|
||||
if _u(a["vscope"]) != 0:
|
||||
raise Unsupported("global components")
|
||||
for name, value in t.get("need", {}).items():
|
||||
if _u(a[name]) != value:
|
||||
raise Unsupported("%s with %s=%d" % (t["name"], name, _u(a[name])))
|
||||
cid = _u(a["id"])
|
||||
owner = struct.pack("<BB", pi, cid) if t["owner"] else b"\xff\xff"
|
||||
slot0 = len(prec) // tft.REC_SIZE
|
||||
@@ -176,7 +202,16 @@ def compile_pages(pages: list[Page], program: str):
|
||||
recs[slot] = struct.pack("<Iiii2sBBBBH", group, _s(a[name], lo < 0), hi, lo, owner, 0, 0, kind, size, 0)
|
||||
amap[MAP[name]] = slot0 + slot
|
||||
for name, (slot, kind, size, hi, lo) in t["ram"].items():
|
||||
ram += bytes(-len(ram) % min(size, 4))
|
||||
if kind == 0x3F: # string: txt_maxl + 1 bytes, not aligned
|
||||
size = _u(a["txt_maxl"]) + 1
|
||||
if size > 255 or len(a[name].value) >= size:
|
||||
raise Unsupported("text longer than txt_maxl, or txt_maxl > 254")
|
||||
else:
|
||||
ram += bytes(-len(ram) % min(size, 4))
|
||||
if hi == "fonts":
|
||||
hi = nfonts - 1
|
||||
if _u(a[name]) > hi:
|
||||
raise Unsupported("font %d does not exist" % _u(a[name]))
|
||||
recs[slot] = struct.pack("<Iiii2sBBBBH", group, g + 4 + len(ram), hi, lo, owner, 0,
|
||||
t.get("action", {}).get(name, 0), kind, size, 0)
|
||||
amap[MAP[name]] = slot0 + slot
|
||||
@@ -197,14 +232,25 @@ def compile_pages(pages: list[Page], program: str):
|
||||
section([])
|
||||
section([])
|
||||
else:
|
||||
if "draw" in t:
|
||||
draw = len(blob)
|
||||
table_ = {**t["const"], **t["ram"]}
|
||||
for raw in t["draw"](rect, lambda name: b"\x01" + struct.pack(
|
||||
"<I", rec_base + slot0 + table_[name][0])):
|
||||
blob += _chunk(raw)
|
||||
section([])
|
||||
if t["flag"] & 0x10:
|
||||
erase = len(blob)
|
||||
blob += _chunk(fill)
|
||||
section([])
|
||||
code_at = [0xFFFFFFFF] * 4 # down, up, timer, unload (object +0x0C..)
|
||||
touch = t["flag"] & 0x10 or t.get("events")
|
||||
if "init" in t:
|
||||
if t["init"] in init:
|
||||
raise Unsupported("two %s components on one page" % t["name"])
|
||||
init.append(t["init"])
|
||||
for i, (label, group_name, wanted) in enumerate((
|
||||
(b"down", "codesdown", t["flag"] & 0x10), (b"up", "codesup", t["flag"] & 0x10),
|
||||
(b"down", "codesdown", touch), (b"up", "codesup", touch),
|
||||
(None, None, False), (b"unload", "codesunload", tid == 121))):
|
||||
if wanted:
|
||||
if ev.get(group_name):
|
||||
@@ -257,7 +303,8 @@ def build_tft(hmi: HMIFile, editor_dir: str = DEFAULT_EDITOR_DIR) -> bytes:
|
||||
raise Unsupported("display direction %d" % pm["direction"])
|
||||
w, h = project_size(hmi)
|
||||
pages = [Page.parse(hmi.read(name)) for rtype, name in pm["resources"] if rtype == "pa"]
|
||||
blob, descriptors, records, ptable, objects, program_off = compile_pages(pages, hmi.read("Program.s").decode("latin-1"))
|
||||
blob, descriptors, records, ptable, objects, program_off = compile_pages(
|
||||
pages, hmi.read("Program.s").decode("latin-1"), sum(1 for x in hmi.names() if re.fullmatch(r"\d+\.zi", x)))
|
||||
area, info = resource_area(hmi, editor_dir)
|
||||
base = tft.RES_START + len(area)
|
||||
body = blob + descriptors + records + ptable + objects
|
||||
|
||||
Reference in new issue
Block a user