From a5f91040f6fa8a205680f2c7a5336313f3a51178 Mon Sep 17 00:00:00 2001 From: Chuck <33324927+ChuckBuilds@users.noreply.github.com> Date: Mon, 7 Apr 2025 21:25:58 -0500 Subject: [PATCH] Slow down redraw rate Slowing redraw to try and fix flicker --- src/clock.py | 23 ++++++++++++++--------- src/display_controller.py | 6 +++--- src/weather_manager.py | 23 ++++------------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/clock.py b/src/clock.py index 5011ee6f..50a1a674 100644 --- a/src/clock.py +++ b/src/clock.py @@ -21,6 +21,7 @@ class Clock: self.clock_config = self.config.get('clock', {}) # Use configured timezone if available, otherwise try to determine it self.timezone = self._get_timezone() + self.last_time = None def _get_timezone(self) -> pytz.timezone: """Get timezone based on location or config.""" @@ -69,16 +70,20 @@ class Clock: def display_time(self) -> None: """Display the current time.""" current_time = self.get_current_time() - logger.debug("Displaying time: %s", current_time) - # Center the text on the display - text_width = self.display_manager.font.getlength(current_time) - x = (self.display_manager.matrix.width - text_width) // 2 - 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) + # Only update if the time has changed + if current_time != self.last_time: + logger.debug("Time changed, updating display from %s to %s", self.last_time, current_time) + self.last_time = current_time + + # Center the text on the display + text_width = self.display_manager.font.getlength(current_time) + x = (self.display_manager.matrix.width - text_width) // 2 + 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) if __name__ == "__main__": clock = Clock() diff --git a/src/display_controller.py b/src/display_controller.py index 7c03e732..9df21332 100644 --- a/src/display_controller.py +++ b/src/display_controller.py @@ -26,7 +26,7 @@ class DisplayController: try: while True: current_time = time.time() - rotation_interval = self.config['display'].get('rotation_interval', 10) + rotation_interval = self.config['display'].get('rotation_interval', 15) # Switch display if interval has passed if current_time - self.last_switch > rotation_interval: @@ -44,8 +44,8 @@ class DisplayController: logger.debug("Updating weather display") self.weather.display_weather() - # Small delay to prevent CPU overload - time.sleep(0.1) + # Sleep for 0.5 seconds since we only need to check for second changes + time.sleep(0.5) except KeyboardInterrupt: print("\nDisplay stopped by user") diff --git a/src/weather_manager.py b/src/weather_manager.py index 9fa646df..e8ee696a 100644 --- a/src/weather_manager.py +++ b/src/weather_manager.py @@ -48,23 +48,8 @@ class WeatherManager: temp = round(weather_data['main']['temp']) condition = weather_data['weather'][0]['main'] - # Format the display string - display_text = self.weather_config.get('display_format', '{temp}°F\n{condition}') - display_text = display_text.format(temp=temp, condition=condition) - - # Split text into lines - lines = display_text.split('\n') + # Format the display string with both temp and condition + display_text = f"{temp}°F\n{condition}" - # Calculate vertical spacing - total_height = len(lines) * 24 # Assuming 24px font height - start_y = (self.display_manager.matrix.height - total_height) // 2 - - # Clear the display - self.display_manager.clear() - - # Draw each line centered - for i, line in enumerate(lines): - text_width = self.display_manager.font.getlength(line) - x = (self.display_manager.matrix.width - text_width) // 2 - y = start_y + (i * 24) - self.display_manager.draw_text(line, x, y) \ No newline at end of file + # 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