Repository navigation
Expand file tree
/
Copy pathcontrol.py
More file actions
49 lines (37 loc) · 1.46 KB
/
Copy pathcontrol.py
File metadata and controls
49 lines (37 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Thread-safe bridge between the HomeKit accessory and the display loop.
The pyhap driver runs in its own thread and mutates this object from setter
callbacks; the main loop reads it every tick. Keep it tiny and lock-guarded so
neither side can see a half-updated value. No third-party deps, so the app
imports cleanly whether or not HomeKit is installed.
"""
from __future__ import annotations
import threading
class DisplayControl:
"""Shared on/off + brightness state for the display.
``enabled`` is whether the time is shown at all (False blanks the 7-seg to
extend its life). ``brightness`` is the HT16K33 dimming level (0-15), the
same scale as Config.brightness.
"""
def __init__(self, *, enabled: bool = True, brightness: int = 3) -> None:
self._lock = threading.Lock()
self._enabled = bool(enabled)
self._brightness = self._clamp(brightness)
@staticmethod
def _clamp(level: int) -> int:
return max(0, min(15, int(level)))
@property
def enabled(self) -> bool:
with self._lock:
return self._enabled
@enabled.setter
def enabled(self, value: bool) -> None:
with self._lock:
self._enabled = bool(value)
@property
def brightness(self) -> int:
with self._lock:
return self._brightness
@brightness.setter
def brightness(self, level: int) -> None:
with self._lock:
self._brightness = self._clamp(level)