Skip to content

Config file selection (--config/TURING_CONFIG_FILE), coalescing display update queue, rev_c test fixes - #1141

Open
Sheridan wants to merge 6 commits into
mathoudebine:mainfrom
Sheridan:pr/config-and-display-improvements
Open

Sheridan wants to merge 6 commits into
mathoudebine:mainfrom
Sheridan:pr/config-and-display-improvements

Conversation

@Sheridan

Copy link
Copy Markdown

Summary

This PR bundles three independent improvements; each is self-contained and can be reviewed commit-by-commit (the commits are grouped by topic):

  1. Config file selection — --config / -c CLI option and TURING_CONFIG_FILE environment variable, to keep a personal configuration outside the install tree
  2. Display update pipeline — bounded display lag: coalesce superseded frames per screen region and skip unchanged regions
  3. Test fix — rev_c golden tests crash on current main (hello-handshake attributes missing in the mock)

No breaking changes: existing invocations, config.yaml / theme schemas and the public LcdComm API keep working unchanged.


1. Configuration file selection (--config / TURING_CONFIG_FILE)

Why

The configuration file is hardcoded to config.yaml next to the code (library/config.py loads MAIN_DIRECTORY / "config.yaml" at import time, configure.py reads/writes the same path). Packaged/distribution installs (e.g. /usr/local/opt/turing-smart-screen/) force users to edit the file inside the install tree, which is clobbered on every update and cannot be kept under personal version control.

What

  • All three entry points (main.py, configure.py, theme-editor.py) accept --config <path> / -c <path> to select the configuration file (and, in configure.py, the file to save back).
  • The TURING_CONFIG_FILE environment variable selects the same file, so systemd services and packaged launchers can set it without command-line plumbing (a tools/turing-smart-screen-python.service example is included).
  • Precedence: --config > TURING_CONFIG_FILE > default (<install>/config.yaml — today's behavior).
  • Resolution logic lives once in library/config_path.py, shared by all entry points; it runs in library/config.py before CONFIG_DATA is loaded. An unusable/unreadable/invalid path fails fast with a clear error naming the path and exits non-zero.
  • Theme and font paths still resolve against the install directory, so an external config can keep using res/themes/... and bundled fonts.
  • theme-editor.py's strict len(sys.argv) != 2 check became a real argparse parser so the positional theme name coexists with the new option.

2. Bounded display lag: coalescing + deduplication in the update queue

Why

Under sustained load the scheduler pushes every redraw into an unbounded FIFO (config.update_queue) drained by one QueueHandler at the device's physical rate. When redraw demand approaches the drain rate (measured on a 3.5" Rev A / CDC-ACM setup: demand ~111 KB/s vs effective ~135 KB/s), any CPU/GIL contention starts a backlog: every queued frame is a snapshot of the past, lag grows without bound, and stale frames are sent even when newer values already exist.

What

  • Region-targeted drawing operations (text, progress bar, radial bar, line graph, bitmap at X/Y) become coalescible: when a newer pending update exists for the same screen region, older pending updates for that region are dropped before being sent. Ordering between different regions is preserved (first-seen order), and each image's command+data burst stays atomic.
  • Deduplication: if a region's serialized payload is byte-identical to what was last sent for it, transmission is skipped (static text/bars, graphs between pixel-visible changes).
  • Non-region operations (brightness, orientation, reset, sleep pacing, direct library calls without a queue) keep existing behavior, in order.
  • Sensor sampling, theme schema, config.yaml schema and LcdComm signatures are untouched.

Effect

Displayed data lags at most by the transmission of one current frame per region instead of accumulating; backlog no longer grows memory.

Relation to #1049

#1049 coalesces widget updates for the TUR_USB driver only (full-frame, rate-limited). This change works one level up — in the shared queue/scheduler pipeline — so it benefits the serial revisions (A/B/C/D, WeAct) with region-level freshness guarantees and per-region dedup. The two approaches are complementary, not conflicting.

3. Fix rev_c golden tests

The rev_c test mock never set sub_revision / rom_version, which are now detected by the hello handshake in the serial driver, so the image tests raised AttributeError before sending anything. The mock now provides the same defaults a 5-inch C screen reports (REV_5INCH, rom 87). Six goldens recorded before the bitmap-size payload field existed were refreshed; the only change in them is that field going from 0x0000 to 0x0E10.


Test plan

  • pytest tests on Linux: 72 passed, 2 skipped (platform-conditional skips)
  • Existing golden tests pass unchanged for the single-update path
  • New unit tests: resolver precedence (CLI > env > default), quoting/spacing, coalescing/dedup/drain behavior, end-to-end config file selection for the three entry points
  • Manual run on real hardware (3.5" Rev A, CDC-ACM): graphs keep up under load where they lagged before
  • CI on Windows/macOS

Development notes

These changes were developed with an AI coding assistant (opencode), under my review and supervision — I read, tested and take responsibility for every line in the diff.

The rev_c mock never set sub_revision/rom_version, which are now detected
by the hello handshake in the serial driver, so the image tests raised
AttributeError before sending anything. The mock now provides the same
defaults the handshake detects on a 5-inch C screen (REV_5INCH, rom 87).

Six full-screen/partial goldens were recorded before the bitmap-size
payload field was added to the display command and were refreshed; the
only change in them is that field going from 0x0000 to 0x0E10.
When the producer (stats refresh) is faster than the serial device can
transfer, the update queue grew without bound: every intermediate frame
was still transmitted, so the screen showed ever older data and shutdown
had to drain a stale backlog. The queue writer now works on "transmission
units" (TUs) instead of plain FIFO items:

Queue items became (fn, args, region_key, tu_id):
- Region-based requests (DisplayPILImage/DisplayLineGraph and the window
  command + data chunks they emit) are tagged with the device-coordinate
  region they paint and a per-image transmission unit id, by beginning a
  TU in the driver before queuing and ending it afterwards
  (LcdComm._begin_tu/_end_tu/_queue_request, mutex-protected, RLock so a
  nested SendCommand() joins the outer TU).
- Everything else (control commands, pacing sleeps, reads) keeps
  (None, None) and is never reordered or dropped.

scheduler._process_update_queue() drains the queue in one go and:
- keeps only the newest TU per region, placed at the position where the
  region first appeared, so regions keep their relative order while
  stale frames are dropped;
- skips a TU whose serialized device payload is byte-identical to the
  last one transmitted for that region (dedup cache on the driver,
  _last_sent_payload, cleared by every Clear()/Reset() since the screen
  content is then lost).

Consequences:
- Displayed data is at most one in-flight frame old instead of
  N-frames-behind-and-growing; graph values are rendered from the full
  sample history each frame, so dropping a superseded frame never skips
  a sample on the time axis; a region is only skipped when the device
  already shows exactly the same bytes.
- With update_queue unset (direct mode, library API users) nothing is
  queued at all, so the byte stream to the device is unchanged; existing
  per-rev golden tests pass untouched.
- Only serial revisions use the queue: TUR_USB and SIMU drivers are
  covered by a source test that they must not gain queue usage.
- On SIGINT/SIGTERM the queue is drained to completion (was: 5 s cap on
  a stale backlog) with the same coalescing, so shutdown is faster.

New tests: scheduler unit tests for grouping/supersession/dedup/
stopping (tests/library/test_scheduler.py), per-driver supersession and
dedup checks (rev_a), queued sleep handling (rev_b), write+read pair
staying in one TU (rev_c), queue-usage source guard. All 54 tests pass;
also validated on real hardware (rev A) under sustained CPU/disk load:
queue backlog drains in ~0 s at shutdown, RSS flat over 2 minutes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant