v.0.8.3 display clear_cox

This commit is contained in:
tiijay
2025-11-19 10:18:43 +00:00
parent 2be6d888b3
commit 72cd1bbe1b
2 changed files with 344 additions and 314 deletions

View File

@@ -1,10 +1,10 @@
from machine import Pin # type: ignore from machine import Pin # type: ignore
from neopixel import NeoPixel # type: ignore from neopixel import NeoPixel # type: ignore
from ..utils import char_width from app.utils import char_width
from .fonts.font_5x7 import font_5x7 from app.display import fonts
from ..utils.colors import GRAY, RAINBOW, BLACK, WHITE from app.utils import colors
from ..utils.time_utils import ( from app.utils.time_utils import (
get_german_timestamp_short, get_german_timestamp_short,
get_datetime_string, get_datetime_string,
get_german_time_ticks, get_german_time_ticks,
@@ -30,7 +30,7 @@ class NeoPixel_64x64(NeoPixel):
super().__init__(Pin(pin), self.MATRIX_WIDTH * self.MATRIX_HEIGHT) super().__init__(Pin(pin), self.MATRIX_WIDTH * self.MATRIX_HEIGHT)
# Font configuration # Font configuration
self.set_font(font_5x7) self.set_font(fonts.font_5x7)
def set_font(self, font): def set_font(self, font):
""" """
@@ -62,7 +62,30 @@ class NeoPixel_64x64(NeoPixel):
def clear(self): def clear(self):
"""Clear the entire display (turn off all pixels)""" """Clear the entire display (turn off all pixels)"""
for i in range(len(self)): for i in range(len(self)):
self[i] = BLACK self[i] = colors.BLACK
self.write()
def clear_box(
self,
from_row: int,
from_col: int,
to_row: int,
to_col: int,
color: tuple[3] = colors.BLACK,
) -> None:
"""löscht einen Bereich (Box) im Display
Args:
from_row (int): Start-Zeile
from_col (int): Start-Spalte
to_row (int): End-Zeile
to_col (int): End-Spalte
"""
for row in range(from_row, to_row):
for col in range(from_col, to_col + 1):
idx: int = row * self.MATRIX_WIDTH + col
self[idx] = color
self.write() self.write()
def clear_row(self, row: int, effect: bool = False) -> None: def clear_row(self, row: int, effect: bool = False) -> None:
@@ -75,9 +98,9 @@ class NeoPixel_64x64(NeoPixel):
start = row * self.MATRIX_WIDTH start = row * self.MATRIX_WIDTH
ende = start + self.font_height * self.MATRIX_WIDTH - 1 ende = start + self.font_height * self.MATRIX_WIDTH - 1
print(f'clear row: {row} --> pixels {start} to {ende}') print(f"clear row: {row} --> pixels {start} to {ende}")
for i in range(start, ende): for i in range(start, ende):
self[i] = BLACK self[i] = colors.BLACK
if effect and i % self.MATRIX_WIDTH == 0: if effect and i % self.MATRIX_WIDTH == 0:
self.write() self.write()
@@ -101,7 +124,7 @@ class NeoPixel_64x64(NeoPixel):
# background for the letter (full font size) # background for the letter (full font size)
[ [
# print(xpos, ypos) # print(xpos, ypos)
self.set_pixel(xpos, ypos, GRAY) self.set_pixel(xpos, ypos, colors.GRAY)
for xpos in range( for xpos in range(
x, x + charwidth x, x + charwidth
) # 8 because full with of character representation ) # 8 because full with of character representation
@@ -117,7 +140,7 @@ class NeoPixel_64x64(NeoPixel):
if row_data & (1 << ((charwidth - 1) - col)): if row_data & (1 << ((charwidth - 1) - col)):
self.set_pixel(x + col, y + row, color) self.set_pixel(x + col, y + row, color)
else: else:
print(f'oops, letter does not exist in the font -> {letter}') print(f"oops, letter does not exist in the font -> {letter}")
def draw_text(self, text, x, y, color): def draw_text(self, text, x, y, color):
""" """
@@ -141,16 +164,16 @@ class NeoPixel_64x64(NeoPixel):
self.clear() self.clear()
# Draw HELLO in rainbow colors # Draw HELLO in rainbow colors
self.draw_text('HELLO!', 6, 4, RAINBOW[2]) self.draw_text("HELLO!", 6, 4, colors.RAINBOW[2])
# Show timestamp # Show timestamp
datetimestr = get_german_timestamp_short() datetimestr = get_german_timestamp_short()
self.draw_text(datetimestr, 2, 15, RAINBOW[4]) self.draw_text(datetimestr, 2, 15, colors.RAINBOW[4])
self.write() self.write()
def vertical_floating_text( def vertical_floating_text(
self, text, x, color=RAINBOW[0], float_range=3, speed=0.2, duration=10 self, text, x, color=colors.RAINBOW[0], float_range=3, speed=0.2, duration=10
): ):
""" """
Vertical floating text animation Vertical floating text animation
@@ -168,7 +191,9 @@ class NeoPixel_64x64(NeoPixel):
while time.time() - start_time < duration: while time.time() - start_time < duration:
# Calculate floating offset using sine wave # Calculate floating offset using sine wave
offset = math.sin(time.time() * speed) * float_range offset = math.sin(time.time() * speed) * float_range
current_y = int(self.MATRIX_HEIGHT // 2 + offset - (len(text) * self.font_height) // 2) current_y = int(
self.MATRIX_HEIGHT // 2 + offset - (len(text) * self.font_height) // 2
)
self.clear() self.clear()
@@ -183,7 +208,7 @@ class NeoPixel_64x64(NeoPixel):
time.sleep(0.05) time.sleep(0.05)
def horizontal_floating_text( def horizontal_floating_text(
self, text, y, color=RAINBOW[0], float_range=3, speed=0.2, duration=10 self, text, y, color=colors.RAINBOW[0], float_range=3, speed=0.2, duration=10
): ):
""" """
Horizontal floating text animation Horizontal floating text animation
@@ -218,7 +243,9 @@ class NeoPixel_64x64(NeoPixel):
counter += speed counter += speed
time.sleep(0.05) time.sleep(0.05)
def rotate_text_left_continuous(self, text, y, color=RAINBOW[0], speed=1.0, duration=10): def rotate_text_left_continuous(
self, text, y, color=colors.RAINBOW[0], speed=1.0, duration=10
):
""" """
Continuous left rotation - text wraps around seamlessly Continuous left rotation - text wraps around seamlessly
@@ -234,7 +261,9 @@ class NeoPixel_64x64(NeoPixel):
# text_width_overall = len(text) * (self.font_width + 1) # text_width_overall = len(text) * (self.font_width + 1)
### TODO: ACHTUNG: noch zu testen !!! ### TODO: ACHTUNG: noch zu testen !!!
# Breite jedes Zeichens # Breite jedes Zeichens
text_width_overall = sum([char_width(self.selected_font[char]) for char in text]) text_width_overall = sum(
[char_width(self.selected_font[char]) for char in text]
)
position = 0 position = 0
@@ -313,7 +342,7 @@ class NeoPixel_64x64(NeoPixel):
err += dx err += dx
y1 += sy y1 += sy
def write_text(self, text: str, xpos: int, ypos: int, color=WHITE) -> None: def write_text(self, text: str, xpos: int, ypos: int, color=colors.WHITE) -> None:
self.draw_text(text, xpos, ypos, color) # Pixel setzen self.draw_text(text, xpos, ypos, color) # Pixel setzen
self.write() # und anzeigen self.write() # und anzeigen
@@ -332,7 +361,7 @@ class NeoPixel_64x64(NeoPixel):
def text_per_row(txt): def text_per_row(txt):
pixs = 0 pixs = 0
visible_text = '' visible_text = ""
for a in txt: for a in txt:
pixs += char_width(self.selected_font[a]) + 1 pixs += char_width(self.selected_font[a]) + 1
if pixs > self.MATRIX_WIDTH: if pixs > self.MATRIX_WIDTH:
@@ -344,7 +373,7 @@ class NeoPixel_64x64(NeoPixel):
# Ganzzahl Division # Ganzzahl Division
max_visible_rows = self.MATRIX_HEIGHT // self.font_height max_visible_rows = self.MATRIX_HEIGHT // self.font_height
print(f'rows_visible: {max_visible_rows}') print(f"rows_visible: {max_visible_rows}")
text_left = text text_left = text
scn_txt = [] scn_txt = []
@@ -358,33 +387,32 @@ class NeoPixel_64x64(NeoPixel):
if not text_left: if not text_left:
break break
scr_txt_dict = {'visible': scn_txt, 'invisible': text_left} scr_txt_dict = {"visible": scn_txt, "invisible": text_left}
return scr_txt_dict return scr_txt_dict
# Example usage # Example usage
if __name__ == '__main__': if __name__ == "__main__":
# Create display instance # Create display instance
display = NeoPixel_64x64() display = NeoPixel_64x64()
print('LED Matrix Display Initialized') print("LED Matrix Display Initialized")
print(get_datetime_string()) print(get_datetime_string())
print(f'German time: {get_german_time_ticks()}') print(f"German time: {get_german_time_ticks()}")
print(f'German date: {get_german_date_ticks()}') print(f"German date: {get_german_date_ticks()}")
# Demo various functions # Demo various functions
display.show_hello() display.show_hello()
time.sleep(3) time.sleep(3)
display.vertical_floating_text('HELLO', 30, RAINBOW[0], 5, 0.15, 5) display.vertical_floating_text("HELLO", 30, colors.RAINBOW[0], 5, 0.15, 5)
display.horizontal_floating_text('FLOAT', 28, RAINBOW[1], 10, 0.1, 5) display.horizontal_floating_text("FLOAT", 28, colors.RAINBOW[1], 10, 0.1, 5)
display.rotate_text_left_continuous('ROTATE FLOAT', 28, speed=8.0, duration=5) display.rotate_text_left_continuous("ROTATE FLOAT", 28, speed=8.0, duration=5)
# Draw some shapes # Draw some shapes
display.clear() display.clear()
display.draw_rectangle(5, 5, 10, 10, RAINBOW[0]) display.draw_rectangle(5, 5, 10, 10, colors.RAINBOW[0])
display.draw_rectangle(20, 20, 15, 8, RAINBOW[2], fill=True) display.draw_rectangle(20, 20, 15, 8, colors.RAINBOW[2], fill=True)
display.draw_line(0, 0, 63, 63, RAINBOW[4]) display.draw_line(0, 0, 63, 63, colors.RAINBOW[4])
display.write() display.write()

View File

@@ -35,10 +35,12 @@ async def print_time_task() -> None:
bottom_ypos = display.MATRIX_HEIGHT - display.font_height bottom_ypos = display.MATRIX_HEIGHT - display.font_height
time_str: str = get_datetime_string("time") time_str: str = get_datetime_string("time")
display.clear_row(bottom_ypos) display.clear_box(bottom_ypos, 0, bottom_ypos + display.font_height, 38)
# display.clear_row(bottom_ypos)
display.write_text(time_str[:8], 0, bottom_ypos, color=colors.NEON_YELLOW) display.write_text(time_str[:8], 0, bottom_ypos, color=colors.NEON_YELLOW)
await asyncio.sleep(10) await asyncio.sleep(5)
async def sync_ntp_time_task() -> None: async def sync_ntp_time_task() -> None: