v.0.5.0 cleanup code, utils ...
This commit is contained in:
@@ -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__':
|
||||
|
||||
Reference in New Issue
Block a user