diff --git a/.claude/skills/nextion-hmi/SKILL.md b/.claude/skills/nextion-hmi/SKILL.md index 0f66e21..4a4f85c 100644 --- a/.claude/skills/nextion-hmi/SKILL.md +++ b/.claude/skills/nextion-hmi/SKILL.md @@ -42,5 +42,5 @@ Back up the user's file first (copy it) unless they said not to; edits write in - Writes projects for Nextion Editor 1.68.2; the user still compiles/uploads the `.tft` with the Editor (or Nexus for uploading). nxhmi cannot compile `.tft` files yet. -- Rendering: radio buttons are ~95 % exact, QR codes are a placeholder, Intelligent-only - components are not drawn. +- Rendering: QR codes are a placeholder, gauge needles can differ by a few pixels, + Intelligent-only components are not drawn. diff --git a/README.md b/README.md index 8a70e3f..7edd4cc 100644 --- a/README.md +++ b/README.md @@ -30,8 +30,8 @@ Every percentage is a count with a stated basis, so it can be checked (details i | Picture formats | **50 %** (1/2) | raw RGB565 (Basic) done; compressed format (Intelligent) not yet | | Font modes | **50 %** (1/2) | black/white done (incl. TrueType import); anti-aliased not yet | | Readable attribute names in JSON | **65 %** (20/31 types) | value lists taken from the Editor for all Basic types; Intelligent-only types still raw numbers | -| Rendering, Basic components | **85 %** (17/20 pixel-exact) | gauge ~99.5 %, radio ~95 %, QR code placeholder | -| Rendering, all components | **55 %** (17/31 pixel-exact) | Intelligent-only components not rendered yet | +| Rendering, Basic components | **90 %** (18/20 pixel-exact) | gauge ~99.5 %, QR code placeholder | +| Rendering, all components | **58 %** (18/31 pixel-exact) | Intelligent-only components not rendered yet | | `.tft` compilation | **31 %** (2.5/8 milestones) | layout mapped, layout table decoded, trailer checksum (same-length edits only); not usable yet | ## Compatibility diff --git a/demo/render/home.png b/demo/render/home.png index a0a48c7..cf370b1 100644 Binary files a/demo/render/home.png and b/demo/render/home.png differ diff --git a/docs/COVERAGE.md b/docs/COVERAGE.md index e859824..1830bbf 100644 --- a/docs/COVERAGE.md +++ b/docs/COVERAGE.md @@ -98,7 +98,6 @@ byte for byte, or a file written by nxhmi was opened in the Editor and showed th ### Rendering | Item | Notes | |---|---| -| Radio circle | ~93-97 %: outline/dot differ by +-1 px at some radii (in progress) | | QR code | placeholder; needs the Editor's QR settings | | Pressed / runtime states, Intelligent-only components | not drawn | diff --git a/docs/RENDER.md b/docs/RENDER.md index 103d029..0902c62 100644 --- a/docs/RENDER.md +++ b/docs/RENDER.md @@ -10,7 +10,7 @@ The demo project (`demo/demo.HMI`, built by `examples/make_demo.py`): | Page | Differing pixels | Notes | |---|---|---| -| home | 94 | all in the two radio buttons and at name-tag edges | +| home | 30 | name-tag edges (checkbox) and gauge needle anti-aliasing | | controls | 5 | name-tag edges | | gallery | 1 | name-tag edge | @@ -32,7 +32,7 @@ Per component (dedicated test pages): | picture, crop (also smaller than the component) | 100 % | only the picture's real area is drawn | | waveform (frame + grid) | 100 % | horizontal grid lines count from the bottom | | gauge | 99.3 - 100 % | needle anti-aliasing differs by a few px | -| radio | ~93 - 97 % | circle outline/dot differ by +-1 px at some radii (work in progress) | +| radio (all sizes, also non-square) | 100 % | the display's own circle routine, see below | | QR code | placeholder only | | | hotspot, touch cap, timer, variable | nothing drawn (same as the Editor) | | @@ -46,10 +46,15 @@ Per component (dedicated test pages): - Buttons with a picture background (`sta` crop or image) get no frame. - Pictures are pasted at their real size; a smaller picture leaves the rest untouched. - Checkbox: `bco` fill, `style` frame, checked = `pco` square from w/4 to 3w/4. -- Radio: circle sized by the component *width* (clipped to the height); ring radius - `(w-1)//2`, colour (65,68,65), filled with `bco`; checked = `pco` dot. +- Radio: circle sized by the component *width* (clipped to the box); ring radius + `r = (w-1)//2` around `(r, r)`, colour (65,68,65), filled with `bco`; checked = `pco` dot of + radius `r // 2`. +- Circles (radio, and the display's `cir`/`cirs` commands): incremental midpoint routine -- + start at `x = r, y = 0`; plot the 8 symmetric points; `y += 1`; if + `x*x - x + y*y + 2*y > r*r + 1` then `x -= 1`; repeat while `x >= y`. Filled circles are every + pixel inside that ring. Verified for every radius 1..159 against the Editor's simulator. - Slider: auto cursor (255) is a square as thick as the slider; position = `int(frac * free)`. - Waveform: vertical grid lines from the left edge, horizontal ones counted from the bottom. -Not covered yet: exact radio circles, QR codes, Intelligent-only components (need Intelligent +Not covered yet: QR codes, Intelligent-only components (need Intelligent picture format 4), anti-aliased fonts, pressed/runtime states. diff --git a/src/nxhmi/render.py b/src/nxhmi/render.py index bcdeb34..ac6b94d 100644 --- a/src/nxhmi/render.py +++ b/src/nxhmi/render.py @@ -21,6 +21,36 @@ from .page import Component, Page from . import pictures +def circle_octant(r: int): + """First octant (x >= y >= 0) of the display's circle routine -- reproduces the Nextion `cir` + command and the Editor canvas exactly (verified for every radius 1..159).""" + pts = [] + x, y = r, 0 + while x >= y: + pts.append((x, y)) + y += 1 + if x * x - x + y * y + 2 * y > r * r + 1: + x -= 1 + return pts + + +def circle_points(r: int): + """All points of a ring of radius r around (0, 0) (8-way symmetric).""" + pts = set() + for x, y in circle_octant(r): + for a, b in ((x, y), (y, x)): + pts.update({(a, b), (-a, b), (a, -b), (-a, -b)}) + return pts + + +def disc_rows(r: int): + """{dy: half-width} of a filled circle (Nextion `cirs`): every pixel inside the ring.""" + rows = {} + for a, b in circle_points(r): + rows[b] = max(rows.get(b, 0), abs(a)) + return rows + + def rgb(v: int): r, g, b = v >> 11, (v >> 5) & 63, v & 31 return (r << 3 | r >> 2, g << 2 | g >> 4, b << 3 | b >> 2) @@ -326,12 +356,28 @@ class Renderer: d.rectangle((x + w // 4, y + h // 4, x + 3 * w // 4 - 1, y + 3 * h // 4 - 1), fill=rgb(c.get("pco") or 0)) def radio(self, img, c): - from PIL import ImageDraw + """Circle sized by the width, centred at ((w-1)//2, (w-1)//2), clipped to the box: + bco disc, ring (65,68,65), and when checked a pco dot of half the radius.""" x, y, w, h = self._box(c) - d = ImageDraw.Draw(img) - d.ellipse((x, y, x + w - 2, y + h - 2), fill=rgb(c.get("bco") or 0), outline=(65, 68, 65)) + r = (w - 1) // 2 + cx, cy = x + r, y + r + px = img.load() + W, H = img.size + + def put(xx, yy, col): + if x <= xx < x + w and y <= yy < y + h and 0 <= xx < W and 0 <= yy < H: + px[xx, yy] = col + + def fill(rad, col): + for dy, hw in disc_rows(rad).items(): + for dx in range(-hw, hw + 1): + put(cx + dx, cy + dy, col) + + fill(r, rgb(c.get("bco") or 0)) + for dx, dy in circle_points(r): + put(cx + dx, cy + dy, (65, 68, 65)) if c.get("val"): - d.ellipse((x + w // 4, y + h // 4, x + 3 * w // 4 - 2, y + 3 * h // 4 - 2), fill=rgb(c.get("pco") or 0)) + fill(r // 2, rgb(c.get("pco") or 0)) def gauge(self, img, c): from PIL import ImageDraw diff --git a/tests/test_render.py b/tests/test_render.py index 429187f..4178a77 100644 --- a/tests/test_render.py +++ b/tests/test_render.py @@ -71,6 +71,25 @@ class Render(unittest.TestCase): img = self.render(p) self.assertEqual(img.getpixel((10, 0)), (65, 68, 65)) # dark top edge when on + def test_circle_routine(self): + from nxhmi.render import circle_octant, disc_rows + # measured from the Editor simulator (`cir 240,160,39,0`) + self.assertEqual(circle_octant(39), [(39, 0), (39, 1), (39, 2), (39, 3), (39, 4), (39, 5), (38, 6), (38, 7), + (38, 8), (38, 9), (37, 10), (37, 11), (37, 12), (36, 13), (36, 14), (36, 15), + (35, 16), (35, 17), (34, 18), (34, 19), (33, 20), (32, 21), (32, 22), (31, 23), + (30, 24), (29, 25), (28, 26), (27, 27)]) + self.assertEqual(circle_octant(13)[-1], (9, 9)) # diagonal point kept + self.assertEqual(disc_rows(19)[19], 3) + + def test_radio(self): + p = C.new_page("p", w=100, h=100) + C.add(p, "radio", x=0, y=0, w=80, h=80, val=1, bco=0xFFE0, pco=0x001F) + img = self.render(p) + self.assertEqual(img.getpixel((0, 39)), (65, 68, 65)) # ring, radius 39 around (39, 39) + self.assertEqual(img.getpixel((39, 39)), (0, 0, 255)) # dot radius 19 + self.assertEqual(img.getpixel((39 + 19, 39)), (0, 0, 255)) + self.assertEqual(img.getpixel((39 + 20, 39)), (255, 255, 0)) + def test_demo_pages_render(self): from nxhmi.render import Renderer r = Renderer(HMIFile.load(FIXTURE))