Add auto dim led panel

This commit is contained in:
Edgar P. Burkhart 2024-12-09 17:01:38 +01:00
parent 3aaaad6fcf
commit 3706fd6ade
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
2 changed files with 44 additions and 3 deletions

View file

@ -1,6 +1,6 @@
import logging import logging
import math import math
from threading import Timer from threading import Thread, Timer
import bdfparser import bdfparser
from sense_hat.sense_hat import SenseHat 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._secondary_pixels: list[tuple[int, int, int]] = [(0, 0, 0)] * 8
self.timer = Timer(0, self.set_pixels) self.timer = Timer(0, self.set_pixels)
self.auto_dim = AutoDim(self.sense)
self.auto_dim.start()
self._held = False self._held = False
self.sense.stick.direction_middle = self.stick_click 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) 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: def format_value(value: float) -> str:
v = math.trunc(value) v = math.trunc(value)
d = "." if (value - v) >= 0.5 else "" d = "." if (value - v) >= 0.5 else ""

View file

@ -106,7 +106,7 @@ class SenseHat:
def gamma(self): ... def gamma(self): ...
@gamma.setter @gamma.setter
def gamma(self, buffer): ... def gamma(self, buffer): ...
def gamma_reset(self): # -> None: def gamma_reset(self) -> None: # -> None:
""" """
Resets the LED matrix gamma correction to default Resets the LED matrix gamma correction to default
""" """
@ -213,7 +213,9 @@ class SenseHat:
def accel(self): ... def accel(self): ...
@property @property
def accelerometer(self): ... 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 Accelerometer x y z raw data in Gs
""" """