Optimize stock news scrolling for better performance: - Use pre-rendered text image for efficient scrolling - Implement cropping and pasting for smoother animation - Remove unnecessary display operations and delays

This commit is contained in:
ChuckBuilds
2025-04-11 10:34:26 -05:00
parent 73f836a77a
commit a7a341b479
2 changed files with 14 additions and 20 deletions

View File

@@ -205,33 +205,30 @@ class StockNewsManager:
separator = " - " # Visual separator between news items
news_text = separator.join(news_texts)
# 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]
# Create a text image for efficient scrolling
text_image = self._create_text_image(news_text)
text_width = text_image.width
text_height = text_image.height
# Calculate display position
display_width = self.display_manager.matrix.width
total_width = text_width + display_width
# Update scroll position
self.scroll_position = (self.scroll_position + 1) % total_width
self.scroll_position = (self.scroll_position + self.scroll_speed) % total_width
# Clear the display
self.display_manager.clear()
# 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
)
# Calculate the visible portion of the text
visible_width = min(display_width, text_width - self.scroll_position)
if visible_width > 0:
# Crop the text image to show only the visible portion
visible_portion = text_image.crop((self.scroll_position, 0,
self.scroll_position + visible_width, text_height))
# Paste the visible portion onto the display
self.display_manager.image.paste(visible_portion, (0, 0))
# Update the display
self.display_manager.update_display()

View File

@@ -46,9 +46,6 @@ def main():
if current_time - last_update >= 0.001: # 1ms minimum between updates
news_manager.display_news()
last_update = current_time
else:
# Small sleep to prevent CPU hogging
time.sleep(0.0001)
except KeyboardInterrupt:
print("\nTest interrupted by user")