Fix news ticker performance with simplified scrolling mechanism

This commit is contained in:
ChuckBuilds
2025-04-11 10:08:19 -05:00
parent 702c6d2c3e
commit 45d9f1eb63
2 changed files with 22 additions and 36 deletions

View File

@@ -94,14 +94,11 @@ class DisplayManager:
# Copy the current image to the offscreen canvas
self.offscreen_canvas.SetImage(self.image)
# Wait for the next vsync before swapping
self.matrix.SwapOnVSync(self.offscreen_canvas)
# Swap buffers immediately
self.matrix.SwapOnVSync(self.offscreen_canvas, False)
# Swap our canvas references
self.offscreen_canvas, self.current_canvas = self.current_canvas, self.offscreen_canvas
# Small delay to ensure stable refresh
time.sleep(0.001)
except Exception as e:
logger.error(f"Error updating display: {e}")

View File

@@ -205,40 +205,33 @@ class StockNewsManager:
separator = " - " # Visual separator between news items
news_text = separator.join(news_texts)
# Pre-render the text image for efficient scrolling
text_image = self._create_text_image(news_text)
text_width = text_image.width
display_width = self.display_manager.matrix.width
# Get text dimensions
bbox = self.display_manager.draw.textbbox((0, 0), news_text, font=self.display_manager.small_font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
# Calculate total width for scrolling
# Calculate display position
display_width = self.display_manager.matrix.width
total_width = text_width + display_width
# Update scroll position with smooth acceleration
scroll_speed = min(int(self.scroll_speed * 1.1), 3) # Convert to integer
self.scroll_position = int(self.scroll_position + scroll_speed) % total_width
# Update scroll position
self.scroll_position = (self.scroll_position + 1) % total_width
# Clear the display
self.display_manager.clear()
# Calculate source and destination regions for efficient blitting
if self.scroll_position < display_width:
# Text is entering from the right
src_x = int(text_width - (display_width - self.scroll_position))
src_width = int(display_width - self.scroll_position)
dst_x = int(self.scroll_position)
self.display_manager.image.paste(
text_image.crop((src_x, 0, src_x + src_width, text_image.height)),
(dst_x, 0)
)
else:
# Text is scrolling off the left
src_x = 0
src_width = text_width
dst_x = int(self.scroll_position - display_width)
self.display_manager.image.paste(
text_image.crop((src_x, 0, src_x + src_width, text_image.height)),
(dst_x, 0)
)
# Draw text at current scroll position
x_pos = display_width - self.scroll_position
y_pos = (self.display_manager.matrix.height - text_height) // 2
# Draw the text
self.display_manager.draw_text(
news_text,
x=x_pos,
y=y_pos,
color=(255, 255, 255),
small_font=True
)
# Update the display
self.display_manager.update_display()
@@ -246,10 +239,6 @@ class StockNewsManager:
# If we've completed a full scroll, move to the next group
if self.scroll_position == 0:
self.current_news_group = (self.current_news_group + 1) % ((total_headlines + headlines_per_rotation - 1) // headlines_per_rotation)
self.scroll_speed = 1 # Reset speed for next group
# Minimal delay to control scroll speed while maintaining smoothness
time.sleep(0.001)
# Log frame rate
self._log_frame_rate()