tft: MCP tool build_tft, skill note, sample generator, notes for the next types
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
99a5240af0
commit
0532a59120
7 files changed
+1194
-1
No files matched your search
@@ -37,6 +37,11 @@ Back up the user's file first (copy it) unless they said not to; edits write in
|
|||||||
`encoding` + `chars` (only the characters you need) and a TrueType file that has them; decode the
|
`encoding` + `chars` (only the characters you need) and a TrueType file that has them; decode the
|
||||||
project with the same encoding to see the text readably. Pictures: `quality` 1-99 stores JPEG
|
project with the same encoding to see the text readably. Pictures: `quality` 1-99 stores JPEG
|
||||||
(Intelligent only, smaller files).
|
(Intelligent only, smaller files).
|
||||||
|
- `build_tft` compiles to `.tft` without the Editor's compiler, but only for model NX4832T035 and a
|
||||||
|
small subset (pages, hotspots, int variables, timers, pictures, fonts, plain script); it says
|
||||||
|
what it cannot compile. It needs the Editor's install folder for the firmware files. Its
|
||||||
|
output is byte-identical to the Editor's for the test projects but has never been loaded onto
|
||||||
|
a display -- tell the user, and never upload it yourself.
|
||||||
- Pictures: `add_picture` returns an id for `pic` / `picc` (crop) / `pic2` etc. Works for Basic
|
- Pictures: `add_picture` returns an id for `pic` / `picc` (crop) / `pic2` etc. Works for Basic
|
||||||
and Intelligent displays (Intelligent keeps PNG transparency).
|
and Intelligent displays (Intelligent keeps PNG transparency).
|
||||||
- Event code is Nextion script, one line per list item, e.g. `["page settings"]`,
|
- Event code is Nextion script, one line per list item, e.g. `["page settings"]`,
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ examples/make_demo.py builds demo/ (demo.HMI, decoded/, render/)
|
|||||||
.mcp.json registers the MCP server for Claude Code in this repo
|
.mcp.json registers the MCP server for Claude Code in this repo
|
||||||
.claude/skills/nextion-hmi/SKILL.md how to use the tools on a user's project (keep in sync with mcp_server.py)
|
.claude/skills/nextion-hmi/SKILL.md how to use the tools on a user's project (keep in sync with mcp_server.py)
|
||||||
tests/ unittest suite; fixtures/demo_editor_saved.HMI = the demo re-saved by the Editor
|
tests/ unittest suite; fixtures/demo_editor_saved.HMI = the demo re-saved by the Editor
|
||||||
tools/ template generator
|
tools/ template generator; make_tft_samples.py (study projects for the .tft compiler)
|
||||||
docs/ FORMAT.md (spec), RENDER.md, COVERAGE.md, NOTES.md (open points, what was tried)
|
docs/ FORMAT.md (spec), RENDER.md, COVERAGE.md, NOTES.md (open points, what was tried)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -168,3 +168,21 @@ Not done: sliders, touch cap and every drawing component (text, number, button,
|
|||||||
gauge, waveform, picture, crop, checkbox, radio, QR code ...); string variables and string
|
gauge, waveform, picture, crop, checkbox, radio, QR code ...); string variables and string
|
||||||
functions; page backgrounds other than a solid colour; global (vscope) components; other
|
functions; page backgrounds other than a solid colour; global (vscope) components; other
|
||||||
models, portrait, other series; and nothing has been loaded onto a display.
|
models, portrait, other series; and nothing has been loaded onto a display.
|
||||||
|
|
||||||
|
Samples: `tools/make_tft_samples.py` writes the 63 study projects (`tools/tft_samples.json`);
|
||||||
|
45 are reproduced, the other 18 are one-component projects of the types still to do (their
|
||||||
|
Editor output is needed again if the scratch copies are gone: ~45 s of Editor time each).
|
||||||
|
|
||||||
|
Starting points for the next component types (from those samples, not yet in the builder):
|
||||||
|
|
||||||
|
- Record slots after 14 follow the type's own attribute list; the RAM order is not the slot
|
||||||
|
order (text: font, isbr, pco, bco, xcen, ycen, pw, then txt with `txt_maxl + 1` bytes).
|
||||||
|
Kinds seen: 0x21/0x31 u8, 0x22/0x32 u16, 0x39 i32, 0x3F string (size = bytes reserved),
|
||||||
|
0x02 `txt_maxl`.
|
||||||
|
- Text draw section: `09 1C 08 x,y,w,h,<font>,<pco>,<bco>,<xcen>,<ycen>,1,<txt>,0,0,<isbr>,0`
|
||||||
|
then `09 13 04` with four `03 7FFF` numbers and a record reference; every `<..>` is a
|
||||||
|
record reference, so changing an attribute at run time needs no recompilation.
|
||||||
|
- Progress bar draw section: `<sys 0005/04>=<val>*w/100`, then two fills (`09 0D 04`) for the
|
||||||
|
filled and the empty part. Gauge: one `09 0A 08` line. QR code: one `09 08 08` line.
|
||||||
|
- Slider and touch cap add the init line `09 16 04 1`; slider has a `slide` event label.
|
||||||
|
- Object flag byte: 0x37 types with a rectangle, 0x27 timer and touch cap, 0x07 variable.
|
||||||
@@ -158,6 +158,17 @@ def t_add_picture(path, image_path, quality=100):
|
|||||||
return {"picture_id": pid}
|
return {"picture_id": pid}
|
||||||
|
|
||||||
|
|
||||||
|
def t_build_tft(path, tft_path, editor_dir=None):
|
||||||
|
from . import tft_build
|
||||||
|
try:
|
||||||
|
out = tft_build.build_tft(_load(path), editor_dir or tft_build.DEFAULT_EDITOR_DIR)
|
||||||
|
except tft_build.Unsupported as e:
|
||||||
|
raise ValueError("cannot compile this project yet: %s" % e)
|
||||||
|
with open(tft_path, "wb") as f:
|
||||||
|
f.write(out)
|
||||||
|
return {"tft": tft_path, "bytes": len(out)}
|
||||||
|
|
||||||
|
|
||||||
def t_add_font(path, height, ttf_path=None, encoding="ascii", chars=None):
|
def t_add_font(path, height, ttf_path=None, encoding="ascii", chars=None):
|
||||||
from . import fonts
|
from . import fonts
|
||||||
h = _load(path)
|
h = _load(path)
|
||||||
@@ -248,6 +259,11 @@ TOOLS = {
|
|||||||
"(then give chars, and a ttf_path that has those characters; the project's encoding "
|
"(then give chars, and a ttf_path that has those characters; the project's encoding "
|
||||||
"should match).",
|
"should match).",
|
||||||
{"path": S, "height": {"type": "integer"}, "ttf_path": S, "encoding": S, "chars": S}, ["path", "height"]),
|
{"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 "
|
||||||
|
"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"]),
|
||||||
"verify": (t_verify, "Check every checksum in the project.", {"path": S}, ["path"]),
|
"verify": (t_verify, "Check every checksum in the project.", {"path": S}, ["path"]),
|
||||||
"list_kinds": (t_kinds, "Component kinds that can be added.", {}, []),
|
"list_kinds": (t_kinds, "Component kinds that can be added.", {}, []),
|
||||||
"schema": (t_schema, "Attributes of a component kind: names, types, defaults, allowed values.", {"kind": S}, ["kind"]),
|
"schema": (t_schema, "Attributes of a component kind: names, types, defaults, allowed values.", {"kind": S}, ["kind"]),
|
||||||
|
|||||||
@@ -77,6 +77,24 @@ class Tools(unittest.TestCase):
|
|||||||
self.assertTrue(r["result"]["isError"])
|
self.assertTrue(r["result"]["isError"])
|
||||||
self.assertIn("pages: home", r["result"]["content"][0]["text"])
|
self.assertIn("pages: home", r["result"]["content"][0]["text"])
|
||||||
|
|
||||||
|
def test_build_tft(self):
|
||||||
|
from nxhmi import project, tft, tft_build
|
||||||
|
for i, n in enumerate(tft_build.FIRMWARE_FILES): # stand-ins for the Editor's files
|
||||||
|
with open(os.path.join(self.tmp, n), "wb") as f:
|
||||||
|
f.write(bytes([i]) * 50)
|
||||||
|
p, out = os.path.join(self.tmp, "min.HMI"), os.path.join(self.tmp, "min.tft")
|
||||||
|
project.new_project("NX4832T035_011").save(p)
|
||||||
|
r = self.j("build_tft", path=p, tft_path=out, editor_dir=self.tmp)
|
||||||
|
with open(out, "rb") as f:
|
||||||
|
data = f.read()
|
||||||
|
self.assertEqual(r["bytes"], len(data))
|
||||||
|
self.assertTrue(tft.verify_header(data) and tft.verify_trailer(data))
|
||||||
|
# the demo uses component types that cannot be compiled yet: a tool error, not a crash
|
||||||
|
r = M.handle({"jsonrpc": "2.0", "id": 1, "method": "tools/call",
|
||||||
|
"params": {"name": "build_tft", "arguments": {"path": self.hmi, "tft_path": out,
|
||||||
|
"editor_dir": self.tmp}}})
|
||||||
|
self.assertTrue(r["result"]["isError"])
|
||||||
|
|
||||||
def test_schema(self):
|
def test_schema(self):
|
||||||
s = self.j("schema", kind="text")
|
s = self.j("schema", kind="text")
|
||||||
xcen = next(a for a in s["attributes"] if a["name"] == "xcen")
|
xcen = next(a for a in s["attributes"] if a["name"] == "xcen")
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
"""Generate the tiny projects used to study and regression-test the .tft compiler.
|
||||||
|
|
||||||
|
python tools/make_tft_samples.py OUT_DIR
|
||||||
|
|
||||||
|
Writes one <name>.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 <name>.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 = {"<component index>:<event group>": [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)
|
||||||
|
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")
|
||||||
|
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))
|
||||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user