mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-08-13 14:48:06 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cf30bbbef | ||
|
|
a51fb7ce11 | ||
|
|
fce1fdac57 | ||
|
|
7171e6c022 | ||
|
|
9fbdd71941 | ||
|
|
2add759f40 |
@@ -600,6 +600,14 @@ These settings are typically only needed for non-standard panels or custom confi
|
||||
- Leave empty unless you need custom mapping
|
||||
- See rpi-rgb-led-matrix documentation for full options
|
||||
|
||||
- **`orientation`** (string, default: "normal")
|
||||
- Rotates the rendered image to match how the panel is physically mounted
|
||||
- Set to `"180"` (or use the "Upside Down" option in the web UI's Display
|
||||
settings) if the panel is mounted upside down — useful for optimizing
|
||||
where the Raspberry Pi and wiring sit relative to the mounting location
|
||||
- Applied independently of `pixel_mapper_config` (appended as a trailing
|
||||
`Rotate:180` mapper), so custom mapper configs keep working alongside it
|
||||
|
||||
- **`row_address_type`** (integer, default: 0)
|
||||
- How rows are addressed on the panel
|
||||
- Most panels use 0 (direct addressing)
|
||||
|
||||
@@ -112,6 +112,7 @@
|
||||
"led_rgb_sequence": "RGB",
|
||||
"limit_refresh_rate_hz": 100,
|
||||
"pixel_mapper_config": "",
|
||||
"orientation": "normal",
|
||||
"row_address_type": 0,
|
||||
"multiplexing": 0,
|
||||
"panel_type": ""
|
||||
|
||||
@@ -66,6 +66,7 @@ in `DisplayManager` (`src/display_manager.py`, ~lines 270–295).
|
||||
| `led_rgb_sequence` | string, `"RGB"` |
|
||||
| `limit_refresh_rate_hz` | int, `100` (code default 90) |
|
||||
| `pixel_mapper_config` | string, `""` — e.g. `"U-mapper"` / `"Rotate:90"` |
|
||||
| `orientation` | string, `"normal"` — `"180"` rotates the rendered image 180° for panels physically mounted upside down (e.g. to move the Pi/wiring to a more convenient side); composed onto `pixel_mapper_config` as a trailing `Rotate:180` mapper, so it stays independent of any custom `pixel_mapper_config` value |
|
||||
| `row_address_type` | int, `0` — non-standard panel row addressing |
|
||||
| `multiplexing` | int, `0` — panel multiplexing scheme |
|
||||
| `panel_type` | string, `""` — set to `"FM6126A"` or `"FM6127"` for panels needing init |
|
||||
|
||||
Vendored
+64
-1
@@ -14,6 +14,13 @@ import zlib
|
||||
from typing import Dict, Any, Optional, Protocol
|
||||
from datetime import datetime
|
||||
|
||||
# How old an abandoned write's temp file must be before the sweep removes it.
|
||||
# A real write holds its temp file for milliseconds, so an hour is far beyond
|
||||
# any in-flight write while still clearing the same day's debris. Deliberately
|
||||
# not tied to the retention policies: those describe how long data stays
|
||||
# useful, and a half-written file was never useful.
|
||||
_ORPHAN_TEMP_MAX_AGE_SECONDS = 3600
|
||||
|
||||
|
||||
|
||||
class CacheStrategyProtocol(Protocol):
|
||||
@@ -347,6 +354,23 @@ class DiskCache:
|
||||
"""Get the cache directory path."""
|
||||
return self.cache_dir
|
||||
|
||||
@staticmethod
|
||||
def _is_orphaned_temp(filename: str) -> bool:
|
||||
"""Whether a name is one of set()'s temp files rather than real data.
|
||||
|
||||
Matches only what this class creates: mkstemp with a prefix of
|
||||
".<cache filename>." , so ".weather.json.a1b2c3d4". The shape is
|
||||
checked rather than just the leading dot, because this predicate
|
||||
deletes things -- a stray dotfile someone left in the cache directory
|
||||
is not ours to remove, and a completed ".json" never is either.
|
||||
"""
|
||||
if not filename.startswith('.') or filename.endswith('.json'):
|
||||
return False
|
||||
head, sep, suffix = filename.rpartition('.json.')
|
||||
# head is the key (non-empty after the leading dot), suffix is
|
||||
# mkstemp's random component.
|
||||
return bool(sep) and len(head) > 1 and bool(suffix)
|
||||
|
||||
def cleanup_expired_files(self, cache_strategy: CacheStrategyProtocol, retention_policies: Dict[str, int]) -> Dict[str, Any]:
|
||||
"""
|
||||
Clean up expired cache files based on retention policies.
|
||||
@@ -381,12 +405,51 @@ class DiskCache:
|
||||
try:
|
||||
with self._lock:
|
||||
# Get snapshot of files while holding lock briefly
|
||||
filenames = [f for f in os.listdir(self.cache_dir) if f.endswith('.json')]
|
||||
entries = os.listdir(self.cache_dir)
|
||||
except OSError as list_error:
|
||||
self.logger.error("Error listing cache directory %s: %s", self.cache_dir, list_error, exc_info=True)
|
||||
stats['errors'] += 1
|
||||
return stats
|
||||
|
||||
filenames = [f for f in entries if f.endswith('.json')]
|
||||
|
||||
# Sweep temp files abandoned by a write that never finished. set()
|
||||
# removes its own in a finally, so these are the ones where the
|
||||
# process died between mkstemp and os.replace -- a SIGKILL, a lost
|
||||
# restart race, a power cut. Nothing ever collected them: they are
|
||||
# named ".<key>.json.<random>", and the scan above only matches
|
||||
# names ending in .json, so they accumulated indefinitely. Measured
|
||||
# on a live rig: 76 files, 1,050 MB, 81% of the whole cache
|
||||
# directory, the oldest six months old.
|
||||
stats['orphan_temp_files_deleted'] = 0
|
||||
for filename in (f for f in entries if self._is_orphaned_temp(f)):
|
||||
# Counted as scanned like any other candidate, so files_deleted
|
||||
# can never exceed files_scanned and the summary line reads
|
||||
# honestly ("77/8864", not "77/0").
|
||||
stats['files_scanned'] += 1
|
||||
path = os.path.join(self.cache_dir, filename)
|
||||
try:
|
||||
# An in-flight write lives for milliseconds, so anything
|
||||
# this old is certainly abandoned rather than in progress.
|
||||
if (current_time - os.path.getmtime(path)) <= _ORPHAN_TEMP_MAX_AGE_SECONDS:
|
||||
continue
|
||||
with self._lock:
|
||||
size = os.path.getsize(path)
|
||||
os.remove(path)
|
||||
stats['files_deleted'] += 1
|
||||
stats['orphan_temp_files_deleted'] += 1
|
||||
stats['space_freed_bytes'] += size
|
||||
except FileNotFoundError:
|
||||
continue # another sweep got there first
|
||||
except OSError as e:
|
||||
stats['errors'] += 1
|
||||
self.logger.warning("Error deleting orphaned temp file %s: %s", filename, e)
|
||||
|
||||
if stats['orphan_temp_files_deleted']:
|
||||
self.logger.info(
|
||||
"Removed %d abandoned cache temp file(s)",
|
||||
stats['orphan_temp_files_deleted'])
|
||||
|
||||
# Process files outside the lock to avoid blocking get/set operations
|
||||
for filename in filenames:
|
||||
stats['files_scanned'] += 1
|
||||
|
||||
+40
-1
@@ -47,6 +47,20 @@ from src.cache.disk_cache import DateTimeEncoder # noqa: F401 - deliberate re-e
|
||||
class CacheManager:
|
||||
"""Manages caching of API responses to reduce API calls."""
|
||||
|
||||
# Which cache directories already have a cleanup thread in this process.
|
||||
#
|
||||
# The sweep is directory-scoped work -- it lists a directory and deletes
|
||||
# from it -- so one per directory is the right number no matter how many
|
||||
# managers exist. Nothing enforced that before: every instance started its
|
||||
# own, and because the loop closes over `self`, a discarded manager could
|
||||
# never be collected and its thread woke to re-scan the same directory
|
||||
# every 24 hours for the life of the process. Startup validation runs
|
||||
# twice and built a throwaway manager each time, so a display process
|
||||
# carried three threads for one cache.
|
||||
_cleanup_owners: Dict[str, 'CacheManager'] = {}
|
||||
_cleanup_owners_lock = threading.Lock()
|
||||
|
||||
|
||||
def __init__(self) -> None:
|
||||
# Initialize logger first
|
||||
self.logger: logging.Logger = get_logger(__name__)
|
||||
@@ -718,11 +732,29 @@ class CacheManager:
|
||||
}
|
||||
|
||||
def start_cleanup_thread(self) -> None:
|
||||
"""Start background thread for periodic disk cache cleanup."""
|
||||
"""Start background thread for periodic disk cache cleanup.
|
||||
|
||||
At most one thread per cache directory per process: the sweep is
|
||||
directory-scoped, so a second one only duplicates the scan.
|
||||
"""
|
||||
if self._cleanup_thread and self._cleanup_thread.is_alive():
|
||||
self.logger.debug("Cleanup thread already running")
|
||||
return
|
||||
|
||||
with CacheManager._cleanup_owners_lock:
|
||||
owner = CacheManager._cleanup_owners.get(self.cache_dir)
|
||||
if owner is not None and owner is not self:
|
||||
thread = owner._cleanup_thread
|
||||
if thread is not None and thread.is_alive():
|
||||
self.logger.debug(
|
||||
"Cleanup thread for %s already owned by another cache "
|
||||
"manager in this process; not starting a second",
|
||||
self.cache_dir)
|
||||
return
|
||||
# The owner's thread died or was stopped -- take over.
|
||||
CacheManager._cleanup_owners[self.cache_dir] = self
|
||||
|
||||
|
||||
def cleanup_loop():
|
||||
"""Background loop that runs cleanup periodically."""
|
||||
self.logger.info("Disk cache cleanup thread started (interval: %d hours)",
|
||||
@@ -770,6 +802,13 @@ class CacheManager:
|
||||
Signals the thread to stop and waits for it to finish (with timeout).
|
||||
This allows for clean shutdown during testing or application termination.
|
||||
"""
|
||||
# Release ownership first and unconditionally, so a manager that never
|
||||
# started a thread (or whose thread already exited) cannot keep the
|
||||
# directory claimed and block a live manager from sweeping it.
|
||||
with CacheManager._cleanup_owners_lock:
|
||||
if CacheManager._cleanup_owners.get(self.cache_dir) is self:
|
||||
del CacheManager._cleanup_owners[self.cache_dir]
|
||||
|
||||
if not self._cleanup_thread or not self._cleanup_thread.is_alive():
|
||||
self.logger.debug("Cleanup thread not running")
|
||||
return
|
||||
|
||||
@@ -44,6 +44,20 @@ from src.common.sync_manager import DisplaySyncManager, SyncRole
|
||||
# Get logger with consistent configuration
|
||||
logger = get_logger(__name__)
|
||||
|
||||
# How long startup will wait for plugins to fetch their first data before
|
||||
# showing anything. Each plugin's update blocks for up to the executor's 30s
|
||||
# timeout and they run one after another, so the uncapped total is the sum of
|
||||
# every slow plugin: 82 seconds on the worst boot measured, with a blank panel
|
||||
# throughout. Whatever does not finish in time is picked up by the scheduled
|
||||
# update tick moments later, with the display already running.
|
||||
_INITIAL_UPDATE_BUDGET_SECONDS = 20.0
|
||||
|
||||
# The least budget worth starting a plugin with. Below this the plugin is
|
||||
# deferred instead: granting it a floor would let the pass run past its
|
||||
# deadline, and granting it the true remainder would record a timeout for a
|
||||
# slot it never had a chance to use.
|
||||
_MIN_INITIAL_UPDATE_TIMEOUT_SECONDS = 2.0
|
||||
|
||||
# Vegas mode import (lazy loaded to avoid circular imports)
|
||||
_vegas_mode_imported = False
|
||||
VegasModeCoordinator = None
|
||||
@@ -90,7 +104,8 @@ class DisplayController:
|
||||
# Validate startup configuration
|
||||
try:
|
||||
from src.startup_validator import StartupValidator
|
||||
validator = StartupValidator(self.config_manager)
|
||||
validator = StartupValidator(self.config_manager,
|
||||
cache_manager=self.cache_manager)
|
||||
is_valid, errors, warnings = validator.validate_all()
|
||||
|
||||
if warnings:
|
||||
@@ -258,7 +273,8 @@ class DisplayController:
|
||||
# Validate plugins after plugin manager is created
|
||||
try:
|
||||
from src.startup_validator import StartupValidator
|
||||
validator = StartupValidator(self.config_manager, self.plugin_manager)
|
||||
validator = StartupValidator(self.config_manager, self.plugin_manager,
|
||||
cache_manager=self.cache_manager)
|
||||
is_valid, errors, warnings = validator.validate_all()
|
||||
|
||||
if warnings:
|
||||
@@ -461,7 +477,7 @@ class DisplayController:
|
||||
# Initial data update for plugins (ensures data available on first display)
|
||||
logger.info("Performing initial plugin data update...")
|
||||
update_start = time.time()
|
||||
self._update_modules()
|
||||
self._update_modules(deadline=update_start + _INITIAL_UPDATE_BUDGET_SECONDS)
|
||||
logger.info("Initial plugin update completed in %.3f seconds", time.time() - update_start)
|
||||
|
||||
# Initialize Vegas mode coordinator
|
||||
@@ -817,14 +833,42 @@ class DisplayController:
|
||||
self._cached_target_brightness = normal_brightness # persist for minute-gate
|
||||
return normal_brightness
|
||||
|
||||
def _update_modules(self):
|
||||
"""Update all plugin modules."""
|
||||
def _update_modules(self, deadline: Optional[float] = None):
|
||||
"""Update all plugin modules.
|
||||
|
||||
Args:
|
||||
deadline: Wall-clock time after which remaining plugins are left
|
||||
for the scheduled update tick instead of being waited on. Each
|
||||
update blocks this thread for up to the executor's timeout, and
|
||||
they run one after another, so without a bound the total is the
|
||||
sum of every slow plugin on the system. Measured at startup on
|
||||
a live rig: 82 seconds, 55 and 26 on the two boots before -- all
|
||||
of it with nothing on the panel.
|
||||
"""
|
||||
if not self.plugin_manager:
|
||||
return
|
||||
|
||||
# Update all loaded plugins
|
||||
plugins_dict = getattr(self.plugin_manager, 'loaded_plugins', None) or getattr(self.plugin_manager, 'plugins', {})
|
||||
deferred = []
|
||||
for plugin_id, plugin_instance in plugins_dict.items():
|
||||
update_timeout = None
|
||||
if deadline is not None:
|
||||
update_timeout = deadline - time.time()
|
||||
if update_timeout < _MIN_INITIAL_UPDATE_TIMEOUT_SECONDS:
|
||||
# Too little left to be worth starting. Deferring rather
|
||||
# than granting a floor keeps the budget a real ceiling --
|
||||
# clamping up to a minimum let a plugin that began with a
|
||||
# sliver left run on past the deadline -- and a plugin
|
||||
# handed a slot it cannot use would just be recorded as
|
||||
# having timed out.
|
||||
#
|
||||
# Nothing is lost either way: a plugin that has never
|
||||
# updated is immediately due, so run_scheduled_updates()
|
||||
# picks it up within seconds, with the display already
|
||||
# running.
|
||||
deferred.append(plugin_id)
|
||||
continue
|
||||
# Check circuit breaker before attempting update
|
||||
if hasattr(self.plugin_manager, 'health_tracker') and self.plugin_manager.health_tracker:
|
||||
if self.plugin_manager.health_tracker.should_skip_plugin(plugin_id):
|
||||
@@ -833,7 +877,13 @@ class DisplayController:
|
||||
|
||||
# Use PluginExecutor if available for safe execution
|
||||
if hasattr(self.plugin_manager, 'plugin_executor'):
|
||||
success = self.plugin_manager.plugin_executor.execute_update(plugin_instance, plugin_id)
|
||||
# The remaining budget is the timeout, so the pass cannot
|
||||
# run past its deadline. Bounding the loop alone did not do
|
||||
# it: the last plugin to start could still block for the
|
||||
# executor's full 30s, which turned a 20s budget into a 31.8s
|
||||
# pass on the rig.
|
||||
success = self.plugin_manager.plugin_executor.execute_update(
|
||||
plugin_instance, plugin_id, timeout=update_timeout)
|
||||
if success and hasattr(self.plugin_manager, 'plugin_last_update'):
|
||||
self.plugin_manager.plugin_last_update[plugin_id] = time.time()
|
||||
else:
|
||||
@@ -852,6 +902,12 @@ class DisplayController:
|
||||
if hasattr(self.plugin_manager, 'health_tracker') and self.plugin_manager.health_tracker:
|
||||
self.plugin_manager.health_tracker.record_failure(plugin_id, exc)
|
||||
|
||||
if deferred:
|
||||
logger.info(
|
||||
"Initial update budget spent; %d plugin(s) left to the update "
|
||||
"tick so the display can start: %s",
|
||||
len(deferred), ", ".join(deferred))
|
||||
|
||||
def _tick_plugin_updates_for_vegas(self) -> None:
|
||||
"""Run scheduled plugin updates and tell Vegas mode which plugins
|
||||
actually got fresh data, so it can hot-swap them into the scroll
|
||||
|
||||
+112
-3
@@ -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
|
||||
@@ -258,6 +259,26 @@ class DisplayManager:
|
||||
# Initialize managers
|
||||
# Calendar manager is now initialized by DisplayController
|
||||
|
||||
# Orientation setting -> rpi-rgb-led-matrix "Rotate:<deg>" pixel-mapper suffix.
|
||||
# "normal" needs no suffix since 0 degrees is the identity transform.
|
||||
_ORIENTATION_ROTATE_DEGREES = {'normal': None, '90': 90, '180': 180, '270': 270}
|
||||
|
||||
def _build_pixel_mapper_config(self, hardware_config: dict) -> str:
|
||||
"""Compose the raw pixel_mapper_config string with the orientation setting.
|
||||
|
||||
`pixel_mapper_config` stays available as a free-form advanced field (e.g.
|
||||
for "U-mapper" chain layouts); `orientation` is the user-facing dropdown
|
||||
for physical mounting (e.g. panels mounted upside down) and is appended as
|
||||
a "Rotate:<deg>" mapper rather than overwriting any existing config.
|
||||
"""
|
||||
base_mapper = (hardware_config.get('pixel_mapper_config') or '').strip()
|
||||
orientation = hardware_config.get('orientation', 'normal')
|
||||
degrees = self._ORIENTATION_ROTATE_DEGREES.get(orientation)
|
||||
if degrees is None:
|
||||
return base_mapper
|
||||
rotate_mapper = f'Rotate:{degrees}'
|
||||
return f'{base_mapper};{rotate_mapper}' if base_mapper else rotate_mapper
|
||||
|
||||
def _setup_matrix(self):
|
||||
"""Initialize the RGB matrix with configuration settings."""
|
||||
_init_error_str = None
|
||||
@@ -283,7 +304,7 @@ class DisplayManager:
|
||||
options.pwm_bits = hardware_config.get('pwm_bits', 10)
|
||||
options.pwm_lsb_nanoseconds = hardware_config.get('pwm_lsb_nanoseconds', 150)
|
||||
options.led_rgb_sequence = hardware_config.get('led_rgb_sequence', 'RGB')
|
||||
options.pixel_mapper_config = hardware_config.get('pixel_mapper_config', '')
|
||||
options.pixel_mapper_config = self._build_pixel_mapper_config(hardware_config)
|
||||
options.row_address_type = hardware_config.get('row_address_type', 0)
|
||||
options.multiplexing = hardware_config.get('multiplexing', 0)
|
||||
options.panel_type = hardware_config.get('panel_type', '')
|
||||
@@ -497,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:
|
||||
@@ -516,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()
|
||||
|
||||
@@ -15,16 +15,23 @@ from src.logging_config import get_logger
|
||||
class StartupValidator:
|
||||
"""Validates system state on startup."""
|
||||
|
||||
def __init__(self, config_manager: Any, plugin_manager: Optional[Any] = None) -> None:
|
||||
def __init__(self, config_manager: Any, plugin_manager: Optional[Any] = None,
|
||||
cache_manager: Optional[Any] = None) -> None:
|
||||
"""
|
||||
Initialize the startup validator.
|
||||
|
||||
Args:
|
||||
config_manager: ConfigManager instance
|
||||
plugin_manager: Optional PluginManager instance
|
||||
cache_manager: The CacheManager the application will actually use.
|
||||
Pass it. Without one this validator builds its own just to read
|
||||
a directory path, which reports on a cache the app does not
|
||||
use and leaves behind a cleanup thread that nothing stops --
|
||||
validation runs twice per startup, so that was two of them.
|
||||
"""
|
||||
self.config_manager = config_manager
|
||||
self.plugin_manager = plugin_manager
|
||||
self.cache_manager = cache_manager
|
||||
self.logger = get_logger(__name__)
|
||||
self.errors: List[str] = []
|
||||
self.warnings: List[str] = []
|
||||
@@ -91,9 +98,21 @@ class StartupValidator:
|
||||
def _validate_cache_directory(self) -> None:
|
||||
"""Validate cache directory permissions."""
|
||||
try:
|
||||
from src.cache_manager import CacheManager
|
||||
cache_manager = CacheManager()
|
||||
cache_dir = cache_manager.get_cache_dir()
|
||||
cache_manager = self.cache_manager
|
||||
if cache_manager is None:
|
||||
# No caller supplied one (older embedders, direct use in a
|
||||
# script). Build one, but do not leave its cleanup thread
|
||||
# running behind us -- this instance is discarded on the next
|
||||
# line but the thread is a closure over it, so it would never
|
||||
# be collected.
|
||||
from src.cache_manager import CacheManager
|
||||
cache_manager = CacheManager()
|
||||
try:
|
||||
cache_dir = cache_manager.get_cache_dir()
|
||||
finally:
|
||||
cache_manager.stop_cleanup_thread()
|
||||
else:
|
||||
cache_dir = cache_manager.get_cache_dir()
|
||||
|
||||
if not cache_dir:
|
||||
self.warnings.append("Cache directory not available - caching will be disabled")
|
||||
|
||||
@@ -12,6 +12,7 @@ Supports three display modes per plugin:
|
||||
"""
|
||||
|
||||
import logging
|
||||
import math
|
||||
import time
|
||||
import threading
|
||||
from typing import Optional, Dict, Any, List, Callable, TYPE_CHECKING
|
||||
@@ -30,6 +31,21 @@ if TYPE_CHECKING:
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _percentile(ordered: List[float], fraction: float) -> float:
|
||||
"""Nearest-rank percentile of an already-sorted list.
|
||||
|
||||
Index ceil(n * fraction) - 1, so 100 samples at 0.99 give the 99th-ranked
|
||||
value. The obvious int(n * fraction) is off by one and, at exactly 100
|
||||
samples, lands on the maximum -- which is the number already reported
|
||||
alongside this one as the worst frame, so the two columns would agree
|
||||
precisely when the sample was smallest.
|
||||
"""
|
||||
if not ordered:
|
||||
return 0.0
|
||||
index = math.ceil(len(ordered) * fraction) - 1
|
||||
return ordered[min(len(ordered) - 1, max(0, index))]
|
||||
|
||||
|
||||
class VegasModeCoordinator:
|
||||
"""
|
||||
Orchestrates Vegas scroll mode operation.
|
||||
@@ -382,6 +398,12 @@ class VegasModeCoordinator:
|
||||
fps_log_interval = 5.0 # Log FPS every 5 seconds
|
||||
last_fps_log_time = start_time
|
||||
fps_frame_count = 0
|
||||
# A mean hides stutter completely. At 120fps a five-second window is
|
||||
# ~600 frames, so a 200ms freeze -- plainly visible on a marquee --
|
||||
# moves the average from 120.0 to 115.4 and reads as healthy. What a
|
||||
# viewer actually notices is the worst frame, so track that too.
|
||||
frame_worst = 0.0
|
||||
frame_times: List[float] = []
|
||||
|
||||
logger.info("Starting Vegas iteration for %.1fs", duration)
|
||||
|
||||
@@ -417,6 +439,11 @@ class VegasModeCoordinator:
|
||||
frame_elapsed = time.time() - frame_started
|
||||
time.sleep(max(0.0, frame_interval - frame_elapsed))
|
||||
|
||||
# Measured before the sleep: time spent working, not pacing.
|
||||
if frame_elapsed > frame_worst:
|
||||
frame_worst = frame_elapsed
|
||||
frame_times.append(frame_elapsed)
|
||||
|
||||
# Increment frame count and check for interrupt periodically
|
||||
frame_count += 1
|
||||
fps_frame_count += 1
|
||||
@@ -425,12 +452,16 @@ class VegasModeCoordinator:
|
||||
current_time = time.time()
|
||||
if current_time - last_fps_log_time >= fps_log_interval:
|
||||
fps = fps_frame_count / (current_time - last_fps_log_time)
|
||||
p99 = _percentile(sorted(frame_times), 0.99)
|
||||
logger.info(
|
||||
"Vegas FPS: %.1f (target: %d, frames: %d)",
|
||||
fps, self.vegas_config.target_fps, fps_frame_count
|
||||
"Vegas FPS: %.1f (target: %d, frames: %d) p99 %.1fms worst %.1fms",
|
||||
fps, self.vegas_config.target_fps, fps_frame_count,
|
||||
p99 * 1000.0, frame_worst * 1000.0
|
||||
)
|
||||
last_fps_log_time = current_time
|
||||
fps_frame_count = 0
|
||||
frame_worst = 0.0
|
||||
frame_times.clear()
|
||||
|
||||
if (self._interrupt_check and
|
||||
frame_count % self._interrupt_check_interval == 0):
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
"""Tests that one cache directory gets one cleanup thread per process.
|
||||
|
||||
The sweep lists a directory and deletes from it, so a second thread over the
|
||||
same directory only duplicates the scan. Nothing enforced that: every
|
||||
CacheManager started its own, and since the loop closes over `self`, a
|
||||
discarded manager could never be collected -- its thread stayed alive and
|
||||
re-scanned the same directory every 24 hours for the life of the process.
|
||||
|
||||
On the dev rig a display process carried three, for one cache directory:
|
||||
|
||||
14:22:59.954 display_controller (the real one)
|
||||
14:22:59.973 startup validation, run 1 (discarded)
|
||||
14:23:01.055 startup validation, run 2 (discarded)
|
||||
|
||||
Startup validation runs twice and built a throwaway manager each time, purely
|
||||
to read a directory path.
|
||||
"""
|
||||
|
||||
import threading
|
||||
|
||||
import pytest
|
||||
|
||||
from src.cache_manager import CacheManager
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clean_registry():
|
||||
CacheManager._cleanup_owners.clear()
|
||||
yield
|
||||
for owner in list(CacheManager._cleanup_owners.values()):
|
||||
owner.stop_cleanup_thread()
|
||||
CacheManager._cleanup_owners.clear()
|
||||
|
||||
|
||||
def _live_cleanup_threads():
|
||||
return [t for t in threading.enumerate()
|
||||
if t.name == 'DiskCacheCleanup' and t.is_alive()]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manager(tmp_path, monkeypatch):
|
||||
"""A CacheManager pinned to a temp dir, so tests never touch the real one."""
|
||||
monkeypatch.setattr(CacheManager, '_get_writable_cache_dir',
|
||||
lambda self: str(tmp_path))
|
||||
return CacheManager
|
||||
|
||||
|
||||
class TestOneThreadPerDirectory:
|
||||
def test_a_single_manager_starts_one(self, manager):
|
||||
before = len(_live_cleanup_threads())
|
||||
m = manager()
|
||||
try:
|
||||
assert len(_live_cleanup_threads()) == before + 1
|
||||
finally:
|
||||
m.stop_cleanup_thread()
|
||||
|
||||
def test_three_managers_still_start_one(self, manager):
|
||||
# Exactly the rig's shape: the real manager plus two throwaways.
|
||||
before = len(_live_cleanup_threads())
|
||||
managers = [manager() for _ in range(3)]
|
||||
try:
|
||||
assert len(_live_cleanup_threads()) == before + 1
|
||||
finally:
|
||||
for m in managers:
|
||||
m.stop_cleanup_thread()
|
||||
|
||||
def test_the_first_one_owns_it(self, manager):
|
||||
first, second = manager(), manager()
|
||||
try:
|
||||
assert CacheManager._cleanup_owners[first.cache_dir] is first
|
||||
assert second._cleanup_thread is None
|
||||
finally:
|
||||
first.stop_cleanup_thread()
|
||||
second.stop_cleanup_thread()
|
||||
|
||||
def test_the_survivor_can_take_over(self, manager):
|
||||
first = manager()
|
||||
first.stop_cleanup_thread()
|
||||
assert not _live_cleanup_threads()
|
||||
|
||||
second = manager()
|
||||
try:
|
||||
# Ownership was released, so the directory is swept again rather
|
||||
# than being left permanently unclaimed by a dead owner.
|
||||
assert len(_live_cleanup_threads()) == 1
|
||||
assert CacheManager._cleanup_owners[second.cache_dir] is second
|
||||
finally:
|
||||
second.stop_cleanup_thread()
|
||||
|
||||
def test_stopping_a_non_owner_does_not_unclaim_the_directory(self, manager):
|
||||
first, second = manager(), manager()
|
||||
try:
|
||||
second.stop_cleanup_thread() # never owned it
|
||||
assert CacheManager._cleanup_owners[first.cache_dir] is first
|
||||
assert len(_live_cleanup_threads()) == 1
|
||||
finally:
|
||||
first.stop_cleanup_thread()
|
||||
|
||||
def test_separate_directories_get_separate_threads(self, tmp_path, monkeypatch):
|
||||
a, b = tmp_path / 'a', tmp_path / 'b'
|
||||
a.mkdir()
|
||||
b.mkdir()
|
||||
dirs = iter([str(a), str(b)])
|
||||
monkeypatch.setattr(CacheManager, '_get_writable_cache_dir',
|
||||
lambda self: next(dirs))
|
||||
first, second = CacheManager(), CacheManager()
|
||||
try:
|
||||
assert first.cache_dir != second.cache_dir
|
||||
assert len(_live_cleanup_threads()) == 2
|
||||
finally:
|
||||
first.stop_cleanup_thread()
|
||||
second.stop_cleanup_thread()
|
||||
|
||||
def test_no_thread_leaks_across_many_constructions(self, manager):
|
||||
before = len(_live_cleanup_threads())
|
||||
made = [manager() for _ in range(12)]
|
||||
try:
|
||||
assert len(_live_cleanup_threads()) == before + 1
|
||||
finally:
|
||||
for m in made:
|
||||
m.stop_cleanup_thread()
|
||||
assert len(_live_cleanup_threads()) == before
|
||||
|
||||
|
||||
class TestValidatorDoesNotBuildItsOwn:
|
||||
def test_it_uses_the_cache_manager_it_is_given(self, manager):
|
||||
from src.startup_validator import StartupValidator
|
||||
|
||||
shared = manager()
|
||||
try:
|
||||
before = len(_live_cleanup_threads())
|
||||
v = StartupValidator(config_manager=object(), cache_manager=shared)
|
||||
v._validate_cache_directory()
|
||||
assert len(_live_cleanup_threads()) == before, (
|
||||
"validation started another cleanup thread")
|
||||
finally:
|
||||
shared.stop_cleanup_thread()
|
||||
|
||||
def test_without_one_it_cleans_up_after_itself(self, manager):
|
||||
from src.startup_validator import StartupValidator
|
||||
|
||||
before = len(_live_cleanup_threads())
|
||||
v = StartupValidator(config_manager=object())
|
||||
v._validate_cache_directory()
|
||||
assert len(_live_cleanup_threads()) == before, (
|
||||
"the fallback manager left its cleanup thread running")
|
||||
@@ -0,0 +1,188 @@
|
||||
"""Tests that abandoned cache temp files get collected.
|
||||
|
||||
DiskCache.set() writes through tempfile.mkstemp and os.replace, removing its
|
||||
own temp file in a finally. That covers a failed write, but not a process that
|
||||
dies between the two -- a SIGKILL, a lost restart race, a power cut, all
|
||||
ordinary on a Pi. Nothing collected what was left behind: the temp names are
|
||||
".<key>.json.<random>", and the expiry sweep only listed names ending in
|
||||
.json, so they accumulated for as long as the card had been in service.
|
||||
|
||||
Measured on a live rig before this fix: 76 orphans totalling 1,050 MB -- 81%
|
||||
of the entire cache directory -- the oldest six months old.
|
||||
|
||||
The predicate that decides what to delete is tested harder than the sweep
|
||||
itself, because a false positive here destroys real data.
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
import pytest
|
||||
|
||||
from src.cache.disk_cache import DiskCache, _ORPHAN_TEMP_MAX_AGE_SECONDS
|
||||
|
||||
|
||||
class FakeStrategy:
|
||||
@staticmethod
|
||||
def get_data_type_from_key(key):
|
||||
return 'default'
|
||||
|
||||
|
||||
POLICIES = {'default': 30}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cache(tmp_path):
|
||||
return DiskCache(str(tmp_path))
|
||||
|
||||
|
||||
def _age(path, seconds):
|
||||
old = time.time() - seconds
|
||||
os.utime(path, (old, old))
|
||||
|
||||
|
||||
def _write(tmp_path, name, body='{}'):
|
||||
p = tmp_path / name
|
||||
p.write_text(body, encoding='utf-8')
|
||||
return p
|
||||
|
||||
|
||||
class TestWhatCountsAsAnOrphan:
|
||||
@pytest.mark.parametrize('name', [
|
||||
'.weather.json.a1b2c3d4',
|
||||
'.odds_espn_football_nfl_401.json.xyz00000',
|
||||
'.a.json.b',
|
||||
])
|
||||
def test_our_temp_files_are_orphans(self, name):
|
||||
assert DiskCache._is_orphaned_temp(name)
|
||||
|
||||
@pytest.mark.parametrize('name', [
|
||||
'weather.json', # real data
|
||||
'.weather.json', # a dotted key that completed
|
||||
'.gitignore', # not ours
|
||||
'.hidden', # not ours
|
||||
'weather.json.bak', # no leading dot: someone else's
|
||||
'.json.abc', # no key between the dot and .json.
|
||||
'.weather.json.', # no random component
|
||||
'notes.txt',
|
||||
])
|
||||
def test_everything_else_is_left_alone(self, name):
|
||||
assert not DiskCache._is_orphaned_temp(name)
|
||||
|
||||
def test_the_names_set_actually_creates_are_matched(self, cache, tmp_path):
|
||||
"""Guard against the predicate and the writer drifting apart."""
|
||||
created = []
|
||||
real = os.replace
|
||||
|
||||
def capture(src, dst):
|
||||
created.append(os.path.basename(src))
|
||||
return real(src, dst)
|
||||
|
||||
import src.cache.disk_cache as mod
|
||||
mod.os.replace = capture
|
||||
try:
|
||||
cache.set('weather', {'v': 1})
|
||||
finally:
|
||||
mod.os.replace = real
|
||||
|
||||
assert created, "set() did not go through the temp-file path"
|
||||
assert all(DiskCache._is_orphaned_temp(n) for n in created), created
|
||||
|
||||
|
||||
class TestTheSweep:
|
||||
def test_an_old_orphan_is_removed(self, cache, tmp_path):
|
||||
p = _write(tmp_path, '.weather.json.a1b2c3d4', 'x' * 5000)
|
||||
_age(p, _ORPHAN_TEMP_MAX_AGE_SECONDS + 60)
|
||||
|
||||
stats = cache.cleanup_expired_files(FakeStrategy(), POLICIES)
|
||||
|
||||
assert not p.exists()
|
||||
assert stats['orphan_temp_files_deleted'] == 1
|
||||
assert stats['space_freed_bytes'] >= 5000
|
||||
|
||||
def test_an_in_flight_write_is_not_snatched_away(self, cache, tmp_path):
|
||||
# The whole risk of this sweep: deleting a temp file another thread is
|
||||
# about to os.replace into place.
|
||||
p = _write(tmp_path, '.weather.json.inflight')
|
||||
|
||||
cache.cleanup_expired_files(FakeStrategy(), POLICIES)
|
||||
|
||||
assert p.exists()
|
||||
|
||||
def test_real_cache_files_survive(self, cache, tmp_path):
|
||||
fresh = _write(tmp_path, 'weather.json')
|
||||
dotted = _write(tmp_path, '.weather.json')
|
||||
_age(dotted, _ORPHAN_TEMP_MAX_AGE_SECONDS + 60)
|
||||
|
||||
cache.cleanup_expired_files(FakeStrategy(), POLICIES)
|
||||
|
||||
assert fresh.exists()
|
||||
assert dotted.exists(), "a completed .json was treated as a temp file"
|
||||
|
||||
def test_unrelated_dotfiles_survive(self, cache, tmp_path):
|
||||
keep = _write(tmp_path, '.gitignore')
|
||||
_age(keep, 400 * 86400)
|
||||
|
||||
cache.cleanup_expired_files(FakeStrategy(), POLICIES)
|
||||
|
||||
assert keep.exists()
|
||||
|
||||
def test_expiry_still_works_alongside_it(self, cache, tmp_path):
|
||||
stale = _write(tmp_path, 'old.json')
|
||||
_age(stale, 40 * 86400) # past the 30-day default
|
||||
orphan = _write(tmp_path, '.old.json.zz999999')
|
||||
_age(orphan, _ORPHAN_TEMP_MAX_AGE_SECONDS + 60)
|
||||
|
||||
stats = cache.cleanup_expired_files(FakeStrategy(), POLICIES)
|
||||
|
||||
assert not stale.exists()
|
||||
assert not orphan.exists()
|
||||
assert stats['files_deleted'] == 2
|
||||
assert stats['orphan_temp_files_deleted'] == 1
|
||||
|
||||
def test_the_rig_scenario(self, cache, tmp_path):
|
||||
"""76 orphans of assorted ages, none of them reachable before."""
|
||||
for i in range(76):
|
||||
p = _write(tmp_path, '.sched_%d.json.r%06d' % (i, i), 'x' * 1000)
|
||||
_age(p, (i + 2) * 86400)
|
||||
keep = _write(tmp_path, 'sched.json')
|
||||
|
||||
stats = cache.cleanup_expired_files(FakeStrategy(), POLICIES)
|
||||
|
||||
assert stats['orphan_temp_files_deleted'] == 76
|
||||
assert keep.exists()
|
||||
assert not list(tmp_path.glob('.sched_*'))
|
||||
# The summary line is "<deleted>/<scanned>", so an orphan that is
|
||||
# deleted but never counted as scanned renders as "76/1".
|
||||
assert stats['files_scanned'] == 77
|
||||
assert stats['files_deleted'] <= stats['files_scanned']
|
||||
|
||||
def test_deleted_never_exceeds_scanned(self, cache, tmp_path):
|
||||
p = _write(tmp_path, '.only.json.a1b2c3d4')
|
||||
_age(p, _ORPHAN_TEMP_MAX_AGE_SECONDS + 60)
|
||||
|
||||
stats = cache.cleanup_expired_files(FakeStrategy(), POLICIES)
|
||||
|
||||
assert stats['files_deleted'] == 1
|
||||
assert stats['files_scanned'] == 1
|
||||
|
||||
def test_a_missing_file_mid_sweep_is_not_an_error(self, cache, tmp_path):
|
||||
p = _write(tmp_path, '.weather.json.a1b2c3d4')
|
||||
_age(p, _ORPHAN_TEMP_MAX_AGE_SECONDS + 60)
|
||||
|
||||
import src.cache.disk_cache as mod
|
||||
real = mod.os.path.getsize
|
||||
|
||||
def vanish(path):
|
||||
if path.endswith('.a1b2c3d4'):
|
||||
os.remove(path)
|
||||
raise FileNotFoundError(path)
|
||||
return real(path)
|
||||
|
||||
mod.os.path.getsize = vanish
|
||||
try:
|
||||
stats = cache.cleanup_expired_files(FakeStrategy(), POLICIES)
|
||||
finally:
|
||||
mod.os.path.getsize = real
|
||||
|
||||
assert stats['errors'] == 0
|
||||
@@ -237,3 +237,45 @@ class TestDisplayManagerDoubleSided:
|
||||
suppress_test_pattern=True)
|
||||
assert dm.set_brightness(70) is True
|
||||
assert mock_rgb_matrix['matrix_instance'].brightness == 70
|
||||
|
||||
|
||||
class TestDisplayManagerOrientation:
|
||||
"""The orientation setting composes onto pixel_mapper_config for panels
|
||||
mounted upside down, without disturbing a custom pixel_mapper_config."""
|
||||
|
||||
def _config(self, **hardware_overrides):
|
||||
config = {
|
||||
'display': {
|
||||
'hardware': {
|
||||
'rows': 32, 'cols': 64, 'chain_length': 2, 'parallel': 1,
|
||||
'hardware_mapping': 'adafruit-hat-pwm', 'brightness': 90,
|
||||
},
|
||||
'runtime': {'gpio_slowdown': 2},
|
||||
},
|
||||
'timezone': 'UTC',
|
||||
'plugin_system': {'plugins_directory': 'plugins'},
|
||||
}
|
||||
config['display']['hardware'].update(hardware_overrides)
|
||||
return config
|
||||
|
||||
def test_default_orientation_leaves_pixel_mapper_config_untouched(self, mock_rgb_matrix):
|
||||
DisplayManager._instance = None
|
||||
with patch.dict('os.environ', {'EMULATOR': 'false'}):
|
||||
DisplayManager(self._config(), suppress_test_pattern=True)
|
||||
options = mock_rgb_matrix['options_class'].return_value
|
||||
assert options.pixel_mapper_config == ''
|
||||
|
||||
def test_orientation_180_appends_rotate_mapper(self, mock_rgb_matrix):
|
||||
DisplayManager._instance = None
|
||||
with patch.dict('os.environ', {'EMULATOR': 'false'}):
|
||||
DisplayManager(self._config(orientation='180'), suppress_test_pattern=True)
|
||||
options = mock_rgb_matrix['options_class'].return_value
|
||||
assert options.pixel_mapper_config == 'Rotate:180'
|
||||
|
||||
def test_orientation_180_composes_with_existing_pixel_mapper_config(self, mock_rgb_matrix):
|
||||
DisplayManager._instance = None
|
||||
with patch.dict('os.environ', {'EMULATOR': 'false'}):
|
||||
DisplayManager(self._config(orientation='180', pixel_mapper_config='U-mapper'),
|
||||
suppress_test_pattern=True)
|
||||
options = mock_rgb_matrix['options_class'].return_value
|
||||
assert options.pixel_mapper_config == 'U-mapper;Rotate:180'
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
"""Tests that startup does not wait indefinitely for plugins to fetch data.
|
||||
|
||||
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.
|
||||
|
||||
Profiled on a live rig with py-spy, the main thread sat 9.34s in
|
||||
|
||||
display_controller._update_modules
|
||||
-> plugin_executor.execute_update
|
||||
-> execute_with_timeout -> threading.join
|
||||
|
||||
and the controller's own log put the full pass at 82 seconds on the worst
|
||||
boot measured (55 and 26 on the two before). 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.
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
# display_controller imports display_manager, which binds the hardware
|
||||
# rgbmatrix module unless EMULATOR=true is set before import (same convention
|
||||
# as test_display_controller_vegas_tick.py).
|
||||
os.environ.setdefault("EMULATOR", "true")
|
||||
|
||||
from src.display_controller import ( # noqa: E402
|
||||
DisplayController, _INITIAL_UPDATE_BUDGET_SECONDS,
|
||||
_MIN_INITIAL_UPDATE_TIMEOUT_SECONDS)
|
||||
|
||||
|
||||
class FakeExecutor:
|
||||
"""Records which plugins were updated, and can make some of them slow."""
|
||||
|
||||
def __init__(self, cost=0.0, slow=()):
|
||||
self.updated = []
|
||||
self.cost = cost
|
||||
self.slow = set(slow)
|
||||
|
||||
def execute_update(self, plugin, plugin_id, timeout=None):
|
||||
self.updated.append(plugin_id)
|
||||
if plugin_id in self.slow:
|
||||
time.sleep(self.cost)
|
||||
return True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tiny_floor(monkeypatch):
|
||||
"""Shrink the "worth starting" floor so timing tests stay quick."""
|
||||
import src.display_controller as mod
|
||||
monkeypatch.setattr(mod, "_MIN_INITIAL_UPDATE_TIMEOUT_SECONDS", 0.01)
|
||||
|
||||
|
||||
def _controller(plugin_ids, executor):
|
||||
c = DisplayController.__new__(DisplayController)
|
||||
c.plugin_manager = Mock()
|
||||
# Both attributes, because _update_modules reads
|
||||
# `loaded_plugins or plugins` and an empty dict is falsy.
|
||||
c.plugin_manager.loaded_plugins = {pid: Mock() for pid in plugin_ids}
|
||||
c.plugin_manager.plugins = dict(c.plugin_manager.loaded_plugins)
|
||||
c.plugin_manager.plugin_executor = executor
|
||||
c.plugin_manager.plugin_last_update = {}
|
||||
c.plugin_manager.health_tracker = None
|
||||
return c
|
||||
|
||||
|
||||
class TestTheBudgetIsRespected:
|
||||
def test_without_a_deadline_every_plugin_is_updated(self):
|
||||
ex = FakeExecutor()
|
||||
_controller(['a', 'b', 'c'], ex)._update_modules()
|
||||
assert ex.updated == ['a', 'b', 'c']
|
||||
|
||||
def test_a_passed_deadline_stops_the_pass(self):
|
||||
ex = FakeExecutor()
|
||||
_controller(['a', 'b', 'c'], ex)._update_modules(deadline=time.time() - 1)
|
||||
assert ex.updated == [], "updated %r after the deadline" % ex.updated
|
||||
|
||||
def test_slow_plugins_do_not_drag_in_the_rest(self, tiny_floor):
|
||||
# One plugin burns the whole budget; the remainder must be left alone
|
||||
# rather than each adding its own wait.
|
||||
ex = FakeExecutor(cost=0.3, slow={'slow'})
|
||||
c = _controller(['slow'] + ['p%d' % i for i in range(20)], ex)
|
||||
started = time.time()
|
||||
c._update_modules(deadline=started + 0.2)
|
||||
elapsed = time.time() - started
|
||||
|
||||
assert ex.updated == ['slow'], "updated %r" % ex.updated
|
||||
# Bounded by the one in-flight update, not by twenty more.
|
||||
assert elapsed < 1.0, "%.2fs" % elapsed
|
||||
|
||||
def test_a_generous_deadline_still_gets_everything(self):
|
||||
ex = FakeExecutor()
|
||||
c = _controller(['a', 'b', 'c'], ex)
|
||||
c._update_modules(deadline=time.time() + 30)
|
||||
assert ex.updated == ['a', 'b', 'c']
|
||||
|
||||
def test_the_deadline_is_checked_before_each_plugin(self, tiny_floor):
|
||||
# Not just once up front: the budget can be spent partway through.
|
||||
ex = FakeExecutor(cost=0.15, slow={'a', 'b', 'c', 'd'})
|
||||
c = _controller(['a', 'b', 'c', 'd'], ex)
|
||||
c._update_modules(deadline=time.time() + 0.2)
|
||||
assert 0 < len(ex.updated) < 4, "updated %r" % ex.updated
|
||||
|
||||
|
||||
class TestThePassIsBoundedInPractice:
|
||||
def test_the_last_plugin_cannot_overrun_the_budget(self):
|
||||
# Checking the deadline before each plugin is not enough on its own:
|
||||
# one that starts with a moment left could still block for the
|
||||
# executor's full timeout. On the rig that turned a 20s budget into a
|
||||
# 31.8s pass, so the remaining budget is passed down as the timeout.
|
||||
seen = []
|
||||
|
||||
class Executor:
|
||||
def execute_update(self, plugin, plugin_id, timeout=None):
|
||||
seen.append(timeout)
|
||||
return True
|
||||
|
||||
c = _controller(['a', 'b', 'c'], Executor())
|
||||
deadline = time.time() + 5
|
||||
c._update_modules(deadline=deadline)
|
||||
|
||||
assert seen and all(t is not None for t in seen), seen
|
||||
assert all(t <= 5.01 for t in seen), seen
|
||||
# The exact remainder, never clamped up: clamping would let the pass
|
||||
# run past its deadline. Anything below the floor is deferred instead,
|
||||
# so what does start always has a usable slot.
|
||||
assert all(t >= _MIN_INITIAL_UPDATE_TIMEOUT_SECONDS for t in seen), seen
|
||||
|
||||
def test_without_a_deadline_the_executor_default_is_left_alone(self):
|
||||
seen = []
|
||||
|
||||
class Executor:
|
||||
def execute_update(self, plugin, plugin_id, timeout=None):
|
||||
seen.append(timeout)
|
||||
return True
|
||||
|
||||
_controller(['a'], Executor())._update_modules()
|
||||
assert seen == [None], seen
|
||||
|
||||
|
||||
class TestTheBudgetItself:
|
||||
def test_it_is_short_enough_to_be_worth_having(self):
|
||||
# The measured uncapped worst case was 82s; a budget near that would
|
||||
# not bound anything.
|
||||
assert _INITIAL_UPDATE_BUDGET_SECONDS <= 30
|
||||
|
||||
def test_it_is_long_enough_for_a_quick_plugin_or_two(self):
|
||||
assert _INITIAL_UPDATE_BUDGET_SECONDS >= 5
|
||||
|
||||
|
||||
class TestNothingIsSilentlyDropped:
|
||||
def test_deferred_plugins_are_named_in_the_log(self, caplog):
|
||||
ex = FakeExecutor()
|
||||
c = _controller(['a', 'b'], ex)
|
||||
with caplog.at_level('INFO'):
|
||||
c._update_modules(deadline=time.time() - 1)
|
||||
text = "\n".join(r.getMessage() for r in caplog.records)
|
||||
assert 'a' in text and 'b' in text, text
|
||||
assert 'budget' in text.lower(), text
|
||||
|
||||
def test_nothing_is_logged_when_all_of_them_ran(self, caplog):
|
||||
ex = FakeExecutor()
|
||||
c = _controller(['a'], ex)
|
||||
with caplog.at_level('INFO'):
|
||||
c._update_modules(deadline=time.time() + 30)
|
||||
assert not any('budget' in r.getMessage().lower() for r in caplog.records)
|
||||
|
||||
|
||||
class TestItDoesNotBreakTheOrdinaryPaths:
|
||||
def test_no_plugin_manager_is_harmless(self):
|
||||
c = DisplayController.__new__(DisplayController)
|
||||
c.plugin_manager = None
|
||||
c._update_modules(deadline=time.time() - 1) # must not raise
|
||||
|
||||
def test_an_empty_plugin_set_is_harmless(self):
|
||||
ex = FakeExecutor()
|
||||
_controller([], ex)._update_modules(deadline=time.time() + 5)
|
||||
assert ex.updated == []
|
||||
|
||||
|
||||
class TestTooLittleBudgetDefersRatherThanClamps:
|
||||
def test_a_plugin_starting_below_the_floor_is_deferred(self):
|
||||
ex = FakeExecutor()
|
||||
c = _controller(['a'], ex)
|
||||
# Just under the floor: previously this was clamped up to the floor and
|
||||
# run anyway, which pushed the pass past its deadline.
|
||||
c._update_modules(
|
||||
deadline=time.time() + _MIN_INITIAL_UPDATE_TIMEOUT_SECONDS - 0.05)
|
||||
assert ex.updated == [], "started a plugin it could not give a slot to"
|
||||
|
||||
def test_a_plugin_starting_above_the_floor_still_runs(self):
|
||||
ex = FakeExecutor()
|
||||
c = _controller(['a'], ex)
|
||||
c._update_modules(
|
||||
deadline=time.time() + _MIN_INITIAL_UPDATE_TIMEOUT_SECONDS + 1)
|
||||
assert ex.updated == ['a']
|
||||
|
||||
def test_the_timeout_is_the_remainder_not_the_floor(self):
|
||||
seen = []
|
||||
|
||||
class Executor:
|
||||
def execute_update(self, plugin, plugin_id, timeout=None):
|
||||
seen.append(timeout)
|
||||
return True
|
||||
|
||||
c = _controller(['a'], Executor())
|
||||
c._update_modules(deadline=time.time() + 9)
|
||||
assert seen and 8.5 <= seen[0] <= 9.01, seen
|
||||
|
||||
def test_the_pass_cannot_outlast_its_deadline(self, tiny_floor):
|
||||
# Every plugin sleeps well past the budget; the deferral keeps the
|
||||
# whole pass inside it rather than overrunning by a floor's worth.
|
||||
ex = FakeExecutor(cost=0.4, slow={'a', 'b', 'c', 'd', 'e'})
|
||||
c = _controller(['a', 'b', 'c', 'd', 'e'], ex)
|
||||
started = time.time()
|
||||
c._update_modules(deadline=started + 0.5)
|
||||
assert time.time() - started < 1.2, "%.2fs" % (time.time() - started)
|
||||
@@ -0,0 +1,190 @@
|
||||
"""Tests the startup screen that shows while plugins fetch their first data.
|
||||
|
||||
That screen is on the panel for the whole initial-update window, and on a
|
||||
headless Pi it is the only place the device's address appears without going
|
||||
looking for it -- so it now carries the address as well as "Initializing".
|
||||
|
||||
Two things have to hold. It must fit every supported panel: the old fixed
|
||||
8px PressStart2P drew "Initializing" 96px wide at x=10, which ran off the
|
||||
side of a 64px panel before an address was ever added. And the lookup must be
|
||||
cheap, because this runs on the startup path that the rest of this change
|
||||
exists to shorten.
|
||||
"""
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import pytest
|
||||
|
||||
os.environ.setdefault("EMULATOR", "true")
|
||||
|
||||
from src.display_manager import DisplayManager # noqa: E402
|
||||
|
||||
SIZES = [(64, 32), (128, 32), (128, 64), (256, 32), (512, 64)]
|
||||
|
||||
|
||||
class FakeMatrix:
|
||||
def __init__(self, width, height):
|
||||
self.width, self.height = width, height
|
||||
|
||||
|
||||
def _manager(width, height):
|
||||
dm = DisplayManager.__new__(DisplayManager)
|
||||
dm.image = Image.new('RGB', (width, height))
|
||||
dm.draw = ImageDraw.Draw(dm.image)
|
||||
dm.matrix = FakeMatrix(width, height)
|
||||
dm.font = ImageFont.truetype('assets/fonts/PressStart2P-Regular.ttf', 8)
|
||||
return dm
|
||||
|
||||
|
||||
def _layout(dm, lines):
|
||||
"""The geometry _draw_startup_banner uses."""
|
||||
font = dm._fitting_font(lines, dm.matrix.width - 2)
|
||||
line_height = dm.draw.textbbox((0, 0), "Ag", font=font)[3] + 1
|
||||
top = max(1, (dm.matrix.height - line_height * len(lines)) // 2)
|
||||
widths = [dm.draw.textlength(t, font=font) for t in lines]
|
||||
return font, widths, top, top + line_height * len(lines)
|
||||
|
||||
|
||||
def _render_over_pattern(width, height, lines):
|
||||
"""Draw the test pattern, then the banner over it, as startup does."""
|
||||
dm = _manager(width, height)
|
||||
dm.draw.rectangle([0, 0, width - 1, height - 1], outline=(255, 0, 0))
|
||||
dm.draw.line([0, 0, width - 1, height - 1], fill=(0, 255, 0))
|
||||
dm._draw_startup_banner(lines, width, height)
|
||||
return dm
|
||||
|
||||
|
||||
class TestTheAddressLookup:
|
||||
def test_it_never_reports_loopback(self):
|
||||
# A loopback address on the panel would be actively misleading -- it is
|
||||
# not something anyone can browse to.
|
||||
ip = DisplayManager._local_ip()
|
||||
assert ip is None or not ip.startswith("127."), ip
|
||||
|
||||
def test_it_looks_like_an_address_when_there_is_one(self):
|
||||
ip = DisplayManager._local_ip()
|
||||
if ip is None:
|
||||
pytest.skip("host has no routable address")
|
||||
parts = ip.split(".")
|
||||
assert len(parts) == 4 and all(p.isdigit() for p in parts), ip
|
||||
|
||||
def test_it_is_cheap_enough_for_the_startup_path(self):
|
||||
DisplayManager._local_ip() # warm anything cacheable
|
||||
started = time.perf_counter()
|
||||
for _ in range(20):
|
||||
DisplayManager._local_ip()
|
||||
per_call = (time.perf_counter() - started) / 20
|
||||
# `hostname -I` with its 2s timeout, which the web launcher uses, would
|
||||
# be thousands of times this.
|
||||
assert per_call < 0.05, "%.1f ms per call" % (per_call * 1000)
|
||||
|
||||
def test_it_returns_none_rather_than_raising(self, monkeypatch):
|
||||
import src.display_manager as mod
|
||||
|
||||
def no_network(*a, **k):
|
||||
raise OSError("network is unreachable")
|
||||
|
||||
monkeypatch.setattr(mod.socket, "socket", no_network)
|
||||
assert DisplayManager._local_ip() is None
|
||||
|
||||
|
||||
class TestItFitsEveryPanel:
|
||||
@pytest.mark.parametrize("width,height", SIZES)
|
||||
def test_both_lines_fit_with_an_address(self, width, height):
|
||||
dm = _manager(width, height)
|
||||
_font, widths, top, bottom = _layout(dm, ["Initializing", "255.255.255.255"])
|
||||
assert all(w <= width - 2 for w in widths), (width, widths)
|
||||
assert bottom <= height and top >= 0, (top, bottom, height)
|
||||
|
||||
@pytest.mark.parametrize("width,height", SIZES)
|
||||
def test_it_still_fits_with_no_address(self, width, height):
|
||||
dm = _manager(width, height)
|
||||
_font, widths, _top, bottom = _layout(dm, ["Initializing"])
|
||||
assert all(w <= width - 2 for w in widths), (width, widths)
|
||||
assert bottom <= height, (bottom, height)
|
||||
|
||||
def test_the_smallest_panel_drops_to_a_narrower_font(self):
|
||||
# The regression this guards: PressStart2P at 8px is 96px wide for
|
||||
# "Initializing", which does not fit 64px however it is positioned.
|
||||
dm = _manager(64, 32)
|
||||
font, widths, _t, _b = _layout(dm, ["Initializing", "10.0.20.104"])
|
||||
assert font is not dm.font, "kept a font that cannot fit"
|
||||
assert max(widths) <= 62, widths
|
||||
|
||||
def test_a_roomy_panel_keeps_the_larger_font(self):
|
||||
dm = _manager(256, 32)
|
||||
font, _w, _t, _b = _layout(dm, ["Initializing", "10.0.20.104"])
|
||||
assert font is dm.font, "needlessly shrank on a panel with room"
|
||||
|
||||
|
||||
class TestPlacement:
|
||||
@pytest.mark.parametrize("width,height", SIZES)
|
||||
def test_the_lines_are_centred(self, width, height):
|
||||
dm = _manager(width, height)
|
||||
lines = ["Initializing", "10.0.20.104"]
|
||||
_font, widths, _t, _b = _layout(dm, lines)
|
||||
for w in widths:
|
||||
left = max(0, (width - w) // 2)
|
||||
assert abs((left + (left + w)) - width) <= 2, (left, w, width)
|
||||
|
||||
def test_the_address_sits_under_the_word(self):
|
||||
dm = _manager(128, 64)
|
||||
font, _w, top, bottom = _layout(dm, ["Initializing", "10.0.20.104"])
|
||||
line_height = dm.draw.textbbox((0, 0), "Ag", font=font)[3] + 1
|
||||
assert bottom - top == line_height * 2
|
||||
|
||||
|
||||
class TestItIsActuallyReadable:
|
||||
"""The point of the address is that someone can read it off the wall."""
|
||||
|
||||
@pytest.mark.parametrize("width,height", SIZES)
|
||||
def test_the_diagonal_does_not_cross_the_text(self, width, height):
|
||||
lines = ["Initializing", "10.0.20.104"]
|
||||
dm = _render_over_pattern(width, height, lines)
|
||||
_font, widths, top, bottom = _layout(dm, lines)
|
||||
# textlength returns a float, so these must be floored before they
|
||||
# can index pixels.
|
||||
block_width = int(max(widths))
|
||||
left = int(max(0, (width - block_width) // 2))
|
||||
|
||||
px = dm.image.load()
|
||||
green = 0
|
||||
for y in range(int(top), min(int(bottom), height)):
|
||||
for x in range(left, min(left + block_width, width)):
|
||||
r, g, b = px[x, y]
|
||||
if g > 128 and r < 128 and b < 128:
|
||||
green += 1
|
||||
assert green == 0, "%d green pixels behind the text at %dx%d" % (
|
||||
green, width, height)
|
||||
|
||||
@pytest.mark.parametrize("width,height", SIZES)
|
||||
def test_the_text_stays_pure_blue(self, width, height):
|
||||
# Not a style choice. The pattern lights one channel per element --
|
||||
# red border, green diagonal, blue text -- so a glance says whether
|
||||
# led_rgb_sequence is right: wire it BGR and the border comes up blue
|
||||
# and this text red. White text would light all three and destroy the
|
||||
# only blue reference on the screen.
|
||||
dm = _render_over_pattern(width, height, ["Initializing", "10.0.20.104"])
|
||||
px = dm.image.load()
|
||||
blue = sum(1 for y in range(height) for x in range(width)
|
||||
if px[x, y] == (0, 0, 255))
|
||||
assert blue > 20, "only %d blue pixels at %dx%d" % (blue, width, height)
|
||||
white = sum(1 for y in range(height) for x in range(width)
|
||||
if px[x, y] == (255, 255, 255))
|
||||
assert white == 0, "%d white pixels would muddy the channel check" % white
|
||||
|
||||
def test_each_element_lights_one_channel(self):
|
||||
# The whole point of the pattern: three pure primaries on screen.
|
||||
dm = _render_over_pattern(128, 64, ["Initializing", "10.0.20.104"])
|
||||
seen = set(dm.image.getdata())
|
||||
assert (255, 0, 0) in seen, "no pure red border"
|
||||
assert (0, 255, 0) in seen, "no pure green diagonal"
|
||||
assert (0, 0, 255) in seen, "no pure blue text"
|
||||
|
||||
def test_nothing_is_drawn_for_no_lines(self):
|
||||
dm = _manager(128, 64)
|
||||
before = dm.image.tobytes()
|
||||
dm._draw_startup_banner([], 128, 64)
|
||||
assert dm.image.tobytes() == before
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Tests the percentile used by the Vegas frame-time log line.
|
||||
|
||||
The FPS line reports p99 next to the worst frame, and the point of having both
|
||||
is that they say different things: p99 is the bad-but-ordinary frame, worst is
|
||||
the outlier. The obvious index, int(n * 0.99), is off by one and at exactly
|
||||
100 samples selects the maximum -- so the two columns would report the same
|
||||
number precisely when the sample was smallest.
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
import pytest
|
||||
|
||||
from src.vegas_mode.coordinator import _percentile
|
||||
|
||||
|
||||
class TestNearestRank:
|
||||
def test_a_hundred_samples_do_not_return_the_maximum(self):
|
||||
ordered = [float(i) for i in range(100)] # 0..99
|
||||
assert _percentile(ordered, 0.99) == 98.0
|
||||
assert _percentile(ordered, 0.99) != max(ordered)
|
||||
|
||||
def test_it_matches_the_nearest_rank_definition(self):
|
||||
for n in (1, 2, 3, 10, 99, 100, 101, 600, 1000):
|
||||
ordered = [float(i) for i in range(n)]
|
||||
expected = ordered[min(n - 1, max(0, math.ceil(n * 0.99) - 1))]
|
||||
assert _percentile(ordered, 0.99) == expected, n
|
||||
|
||||
@pytest.mark.parametrize('fraction,expected', [
|
||||
(0.0, 0.0), # first
|
||||
(0.5, 49.0), # median, nearest-rank
|
||||
(1.0, 99.0), # last
|
||||
])
|
||||
def test_other_fractions(self, fraction, expected):
|
||||
assert _percentile([float(i) for i in range(100)], fraction) == expected
|
||||
|
||||
|
||||
class TestEdges:
|
||||
def test_empty_is_zero_not_an_error(self):
|
||||
# The loop calls this before any frame has been timed.
|
||||
assert _percentile([], 0.99) == 0.0
|
||||
|
||||
def test_a_single_sample_is_itself(self):
|
||||
assert _percentile([4.2], 0.99) == 4.2
|
||||
|
||||
def test_it_never_indexes_past_the_end(self):
|
||||
for n in range(1, 50):
|
||||
_percentile([float(i) for i in range(n)], 1.0) # must not raise
|
||||
|
||||
|
||||
class TestItSaysSomethingUsefulAboutFrames:
|
||||
def test_one_freeze_does_not_drag_p99_up(self):
|
||||
# 599 healthy frames and one 3.2s freeze: p99 should still describe
|
||||
# the healthy population, while the worst frame is reported separately.
|
||||
frames = [0.0083] * 599 + [3.2]
|
||||
p99 = _percentile(sorted(frames), 0.99)
|
||||
assert p99 == pytest.approx(0.0083), p99
|
||||
assert max(frames) == 3.2
|
||||
|
||||
def test_sustained_slowness_does_move_it(self):
|
||||
# Ten percent of frames slow is not an outlier, it is the shape of the
|
||||
# distribution, and p99 must reflect that.
|
||||
frames = [0.0083] * 540 + [0.05] * 60
|
||||
assert _percentile(sorted(frames), 0.99) == pytest.approx(0.05)
|
||||
@@ -796,7 +796,7 @@ def save_main_config():
|
||||
'gpio_slowdown', 'rp1_rio', 'scan_mode', 'disable_hardware_pulsing', 'inverse_colors', 'show_refresh_rate',
|
||||
'pwm_bits', 'pwm_dither_bits', 'pwm_lsb_nanoseconds', 'limit_refresh_rate_hz', 'use_short_date_format',
|
||||
'max_dynamic_duration_seconds', 'led_rgb_sequence', 'multiplexing', 'panel_type',
|
||||
'row_address_type', 'pixel_mapper_config']
|
||||
'row_address_type', 'pixel_mapper_config', 'orientation']
|
||||
|
||||
if any(k in data for k in display_fields):
|
||||
if 'display' not in current_config:
|
||||
@@ -831,6 +831,11 @@ def save_main_config():
|
||||
if 'pixel_mapper_config' in data and not isinstance(data['pixel_mapper_config'], str):
|
||||
return jsonify({'status': 'error', 'message': 'pixel_mapper_config must be a string (e.g. "U-mapper;Rotate:90" or empty)'}), 400
|
||||
|
||||
# Validate orientation (physical mounting rotation; composed onto pixel_mapper_config at runtime)
|
||||
ORIENTATION_ALLOWED = {'normal', '180'}
|
||||
if 'orientation' in data and data['orientation'] not in ORIENTATION_ALLOWED:
|
||||
return jsonify({'status': 'error', 'message': f"Invalid orientation '{data['orientation']}'. Allowed values: {', '.join(sorted(ORIENTATION_ALLOWED))}"}), 400
|
||||
|
||||
# Validate row_address_type
|
||||
if 'row_address_type' in data:
|
||||
try:
|
||||
@@ -844,7 +849,7 @@ def save_main_config():
|
||||
for field in ['rows', 'cols', 'chain_length', 'parallel', 'brightness', 'hardware_mapping', 'scan_mode',
|
||||
'pwm_bits', 'pwm_dither_bits', 'pwm_lsb_nanoseconds', 'limit_refresh_rate_hz',
|
||||
'led_rgb_sequence', 'multiplexing', 'panel_type', 'row_address_type',
|
||||
'pixel_mapper_config']:
|
||||
'pixel_mapper_config', 'orientation']:
|
||||
if field in data:
|
||||
if field in ['rows', 'cols', 'chain_length', 'parallel', 'brightness', 'scan_mode',
|
||||
'pwm_bits', 'pwm_dither_bits', 'pwm_lsb_nanoseconds', 'limit_refresh_rate_hz',
|
||||
|
||||
@@ -117,6 +117,14 @@
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group" id="setting-display-orientation" data-setting-key="display.hardware.orientation">
|
||||
<label for="orientation" class="block text-sm font-medium text-gray-700">Panel Orientation{{ ui.help_tip('Rotates the rendered image to match how the panel is physically mounted.\nUse "Upside Down" if you flipped the panel 180° to move the Raspberry Pi / wiring to a more convenient side.', 'Panel Orientation') }}</label>
|
||||
<select id="orientation" name="orientation" class="form-control">
|
||||
<option value="normal" {% if main_config.display.hardware.get('orientation', 'normal') == "normal" %}selected{% endif %}>Normal</option>
|
||||
<option value="180" {% if main_config.display.hardware.get('orientation', 'normal') == "180" %}selected{% endif %}>Upside Down (180°)</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="form-group" id="setting-display-led_rgb_sequence" data-setting-key="display.hardware.led_rgb_sequence">
|
||||
<label for="led_rgb_sequence" class="block text-sm font-medium text-gray-700">LED RGB Sequence{{ ui.help_tip('Order the panel expects color channels in.\nChange this only if reds/greens/blues look swapped. Default: RGB.', 'LED RGB Sequence') }}</label>
|
||||
<select id="led_rgb_sequence" name="led_rgb_sequence" class="form-control">
|
||||
|
||||
Reference in New Issue
Block a user