dashboard/dashboard/backlight.py
bashynx fb3c01cdd7 dashPi: a self-hosted home dashboard, its sync server and phone app
The dashboard (Raspberry Pi 3.5" screen + web pages), the end-to-end
encrypted sync mailbox (PHP + SQLite) and the Android companion app
(Flutter), with docs, clean-install checks and screenshots made from
demo data.

Built mostly with an AI coding assistant; not yet reviewed line by line
by a human (see README and SECURITY.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-24 09:56:19 +02:00

53 lines
2.3 KiB
Python

"""Auto brightness via render-side multiplier.
The hardware backlight on this panel is hardwired full-on. We dim by
multiplying RGB values in software before writing to the framebuffer.
Brightness follows the sun (sunrise/sunset from the weather plugin — Open-Meteo — so this
falls back to MID until it has fetched once; with the weather plugin off, fixed 07:00 /
19:00):
Morning 1h before sunrise (LOW) → sunrise (MID) → 1h after (HIGH)
Day HIGH
Evening 1h before sunset (HIGH) → sunset (MID) → 1h after (LOW)
Night LOW
"""
from datetime import datetime, timedelta
LOW, MID, HIGH = 0.25, 0.55, 1.0
HOUR = timedelta(hours=1)
def compute(store):
"""Multiplier 0..1 for the next frame, from cached sunrise/sunset."""
sr, ss = sun_times(store)
return _from_sun(datetime.now(), sr, ss)
FIXED = (7, 19) # without the weather plugin: "sunrise" 07:00, "sunset" 19:00
def sun_times(store):
"""Today's (sunrise, sunset): the weather plugin's (pluginhost.ask "sun"), (None, None)
until it has fetched once, fixed hours (FIXED) when it's off."""
import pluginhost
if not pluginhost.enabled(store, "weather"):
today = datetime.now().replace(minute=0, second=0, microsecond=0)
return today.replace(hour=FIXED[0]), today.replace(hour=FIXED[1])
return pluginhost.ask(store, "weather", "sun") or (None, None)
def _from_sun(now, sunrise, sunset):
if sunrise is None or sunset is None:
return MID # safe default
if sunrise - HOUR <= now < sunrise: # dawn: LOW -> MID
return LOW + (MID - LOW) * ((now - (sunrise - HOUR)) / HOUR)
if sunrise <= now < sunrise + HOUR: # post: MID -> HIGH
return MID + (HIGH - MID) * ((now - sunrise) / HOUR)
if sunset - HOUR <= now < sunset: # dusk: HIGH -> MID
return HIGH - (HIGH - MID) * ((now - (sunset - HOUR)) / HOUR)
if sunset <= now < sunset + HOUR: # post: MID -> LOW
return MID - (MID - LOW) * ((now - sunset) / HOUR)
if sunrise + HOUR <= now < sunset - HOUR:
return HIGH # daytime
return LOW # deep night