Halve the screen brightness from midnight to 6 am

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
rickzkmandClaude Opus 5.5 committed 2026-10-28 22:55:08 +01:00
1 parent 3ad6dc6963
commit 33d14bd1ab
4 files changed
+20 -2

No files matched your search

+1 -1
View File
@@ -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_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_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) | | `BOARD_TZ` | `Europe/Prague` | time zone for departures (the container's own `TZ` is ignored) |
| `TZ` | `Europe/Prague` | container time zone (logs only) | | `TZ` | `Europe/Prague` | container time zone (logs only) |
| `DATA_DIR` | `/data` | cache directory | | `DATA_DIR` | `/data` | cache directory |
+6
View File
@@ -16,6 +16,7 @@ log = logging.getLogger(__name__)
DEFAULT_TZ = "Europe/Prague" DEFAULT_TZ = "Europe/Prague"
REFRESH_HOUR = 3 REFRESH_HOUR = 3
RETRY = timedelta(minutes=15) 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: 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 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( def load_cache(
lines, data_dir, now: datetime, previous_timetables: list[Timetable], previous_stale: bool lines, data_dir, now: datetime, previous_timetables: list[Timetable], previous_stale: bool
) -> tuple[list[Timetable], bool]: ) -> tuple[list[Timetable], bool]:
+6 -1
View File
@@ -48,7 +48,6 @@ def main() -> None:
lcd = LcdCommRevA(com_port=DEVICE) if DEVICE != "AUTO" else LcdCommRevA() lcd = LcdCommRevA(com_port=DEVICE) if DEVICE != "AUTO" else LcdCommRevA()
lcd.InitializeComm() lcd.InitializeComm()
lcd.SetOrientation(ORIENTATION) lcd.SetOrientation(ORIENTATION)
lcd.SetBrightness(BRIGHTNESS)
running = True running = True
@@ -63,6 +62,7 @@ def main() -> None:
last_ok = True last_ok = True
mode = None # "board" or "clock" mode = None # "board" or "clock"
last_minute = last_second = None last_minute = last_second = None
level = None # backlight level last sent to the screen
draw_failures = 0 draw_failures = 0
# Draw from the existing cache straight away; only refresh at startup if it is old. # Draw from the existing cache straight away; only refresh at startup if it is old.
@@ -100,6 +100,11 @@ def main() -> None:
try: try:
if now.minute != last_minute: if now.minute != last_minute:
last_minute = now.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) image = app.board_image(timetables, now, board_fonts, stale, state_holidays)
new_mode = "board" if image is not None else "clock" new_mode = "board" if image is not None else "clock"
if new_mode != mode: if new_mode != mode:
+7
View File
@@ -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 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): def test_local_now_is_naive_prague_time(monkeypatch):
monkeypatch.setenv("TZ", "UTC") # a container without a proper TZ monkeypatch.setenv("TZ", "UTC") # a container without a proper TZ
time_module.tzset() time_module.tzset()