v.0.2.0 font_5x7_optimized

This commit is contained in:
tiijay
2025-10-21 17:31:59 +02:00
parent 99d9e753f0
commit ad0bf2eed1
5 changed files with 364 additions and 16 deletions

View File

@@ -0,0 +1,236 @@
# fonts.py - Optimized fonts for LED matrix with character width adjustment
# 5x7 Font (Optimized Width)
font_5x7 = {
# Uppercase letters (A-Z) - Optimized for better spacing
'A': [0x0E, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x11], # Width: 5
'B': [0x1E, 0x11, 0x11, 0x1E, 0x11, 0x11, 0x1E], # Width: 5
'C': [0x0E, 0x11, 0x10, 0x10, 0x10, 0x11, 0x0E], # Width: 5
'D': [0x1E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x1E], # Width: 5
'E': [0x1F, 0x10, 0x10, 0x1E, 0x10, 0x10, 0x1F], # Width: 5
'F': [0x1F, 0x10, 0x10, 0x1E, 0x10, 0x10, 0x10], # Width: 5
'G': [0x0E, 0x11, 0x10, 0x13, 0x11, 0x11, 0x0F], # Width: 5
'H': [0x11, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x11], # Width: 5
# 'I': [0x0E, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0E], # Width: 3
'I': [0x07, 0x02, 0x02, 0x02, 0x02, 0x02, 0x07], # Width: 3
'J': [0x07, 0x02, 0x02, 0x02, 0x02, 0x12, 0x0C], # Width: 4
'K': [0x11, 0x12, 0x14, 0x18, 0x14, 0x12, 0x11], # Width: 5
'L': [0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1F], # Width: 5
'M': [0x11, 0x1B, 0x15, 0x15, 0x11, 0x11, 0x11], # Width: 5
'N': [0x11, 0x19, 0x19, 0x15, 0x13, 0x13, 0x11], # Width: 5
'O': [0x0E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E], # Width: 5
'P': [0x1E, 0x11, 0x11, 0x1E, 0x10, 0x10, 0x10], # Width: 5
'Q': [0x0E, 0x11, 0x11, 0x11, 0x15, 0x12, 0x0D], # Width: 5
'R': [0x1E, 0x11, 0x11, 0x1E, 0x14, 0x12, 0x11], # Width: 5
'S': [0x0F, 0x10, 0x10, 0x0E, 0x01, 0x01, 0x1E], # Width: 5
'T': [0x1F, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04], # Width: 5
'U': [0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E], # Width: 5
'V': [0x11, 0x11, 0x11, 0x11, 0x11, 0x0A, 0x04], # Width: 5
'W': [0x11, 0x11, 0x11, 0x15, 0x15, 0x15, 0x0A], # Width: 5
'X': [0x11, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x11], # Width: 5
'Y': [0x11, 0x11, 0x0A, 0x04, 0x04, 0x04, 0x04], # Width: 5
'Z': [0x1F, 0x01, 0x02, 0x04, 0x08, 0x10, 0x1F], # Width: 5
# Lowercase letters (a-z) - Optimized for compact display
'a': [0x00, 0x00, 0x0E, 0x01, 0x0F, 0x11, 0x0F], # Width: 4
'b': [0x10, 0x10, 0x16, 0x19, 0x11, 0x11, 0x1E], # Width: 5
'c': [0x00, 0x00, 0x0E, 0x11, 0x10, 0x11, 0x0E], # Width: 4
'd': [0x01, 0x01, 0x0D, 0x13, 0x11, 0x11, 0x0F], # Width: 5
'e': [0x00, 0x00, 0x0E, 0x11, 0x1F, 0x10, 0x0E], # Width: 4
'f': [0x06, 0x09, 0x08, 0x1C, 0x08, 0x08, 0x08], # Width: 4
'g': [0x00, 0x0F, 0x11, 0x11, 0x0F, 0x01, 0x0E], # Width: 5
'h': [0x10, 0x10, 0x16, 0x19, 0x11, 0x11, 0x11], # Width: 5
# 'i': [0x00, 0x04, 0x00, 0x0C, 0x04, 0x04, 0x0E], # Width: 2
'i': [0x00, 0x02, 0x00, 0x06, 0x02, 0x02, 0x07], # Width: 2
'j': [0x00, 0x02, 0x00, 0x06, 0x02, 0x12, 0x0C], # Width: 3
'k': [0x10, 0x10, 0x12, 0x14, 0x18, 0x14, 0x12], # Width: 4
# 'l': [0x0C, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0E], # Width: 3
'l': [0x06, 0x02, 0x02, 0x02, 0x02, 0x02, 0x07], # Width: 3
'm': [0x00, 0x00, 0x1A, 0x15, 0x15, 0x15, 0x15], # Width: 5
'n': [0x00, 0x00, 0x16, 0x19, 0x11, 0x11, 0x11], # Width: 5
'o': [0x00, 0x00, 0x0E, 0x11, 0x11, 0x11, 0x0E], # Width: 4
'p': [0x00, 0x00, 0x1E, 0x11, 0x1E, 0x10, 0x10], # Width: 5
'q': [0x00, 0x00, 0x0D, 0x13, 0x0F, 0x01, 0x01], # Width: 5
'r': [0x00, 0x00, 0x16, 0x19, 0x10, 0x10, 0x10], # Width: 5
's': [0x00, 0x00, 0x0E, 0x10, 0x0E, 0x01, 0x1E], # Width: 4
't': [0x08, 0x08, 0x1C, 0x08, 0x08, 0x09, 0x06], # Width: 4
'u': [0x00, 0x00, 0x11, 0x11, 0x11, 0x13, 0x0D], # Width: 5
'v': [0x00, 0x00, 0x11, 0x11, 0x11, 0x0A, 0x04], # Width: 5
'w': [0x00, 0x00, 0x11, 0x11, 0x15, 0x15, 0x0A], # Width: 5
'x': [0x00, 0x00, 0x11, 0x0A, 0x04, 0x0A, 0x11], # Width: 5
'y': [0x00, 0x00, 0x11, 0x11, 0x0F, 0x01, 0x0E], # Width: 5
'z': [0x00, 0x00, 0x1F, 0x02, 0x04, 0x08, 0x1F], # Width: 5
# Numbers (0-9) - Optimized for consistent width
'0': [0x0E, 0x11, 0x13, 0x15, 0x19, 0x11, 0x0E], # Width: 5
# '1': [0x04, 0x0C, 0x04, 0x04, 0x04, 0x04, 0x0E], # Width: 3
'1': [0x02, 0x06, 0x02, 0x02, 0x02, 0x02, 0x07], # Width: 3
'2': [0x0E, 0x11, 0x01, 0x02, 0x04, 0x08, 0x1F], # Width: 5
'3': [0x1F, 0x02, 0x04, 0x02, 0x01, 0x11, 0x0E], # Width: 5
'4': [0x02, 0x06, 0x0A, 0x12, 0x1F, 0x02, 0x02], # Width: 5
'5': [0x1F, 0x10, 0x1E, 0x01, 0x01, 0x11, 0x0E], # Width: 5
'6': [0x06, 0x08, 0x10, 0x1E, 0x11, 0x11, 0x0E], # Width: 5
'7': [0x1F, 0x01, 0x02, 0x04, 0x08, 0x08, 0x08], # Width: 5
'8': [0x0E, 0x11, 0x11, 0x0E, 0x11, 0x11, 0x0E], # Width: 5
'9': [0x0E, 0x11, 0x11, 0x0F, 0x01, 0x02, 0x0C], # Width: 5
# Punctuation and symbols - Optimized widths
' ': [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], # Width: 2
# '!': [0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x04], # Width: 1
'!': [0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01], # Width: 1
'?': [0x0E, 0x11, 0x02, 0x04, 0x04, 0x00, 0x04], # Width: 5
'.': [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01], # Width: 1
',': [0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02], # Width: 1
':': [0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00], # Width: 1
';': [0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x02], # Width: 1
"'": [0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00], # Width: 1
'"': [0x05, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00], # Width: 3
'-': [0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00], # Width: 5
'_': [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F], # Width: 5
'+': [0x00, 0x04, 0x04, 0x1F, 0x04, 0x04, 0x00], # Width: 5
'=': [0x00, 0x00, 0x1F, 0x00, 0x1F, 0x00, 0x00], # Width: 5
'*': [0x00, 0x0A, 0x04, 0x1F, 0x04, 0x0A, 0x00], # Width: 5
'/': [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x00], # Width: 5
'\\': [0x00, 0x10, 0x08, 0x04, 0x02, 0x01, 0x00], # Width: 5
'(': [0x01, 0x02, 0x04, 0x04, 0x04, 0x02, 0x01], # Width: 3
')': [0x04, 0x02, 0x01, 0x01, 0x01, 0x02, 0x04], # Width: 3
'[': [0x07, 0x04, 0x04, 0x04, 0x04, 0x04, 0x07], # Width: 3
']': [0x07, 0x01, 0x01, 0x01, 0x01, 0x01, 0x07], # Width: 3
'{': [0x03, 0x02, 0x02, 0x04, 0x02, 0x02, 0x03], # Width: 3
'}': [0x06, 0x02, 0x02, 0x01, 0x02, 0x02, 0x06], # Width: 3
'<': [0x00, 0x01, 0x02, 0x04, 0x02, 0x01, 0x00], # Width: 4
'>': [0x00, 0x04, 0x02, 0x01, 0x02, 0x04, 0x00], # Width: 4
'@': [0x0E, 0x11, 0x17, 0x15, 0x17, 0x10, 0x0E], # Width: 5
'#': [0x0A, 0x0A, 0x1F, 0x0A, 0x1F, 0x0A, 0x0A], # Width: 5
'$': [0x04, 0x0F, 0x14, 0x0E, 0x05, 0x1E, 0x04], # Width: 5
'%': [0x18, 0x19, 0x02, 0x04, 0x08, 0x13, 0x03], # Width: 5
'&': [0x0C, 0x12, 0x14, 0x08, 0x15, 0x12, 0x0D], # Width: 5
'^': [0x04, 0x0A, 0x11, 0x00, 0x00, 0x00, 0x00], # Width: 5
'~': [0x00, 0x00, 0x00, 0x0D, 0x12, 0x00, 0x00], # Width: 5
# Special characters
'°': [0x07, 0x05, 0x07, 0x00, 0x00, 0x00, 0x00], # Width: 3 (Degree symbol)
'|': [0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01], # Width: 1 (Vertical bar)
'`': [0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00], # Width: 2 (Backtick)
'': [0x0F, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D], # Width: 5 (Paragraph symbol)
'': [0x00, 0x00, 0x02, 0x07, 0x02, 0x00, 0x00], # Width: 3 (Bullet point)
}
# Character width mapping for optimized spacing
char_width = {
# Wide characters (5 pixels)
'A': 5,
'B': 5,
'C': 5,
'D': 5,
'E': 5,
'F': 5,
'G': 5,
'H': 5,
'K': 5,
'L': 5,
'M': 5,
'N': 5,
'O': 5,
'P': 5,
'Q': 5,
'R': 5,
'S': 5,
'T': 5,
'U': 5,
'V': 5,
'W': 5,
'X': 5,
'Y': 5,
'Z': 5,
'b': 5,
'd': 5,
'g': 5,
'h': 5,
'm': 5,
'n': 5,
'p': 5,
'q': 5,
'u': 5,
'v': 5,
'w': 5,
'x': 5,
'y': 5,
'z': 5,
'0': 5,
'2': 5,
'3': 5,
'4': 5,
'5': 5,
'6': 5,
'7': 5,
'8': 5,
'9': 5,
'?': 5,
'-': 5,
'_': 5,
'+': 5,
'=': 5,
'*': 5,
'/': 5,
'\\': 5,
'@': 5,
'#': 5,
'$': 5,
'%': 5,
'&': 5,
'^': 5,
'~': 5,
'': 4,
# Medium characters (4 pixels)
'J': 5,
'a': 5,
'c': 5,
'e': 5,
'f': 5,
'k': 5,
'o': 5,
's': 5,
't': 5,
'<': 4,
'>': 4,
# Narrow characters (3 pixels)
'I': 3,
'1': 3,
'l': 3,
'j': 4,
'(': 3,
')': 3,
'[': 3,
']': 3,
'{': 3,
'}': 3,
'°': 3,
'': 3,
# Very narrow characters (2 pixels)
'i': 3,
' ': 1,
'`': 2,
# Single pixel characters
'!': 1,
'.': 1,
',': 2,
':': 1,
';': 2,
"'": 1,
'|': 1,
# Special cases
'"': 3,
}
def get_char_width(char):
"""Get the display width of a character for proper spacing"""
return char_width.get(char, 5) # Default to 5 if character not found
def get_text_width(text):
"""Calculate total width of text including character spacing"""
width = 0
for char in text:
if char in char_width:
width += char_width[char] + 1 # +1 for spacing between characters
else:
width += 6 # Default width + spacing
return max(0, width - 1) # Remove last spacing

View File

@@ -1,5 +1,6 @@
from .font_3x5 import font_3x5
from .font_5x7 import font_5x7
from .font_5x7_opt import font_5x7 as font_5x7_opt
from .font_8x8 import font_8x8
from .font_16x16 import font_16x16
from ..emoji.emoji_5x7 import emoji_5x7
@@ -19,6 +20,8 @@ def fonts_meta(font):
return fonts_meta_[3]
elif font == font_5x7:
return fonts_meta_[5]
elif font == font_5x7_opt:
return fonts_meta_[5]
elif font == font_8x8:
return fonts_meta_[8]
elif font == font_16x16:

View File

@@ -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__':