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

@@ -2,6 +2,8 @@ from .font_3x5 import font_3x5
from .font_5x7 import font_5x7
from .font_8x8 import font_8x8
from .font_16x16 import font_16x16
from .fonts_utils import char_width
__all__ = ['font_3x5', 'font_5x7', 'font_8x8', 'font_16x16', 'char_width']
fonts_installed = [font_3x5, font_5x7, font_8x8, font_16x16]
__all__ = ['font_3x5', 'font_5x7', 'font_8x8', 'font_16x16', 'fonts_installed']

View File

@@ -1,135 +0,0 @@
import app.utils as utils
# FIXME: der Import klappt nicht !!!
# from app.display.neopixel_64x64 import NeoPixel_64x64
# from ...display.neopixel_64x64 import NeoPixel_64x64
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:')
utils.show_byte_matrix(char, letter)
print('shifted')
utils.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)}'
def screen_text(text: str, display: NeoPixel_64x64):
"""Text für einen Screen anpassen,
Anzahl der Zeichen je Zeile begrenzen,
ebenso wird die Höhe des Screen brücksichtigt
Args:
text (str): Text der ausgegeben werden soll
font: Berechnungen für den Font
height (int): Pixel
width (int): Pixel
"""
def text_per_row(txt):
pixs = 0
visible_text = ''
for a in txt:
pixs += char_width(display.selected_font[a]) + 1
if pixs > display.MATRIX_WIDTH:
# Zeilenende erreicht
break
visible_text += a
return visible_text
# Ganzzahl Division
max_visible_rows = display.MATRIX_HEIGHT // display.font_height
print(f'rows_visible: {max_visible_rows}')
text_left = text
scn_txt = []
for _ in range(max_visible_rows):
visible_text = text_per_row(text_left)
visible_text_len = len(visible_text)
text_left = text_left[visible_text_len:]
scn_txt.append(visible_text)
if not text_left:
break
scr_txt_dict = {'visible': scn_txt, 'invisible': text_left}
return scr_txt_dict

View File

@@ -1,10 +1,10 @@
from machine import Pin # type: ignore
from neopixel import NeoPixel # type: ignore
from .fonts import char_width as charwidth
from ..utils import char_width
from .fonts.font_5x7 import font_5x7
from ..utils.colors import GRAY, RAINBOW, BLACK, WHITE
from ..utils.utils import (
from ..utils.time_utils import (
get_german_timestamp_short,
get_datetime_string,
get_german_time_ticks,
@@ -97,14 +97,14 @@ class NeoPixel_64x64(NeoPixel):
if letter in self.selected_font:
char_data = self.selected_font[letter]
char_width = charwidth(char_data)
charwidth = char_width(char_data)
# background for the letter (full font size)
[
# print(xpos, ypos)
self.set_pixel(xpos, ypos, GRAY)
for xpos in range(
x, x + char_width
x, x + charwidth
) # 8 because full with of character representation
for ypos in range(y, y + self.font_height)
]
@@ -112,10 +112,10 @@ class NeoPixel_64x64(NeoPixel):
for row in range(self.font_height):
row_data = char_data[row]
for col in range(char_width):
for col in range(charwidth):
# Check if pixel should be lit (MSB first)
# Only check bits within the actual character width
if row_data & (1 << ((char_width - 1) - col)):
if row_data & (1 << ((charwidth - 1) - col)):
self.set_pixel(x + col, y + row, color)
else:
print(f'oops, letter does not exist in the font -> {letter}')
@@ -134,8 +134,8 @@ class NeoPixel_64x64(NeoPixel):
for char in text:
self.draw_letter(char, current_x, y, color)
# Move cursor by character width + 1 pixel spacing
char_width = charwidth(self.selected_font[char])
current_x += char_width + 1
charwidth = char_width(self.selected_font[char])
current_x += charwidth + 1
def show_hello(self):
"""Display HELLO with timestamp"""
@@ -209,10 +209,10 @@ class NeoPixel_64x64(NeoPixel):
# Draw text at floating position
for i, char in enumerate(text):
char_width = charwidth(self.selected_font[char])
char_x = current_x + (i * (char_width + 1))
charwidth = char_width(self.selected_font[char])
char_x = current_x + (i * (charwidth + 1))
# Keep text within matrix bounds
if 0 <= char_x < self.MATRIX_WIDTH - char_width:
if 0 <= char_x < self.MATRIX_WIDTH - charwidth:
self.draw_letter(char, char_x, y, color)
self.write()
@@ -235,7 +235,7 @@ class NeoPixel_64x64(NeoPixel):
# text_width_overall = len(text) * (self.font_width + 1)
### TODO: ACHTUNG: noch zu testen !!!
# Breite jedes Zeichens
text_width_overall = sum([charwidth(self.selected_font[char]) for char in text])
text_width_overall = sum([char_width(self.selected_font[char]) for char in text])
position = 0
@@ -244,10 +244,10 @@ class NeoPixel_64x64(NeoPixel):
# Draw text at current position
for i, char in enumerate(text):
char_width = charwidth(self.selected_font[char])
char_x = int(position + (i * (char_width + 1)))
charwidth = char_width(self.selected_font[char])
char_x = int(position + (i * (charwidth + 1)))
# Handle wrapping
if char_x < -char_width:
if char_x < -charwidth:
char_x += self.MATRIX_WIDTH + text_width_overall
if 0 <= char_x < self.MATRIX_WIDTH:
self.draw_letter(char, char_x, y, color)
@@ -318,6 +318,52 @@ class NeoPixel_64x64(NeoPixel):
self.draw_text(text, xpos, ypos, color) # Pixel setzen
self.write() # und anzeigen
def screen_text(self, text: str):
"""Text für einen Screen anpassen,
Anzahl der Zeichen je Zeile begrenzen,
ebenso wird die Höhe des Screen brücksichtigt
Args:
text (str): Text der ausgegeben werden soll
font: Berechnungen für den Font
height (int): Pixel
width (int): Pixel
"""
def text_per_row(txt):
pixs = 0
visible_text = ''
for a in txt:
pixs += char_width(self.selected_font[a]) + 1
if pixs > self.MATRIX_WIDTH:
# Zeilenende erreicht
break
visible_text += a
return visible_text
# Ganzzahl Division
max_visible_rows = self.MATRIX_HEIGHT // self.font_height
print(f'rows_visible: {max_visible_rows}')
text_left = text
scn_txt = []
for _ in range(max_visible_rows):
visible_text = text_per_row(text_left)
visible_text_len = len(visible_text)
text_left = text_left[visible_text_len:]
scn_txt.append(visible_text)
if not text_left:
break
scr_txt_dict = {'visible': scn_txt, 'invisible': text_left}
return scr_txt_dict
# Example usage
if __name__ == '__main__':