All five display series; exact component colours; Editor's literal rule for format 4
- 15 more models (Discovery, Enhanced, Edge, NX8048P043): ids and series bytes 1/6/100 from the Editor; native orientation per series; Discovery/Enhanced/Edge use the Basic templates - per series picture format and storage direction: Enhanced like Basic, Edge format 4, Discovery format 3 (20 zero bytes + raw RGB565) always stored row by row; fonts likewise - component colours are scaled (v*255//31) on every series -- exact against the canvas with zero tolerance; format-0 pictures stay bit-replicated - format-4 encoder: literal pixels when the next 8 runs cover <= 10 pixels (7+ runs), 8 pixels at a time -- reproduces the Editor on 12 designed patterns plus the 28 earlier samples Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
922ca21f0f
commit
ec35ceb615
9 files changed
+145
-62
No files matched your search
+35
-4
@@ -1,7 +1,8 @@
|
||||
"""Display models: id stored in main.HMI (offset 16), series byte (offset 11), native resolution.
|
||||
|
||||
Only series whose series byte was confirmed in the Editor are listed (Basic = 0, Intelligent = 3).
|
||||
Model ids are the 32-bit values the Editor writes for each model.
|
||||
Every model in Nextion Editor 1.68's model dialog, with the series byte and model id the Editor
|
||||
writes (Basic = 0, Enhanced = 1, Intelligent = 3, Edge = 6, Discovery = 100).
|
||||
Sizes are listed landscape (width >= height).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -19,14 +20,32 @@ MODELS = {
|
||||
"NX8048P070_011": (0xDA27BAA7, 3, 800, 480),
|
||||
"NX1060P070_011": (0xC43473C2, 3, 1024, 600),
|
||||
"NX1060P101_011": (0x4FC44FA0, 3, 1024, 600),
|
||||
"NX8048P043_011": (0xA859A323, 3, 800, 480),
|
||||
# Discovery (F)
|
||||
"NX3224F024_011": (0x5B49C1BC, 100, 320, 240),
|
||||
"NX3224F028_011": (0xBA9C8E20, 100, 320, 240),
|
||||
"NX4832F035_011": (0x1CE47603, 100, 480, 320),
|
||||
# Enhanced (K)
|
||||
"NX3224K024_011": (0x5C7E9301, 1, 320, 240),
|
||||
"NX3224K028_011": (0xBDABDC9D, 1, 320, 240),
|
||||
"NX4024K032_011": (0xC4A1A90F, 1, 400, 240),
|
||||
"NX4832K035_011": (0x1BD324BE, 1, 480, 320),
|
||||
"NX4827K043_011": (0x63C18A17, 1, 480, 272),
|
||||
"NX8048K050_011": (0x2E45DE45, 1, 800, 480),
|
||||
"NX8048K070_011": (0xA1F7596A, 1, 800, 480),
|
||||
# Edge (E)
|
||||
"NX4848E021_011": (0xA2770AFE, 6, 480, 480),
|
||||
"NX4848E028_011": (0x0B3DB14B, 6, 480, 480),
|
||||
"NX4848E040_011": (0x21B2AF65, 6, 480, 480),
|
||||
"NX4880E043_011": (0xB7A00D7C, 6, 800, 480),
|
||||
}
|
||||
BY_ID = {v[0]: k for k, v in MODELS.items()}
|
||||
SERIES = {0: "Basic", 3: "Intelligent"}
|
||||
SERIES = {0: "Basic", 1: "Enhanced", 3: "Intelligent", 6: "Edge", 100: "Discovery"}
|
||||
|
||||
# main.HMI offset 12: display direction, in 90-degree steps as chosen in the Editor
|
||||
# (Setting > Display: 0 / 90 / 180 / 270). Basic panels are portrait at 0 degrees, so the
|
||||
# landscape sizes listed above need 90 or 270; Intelligent panels are landscape at 0 and 180.
|
||||
NATIVE_PORTRAIT = {0: True, 3: False}
|
||||
NATIVE_PORTRAIT = {0: True, 1: True, 3: False, 6: True, 100: True} # as listed in the model dialog
|
||||
|
||||
|
||||
def direction_byte(model: str, landscape: bool = True, flipped: bool = False) -> int:
|
||||
@@ -35,6 +54,18 @@ def direction_byte(model: str, landscape: bool = True, flipped: bool = False) ->
|
||||
return (1 if landscape == portrait_native else 0) + (2 if flipped else 0)
|
||||
|
||||
|
||||
def storage_direction(series: int, direction: int) -> int:
|
||||
"""Direction pictures and fonts are stored in (their byte 0 / 3 = 0x0A + this): the project
|
||||
direction, except Discovery, which always stores row by row (0) -- measured in Editor 1.68."""
|
||||
return 0 if series == 100 else direction % 4
|
||||
|
||||
|
||||
def picture_format(series: int) -> int:
|
||||
"""N.i format the Editor writes: 0 raw (Basic, Enhanced), 4 run-length/JPEG (Intelligent, Edge),
|
||||
3 raw with a 20-byte zero header (Discovery)."""
|
||||
return {3: 4, 6: 4, 100: 3}.get(series, 0)
|
||||
|
||||
|
||||
def page_size(model: str, direction: int):
|
||||
"""(width, height) of the pages for main.HMI byte 12 = direction."""
|
||||
_, series, w, h = lookup(model)
|
||||
|
||||
+50
-37
@@ -158,51 +158,56 @@ def encode_format4(pixels_rgba, width: int, height: int, direction: int = 0) ->
|
||||
"""Format-4 ``N.i`` (Intelligent series), byte-identical to Nextion Editor 1.68's output.
|
||||
pixels_rgba: callable (x, y) -> (r, g, b[, a]). Colours run in groups of up to 13 pixels;
|
||||
eight ops make a block; blocks of eight 13-runs of one colour merge into a long run (op 14),
|
||||
blocks of eight single pixels into a literal block (op 15)."""
|
||||
a stretch where the next 8 runs cover at most 10 pixels (7+ runs, 8+ pixels left) becomes
|
||||
literal pixels, 8 at a time (op 15) -- measured on designed run patterns."""
|
||||
order = stored_order(width, height, direction)
|
||||
rgba = [tuple(pixels_rgba(x, y)) + (255,) for x, y in order]
|
||||
px = [_565(c) if c[3] else 0 for c in rgba]
|
||||
alpha = [c[3] * 127 // 255 for c in rgba]
|
||||
has_alpha = any(a != 127 for a in alpha)
|
||||
n = len(px)
|
||||
ops, i = [], 0
|
||||
while i < n:
|
||||
j = i
|
||||
while j < n and px[j] == px[i] and j - i < 13:
|
||||
j += 1
|
||||
ops.append((px[i], j - i))
|
||||
i = j
|
||||
blocks = [ops[k:k + 8] for k in range(0, len(ops), 8)]
|
||||
pw, ph = (height, width) if direction % 2 else (width, height)
|
||||
out = bytearray(struct.pack("<IIIII", 1, pw, ph, 0, 0))
|
||||
|
||||
def long_run(b, colour):
|
||||
return len(b) == 8 and all(c == colour and length == 13 for c, length in b)
|
||||
|
||||
def literal(b):
|
||||
return len(b) == 8 and all(length == 1 for _, length in b)
|
||||
k = 0
|
||||
while k < len(blocks):
|
||||
b = blocks[k]
|
||||
if long_run(b, b[0][0]):
|
||||
m = k
|
||||
while m < len(blocks) and long_run(blocks[m], b[0][0]) and (m - k + 1) * 104 <= 0xFFF:
|
||||
m += 1
|
||||
out += struct.pack("<I8H", 14 | (m - k) * 104 << 4, *[b[0][0]] * 8)
|
||||
k = m
|
||||
elif literal(b):
|
||||
m = k
|
||||
while m < len(blocks) and literal(blocks[m]) and (m - k + 1) * 8 <= 0xFFF:
|
||||
m += 1
|
||||
cols = [c for bb in blocks[k:m] for c, _ in bb]
|
||||
out += struct.pack("<I%dH" % len(cols), 15 | len(cols) << 4, *cols)
|
||||
k = m
|
||||
else:
|
||||
ctrl = 0
|
||||
for slot, (_, length) in enumerate(b):
|
||||
ctrl |= length << (4 * slot)
|
||||
out += struct.pack("<I8H", ctrl, *([c for c, _ in b] + [0] * (8 - len(b))))
|
||||
k += 1
|
||||
def next_ops(i):
|
||||
ops = []
|
||||
while i < n and len(ops) < 8:
|
||||
j = i
|
||||
while j < n and px[j] == px[i] and j - i < 13:
|
||||
j += 1
|
||||
ops.append((px[i], j - i))
|
||||
i = j
|
||||
return ops
|
||||
i = 0
|
||||
while i < n:
|
||||
ops = next_ops(i)
|
||||
if len(ops) >= 7 and sum(l for _, l in ops) <= 10 and n - i >= 8:
|
||||
# busy stretch: literal pixels, 8 at a time, merged while the rule keeps holding
|
||||
k = i
|
||||
while k < n and n - k >= 8 and k - i + 8 <= 0xFFF:
|
||||
o = next_ops(k)
|
||||
if not (len(o) >= 7 and sum(l for _, l in o) <= 10):
|
||||
break
|
||||
k += 8
|
||||
out += struct.pack("<I%dH" % (k - i), 15 | (k - i) << 4, *px[i:k])
|
||||
i = k
|
||||
continue
|
||||
if len(ops) == 8 and all(l == 13 and c == ops[0][0] for c, l in ops):
|
||||
# eight 13-runs of one colour: long run (op 14), consecutive blocks merge
|
||||
k, total = i, 0
|
||||
while total + 104 <= 0xFFF:
|
||||
o = next_ops(k)
|
||||
if not (len(o) == 8 and all(l == 13 and c == ops[0][0] for c, l in o)):
|
||||
break
|
||||
k += 104; total += 104
|
||||
out += struct.pack("<I8H", 14 | total << 4, *[ops[0][0]] * 8)
|
||||
i = k
|
||||
continue
|
||||
ctrl = 0
|
||||
for slot, (_, length) in enumerate(ops):
|
||||
ctrl |= length << (4 * slot)
|
||||
out += struct.pack("<I8H", ctrl, *([c for c, _ in ops] + [0] * (8 - len(ops))))
|
||||
i += sum(l for _, l in ops)
|
||||
aoff = 0
|
||||
if has_alpha:
|
||||
aoff = len(out)
|
||||
@@ -218,7 +223,11 @@ def from_image_file(data: bytes, direction: int = 1, series: int = 0, quality: i
|
||||
img = Image.open(io.BytesIO(data))
|
||||
ftype = (img.format or "png").lower()[:3]
|
||||
w, h = img.size
|
||||
if series == 3 and w * h > 2: # the Editor keeps 1-2 pixel pictures in format 0
|
||||
if series == 100: # Discovery: format 3 = 20 zero bytes + raw RGB565
|
||||
rgb = img.convert("RGB").load()
|
||||
pic = make_picture(bytes(20) + to_rgb565(lambda x, y: rgb[x, y], w, h, direction), w, h, direction)
|
||||
pic = pic[:3] + bytes([3]) + pic[4:]
|
||||
elif series in (3, 6) and w * h > 2: # the Editor keeps 1-2 pixel pictures in format 0
|
||||
rgba = img.convert("RGBA").load()
|
||||
if quality < 100:
|
||||
pic = encode_jpeg(lambda x, y: rgba[x, y], w, h, direction, quality)
|
||||
@@ -314,6 +323,8 @@ def decode_format4(blob: bytes):
|
||||
|
||||
|
||||
def _rgb(v: int):
|
||||
"""Format-0 picture pixel RGB565 -> RGB as the Editor canvas shows it: bit replication
|
||||
(component colours and format-4 pictures are scaled instead, v * 255 // max)."""
|
||||
r, g, b = v >> 11, (v >> 5) & 63, v & 31
|
||||
return (r << 3 | r >> 2, g << 2 | g >> 4, b << 3 | b >> 2)
|
||||
|
||||
@@ -331,6 +342,8 @@ def to_image(blob: bytes):
|
||||
_, _, px, alpha = decode_format4(blob)
|
||||
elif d["format"] == 0:
|
||||
px, alpha = struct.unpack_from("<%dH" % (w * h), blob, 24), None
|
||||
elif d["format"] == 3: # Discovery: 20-byte zero header, then raw RGB565
|
||||
px, alpha = struct.unpack_from("<%dH" % (w * h), blob, 44), None
|
||||
else:
|
||||
raise NotImplementedError("picture format %d not supported" % d["format"])
|
||||
img = Image.new("RGBA" if alpha else "RGB", (w, h))
|
||||
|
||||
@@ -223,8 +223,10 @@ def add_picture(h: HMIFile, image_file: bytes, quality: int = 100) -> int:
|
||||
n = 0
|
||||
while h.find("%d.i" % n):
|
||||
n += 1
|
||||
from . import models
|
||||
main = h.read("main.HMI")
|
||||
pic, src = pictures.from_image_file(image_file, direction=main[12] % 4, series=main[11], quality=quality)
|
||||
pic, src = pictures.from_image_file(image_file, direction=models.storage_direction(main[11], main[12]),
|
||||
series=main[11], quality=quality)
|
||||
h.write("%d.is" % n, src)
|
||||
h.write("%d.i" % n, pic)
|
||||
h.write("main.HMI", main_insert_resource(h.read("main.HMI"), "i", "%d.i" % n))
|
||||
@@ -258,9 +260,10 @@ def add_font(h: HMIFile, font) -> int:
|
||||
n = 0
|
||||
while h.find("%d.zi" % n):
|
||||
n += 1
|
||||
from . import models
|
||||
main = h.read("main.HMI")
|
||||
font.direction = main[12] % 4 # glyphs are stored rotated like pictures
|
||||
font.align = main[11] == 3 # Intelligent: glyph data 8-byte aligned
|
||||
font.direction = models.storage_direction(main[11], main[12]) # stored rotated like pictures
|
||||
font.align = main[11] in (3, 100) # Intelligent/Discovery: glyph data 8-byte aligned
|
||||
h.write("%d.zi" % n, font.pack())
|
||||
h.write("main.HMI", main_insert_resource(h.read("main.HMI"), "zi", "%d.zi" % n))
|
||||
return n
|
||||
|
||||
+7
-5
@@ -118,12 +118,14 @@ def qr_rects(txt, w: int, dis: int):
|
||||
return rects
|
||||
|
||||
def rgb(v: int, series: int = 0):
|
||||
"""RGB565 -> 8-bit RGB the way the Editor shows it: Basic replicates the top bits,
|
||||
Intelligent scales (``v * 255 // 31``), e.g. 0xC618 is (198,195,198) vs (197,194,197)."""
|
||||
"""Component colour RGB565 -> 8-bit RGB the way the Editor shows it, on every series (canvas and
|
||||
simulator, measured exactly): scaled, ``v * 255 // 31``, e.g. 0xC618 -> (197,194,197).
|
||||
(Format-0 picture pixels are bit-replicated instead, see pictures._rgb.) ``series=None``
|
||||
gives bit replication."""
|
||||
r, g, b = v >> 11, (v >> 5) & 63, v & 31
|
||||
if series == 3:
|
||||
return (r * 255 // 31, g * 255 // 63, b * 255 // 31)
|
||||
return (r << 3 | r >> 2, g << 2 | g >> 4, b << 3 | b >> 2)
|
||||
if series is None: # bit replication (old behaviour, for reference)
|
||||
return (r << 3 | r >> 2, g << 2 | g >> 4, b << 3 | b >> 2)
|
||||
return (r * 255 // 31, g * 255 // 63, b * 255 // 31)
|
||||
|
||||
|
||||
class Renderer:
|
||||
|
||||
Reference in new issue
Block a user