Static Rendering and Clock Improvement

added date to clock and removed all scrolling from weather
This commit is contained in:
Chuck
2025-04-08 18:26:32 -05:00
parent ec11f57375
commit f569881394
3 changed files with 149 additions and 187 deletions

View File

@@ -22,6 +22,13 @@ class Clock:
# Use configured timezone if available, otherwise try to determine it # Use configured timezone if available, otherwise try to determine it
self.timezone = self._get_timezone() self.timezone = self._get_timezone()
self.last_time = None self.last_time = None
self.last_date = None
# Colors for different elements
self.COLORS = {
'time': (255, 255, 255), # White for time
'ampm': (200, 200, 200), # Light gray for AM/PM
'date': (150, 150, 150) # Darker gray for date
}
def _get_timezone(self) -> pytz.timezone: def _get_timezone(self) -> pytz.timezone:
"""Get timezone based on location or config.""" """Get timezone based on location or config."""
@@ -62,27 +69,78 @@ class Clock:
print("Using fallback timezone: US/Central") print("Using fallback timezone: US/Central")
return pytz.timezone('US/Central') return pytz.timezone('US/Central')
def get_current_time(self) -> str: def _get_ordinal_suffix(self, day: int) -> str:
"""Get the current time in the configured timezone.""" """Get the ordinal suffix for a day number (1st, 2nd, 3rd, etc.)."""
current_time = datetime.now(self.timezone) if 10 <= day % 100 <= 20:
return current_time.strftime(self.clock_config.get('format', '%H:%M:%S')) suffix = 'th'
else:
suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(day % 10, 'th')
return suffix
def get_current_time(self) -> tuple:
"""Get the current time and date in the configured timezone."""
current = datetime.now(self.timezone)
# Format time in 12-hour format with AM/PM
time_str = current.strftime('%I:%M') # Remove leading zero from hour
if time_str.startswith('0'):
time_str = time_str[1:]
# Get AM/PM
ampm = current.strftime('%p')
# Format date with ordinal suffix
day_suffix = self._get_ordinal_suffix(current.day)
date_str = current.strftime(f'%A, %B %-d{day_suffix}')
return time_str, ampm, date_str
def display_time(self, force_clear: bool = False) -> None: def display_time(self, force_clear: bool = False) -> None:
"""Display the current time.""" """Display the current time and date."""
current_time = self.get_current_time() time_str, ampm, date_str = self.get_current_time()
# Only update if the time has changed # Only update if something has changed
if current_time != self.last_time: if time_str != self.last_time or date_str != self.last_date or force_clear:
logger.debug("Time changed, updating display from %s to %s", self.last_time, current_time) # Clear the display
self.last_time = current_time self.display_manager.clear()
# Center the text on the display # Calculate positions
text_width = self.display_manager.font.getlength(current_time) display_width = self.display_manager.matrix.width
x = (self.display_manager.matrix.width - text_width) // 2 display_height = self.display_manager.matrix.height
y = (self.display_manager.matrix.height - 24) // 2
logger.debug("Drawing time at position (%d, %d)", x, y) # Draw time (large, centered)
self.display_manager.draw_text(current_time, x, y, force_clear=force_clear) self.display_manager.draw_text(
time_str,
y=4, # Near top
color=self.COLORS['time'],
small_font=False
)
# Draw AM/PM (small, next to time)
time_width = self.display_manager.font.getlength(time_str)
ampm_x = (display_width + time_width) // 2 + 2 # Right of time
self.display_manager.draw_text(
ampm,
x=ampm_x,
y=6, # Align with time
color=self.COLORS['ampm'],
small_font=True
)
# Draw date (small, centered below time)
self.display_manager.draw_text(
date_str,
y=display_height - 10, # Near bottom
color=self.COLORS['date'],
small_font=True
)
# Update display
self.display_manager.update_display()
# Update cache
self.last_time = time_str
self.last_date = date_str
if __name__ == "__main__": if __name__ == "__main__":
clock = Clock() clock = Clock()

View File

@@ -19,12 +19,8 @@ class DisplayController:
self.weather = WeatherManager(self.config, self.display_manager) self.weather = WeatherManager(self.config, self.display_manager)
self.current_display = 'clock' self.current_display = 'clock'
self.last_switch = time.time() self.last_switch = time.time()
self.scroll_position = 0
self.scroll_speed = 1 # Reduced scroll speed
self.last_scroll = time.time()
self.scroll_interval = 0.2 # Increased scroll interval for smoother scrolling
self.force_clear = True # Start with a clear screen self.force_clear = True # Start with a clear screen
self.update_interval = 0.1 # Consistent update interval self.update_interval = 0.5 # Slower updates for better stability
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):
@@ -40,7 +36,6 @@ class DisplayController:
self.current_display = 'weather' self.current_display = 'weather'
elif self.current_display == 'weather': elif self.current_display == 'weather':
self.current_display = 'hourly' self.current_display = 'hourly'
self.scroll_position = 0 # Reset scroll position when switching to hourly
elif self.current_display == 'hourly': elif self.current_display == 'hourly':
self.current_display = 'daily' self.current_display = 'daily'
else: # daily else: # daily
@@ -48,20 +43,9 @@ 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.force_clear = True # Clear screen on mode switch self.force_clear = True
self.display_manager.clear() # Ensure clean transition self.display_manager.clear() # Ensure clean transition
# Update scroll position for hourly forecast if needed
if self.current_display == 'hourly' and current_time - self.last_scroll > self.scroll_interval:
self.scroll_position += self.scroll_speed
self.last_scroll = current_time
# Reset scroll position if we've gone through all forecasts
if self.scroll_position > self.display_manager.matrix.width * 3:
self.scroll_position = 0
self.force_clear = True
self.display_manager.clear()
# Display current screen # Display current screen
try: try:
if self.current_display == 'clock': if self.current_display == 'clock':
@@ -69,7 +53,7 @@ class DisplayController:
elif self.current_display == 'weather': elif self.current_display == 'weather':
self.weather.display_weather(force_clear=self.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=self.force_clear) self.weather.display_hourly_forecast(force_clear=self.force_clear)
else: # daily else: # daily
self.weather.display_daily_forecast(force_clear=self.force_clear) self.weather.display_daily_forecast(force_clear=self.force_clear)
except Exception as e: except Exception as e:
@@ -80,7 +64,7 @@ class DisplayController:
# Reset force clear flag after use # Reset force clear flag after use
self.force_clear = False self.force_clear = False
# Consistent sleep time for all modes # Sleep between updates
time.sleep(self.update_interval) time.sleep(self.update_interval)
except KeyboardInterrupt: except KeyboardInterrupt:

View File

@@ -34,7 +34,6 @@ class WeatherManager:
self.forecast_data = None self.forecast_data = None
self.hourly_forecast = None self.hourly_forecast = None
self.daily_forecast = None self.daily_forecast = None
self.scroll_position = 0
self.last_draw_time = 0 self.last_draw_time = 0
# Layout constants # Layout constants
self.PADDING = 2 self.PADDING = 2
@@ -50,28 +49,6 @@ 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."""
@@ -181,98 +158,72 @@ class WeatherManager:
self._fetch_weather() self._fetch_weather()
return self.weather_data return self.weather_data
def _prepare_display(self, force_clear: bool = False):
"""Prepare the display for drawing."""
if force_clear:
self.display_manager.clear()
self.display_manager.image = Image.new('RGB', (self.display_manager.matrix.width, self.display_manager.matrix.height))
self.display_manager.draw = ImageDraw.Draw(self.display_manager.image)
def display_weather(self, force_clear: bool = False) -> None: def display_weather(self, force_clear: bool = False) -> None:
"""Display current weather information using an improved layout.""" """Display current weather information using a static layout."""
weather_data = self.get_weather() weather_data = self.get_weather()
if not weather_data: if not weather_data:
return return
current_time = time.time() # Always clear and redraw
self.display_manager.clear()
# Calculate layout
display_width = self.display_manager.matrix.width
display_height = self.display_manager.matrix.height
# Get weather data
temp = round(weather_data['main']['temp']) temp = round(weather_data['main']['temp'])
condition = weather_data['weather'][0]['main'] condition = weather_data['weather'][0]['main']
humidity = weather_data['main']['humidity'] humidity = weather_data['main']['humidity']
# Only update display if forced or data changed # Draw temperature (large, centered)
if force_clear or not hasattr(self, 'last_temp') or temp != self.last_temp or condition != self.last_condition: temp_text = f"{temp}°F"
# Prepare display self.display_manager.draw_text(
self._prepare_display(force_clear) temp_text,
y=2, # Near top
# Calculate layout color=self.COLORS['highlight'],
display_width = self.display_manager.matrix.width small_font=False
display_height = self.display_manager.matrix.height )
# Draw main temperature in large format # Draw weather icon below temperature
temp_text = f"{temp}°" icon_x = (display_width - self.ICON_SIZE['large']) // 2
x_pos = display_width // 4 icon_y = display_height // 2 - 4
y_pos = display_height // 2 - 4 self.display_manager.draw_weather_icon(condition, icon_x, icon_y, size=self.ICON_SIZE['large'])
# Draw temperature # Draw humidity at bottom
self.display_manager.draw_text( humidity_text = f"Humidity: {humidity}%"
temp_text, self.display_manager.draw_text(
x=x_pos, humidity_text,
y=y_pos, y=display_height - 8,
color=self.COLORS['highlight'], color=self.COLORS['text'],
small_font=False, small_font=True
force_clear=False # Don't clear since we already prepared the display )
)
# Update display
# Draw weather icon self.display_manager.update_display()
icon_x = display_width * 3 // 4 - self.ICON_SIZE['large'] // 2
icon_y = display_height // 2 - self.ICON_SIZE['large'] // 2
self.display_manager.draw_weather_icon(condition, icon_x, icon_y, size=self.ICON_SIZE['large'])
# Draw humidity below temperature
humidity_text = f"Humidity: {humidity}%"
self.display_manager.draw_text(
humidity_text,
x=self.PADDING,
y=display_height - 8,
color=self.COLORS['text'],
small_font=True,
force_clear=False # Don't clear since we already prepared the display
)
# Update display
self.display_manager.update_display()
# Update cache
self.last_temp = temp
self.last_condition = condition
def display_hourly_forecast(self, scroll_position: int = 0, force_clear: bool = False) -> None: def display_hourly_forecast(self, scroll_position: int = 0, force_clear: bool = False) -> None:
"""Display scrolling hourly forecast with improved layout.""" """Display static hourly forecast showing next 3 hours."""
if not self.hourly_forecast: if not self.hourly_forecast:
self.get_weather() self.get_weather()
if not self.hourly_forecast: if not self.hourly_forecast:
return return
current_time = time.time() # Always clear and redraw
if not force_clear and current_time - self.last_draw_time < 0.1: self.display_manager.clear()
return
# Prepare display
self._prepare_display(force_clear)
# 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
forecast_width = display_width // 2 section_width = display_width // 3 # Show 3 hours
# Create header # Draw header
header_text = "HOURLY" header_text = "NEXT 3 HOURS"
self.display_manager.draw_text( self.display_manager.draw_text(
header_text, header_text,
y=1, y=1,
color=self.COLORS['highlight'], color=self.COLORS['highlight'],
small_font=True, small_font=True
force_clear=False # Don't clear since we already prepared the display
) )
# Draw separator line # Draw separator line
@@ -281,25 +232,21 @@ class WeatherManager:
fill=self.COLORS['separator'] fill=self.COLORS['separator']
) )
# Create forecasts # Show first 3 hours
for i, forecast in enumerate(self.hourly_forecast): for i, forecast in enumerate(self.hourly_forecast[:3]):
x_pos = display_width - scroll_position + (i * forecast_width) x_base = i * section_width
if x_pos < -forecast_width or x_pos > display_width:
continue
# Draw time # Draw time
self.display_manager.draw_text( self.display_manager.draw_text(
forecast['hour'], forecast['hour'],
x=x_pos + forecast_width // 2, x=x_base + section_width // 2,
y=10, y=10,
color=self.COLORS['text'], color=self.COLORS['text'],
small_font=True, small_font=True
force_clear=False # Don't clear since we already prepared the display
) )
# Draw icon # Draw icon
icon_x = x_pos + (forecast_width - self.ICON_SIZE['medium']) // 2 icon_x = x_base + (section_width - self.ICON_SIZE['medium']) // 2
icon_y = 14 icon_y = 14
self.display_manager.draw_weather_icon( self.display_manager.draw_weather_icon(
forecast['condition'], forecast['condition'],
@@ -309,56 +256,48 @@ class WeatherManager:
) )
# Draw temperature # Draw temperature
temp_text = f"{forecast['temp']}°" temp_text = f"{forecast['temp']}°F"
self.display_manager.draw_text( self.display_manager.draw_text(
temp_text, temp_text,
x=x_pos + forecast_width // 2, x=x_base + section_width // 2,
y=display_height - 8, y=display_height - 8,
color=self.COLORS['text'], color=self.COLORS['text'],
small_font=True, small_font=True
force_clear=False # Don't clear since we already prepared the display
) )
# Draw separator line # Draw separator lines
if i < len(self.hourly_forecast) - 1: if i < 2: # Only draw between sections
sep_x = x_pos + forecast_width - 1 sep_x = x_base + section_width - 1
if 0 <= sep_x <= display_width: self.display_manager.draw.line(
self.display_manager.draw.line( [(sep_x, 8), (sep_x, display_height)],
[(sep_x, 8), (sep_x, display_height)], fill=self.COLORS['separator']
fill=self.COLORS['separator'] )
)
# Update display # Update display
self.display_manager.update_display() self.display_manager.update_display()
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:
"""Display daily forecast with improved layout and visual hierarchy.""" """Display static 3-day forecast."""
if not self.daily_forecast: if not self.daily_forecast:
self.get_weather() self.get_weather()
if not self.daily_forecast: if not self.daily_forecast:
return return
current_time = time.time() # Always clear and redraw
if not force_clear and current_time - self.last_draw_time < 0.1: self.display_manager.clear()
return
# Prepare display
self._prepare_display(force_clear)
# 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
section_width = display_width // 3 section_width = display_width // 3
# Create header # Draw header
header_text = "3-DAY FORECAST" header_text = "3-DAY FORECAST"
self.display_manager.draw_text( self.display_manager.draw_text(
header_text, header_text,
y=1, y=1,
color=self.COLORS['highlight'], color=self.COLORS['highlight'],
small_font=True, small_font=True
force_clear=False # Don't clear since we already prepared the display
) )
# Draw separator line # Draw separator line
@@ -377,8 +316,7 @@ class WeatherManager:
x=x_base + section_width // 2, x=x_base + section_width // 2,
y=10, y=10,
color=self.COLORS['text'], color=self.COLORS['text'],
small_font=True, small_font=True
force_clear=False # Don't clear since we already prepared the display
) )
# Draw weather icon # Draw weather icon
@@ -392,34 +330,17 @@ class WeatherManager:
) )
# Draw temperatures with different colors for high/low # Draw temperatures with different colors for high/low
low_text = f"{day['temp_low']}°" temp_text = f"{day['temp_low']}°/{day['temp_high']}°"
high_text = f"{day['temp_high']}°"
# Calculate positions for temperatures
temp_y = display_height - 8
low_x = x_base + section_width // 3
high_x = x_base + 2 * section_width // 3
# Draw temperatures
self.display_manager.draw_text( self.display_manager.draw_text(
low_text, temp_text,
x=low_x, x=x_base + section_width // 2,
y=temp_y, y=display_height - 8,
color=self.COLORS['temp_low'], color=self.COLORS['text'],
small_font=True, small_font=True
force_clear=False # Don't clear since we already prepared the display
)
self.display_manager.draw_text(
high_text,
x=high_x,
y=temp_y,
color=self.COLORS['temp_high'],
small_font=True,
force_clear=False # Don't clear since we already prepared the display
) )
# Draw separator lines # Draw separator lines
if i < len(self.daily_forecast) - 1: if i < 2: # Only draw between sections
sep_x = x_base + section_width - 1 sep_x = x_base + section_width - 1
self.display_manager.draw.line( self.display_manager.draw.line(
[(sep_x, 8), (sep_x, display_height)], [(sep_x, 8), (sep_x, display_height)],
@@ -427,5 +348,4 @@ class WeatherManager:
) )
# Update display # Update display
self.display_manager.update_display() self.display_manager.update_display()
self.last_draw_time = current_time