Flicker reduction

flicker improvements
This commit is contained in:
Chuck
2025-04-08 18:18:06 -05:00
parent a52886a421
commit 448860d77d
2 changed files with 70 additions and 34 deletions

View File

@@ -20,9 +20,10 @@ class DisplayController:
self.current_display = 'clock' self.current_display = 'clock'
self.last_switch = time.time() self.last_switch = time.time()
self.scroll_position = 0 self.scroll_position = 0
self.scroll_speed = 2 # Pixels per update self.scroll_speed = 1 # Reduced scroll speed
self.last_scroll = time.time() self.last_scroll = time.time()
self.scroll_interval = 0.05 # 50ms between scroll updates self.scroll_interval = 0.1 # Increased scroll interval for smoother scrolling
self.force_clear = False
logger.info("DisplayController initialized with display_manager: %s", id(self.display_manager)) logger.info("DisplayController initialized with display_manager: %s", id(self.display_manager))
def run(self): def run(self):
@@ -46,37 +47,36 @@ class DisplayController:
logger.info("Switching display to: %s", self.current_display) logger.info("Switching display to: %s", self.current_display)
self.last_switch = current_time self.last_switch = current_time
self.display_manager.clear() # Clear display when switching modes self.force_clear = True # Set force clear flag instead of clearing immediately
force_clear = True
else:
force_clear = False
# Update scroll position for hourly forecast if needed # Update scroll position for hourly forecast if needed
if self.current_display == 'hourly': if self.current_display == 'hourly' and current_time - self.last_scroll > self.scroll_interval:
if current_time - self.last_scroll > self.scroll_interval: self.scroll_position += self.scroll_speed
self.scroll_position += self.scroll_speed self.last_scroll = current_time
self.last_scroll = current_time
# Reset scroll position if we've gone through all forecasts # Reset scroll position if we've gone through all forecasts
if self.scroll_position > self.display_manager.matrix.width * 3: if self.scroll_position > self.display_manager.matrix.width * 3:
self.scroll_position = 0 self.scroll_position = 0
force_clear = True # Clear when resetting scroll self.force_clear = True
# Display current screen # Display current screen
if self.current_display == 'clock': if self.current_display == 'clock':
self.clock.display_time(force_clear=force_clear) self.clock.display_time(force_clear=self.force_clear)
elif self.current_display == 'weather': elif self.current_display == 'weather':
self.weather.display_weather(force_clear=force_clear) self.weather.display_weather(force_clear=self.force_clear)
elif self.current_display == 'hourly': elif self.current_display == 'hourly':
self.weather.display_hourly_forecast(self.scroll_position, force_clear=force_clear) self.weather.display_hourly_forecast(self.scroll_position, force_clear=self.force_clear)
else: # daily else: # daily
self.weather.display_daily_forecast(force_clear=force_clear) self.weather.display_daily_forecast(force_clear=self.force_clear)
# Reset force clear flag after use
self.force_clear = False
# Sleep longer when not scrolling # Sleep longer when not scrolling
if self.current_display == 'hourly': if self.current_display == 'hourly':
time.sleep(0.05) # 50ms for smooth scrolling time.sleep(self.scroll_interval) # Use scroll interval for consistent timing
else: else:
time.sleep(0.1) # 100ms for static displays time.sleep(0.2) # Longer sleep for static displays
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nDisplay stopped by user") print("\nDisplay stopped by user")

View File

@@ -50,6 +50,28 @@ class WeatherManager:
'temp_high': (255, 100, 100), 'temp_high': (255, 100, 100),
'temp_low': (100, 100, 255) 'temp_low': (100, 100, 255)
} }
# Initialize image buffer
self.image = None
self.draw = None
self._init_buffer()
def _init_buffer(self):
"""Initialize or reset the image buffer."""
if self.display_manager and self.display_manager.matrix:
self.image = Image.new('RGB', (self.display_manager.matrix.width, self.display_manager.matrix.height))
self.draw = ImageDraw.Draw(self.image)
def _clear_buffer(self):
"""Clear the image buffer without updating display."""
if self.image and self.draw:
self.draw.rectangle((0, 0, self.image.size[0], self.image.size[1]), fill=(0, 0, 0))
def _update_display(self):
"""Update the display with current buffer contents."""
if self.image:
self.display_manager.image = self.image.copy()
self.display_manager.draw = ImageDraw.Draw(self.display_manager.image)
self.display_manager.update_display()
def _fetch_weather(self) -> None: def _fetch_weather(self) -> None:
"""Fetch current weather and forecast data from OpenWeatherMap API.""" """Fetch current weather and forecast data from OpenWeatherMap API."""
@@ -172,8 +194,9 @@ class WeatherManager:
# Only update display if forced or data changed # Only update display if forced or data changed
if force_clear or not hasattr(self, 'last_temp') or temp != self.last_temp or condition != self.last_condition: if force_clear or not hasattr(self, 'last_temp') or temp != self.last_temp or condition != self.last_condition:
if force_clear: # Reset buffer
self.display_manager.clear() self._init_buffer()
self._clear_buffer()
# Calculate layout # Calculate layout
display_width = self.display_manager.matrix.width display_width = self.display_manager.matrix.width
@@ -181,10 +204,14 @@ class WeatherManager:
# Draw main temperature in large format # Draw main temperature in large format
temp_text = f"{temp}°" temp_text = f"{temp}°"
x_pos = display_width // 4
y_pos = display_height // 2 - 4
# Draw to buffer
self.display_manager.draw_text( self.display_manager.draw_text(
temp_text, temp_text,
x=display_width // 4, x=x_pos,
y=display_height // 2 - 4, y=y_pos,
color=self.COLORS['highlight'], color=self.COLORS['highlight'],
small_font=False small_font=False
) )
@@ -204,6 +231,9 @@ class WeatherManager:
small_font=True small_font=True
) )
# Update display once
self._update_display()
# Update cache # Update cache
self.last_temp = temp self.last_temp = temp
self.last_condition = condition self.last_condition = condition
@@ -219,9 +249,9 @@ class WeatherManager:
if not force_clear and current_time - self.last_draw_time < 0.1: if not force_clear and current_time - self.last_draw_time < 0.1:
return return
# Clear display when starting new scroll # Reset buffer
if force_clear: self._init_buffer()
self.display_manager.clear() self._clear_buffer()
# Calculate layout parameters # Calculate layout parameters
display_width = self.display_manager.matrix.width display_width = self.display_manager.matrix.width
@@ -238,7 +268,7 @@ class WeatherManager:
) )
# Draw separator line # Draw separator line
self.display_manager.draw.line( self.draw.line(
[(0, 8), (display_width, 8)], [(0, 8), (display_width, 8)],
fill=self.COLORS['separator'] fill=self.COLORS['separator']
) )
@@ -283,12 +313,13 @@ class WeatherManager:
if i < len(self.hourly_forecast) - 1: if i < len(self.hourly_forecast) - 1:
sep_x = x_pos + forecast_width - 1 sep_x = x_pos + forecast_width - 1
if 0 <= sep_x <= display_width: if 0 <= sep_x <= display_width:
self.display_manager.draw.line( self.draw.line(
[(sep_x, 8), (sep_x, display_height)], [(sep_x, 8), (sep_x, display_height)],
fill=self.COLORS['separator'] fill=self.COLORS['separator']
) )
self.display_manager.update_display() # Update display once
self._update_display()
self.last_draw_time = current_time self.last_draw_time = current_time
def display_daily_forecast(self, force_clear: bool = False) -> None: def display_daily_forecast(self, force_clear: bool = False) -> None:
@@ -302,6 +333,10 @@ class WeatherManager:
if not force_clear and current_time - self.last_draw_time < 0.1: if not force_clear and current_time - self.last_draw_time < 0.1:
return return
# Reset buffer
self._init_buffer()
self._clear_buffer()
# Calculate layout parameters # Calculate layout parameters
display_width = self.display_manager.matrix.width display_width = self.display_manager.matrix.width
display_height = self.display_manager.matrix.height display_height = self.display_manager.matrix.height
@@ -317,7 +352,7 @@ class WeatherManager:
) )
# Draw separator line # Draw separator line
self.display_manager.draw.line( self.draw.line(
[(0, 8), (display_width, 8)], [(0, 8), (display_width, 8)],
fill=self.COLORS['separator'] fill=self.COLORS['separator']
) )
@@ -373,10 +408,11 @@ class WeatherManager:
# Draw separator lines # Draw separator lines
if i < len(self.daily_forecast) - 1: if i < len(self.daily_forecast) - 1:
sep_x = x_base + section_width - 1 sep_x = x_base + section_width - 1
self.display_manager.draw.line( self.draw.line(
[(sep_x, 8), (sep_x, display_height)], [(sep_x, 8), (sep_x, display_height)],
fill=self.COLORS['separator'] fill=self.COLORS['separator']
) )
self.display_manager.update_display() # Update display once
self._update_display()
self.last_draw_time = current_time self.last_draw_time = current_time