"""Generate the tiny projects used to study and regression-test the .tft compiler. python tools/make_tft_samples.py OUT_DIR Writes one .HMI per entry of tools/tft_samples.json. Compile each in Nextion Editor (Compile, then File > "TFT file output" > Output) and put the result next to it as .tft; then NXHMI_TFT_SAMPLES=OUT_DIR python -m unittest discover -s tests -p test_tft_build.py checks that nxhmi.tft_build reproduces every one it supports byte for byte. The .tft files embed Nextion firmware: keep OUT_DIR outside the repo. Entry keys: fonts [heights], pics [[w, h], ...], program (Program.s text), and either page/comps/events for a single page or pages [{name, page, comps, events}, ...]; comps = [[kind, {attributes}], ...], events = {":": [lines]}. """ import io import json import os import sys sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src")) from nxhmi import components as C, fonts, project # noqa: E402 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() 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: project.add_page(h, ps.get("name", "page%d" % pi)) entry = "%d.pa" % pi p = Page.parse(h.read(entry)) for k, v in ps.get("page", {}).items(): p.components[0].set(k, v) for kind, attrs in ps.get("comps", []): C.add(p, kind, **attrs) for key, lines in ps.get("events", {}).items(): i, group = key.split(":") p.components[int(i)].set_event(group, lines) h.write(entry, p.pack()) return h if __name__ == "__main__": out = sys.argv[1] os.makedirs(out, exist_ok=True) with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "tft_samples.json")) as f: specs = json.load(f) for name, spec in specs.items(): make(spec).save(os.path.join(out, name + ".HMI")) print("%d projects written to %s" % (len(specs), out))