v.0.2.0 font_5x7_optimized
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
from machine import Pin, RTC
|
||||
from neopixel import NeoPixel
|
||||
from .fonts.fonts_utils import fonts_meta
|
||||
|
||||
from .fonts.font_5x7 import font_5x7
|
||||
from ..utils.colors import RAINBOW, BLACK
|
||||
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,
|
||||
get_datetime_string,
|
||||
get_german_time_ticks,
|
||||
get_german_date_ticks,
|
||||
number_to_bitarray_msb,
|
||||
)
|
||||
import time
|
||||
import math
|
||||
@@ -29,7 +32,7 @@ class NeoPixel_64x64(NeoPixel):
|
||||
super().__init__(Pin(pin), self.MATRIX_WIDTH * self.MATRIX_HEIGHT)
|
||||
|
||||
# Font configuration
|
||||
self.set_font(font_5x7)
|
||||
self.set_font(font_5x7_opt)
|
||||
|
||||
def set_font(self, font):
|
||||
"""
|
||||
@@ -89,7 +92,7 @@ class NeoPixel_64x64(NeoPixel):
|
||||
|
||||
self.write()
|
||||
|
||||
def draw_letter(self, letter, x, y, color):
|
||||
def draw_letter___obsolet(self, letter, x, y, color):
|
||||
"""
|
||||
Draw a single letter using current font
|
||||
|
||||
@@ -110,7 +113,42 @@ class NeoPixel_64x64(NeoPixel):
|
||||
else:
|
||||
print(f'oops, letter does not exist in the font -> {letter}')
|
||||
|
||||
def draw_text(self, text, x, y, color=RAINBOW[2], spacing=1):
|
||||
def draw_letter(self, letter, x, y, color):
|
||||
"""
|
||||
Draw a single letter using current font with optimized width
|
||||
|
||||
Args:
|
||||
letter: Character to draw
|
||||
x: X position
|
||||
y: Y position
|
||||
color: RGB color tuple
|
||||
"""
|
||||
|
||||
# background for the letter (full font size)
|
||||
[
|
||||
# print(xpos, ypos)
|
||||
self.set_pixel(xpos, ypos, GRAY)
|
||||
for xpos in range(x, x + 8) # 8 because full with of character representation
|
||||
for ypos in range(y, y + self.font_height)
|
||||
]
|
||||
|
||||
if letter in self.selected_font:
|
||||
char_data = self.selected_font[letter]
|
||||
char_width = get_char_width(letter)
|
||||
|
||||
print(letter)
|
||||
for row in range(self.font_height):
|
||||
row_data = char_data[row]
|
||||
print(number_to_bitarray_msb(row_data))
|
||||
for col in range(char_width):
|
||||
# Check if pixel should be lit (MSB first)
|
||||
# Only check bits within the actual character width
|
||||
if row_data & (1 << ((char_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_text___obsolete(self, text, x, y, color=RAINBOW[2], spacing=1):
|
||||
"""
|
||||
Draw text string at specified position
|
||||
|
||||
@@ -125,6 +163,23 @@ class NeoPixel_64x64(NeoPixel):
|
||||
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
|
||||
|
||||
Args:
|
||||
text: Text to draw
|
||||
x: Starting X position
|
||||
y: Starting Y position
|
||||
color: RGB color tuple
|
||||
"""
|
||||
current_x = x
|
||||
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)
|
||||
current_x += char_width + 1
|
||||
|
||||
def show_hello(self):
|
||||
"""Display HELLO with timestamp"""
|
||||
self.clear()
|
||||
@@ -296,6 +351,13 @@ class NeoPixel_64x64(NeoPixel):
|
||||
err += dx
|
||||
y1 += sy
|
||||
|
||||
def write_text(self, text: str, xpos: int, line_no: int, color=WHITE) -> None:
|
||||
"""Textausgabe in Zeile line_no"""
|
||||
self.draw_text(
|
||||
text, xpos * (self.font_width + 1), line_no * (self.font_height + 1), color
|
||||
) # Pixel setzen
|
||||
self.write() # und anzeigen
|
||||
|
||||
|
||||
# Example usage
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user