openhmi-nx: decode, encode, render Nextion HMI projects; .tft research
Black-box reverse-engineered .HMI format (container, three checksums, pages, all 31 component types, pictures, fonts), readable JSON, page renderer matching Nextion Editor, demo project built from code and a test suite (66 tests). See docs/COVERAGE.md for status. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
69e22cfb06
70 files changed
+25058
No files matched your search
@@ -0,0 +1,91 @@
|
||||
import contextlib
|
||||
import io
|
||||
import os
|
||||
import shutil
|
||||
import struct
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from helpers import FIXTURE, HAVE_PIL, bitwise_word_crc
|
||||
|
||||
from nxhmi import cli, tft
|
||||
|
||||
|
||||
def fake_tft(payload: bytes) -> bytes:
|
||||
"""A synthetic .tft-shaped blob: layout table + trailer CRC computed the way the trailer behaves
|
||||
(word-per-byte CRC whose stream ends at the trailer). Real .tft files cannot be used in tests:
|
||||
they contain Nextion's firmware."""
|
||||
head = bytearray(0x200)
|
||||
struct.pack_into("<HH", head, 0x0C, 480, 320)
|
||||
for i, o in enumerate(range(tft.LAYOUT_START, tft.LAYOUT_END, 4)):
|
||||
struct.pack_into("<I", head, o, (i * 1000) ^ tft.LAYOUT_KEY)
|
||||
body = bytes(head) + payload
|
||||
return body + struct.pack("<I", bitwise_word_crc(body, 0x12345678))
|
||||
|
||||
|
||||
class Tft(unittest.TestCase):
|
||||
def test_layout_table(self):
|
||||
t = fake_tft(b"x" * 10)
|
||||
self.assertEqual(tft.layout_table(t)[:3], [0, 1000, 2000])
|
||||
self.assertEqual(tft.header_info(t)["width"], 480)
|
||||
|
||||
def test_patch_trailer_same_length(self):
|
||||
old = fake_tft(b"page data: colour ff ff")
|
||||
new_body = old[:-4].replace(b"ff ff", b"00 f8")
|
||||
expect = fake_tft(b"page data: colour 00 f8")
|
||||
self.assertEqual(tft.patch_trailer(old, new_body + b"\0\0\0\0"), expect)
|
||||
|
||||
def test_patch_trailer_rejects_length_change(self):
|
||||
with self.assertRaises(ValueError):
|
||||
tft.patch_trailer(b"\0" * 20, b"\0" * 24)
|
||||
|
||||
|
||||
class Cli(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.mkdtemp()
|
||||
self.hmi = os.path.join(self.tmp, "p.HMI")
|
||||
shutil.copy(FIXTURE, self.hmi)
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tmp)
|
||||
|
||||
def run_cli(self, *args):
|
||||
out = io.StringIO()
|
||||
with contextlib.redirect_stdout(out):
|
||||
rc = cli.main(list(args))
|
||||
return rc, out.getvalue()
|
||||
|
||||
def test_info_verify_dump(self):
|
||||
self.assertIn("home", self.run_cli("info", self.hmi)[1])
|
||||
self.assertEqual(self.run_cli("verify", self.hmi)[0], 0)
|
||||
self.assertIn('"title"', self.run_cli("dump", self.hmi, "--page", "home")[1])
|
||||
|
||||
def test_decode_encode(self):
|
||||
d = os.path.join(self.tmp, "dec")
|
||||
self.run_cli("decode", self.hmi, d)
|
||||
self.run_cli("encode", d, os.path.join(self.tmp, "re.HMI"))
|
||||
self.assertEqual(self.run_cli("verify", os.path.join(self.tmp, "re.HMI"))[0], 0)
|
||||
|
||||
def test_set_add_newpage_kinds(self):
|
||||
self.run_cli("set", self.hmi, "home", "title", "txt", "hello")
|
||||
self.run_cli("add", self.hmi, "home", "button", "x=1", "y=2", "w=30", "h=20", "txt=ok", "--name", "bNew")
|
||||
self.run_cli("new-page", self.hmi, "extra")
|
||||
out = self.run_cli("info", self.hmi)[1]
|
||||
self.assertIn("extra", out)
|
||||
self.assertEqual(self.run_cli("verify", self.hmi)[0], 0)
|
||||
self.assertIn("button", self.run_cli("kinds")[1])
|
||||
|
||||
@unittest.skipUnless(HAVE_PIL, "needs Pillow")
|
||||
def test_add_picture_font_render(self):
|
||||
from PIL import Image
|
||||
png = os.path.join(self.tmp, "i.png")
|
||||
Image.new("RGB", (8, 8), (1, 2, 3)).save(png)
|
||||
self.run_cli("add-picture", self.hmi, png)
|
||||
self.assertEqual(self.run_cli("verify", self.hmi)[0], 0)
|
||||
out = os.path.join(self.tmp, "png")
|
||||
self.run_cli("render", self.hmi, out)
|
||||
self.assertTrue(os.path.exists(os.path.join(out, "home.png")))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user