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 separator = " - " # Visual separator between news items
news_text = separator.join(news_texts) news_text = separator.join(news_texts)
# Get text dimensions # Create a text image for efficient scrolling
bbox = self.display_manager.draw.textbbox((0, 0), news_text, font=self.display_manager.small_font) text_image = self._create_text_image(news_text)
text_width = bbox[2] - bbox[0] text_width = text_image.width
text_height = bbox[3] - bbox[1] text_height = text_image.height
# Calculate display position # Calculate display position
display_width = self.display_manager.matrix.width display_width = self.display_manager.matrix.width
total_width = text_width + display_width total_width = text_width + display_width
# Update scroll position # 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 # Clear the display
self.display_manager.clear() self.display_manager.clear()
# Draw text at current scroll position # Calculate the visible portion of the text
x_pos = display_width - self.scroll_position visible_width = min(display_width, text_width - self.scroll_position)
y_pos = (self.display_manager.matrix.height - text_height) // 2 if visible_width > 0:
# Crop the text image to show only the visible portion
# Draw the text visible_portion = text_image.crop((self.scroll_position, 0,
self.display_manager.draw_text( self.scroll_position + visible_width, text_height))
news_text,
x=x_pos, # Paste the visible portion onto the display
y=y_pos, self.display_manager.image.paste(visible_portion, (0, 0))
color=(255, 255, 255),
small_font=True
)
# Update the display # Update the display
self.display_manager.update_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 if current_time - last_update >= 0.001: # 1ms minimum between updates
news_manager.display_news() news_manager.display_news()
last_update = current_time last_update = current_time
else:
# Small sleep to prevent CPU hogging
time.sleep(0.0001)
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nTest interrupted by user") print("\nTest interrupted by user")