54 lines
1.8 KiB
Python
54 lines
1.8 KiB
Python
from display.neopixel_64x64 import NeoPixel_64x64
|
|
from utils import get_datetime_string, text_width, find_first_mismatch
|
|
from utils import colors
|
|
|
|
|
|
class DigitalClock:
|
|
stored_time_str: str = "99:99:99"
|
|
text_color: tuple[int[3]] = colors.NEON_YELLOW
|
|
clear_color: tuple[int[3]] = colors.BLACK
|
|
|
|
def __init__(self, display: NeoPixel_64x64, xpos: int, ypos: int):
|
|
self.display = display
|
|
self.xpos = xpos
|
|
self.ypos = ypos
|
|
|
|
def _toggle_clear_color(self) -> tuple[int]:
|
|
self.clear_color = (
|
|
colors.BLACK if self.clear_color != colors.BLACK else colors.MAGENTA
|
|
)
|
|
return self.clear_color
|
|
|
|
def _text_width(self, text: str) -> int:
|
|
return text_width(text, self.display.selected_font)
|
|
|
|
async def tick(self):
|
|
time_str: str = get_datetime_string("time")[:8]
|
|
|
|
# ab welcher Position malen wir den String neu
|
|
mismatch_pos: int = find_first_mismatch(self.stored_time_str, time_str)
|
|
|
|
untouched_part: str = time_str[:mismatch_pos]
|
|
fresh_part: str = time_str[mismatch_pos:]
|
|
part_to_clear: str = self.stored_time_str[mismatch_pos:]
|
|
|
|
textwidth_untouched_part: int = self._text_width(untouched_part)
|
|
clear_x_start_pos: int = self.xpos + textwidth_untouched_part + 1
|
|
|
|
textwidth_part_to_clear: int = self._text_width(part_to_clear)
|
|
clear_x_end_pos: int = clear_x_start_pos + textwidth_part_to_clear - 1
|
|
|
|
self.display.clear_box(
|
|
self.ypos,
|
|
clear_x_start_pos,
|
|
self.ypos + self.display.font_height,
|
|
clear_x_end_pos,
|
|
color=self.clear_color,
|
|
)
|
|
|
|
self.display.write_text(
|
|
fresh_part, clear_x_start_pos, self.ypos, color=self.text_color
|
|
)
|
|
|
|
self.stored_time_str = time_str
|