Render gauge needles pixel-exact
The needle is the display's own routine, measured on 112 needles in the Editor simulator: tip from quadrant-reduced sin/cos, truncated; line from tip to centre with a biased DDA; wider needles stamp wid x wid squares along a line shortened by wid - 1. Length follows the height. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9b50eea03f
commit
f0bcc608b1
7 files changed
+67
-14
No files matched your search
@@ -42,5 +42,4 @@ 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: QR codes are a placeholder, gauge needles can differ by a few pixels,
|
||||
Intelligent-only components are not drawn.
|
||||
- Rendering: QR codes are a placeholder, Intelligent-only components are not drawn.
|
||||
@@ -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 | **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 |
|
||||
| Rendering, Basic components | **95 %** (19/20 pixel-exact) | QR code placeholder |
|
||||
| Rendering, all components | **61 %** (19/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
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 5.4 KiB After Width: | Height: | Size: 5.4 KiB |
+1
-1
@@ -15,7 +15,7 @@ byte for byte, or a file written by nxhmi was opened in the Editor and showed th
|
||||
| Readable JSON (enum names, colours) | done for every value list the Editor documents |
|
||||
| Pictures | Basic series **complete**; Intelligent compressed format **missing** |
|
||||
| Fonts | black/white **complete** incl. generation from TrueType; anti-aliased **missing** |
|
||||
| Rendering pages like the Editor | **pixel-identical** for most components incl. picture skins; radio ~93-97 %, QR placeholder |
|
||||
| Rendering pages like the Editor | **pixel-identical** for most components incl. picture skins, radios and gauges; QR placeholder |
|
||||
| New projects from scratch | `project.new_project(model)` for 12 Basic/Intelligent models; the generated demo is byte-identical to the Editor's re-save |
|
||||
| Compiling `.tft` firmware | **research only** -- not possible yet |
|
||||
|
||||
|
||||
+9
-2
@@ -10,7 +10,7 @@ The demo project (`demo/demo.HMI`, built by `examples/make_demo.py`):
|
||||
|
||||
| Page | Differing pixels | Notes |
|
||||
|---|---|---|
|
||||
| home | 30 | name-tag edges (checkbox) and gauge needle anti-aliasing |
|
||||
| home | 25 | name-tag edges (checkbox, buttons) |
|
||||
| controls | 5 | name-tag edges |
|
||||
| gallery | 1 | name-tag edge |
|
||||
|
||||
@@ -31,7 +31,7 @@ Per component (dedicated test pages):
|
||||
| checkbox (3D down, border, flat) | 100 % | |
|
||||
| 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 |
|
||||
| gauge (every angle, needle width 1-8, odd/even/non-square sizes) | 100 % | the needle routine, see below |
|
||||
| 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) | |
|
||||
@@ -53,6 +53,13 @@ Per component (dedicated test pages):
|
||||
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.
|
||||
- Gauge needle (verified on 112 needles): centre `(w//2, h//2)`, length `h//2 - wid` (the
|
||||
*height* sets it, also for non-square gauges, so the needle can leave the box). Tip =
|
||||
centre + `(int(-len*cos a), int(-len*sin a))` with `val` in degrees, 0 = left, 90 = up, where
|
||||
sin/cos are taken after reducing the angle to 0..90 degrees (so `sin 30` is a hair under 0.5
|
||||
and `24*sin 30` truncates to 11). The line runs from the tip to the centre: step the major
|
||||
axis, minor offset `(k*minor + minor - 1) // major`. Width > 1: every line pixel is stamped
|
||||
with a `wid` x `wid` square starting at `-(wid//2)`.
|
||||
- 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.
|
||||
|
||||
|
||||
+39
-7
@@ -51,6 +51,41 @@ def disc_rows(r: int):
|
||||
return rows
|
||||
|
||||
|
||||
def qsin(deg: int) -> float:
|
||||
"""sin() as the display computes it: reduced to 0..90 degrees first, so sin(30) is a hair
|
||||
under 0.5 and the truncated needle tip lands one pixel short on exact halves."""
|
||||
deg %= 360
|
||||
sign = 1
|
||||
if deg >= 180:
|
||||
deg, sign = deg - 180, -1
|
||||
if deg > 90:
|
||||
deg = 180 - deg
|
||||
return sign * math.sin(math.radians(deg))
|
||||
|
||||
|
||||
def needle_line(x0: int, y0: int, x1: int, y1: int):
|
||||
"""Line from the tip (x0, y0) to the centre (x1, y1): step the major axis, minor offset
|
||||
`(k * minor + minor - 1) // major` (the gauge needle routine)."""
|
||||
dx, dy = abs(x1 - x0), abs(y1 - y0)
|
||||
sx, sy = (1 if x1 >= x0 else -1), (1 if y1 >= y0 else -1)
|
||||
if dx >= dy:
|
||||
return [(x0 + sx * k, y0 + sy * ((k * dy + max(dy - 1, 0)) // dx if dx else 0)) for k in range(dx + 1)]
|
||||
return [(x0 + sx * ((k * dx + max(dx - 1, 0)) // dy), y0 + sy * k) for k in range(dy + 1)]
|
||||
|
||||
|
||||
def gauge_needle(w: int, h: int, val: int, wid: int):
|
||||
"""Pixels of a gauge needle relative to the component's top-left corner.
|
||||
Centre (w//2, h//2); tip at length h//2 - wid; every pixel of that thin line is stamped
|
||||
with a wid x wid square starting at -(wid//2). The needle may leave the component box."""
|
||||
wid = max(1, wid)
|
||||
cx, cy = w // 2, h // 2
|
||||
r = h // 2 - wid
|
||||
tip = (cx + int(-r * qsin(val + 90)), cy + int(-r * qsin(val)))
|
||||
lo = -(wid // 2)
|
||||
return {(x + ax, y + ay) for x, y in needle_line(*tip, cx, cy)
|
||||
for ax in range(lo, lo + wid) for ay in range(lo, lo + wid)}
|
||||
|
||||
|
||||
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)
|
||||
@@ -380,15 +415,12 @@ class Renderer:
|
||||
fill(r // 2, rgb(c.get("pco") or 0))
|
||||
|
||||
def gauge(self, img, c):
|
||||
from PIL import ImageDraw
|
||||
x, y, w, h = self._box(c)
|
||||
self._background(img, c, x, y, w, h)
|
||||
d = ImageDraw.Draw(img)
|
||||
r = min(w, h) // 2 - 1
|
||||
cx, cy = x + w // 2, y + h // 2
|
||||
a = math.radians(c.get("val") or 0)
|
||||
d.line((cx, cy, cx - int(r * math.cos(a)), cy - int(r * math.sin(a))), fill=rgb(c.get("pco") or 0),
|
||||
width=max(1, c.get("wid") or 1))
|
||||
px, col = img.load(), rgb(c.get("pco") or 0)
|
||||
for ax, ay in gauge_needle(w, h, c.get("val") or 0, c.get("wid") or 1):
|
||||
if 0 <= x + ax < img.width and 0 <= y + ay < img.height:
|
||||
px[x + ax, y + ay] = col
|
||||
|
||||
def waveform(self, img, c):
|
||||
from PIL import ImageDraw
|
||||
|
||||
@@ -90,6 +90,21 @@ class Render(unittest.TestCase):
|
||||
self.assertEqual(img.getpixel((39 + 19, 39)), (0, 0, 255))
|
||||
self.assertEqual(img.getpixel((39 + 20, 39)), (255, 255, 0))
|
||||
|
||||
def test_gauge_needle(self):
|
||||
from nxhmi.render import gauge_needle, qsin
|
||||
# all measured in the Editor simulator
|
||||
thin = gauge_needle(58, 58, 9, 1) # tip (2, 25), runs of 7 counted from the centre
|
||||
self.assertEqual(len(thin), 28)
|
||||
self.assertEqual({y for x, y in thin if 22 <= x <= 28}, {28})
|
||||
self.assertEqual({y for x, y in thin if 2 <= x <= 7}, {25})
|
||||
self.assertIn((29, 29), thin)
|
||||
self.assertLess(qsin(30), 0.5) # reduced to 0..90 degrees, a hair under 0.5
|
||||
self.assertIn((15, 14), gauge_needle(70, 50, 30, 1)) # tip: 24*sin(30) truncates to 11
|
||||
self.assertIn((-4, 19), gauge_needle(50, 70, 30, 1)) # radius from height, leaves the box
|
||||
self.assertEqual(len(gauge_needle(58, 58, 46, 5)), 174) # 5x5 squares along a shorter line
|
||||
self.assertEqual(len(gauge_needle(58, 58, 69, 8)), 265)
|
||||
self.assertEqual(gauge_needle(58, 58, 0, 2), {(x, y) for x in range(1, 30) for y in (28, 29)})
|
||||
|
||||
def test_demo_pages_render(self):
|
||||
from nxhmi.render import Renderer
|
||||
r = Renderer(HMIFile.load(FIXTURE))
|
||||
|
||||
Reference in new issue
Block a user