v.0.3.1 font 3x5 pretty

This commit is contained in:
tiijay
2025-10-23 19:06:30 +02:00
parent a66b00989f
commit 9fcc7712e6
7 changed files with 334 additions and 592 deletions

View File

@@ -1,9 +1,8 @@
from machine import Pin, RTC
from neopixel import NeoPixel
from .fonts.fonts_utils import fonts_meta, char_width as charwidth
from .fonts.fonts_utils import char_width as charwidth
from .fonts.font_5x7 import font_5x7
from .fonts.font_5x7_opt import font_5x7 as font_5x7_opt, get_char_width
from ..utils.colors import GRAY, RAINBOW, BLACK, WHITE, LIME
from ..utils.utils import (
get_german_timestamp_short,
@@ -32,7 +31,7 @@ class NeoPixel_64x64(NeoPixel):
super().__init__(Pin(pin), self.MATRIX_WIDTH * self.MATRIX_HEIGHT)
# Font configuration
self.set_font(font_5x7_opt)
self.set_font(font_5x7)
def set_font(self, font):
"""
@@ -43,10 +42,10 @@ class NeoPixel_64x64(NeoPixel):
"""
self.selected_font = font
self.selected_font_meta = fonts_meta(font)
self.font_width = self.selected_font_meta['w']
self.font_height = self.selected_font_meta['h']
print(f'Font set: width: {self.font_width}; height: {self.font_height}')
# Höhe des Fonts, Anzahl der Zeilen für jedes Zeichen
# wir holen den Wert aus "A"
self.font_height = len(self.selected_font['A'])
print(f'Font set: width: per char; height: {self.font_height}')
def set_pixel(self, x, y, color):
"""
@@ -92,27 +91,6 @@ class NeoPixel_64x64(NeoPixel):
self.write()
def draw_letter___obsolet(self, letter, x, y, color):
"""
Draw a single letter using current font
Args:
letter: Character to draw
x: X position
y: Y position
color: RGB color tuple
"""
if letter in self.selected_font:
char_data = self.selected_font[letter]
for row in range(self.font_height):
row_data = char_data[row]
for col in range(self.font_width):
# Check if pixel should be lit (MSB first)
if row_data & (1 << ((self.font_width - 1) - col)):
self.set_pixel(x + col, y + row, color)
else:
print(f'oops, letter does not exist in the font -> {letter}')
def draw_letter(self, letter, x, y, color):
"""
Draw a single letter using current font with optimized width
@@ -126,7 +104,6 @@ class NeoPixel_64x64(NeoPixel):
if letter in self.selected_font:
char_data = self.selected_font[letter]
# char_width = get_char_width(letter, default_witdth=16)
char_width = charwidth(char_data)
# background for the letter (full font size)
@@ -150,21 +127,6 @@ class NeoPixel_64x64(NeoPixel):
else:
print(f'oops, letter does not exist in the font -> {letter}')
def draw_text___obsolete(self, text, x, y, color=RAINBOW[2], spacing=1):
"""
Draw text string at specified position
Args:
text: String to display
x: Starting X position
y: Starting Y position
color: RGB color tuple
spacing: Pixels between characters
"""
for idx, char in enumerate(text):
char_x = x + (idx * (self.font_width + spacing))
self.draw_letter(char, char_x, y, color)
def draw_text(self, text, x, y, color):
"""
Draw text with optimized character spacing
@@ -179,7 +141,7 @@ 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 = get_char_width(char)
char_width = charwidth(self.selected_font[char])
current_x += char_width + 1
def show_hello(self):
@@ -243,7 +205,6 @@ class NeoPixel_64x64(NeoPixel):
duration: How long to run animation in seconds
"""
start_time = time.time()
text_width = len(text) * (self.font_width + 1)
counter = 0
while time.time() - start_time < duration:
@@ -255,9 +216,10 @@ class NeoPixel_64x64(NeoPixel):
# Draw text at floating position
for i, char in enumerate(text):
char_x = current_x + (i * (self.font_width + 1))
char_width = charwidth(self.selected_font[char])
char_x = current_x + (i * (char_width + 1))
# Keep text within matrix bounds
if 0 <= char_x < self.MATRIX_WIDTH - self.font_width:
if 0 <= char_x < self.MATRIX_WIDTH - char_width:
self.draw_letter(char, char_x, y, color)
self.write()
@@ -276,7 +238,12 @@ class NeoPixel_64x64(NeoPixel):
duration: How long to run animation in seconds
"""
start_time = time.time()
text_width = len(text) * (self.font_width + 1)
# hier müssen wir die Länge/Breite in Pixel des gesamten Textes berechnen
# 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])
position = 0
while time.time() - start_time < duration:
@@ -284,10 +251,11 @@ class NeoPixel_64x64(NeoPixel):
# Draw text at current position
for i, char in enumerate(text):
char_x = int(position + (i * (self.font_width + 1)))
char_width = charwidth(self.selected_font[char])
char_x = int(position + (i * (char_width + 1)))
# Handle wrapping
if char_x < -self.font_width:
char_x += self.MATRIX_WIDTH + text_width
if char_x < -char_width:
char_x += self.MATRIX_WIDTH + text_width_overall
if 0 <= char_x < self.MATRIX_WIDTH:
self.draw_letter(char, char_x, y, color)
@@ -295,7 +263,7 @@ class NeoPixel_64x64(NeoPixel):
# Move left and wrap around
position -= speed
if position < -text_width:
if position < -text_width_overall:
position = self.MATRIX_WIDTH
time.sleep(0.05)