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 # Copy the current image to the offscreen canvas
self.offscreen_canvas.SetImage(self.image) self.offscreen_canvas.SetImage(self.image)
# Wait for the next vsync before swapping # Swap buffers immediately
self.matrix.SwapOnVSync(self.offscreen_canvas) self.matrix.SwapOnVSync(self.offscreen_canvas, False)
# Swap our canvas references # Swap our canvas references
self.offscreen_canvas, self.current_canvas = self.current_canvas, self.offscreen_canvas 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: except Exception as e:
logger.error(f"Error updating display: {e}") logger.error(f"Error updating display: {e}")

View File

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