v.0.5.0 cleanup code, utils ...

This commit is contained in:
tiijay
2025-11-12 10:49:53 +01:00
parent 74eca565a1
commit e58029f165
10 changed files with 135 additions and 95 deletions

View File

@@ -1,3 +1,5 @@
from .system_load import show_system_load
from .colors import *
from .utils import *
from .time_utils import *
from .font_utils import *
from .math_utils import *

82
app/utils/font_utils.py Normal file
View File

@@ -0,0 +1,82 @@
from .math_utils import show_byte_matrix
def char_width(char_matrix) -> int:
"""Berechnung der Bits für die Zeichenbreite
Args:
char_matrix (int): Zeichen als Array[int]
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
return cw
def shift_letter_right(char, letter, debug=False) -> bool:
"""Prüfe ob das Zeichen auch komplett nach rechts gezogen ist
Args:
letter (_type_): Array[0..nBytes]
Returns:
bool: _description_
"""
def isLetterRight(letter):
def isByteRight(byte):
return True if 1 & byte == 1 else False
for byte in letter:
if isByteRight(byte):
return True
return False
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
return shifted
if isLetterRight(letter):
return 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)
return shifted
def align_font(font, debug=False):
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('}')
# return f'#keys: {len(list(font.keys()))}'
return f'#keys: {len(font)}'

9
app/utils/math_utils.py Normal file
View File

@@ -0,0 +1,9 @@
def show_byte_matrix(char, matrix):
print(f'byte_matrix: char({char})')
matrix_str = [f'0x{byte:02X}' for byte in matrix]
[print(f'{matrix_str[idx]} {number_to_bitarray_msb(byte)}') for idx, byte in enumerate(matrix)]
def number_to_bitarray_msb(number, bits=8):
"""Convert 8/16-bit number to bit array (MSB first)"""
return [(number >> i) & 1 for i in range(bits - 1, -1, -1)]

View File

@@ -1,17 +1,5 @@
import time
def show_byte_matrix(char, matrix):
print(f'byte_matrix: char({char})')
matrix_str = [f'0x{byte:02X}' for byte in matrix]
[print(f'{matrix_str[idx]} {number_to_bitarray_msb(byte)}') for idx, byte in enumerate(matrix)]
def number_to_bitarray_msb(number, bits=8):
"""Convert 8/16-bit number to bit array (MSB first)"""
return [(number >> i) & 1 for i in range(bits - 1, -1, -1)]
def get_datetime_string(format='full'):
"""
Return date/time as string with different formats