diff --git a/oin_thermostat/screen.py b/oin_thermostat/screen.py index 256cb64..8f87590 100644 --- a/oin_thermostat/screen.py +++ b/oin_thermostat/screen.py @@ -1,6 +1,6 @@ import logging import math -from threading import Timer +from threading import Thread, Timer import bdfparser from sense_hat.sense_hat import SenseHat @@ -36,6 +36,8 @@ class Screen: self._secondary_pixels: list[tuple[int, int, int]] = [(0, 0, 0)] * 8 self.timer = Timer(0, self.set_pixels) + self.auto_dim = AutoDim(self.sense) + self.auto_dim.start() self._held = False self.sense.stick.direction_middle = self.stick_click @@ -142,6 +144,43 @@ class Screen: return self.font.draw(value, mode=0).crop(8, 7).todata(3) +class AutoDim(Thread): + def __init__(self, sense: SenseHat): + super().__init__() + + self.daemon = True + self.sense = sense + self.dim = False + self.switching = False + self.sense.gamma_reset() + + def run(self) -> None: + while True: + self.auto_dim() + + def auto_dim(self) -> None: + accel_z = self.sense.get_accelerometer_raw()["z"] + + if not self.switching and accel_z < 0.9: + self.switching = True + self.dim = not self.dim + + elif self.switching and accel_z > 0.98: + self.switching = False + + @property + def dim(self) -> bool: + return self._dim + + @dim.setter + def dim(self, value: bool) -> None: + if value: + self.sense.gamma = [0] * 32 + else: + self.sense.gamma_reset() + self._dim = value + + def format_value(value: float) -> str: v = math.trunc(value) d = "." if (value - v) >= 0.5 else "" diff --git a/typings/sense_hat/sense_hat.pyi b/typings/sense_hat/sense_hat.pyi index 247b220..9de2560 100644 --- a/typings/sense_hat/sense_hat.pyi +++ b/typings/sense_hat/sense_hat.pyi @@ -106,7 +106,7 @@ class SenseHat: def gamma(self): ... @gamma.setter def gamma(self, buffer): ... - def gamma_reset(self): # -> None: + def gamma_reset(self) -> None: # -> None: """ Resets the LED matrix gamma correction to default """ @@ -213,7 +213,9 @@ class SenseHat: def accel(self): ... @property def accelerometer(self): ... - def get_accelerometer_raw(self): # -> dict[str, Any] | dict[str, int]: + def get_accelerometer_raw( + self, + ) -> dict[str, int]: # -> dict[str, Any] | dict[str, int]: """ Accelerometer x y z raw data in Gs """