mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-08-19 17:39:06 +00:00
fix(startup): bound the initial plugin update so the panel lights sooner (#456)
* fix(startup): bound the initial plugin update so the panel lights sooner
DisplayController.__init__ calls _update_modules() once to populate
plugin data before the first frame. It walks every loaded plugin in
turn, and each update blocks the calling thread for up to the executor's
30s timeout, so the uncapped total is the sum of every slow plugin on
the system. The rig's own log:
Initial plugin update completed in 82.255 seconds
Initial plugin update completed in 55.123 seconds
Initial plugin update completed in 25.975 seconds
The panel shows nothing for all of it.
Nothing is lost by stopping early. A plugin that has never updated is
immediately due, so run_scheduled_updates() collects it seconds later --
with the display already running rather than blank.
A deadline alone was not enough: it is checked before each plugin, so
the last one to start could still block for the full 30s, and a 20s
budget produced a 31.8s pass on the rig. The remaining budget is now
passed down as that update's timeout too, with a floor so a plugin
starting on the last sliver is not handed ~0s and recorded as having
timed out for a slot it never had. Measured after: 20.006s.
Found while profiling a scroll freeze with py-spy, which caught the main
thread 9.34s inside execute_with_timeout's join. Worth being clear that
this is startup latency, not the recurring stutter -- _update_modules
has exactly one caller and runtime updates already run off the display
thread.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Udr6MfaFLUPhX5Fgo67Jf5
* feat(display): show the device address on the startup screen
That screen is what the panel holds for the whole initial plugin update,
and on a headless Pi it is the only place the address appears without
going looking for it -- so it now carries the address under
"Initializing".
The lookup connects a UDP socket, which sends no packets: it only asks
the kernel which source address it would route from. That costs 0.03ms
and works with the network down so long as a route exists. Deliberately
not `hostname -I` plus a systemctl probe for AP mode, which is how the
web launcher does it -- two subprocesses with multi-second timeouts, on
the startup path this branch exists to shorten.
Two things had to change for the address to be worth putting there.
The text is now sized to fit rather than fixed at 8px: "Initializing" is
96px in PressStart2P, drawn at x=10, so it already ran off the side of a
64px panel before an address was added. It falls back to 4x6 where that
does not fit, and both lines are centred.
And the test pattern is punched out from behind the block, with the text
drawn white rather than blue. The diagonal runs through the middle of
the panel, which is exactly where this sits, and blue on black reads
fine on a monitor but is marginal on a dim panel. An address that cannot
be read off the wall is not worth showing.
The rendering tests assert against pixels -- no green left behind the
text at any supported size, enough lit pixels to be visible -- rather
than against the geometry that produced them.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Udr6MfaFLUPhX5Fgo67Jf5
* fix(display): keep the startup text blue -- it is a channel reference
The test pattern lights one pure channel per element: red border, green
diagonal, blue text. That is how a glance at the panel tells you whether
led_rgb_sequence is right -- wire it BGR and the border comes up blue
and the text red. Drawing the text white, as the previous commit did for
contrast, lights all three channels and destroys the only blue reference
on the screen.
Reverted to blue, with the reason written down so it is not treated as a
style preference again, and with tests that pin it: the text must be
pure blue, nothing on the screen may be white, and all three primaries
must be present.
The punched-out backdrop stays. It only removes the diagonal from behind
the glyphs, which costs nothing diagnostically -- the diagonal is still
plainly visible across the rest of the panel -- and it is what makes the
address readable at all.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Udr6MfaFLUPhX5Fgo67Jf5
* fix(startup): defer a plugin with too little budget, rather than clamp it
The per-plugin timeout was clamped up to a floor, so a plugin that began
with a sliver of budget left was granted the full floor and ran on past
the deadline: a 20s budget could take 22. The floor existed to stop a
plugin being handed a slot too short to use and then recorded as having
timed out, which is a real concern, but clamping solved it by breaking
the bound.
Deferring solves both. Below the floor the plugin is left to the update
tick, which was already the fate of everything after the deadline, so
nothing new is lost -- a plugin that has never updated is immediately
due. Above it, the timeout is the exact remainder, and the pass cannot
outlast its deadline.
Measured on the rig after the change: 20.002s, 5 plugins deferred.
Also names an unused binding in the initializing-screen test.
Both reported by CodeRabbit.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Udr6MfaFLUPhX5Fgo67Jf5
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+91
-2
@@ -25,6 +25,7 @@ the same object.
|
||||
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import tempfile
|
||||
if os.getenv("EMULATOR", "false") == "true":
|
||||
from RGBMatrixEmulator import RGBMatrix, RGBMatrixOptions
|
||||
@@ -517,6 +518,91 @@ class DisplayManager:
|
||||
logger.warning(f"[BRIGHTNESS] Matrix does not support brightness property: {e}", exc_info=True)
|
||||
return -1
|
||||
|
||||
@staticmethod
|
||||
def _local_ip() -> Optional[str]:
|
||||
"""This device's address on the network it routes through, or None.
|
||||
|
||||
Deliberately not `hostname -I` or a systemctl probe for AP mode, which
|
||||
is how the web launcher does it: both spawn processes with multi-second
|
||||
timeouts, and this runs on the startup path the rest of this change
|
||||
exists to shorten. Connecting a UDP socket sends no packets -- it only
|
||||
asks the kernel which source address it would use -- so it costs
|
||||
microseconds and works with the network down, as long as a route
|
||||
exists.
|
||||
"""
|
||||
sock = None
|
||||
try:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sock.settimeout(0.2)
|
||||
sock.connect(("8.8.8.8", 80)) # nosec B104 - no traffic; selects a route
|
||||
ip = sock.getsockname()[0]
|
||||
return ip if ip and not ip.startswith("127.") else None
|
||||
except OSError:
|
||||
return None
|
||||
finally:
|
||||
if sock is not None:
|
||||
try:
|
||||
sock.close()
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
def _fitting_font(self, lines, width):
|
||||
"""The largest font from the usual ladder that fits every line."""
|
||||
candidates = [self.font,
|
||||
("assets/fonts/4x6-font.ttf", 6)]
|
||||
for candidate in candidates:
|
||||
try:
|
||||
font = candidate
|
||||
if isinstance(candidate, tuple):
|
||||
font = ImageFont.truetype(candidate[0], candidate[1])
|
||||
if all(self.draw.textlength(t, font=font) <= width for t in lines):
|
||||
return font
|
||||
except (OSError, ValueError, AttributeError):
|
||||
continue
|
||||
return self.font
|
||||
|
||||
def _draw_startup_banner(self, lines, width: int, height: int) -> None:
|
||||
"""Centre `lines` over whatever the test pattern already drew.
|
||||
|
||||
This screen stays on the panel for the whole initial plugin update, and
|
||||
on a headless Pi it is the only place the device's address appears
|
||||
without going looking for it -- so it has to be readable off a wall,
|
||||
not merely present.
|
||||
|
||||
The font is chosen to fit rather than fixed at 8px: "Initializing" is
|
||||
96px in PressStart2P, which ran off the side of a 64px panel even
|
||||
before an address was added. And the pattern is punched out behind the
|
||||
text, because the diagonal runs through the middle of the panel, which
|
||||
is exactly where this sits.
|
||||
|
||||
The text stays blue. It is not decoration: the pattern draws one pure
|
||||
channel per element -- red border, green diagonal, blue text -- so that
|
||||
a glance at the panel says whether led_rgb_sequence is right. Swap the
|
||||
wiring to BGR and the border comes up blue and this text red. Drawing
|
||||
it white would light all three channels and destroy the only blue
|
||||
reference on the screen, which is why it is worth a comment rather
|
||||
than a quiet preference.
|
||||
"""
|
||||
if not lines:
|
||||
return
|
||||
font = self._fitting_font(lines, width - 2)
|
||||
line_height = self.draw.textbbox((0, 0), "Ag", font=font)[3] + 1
|
||||
block_height = line_height * len(lines)
|
||||
block_top = max(1, (height - block_height) // 2)
|
||||
block_width = max(self.draw.textlength(t, font=font) for t in lines)
|
||||
block_left = max(0, (width - block_width) // 2)
|
||||
|
||||
self.draw.rectangle(
|
||||
[block_left - 2, block_top - 1,
|
||||
block_left + block_width + 1, block_top + block_height],
|
||||
fill=(0, 0, 0))
|
||||
|
||||
for row, line in enumerate(lines):
|
||||
line_width = self.draw.textlength(line, font=font)
|
||||
self.draw.text(
|
||||
(max(0, (width - line_width) // 2), block_top + row * line_height),
|
||||
line, font=font, fill=(0, 0, 255))
|
||||
|
||||
def _draw_test_pattern(self):
|
||||
"""Draw a test pattern to verify the display is working."""
|
||||
try:
|
||||
@@ -536,8 +622,11 @@ class DisplayManager:
|
||||
# Draw a diagonal line
|
||||
self.draw.line([0, 0, self.matrix.width-1, self.matrix.height-1], fill=(0, 255, 0))
|
||||
|
||||
# Draw some text - changed from "TEST" to "Initializing" with smaller font
|
||||
self.draw.text((10, 10), "Initializing", font=self.font, fill=(0, 0, 255))
|
||||
lines = ["Initializing"]
|
||||
ip = self._local_ip()
|
||||
if ip:
|
||||
lines.append(ip)
|
||||
self._draw_startup_banner(lines, self.matrix.width, self.matrix.height)
|
||||
|
||||
# Update the display once after everything is drawn
|
||||
self.update_display()
|
||||
|
||||
Reference in New Issue
Block a user