diff options
Diffstat (limited to 'deckit')
| -rw-r--r-- | deckit/modules/tablet.py | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/deckit/modules/tablet.py b/deckit/modules/tablet.py index 3f4b0d9..01a5eba 100644 --- a/deckit/modules/tablet.py +++ b/deckit/modules/tablet.py @@ -25,7 +25,7 @@ import subprocess import threading import time -from ..module import Module +from ..module import Module, ModuleError STATUS = {"2": "charging", "3": "discharging", "4": "not charging", "5": "full"} TTL = 10 # seconds; each read is two adb round trips @@ -105,4 +105,45 @@ def make(cfg: dict) -> Module: cache["t"] = time.monotonic() return cache["v"] - return Module(name="tablet", title="Tablet", state=state) + tcfg = cfg.get("tablet", {}) + dim_after = max(0, int(tcfg.get("dim_after", 120))) + level = min(255, max(0, int(tcfg.get("dim_brightness", 8)))) + saved = {} # mode and brightness from before a dim; empty while bright + + def shell(cmd): + serial = cache["serial"] or _find_tablet() + if not serial: + raise ModuleError("no tablet with the dashboard tunnel") + try: + return _adb("-s", serial, "shell", cmd).strip() + except (subprocess.SubprocessError, OSError) as e: + raise ModuleError(str(e) or type(e).__name__) + + # The backlight is what drains an LCD, so a dim page alone saves little. + # Auto brightness is switched off while dimmed, else it raises it again. + def dim(_=None): + with lock: + if not saved: + saved["mode"] = shell("settings get system screen_brightness_mode") + saved["level"] = shell("settings get system screen_brightness") + shell(f"settings put system screen_brightness_mode 0;" + f" settings put system screen_brightness {level}") + + # ponytail: the saved level lives in memory; a crash while dimmed leaves + # the tablet dim until its brightness is raised by hand. + def undim(_=None): + with lock: + if saved: + shell(f"settings put system screen_brightness {int(saved['level'])};" + f" settings put system screen_brightness_mode {int(saved['mode'])}") + saved.clear() + + def stop(): + try: + undim() + except ModuleError: + pass + + return Module(name="tablet", title="Tablet", + state=lambda: {**state(), "dim_after": dim_after}, + actions={"dim": dim, "undim": undim}, stop=stop) |
