From d49f901ffcb618eeb887505840d29c2fc25c4369 Mon Sep 17 00:00:00 2001 From: Chuck <33324927+ChuckBuilds@users.noreply.github.com> Date: Mon, 7 Apr 2025 21:31:26 -0500 Subject: [PATCH] Double buffering and Force Clear Attempting to clean up flicker - otherwise working --- src/clock.py | 5 ++--- src/display_controller.py | 8 ++++++-- src/display_manager.py | 34 ++++++++++++++++++++++------------ src/weather_manager.py | 4 ++-- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/clock.py b/src/clock.py index 50a1a674..06d1530d 100644 --- a/src/clock.py +++ b/src/clock.py @@ -67,7 +67,7 @@ class Clock: current_time = datetime.now(self.timezone) 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.""" current_time = self.get_current_time() @@ -82,8 +82,7 @@ class Clock: y = (self.display_manager.matrix.height - 24) // 2 logger.debug("Drawing time at position (%d, %d)", x, y) - self.display_manager.clear() - self.display_manager.draw_text(current_time, x, y) + self.display_manager.draw_text(current_time, x, y, force_clear=force_clear) if __name__ == "__main__": clock = Clock() diff --git a/src/display_controller.py b/src/display_controller.py index 9df21332..4cc2fa31 100644 --- a/src/display_controller.py +++ b/src/display_controller.py @@ -28,6 +28,9 @@ class DisplayController: current_time = time.time() rotation_interval = self.config['display'].get('rotation_interval', 15) + # Track if we're switching modes + switching_modes = False + # Switch display if interval has passed if current_time - self.last_switch > rotation_interval: logger.info("Switching display from %s to %s", @@ -35,14 +38,15 @@ class DisplayController: 'weather' if self.current_display == 'clock' else 'clock') self.current_display = 'weather' if self.current_display == 'clock' else 'clock' self.last_switch = current_time + switching_modes = True # Display current screen if self.current_display == 'clock': logger.debug("Updating clock display") - self.clock.display_time() + self.clock.display_time(force_clear=switching_modes) else: 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 time.sleep(0.5) diff --git a/src/display_manager.py b/src/display_manager.py index 31a86fd3..24ae976c 100644 --- a/src/display_manager.py +++ b/src/display_manager.py @@ -59,6 +59,9 @@ class DisplayManager: logger.info("RGB matrix initialized successfully") 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 self.image = Image.new('RGB', (self.matrix.width, self.matrix.height)) self.draw = ImageDraw.Draw(self.image) @@ -88,25 +91,32 @@ class DisplayManager: # Draw some text self.draw.text((10, 10), "TEST", font=self.font, fill=(0, 0, 255)) - # Update the display - self.matrix.SetImage(self.image) + # Update the display using double buffering + self.update_display() # Wait a moment time.sleep(2) - def _draw_text(self, text, x, y, font, color=(255, 255, 255)): - """Draw text on the canvas.""" - self.draw.text((x, y), text, font=font, fill=color) - self.matrix.SetImage(self.image) + def update_display(self): + """Update the display using double buffering.""" + # Copy the current image to the offscreen canvas + self.offscreen_canvas.SetImage(self.image) + # Swap the canvases + self.offscreen_canvas = self.matrix.SwapOnVSync(self.offscreen_canvas) def clear(self): """Clear the display.""" 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.""" - 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 lines = text.split('\n') @@ -138,7 +148,7 @@ class DisplayManager: current_y = y for i, line in enumerate(lines): 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 else: line_x = x @@ -155,8 +165,8 @@ class DisplayManager: # Calculate next line position current_y += line_heights[i] + padding - # Update the display with the new image - self.matrix.SetImage(self.image) + # Update the display using double buffering + self.update_display() def cleanup(self): """Clean up resources.""" diff --git a/src/weather_manager.py b/src/weather_manager.py index e8ee696a..db0b3ea5 100644 --- a/src/weather_manager.py +++ b/src/weather_manager.py @@ -39,7 +39,7 @@ class WeatherManager: self._fetch_weather() 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.""" weather_data = self.get_weather() if not weather_data: @@ -52,4 +52,4 @@ class WeatherManager: display_text = f"{temp}°F\n{condition}" # Draw both lines at once using the multi-line support in draw_text - self.display_manager.draw_text(display_text) \ No newline at end of file + self.display_manager.draw_text(display_text, force_clear=force_clear) \ No newline at end of file