mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-10 13:02:59 +00:00
Double buffering and Force Clear
Attempting to clean up flicker - otherwise working
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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."""
|
||||
|
||||
@@ -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)
|
||||
self.display_manager.draw_text(display_text, force_clear=force_clear)
|
||||
Reference in New Issue
Block a user