v.0.9.0 DigitalClock: optimzed update the display

This commit is contained in:
tiijay
2025-11-19 16:45:37 +00:00
parent f9e0e6b74b
commit c20625278d
3 changed files with 123 additions and 66 deletions

View File

@@ -1,82 +1,108 @@
from .math_utils import show_byte_matrix
def text_width(text: str, font: dict[str, list[int]]) -> int:
text_width_overall: int = sum([char_width(font[char]) for char in text])
return text_width_overall + len(text) - 1
def char_width(char_matrix) -> int:
"""Berechnung der Bits für die Zeichenbreite
"""Berechnung der Bits für die Zeichenbreite
Args:
char_matrix (int): Zeichen als Array[int]
Args:
char_matrix (int): Zeichen als Array[int]
Returns:
int: Anzahl Bits für die Zeichenbreite
"""
max_val = max(char_matrix)
Returns:
int: Anzahl Bits für die Zeichenbreite
"""
max_val = max(char_matrix)
val = max_val
cw = 0
while 0xFFFFFFFF & val:
"""rechts shiften, bis alles Nullen da sind"""
val >>= 1
cw += 1
val = max_val
cw = 0
while 0xFFFFFFFF & val:
"""rechts shiften, bis alles Nullen da sind"""
val >>= 1
cw += 1
return cw
return cw
def shift_letter_right(char, letter, debug=False) -> bool:
"""Prüfe ob das Zeichen auch komplett nach rechts gezogen ist
"""Prüfe ob das Zeichen auch komplett nach rechts gezogen ist
Args:
letter (_type_): Array[0..nBytes]
Args:
letter (_type_): Array[0..nBytes]
Returns:
bool: _description_
"""
Returns:
bool: _description_
"""
def isLetterRight(letter):
def isByteRight(byte):
return True if 1 & byte == 1 else False
def isLetterRight(letter):
def isByteRight(byte):
return True if 1 & byte == 1 else False
for byte in letter:
if isByteRight(byte):
return True
for byte in letter:
if isByteRight(byte):
return True
return False
return False
def shiftLetterRight(letter):
def shift(letter):
return [byte >> 1 for byte in letter]
def shiftLetterRight(letter):
def shift(letter):
return [byte >> 1 for byte in letter]
shifted = letter
for i in range(8): # max 1 Bit's
shifted = shift(shifted)
if isLetterRight(shifted):
break
shifted = letter
for i in range(8): # max 1 Bit's
shifted = shift(shifted)
if isLetterRight(shifted):
break
return shifted
return shifted
if isLetterRight(letter):
return letter
if isLetterRight(letter):
return letter
# letter is not right shifted
shifted = shiftLetterRight(letter)
# letter is not right shifted
shifted = shiftLetterRight(letter)
if debug:
print('origin:')
show_byte_matrix(char, letter)
print('shifted')
show_byte_matrix(char, shifted)
if debug:
print("origin:")
show_byte_matrix(char, letter)
print("shifted")
show_byte_matrix(char, shifted)
return shifted
return shifted
def align_font(font, debug=False):
chars = [char for char in font]
print(chars)
chars = [char for char in font]
print(chars)
# Print the dictionary
print('font_pretty = {')
for char in sorted(font.keys()):
shifted = shift_letter_right(char=char, letter=font[char], debug=debug)
hex_values = [f'0x{val:02X}' for val in shifted]
print(f"\t'{char}': [{', '.join(hex_values)}],")
print('}')
# Print the dictionary
print("font_pretty = {")
for char in sorted(font.keys()):
shifted = shift_letter_right(char=char, letter=font[char], debug=debug)
hex_values = [f"0x{val:02X}" for val in shifted]
print(f"\t'{char}': [{', '.join(hex_values)}],")
print("}")
# return f'#keys: {len(list(font.keys()))}'
return f'#keys: {len(font)}'
# return f'#keys: {len(list(font.keys()))}'
return f"#keys: {len(font)}"
def find_first_mismatch(str1: str, str2: str) -> int:
"""
Compare two strings of equal length and return the position of first mismatch.
Args:
str1 (str): First string
str2 (str): Second string (same length as str1)
Returns:
int: Position of first mismatch, or -1 if strings are identical
"""
for i in range(len(str1)):
if str1[i] != str2[i]:
return i
return -1