Double buffering and Force Clear

Attempting to clean up flicker - otherwise working
This commit is contained in:
Chuck
2025-04-07 21:31:26 -05:00
parent a5f91040f6
commit d49f901ffc
4 changed files with 32 additions and 19 deletions

View File

@@ -67,7 +67,7 @@ class Clock:
current_time = datetime.now(self.timezone) current_time = datetime.now(self.timezone)
return current_time.strftime(self.clock_config.get('format', '%H:%M:%S')) return current_time.strftime(self.clock_config.get('format', '%H:%M:%S'))
def display_time(self) -> None: def display_time(self, force_clear: bool = False) -> None:
"""Display the current time.""" """Display the current time."""
current_time = self.get_current_time() current_time = self.get_current_time()
@@ -82,8 +82,7 @@ class Clock:
y = (self.display_manager.matrix.height - 24) // 2 y = (self.display_manager.matrix.height - 24) // 2
logger.debug("Drawing time at position (%d, %d)", x, y) logger.debug("Drawing time at position (%d, %d)", x, y)
self.display_manager.clear() self.display_manager.draw_text(current_time, x, y, force_clear=force_clear)
self.display_manager.draw_text(current_time, x, y)
if __name__ == "__main__": if __name__ == "__main__":
clock = Clock() clock = Clock()

View File

@@ -28,6 +28,9 @@ class DisplayController:
current_time = time.time() current_time = time.time()
rotation_interval = self.config['display'].get('rotation_interval', 15) rotation_interval = self.config['display'].get('rotation_interval', 15)
# Track if we're switching modes
switching_modes = False
# Switch display if interval has passed # Switch display if interval has passed
if current_time - self.last_switch > rotation_interval: if current_time - self.last_switch > rotation_interval:
logger.info("Switching display from %s to %s", logger.info("Switching display from %s to %s",
@@ -35,14 +38,15 @@ class DisplayController:
'weather' if self.current_display == 'clock' else 'clock') 'weather' if self.current_display == 'clock' else 'clock')
self.current_display = 'weather' if self.current_display == 'clock' else 'clock' self.current_display = 'weather' if self.current_display == 'clock' else 'clock'
self.last_switch = current_time self.last_switch = current_time
switching_modes = True
# Display current screen # Display current screen
if self.current_display == 'clock': if self.current_display == 'clock':
logger.debug("Updating clock display") logger.debug("Updating clock display")
self.clock.display_time() self.clock.display_time(force_clear=switching_modes)
else: else:
logger.debug("Updating weather display") logger.debug("Updating weather display")
self.weather.display_weather() self.weather.display_weather(force_clear=switching_modes)
# Sleep for 0.5 seconds since we only need to check for second changes # Sleep for 0.5 seconds since we only need to check for second changes
time.sleep(0.5) time.sleep(0.5)

View File

@@ -59,6 +59,9 @@ class DisplayManager:
logger.info("RGB matrix initialized successfully") logger.info("RGB matrix initialized successfully")
logger.info(f"Matrix dimensions: {self.matrix.width}x{self.matrix.height}") logger.info(f"Matrix dimensions: {self.matrix.width}x{self.matrix.height}")
# Create double buffer
self.offscreen_canvas = self.matrix.CreateFrameCanvas()
# Create image with full chain width # Create image with full chain width
self.image = Image.new('RGB', (self.matrix.width, self.matrix.height)) self.image = Image.new('RGB', (self.matrix.width, self.matrix.height))
self.draw = ImageDraw.Draw(self.image) self.draw = ImageDraw.Draw(self.image)
@@ -88,25 +91,32 @@ class DisplayManager:
# Draw some text # Draw some text
self.draw.text((10, 10), "TEST", font=self.font, fill=(0, 0, 255)) self.draw.text((10, 10), "TEST", font=self.font, fill=(0, 0, 255))
# Update the display # Update the display using double buffering
self.matrix.SetImage(self.image) self.update_display()
# Wait a moment # Wait a moment
time.sleep(2) time.sleep(2)
def _draw_text(self, text, x, y, font, color=(255, 255, 255)): def update_display(self):
"""Draw text on the canvas.""" """Update the display using double buffering."""
self.draw.text((x, y), text, font=font, fill=color) # Copy the current image to the offscreen canvas
self.matrix.SetImage(self.image) self.offscreen_canvas.SetImage(self.image)
# Swap the canvases
self.offscreen_canvas = self.matrix.SwapOnVSync(self.offscreen_canvas)
def clear(self): def clear(self):
"""Clear the display.""" """Clear the display."""
self.draw.rectangle((0, 0, self.matrix.width, self.matrix.height), fill=(0, 0, 0)) self.draw.rectangle((0, 0, self.matrix.width, self.matrix.height), fill=(0, 0, 0))
self.matrix.SetImage(self.image) self.update_display()
def draw_text(self, text: str, x: int = None, y: int = None, color: tuple = (255, 255, 255)): def draw_text(self, text: str, x: int = None, y: int = None, color: tuple = (255, 255, 255), force_clear: bool = False):
"""Draw text on the display with automatic centering.""" """Draw text on the display with automatic centering."""
self.clear() if force_clear:
self.clear()
else:
# Just create a new blank image without updating display
self.image = Image.new('RGB', (self.matrix.width, self.matrix.height))
self.draw = ImageDraw.Draw(self.image)
# Split text into lines if it contains newlines # Split text into lines if it contains newlines
lines = text.split('\n') lines = text.split('\n')
@@ -138,7 +148,7 @@ class DisplayManager:
current_y = y current_y = y
for i, line in enumerate(lines): for i, line in enumerate(lines):
if x is None: if x is None:
# Center this line horizontally across full width (128 pixels) # Center this line horizontally
line_x = (self.matrix.width - line_widths[i]) // 2 line_x = (self.matrix.width - line_widths[i]) // 2
else: else:
line_x = x line_x = x
@@ -155,8 +165,8 @@ class DisplayManager:
# Calculate next line position # Calculate next line position
current_y += line_heights[i] + padding current_y += line_heights[i] + padding
# Update the display with the new image # Update the display using double buffering
self.matrix.SetImage(self.image) self.update_display()
def cleanup(self): def cleanup(self):
"""Clean up resources.""" """Clean up resources."""

View File

@@ -39,7 +39,7 @@ class WeatherManager:
self._fetch_weather() self._fetch_weather()
return self.weather_data return self.weather_data
def display_weather(self) -> None: def display_weather(self, force_clear: bool = False) -> None:
"""Display weather information on the LED matrix.""" """Display weather information on the LED matrix."""
weather_data = self.get_weather() weather_data = self.get_weather()
if not weather_data: if not weather_data:
@@ -52,4 +52,4 @@ class WeatherManager:
display_text = f"{temp}°F\n{condition}" display_text = f"{temp}°F\n{condition}"
# Draw both lines at once using the multi-line support in draw_text # Draw both lines at once using the multi-line support in draw_text
self.display_manager.draw_text(display_text) self.display_manager.draw_text(display_text, force_clear=force_clear)