From 33d14bd1ab33f4b579128c2cf5595d63cc83b4fa Mon Sep 17 00:00:00 2001 From: Richard Kacerek Date: Wed, 28 Oct 2026 22:55:08 +0100 Subject: [PATCH] Halve the screen brightness from midnight to 6 am Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- app.py | 6 ++++++ main.py | 7 ++++++- tests/test_app.py | 7 +++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 63c54d0..bd6d20b 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,7 @@ Stop it again before using Coolify, for the serial-port reason above. |---|---|---| | `SCREEN_DEVICE` | `/dev/serial/by-id/usb-2017-2-25_UsbMonitor_USB35INCHIPSV2-if00` | serial device, also passed into the container | | `SCREEN_ORIENTATION` | `REVERSE_LANDSCAPE` | flip to `LANDSCAPE` if upside-down | -| `SCREEN_BRIGHTNESS` | `50` | 0–100 | +| `SCREEN_BRIGHTNESS` | `50` | 0–100; halved from 00:00 to 06:00 | | `BOARD_TZ` | `Europe/Prague` | time zone for departures (the container's own `TZ` is ignored) | | `TZ` | `Europe/Prague` | container time zone (logs only) | | `DATA_DIR` | `/data` | cache directory | diff --git a/app.py b/app.py index e49c0ce..7099308 100644 --- a/app.py +++ b/app.py @@ -16,6 +16,7 @@ log = logging.getLogger(__name__) DEFAULT_TZ = "Europe/Prague" REFRESH_HOUR = 3 RETRY = timedelta(minutes=15) +NIGHT_END_HOUR = 6 # the screen is dimmed from midnight until this hour def local_now(tz_name: str = DEFAULT_TZ) -> datetime: @@ -33,6 +34,11 @@ def refresh_due(last_attempt: datetime | None, now: datetime, last_ok: bool) -> return last_attempt < today_refresh <= now +def brightness(now: datetime, base: int) -> int: + """Backlight level for `now`: half of `base` from 00:00 to 06:00, `base` otherwise.""" + return base // 2 if now.hour < NIGHT_END_HOUR else base + + def load_cache( lines, data_dir, now: datetime, previous_timetables: list[Timetable], previous_stale: bool ) -> tuple[list[Timetable], bool]: diff --git a/main.py b/main.py index b9f4277..bdf938b 100644 --- a/main.py +++ b/main.py @@ -48,7 +48,6 @@ def main() -> None: lcd = LcdCommRevA(com_port=DEVICE) if DEVICE != "AUTO" else LcdCommRevA() lcd.InitializeComm() lcd.SetOrientation(ORIENTATION) - lcd.SetBrightness(BRIGHTNESS) running = True @@ -63,6 +62,7 @@ def main() -> None: last_ok = True mode = None # "board" or "clock" last_minute = last_second = None + level = None # backlight level last sent to the screen draw_failures = 0 # Draw from the existing cache straight away; only refresh at startup if it is old. @@ -100,6 +100,11 @@ def main() -> None: try: if now.minute != last_minute: last_minute = now.minute + new_level = app.brightness(now, BRIGHTNESS) + if new_level != level: + log.info("brightness %d", new_level) + lcd.SetBrightness(new_level) + level = new_level # only recorded once the screen accepted it image = app.board_image(timetables, now, board_fonts, stale, state_holidays) new_mode = "board" if image is not None else "clock" if new_mode != mode: diff --git a/tests/test_app.py b/tests/test_app.py index b344317..b65b21e 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -26,6 +26,13 @@ def test_refresh_retries_every_fifteen_minutes_after_a_failure(): assert app.refresh_due(last, datetime(2026, 10, 6, 12, 15), last_ok=False) is True +def test_brightness_is_halved_from_midnight_to_six(): + assert app.brightness(datetime(2026, 10, 6, 23, 59), 50) == 50 + assert app.brightness(datetime(2026, 10, 7, 0, 0), 50) == 25 + assert app.brightness(datetime(2026, 10, 7, 5, 59), 50) == 25 + assert app.brightness(datetime(2026, 10, 7, 6, 0), 50) == 50 + + def test_local_now_is_naive_prague_time(monkeypatch): monkeypatch.setenv("TZ", "UTC") # a container without a proper TZ time_module.tzset()