mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-11 13:23:00 +00:00
Update weather_manager.py
improve padding and display improvements
This commit is contained in:
@@ -35,7 +35,21 @@ class WeatherManager:
|
|||||||
self.hourly_forecast = None
|
self.hourly_forecast = None
|
||||||
self.daily_forecast = None
|
self.daily_forecast = None
|
||||||
self.scroll_position = 0
|
self.scroll_position = 0
|
||||||
self.last_draw_time = 0 # Add draw time tracking to reduce flickering
|
self.last_draw_time = 0
|
||||||
|
# Layout constants
|
||||||
|
self.PADDING = 2
|
||||||
|
self.ICON_SIZE = {
|
||||||
|
'large': 16,
|
||||||
|
'medium': 12,
|
||||||
|
'small': 8
|
||||||
|
}
|
||||||
|
self.COLORS = {
|
||||||
|
'text': (255, 255, 255),
|
||||||
|
'highlight': (255, 200, 0),
|
||||||
|
'separator': (64, 64, 64),
|
||||||
|
'temp_high': (255, 100, 100),
|
||||||
|
'temp_low': (100, 100, 255)
|
||||||
|
}
|
||||||
|
|
||||||
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."""
|
||||||
@@ -146,7 +160,7 @@ class WeatherManager:
|
|||||||
return self.weather_data
|
return self.weather_data
|
||||||
|
|
||||||
def display_weather(self, force_clear: bool = False) -> None:
|
def display_weather(self, force_clear: bool = False) -> None:
|
||||||
"""Display current weather information on the LED matrix."""
|
"""Display current weather information using an improved layout."""
|
||||||
weather_data = self.get_weather()
|
weather_data = self.get_weather()
|
||||||
if not weather_data:
|
if not weather_data:
|
||||||
return
|
return
|
||||||
@@ -154,39 +168,54 @@ class WeatherManager:
|
|||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
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']
|
||||||
|
|
||||||
# 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:
|
||||||
# Draw temperature text and weather icon
|
|
||||||
text = f"{temp}°F"
|
|
||||||
icon_x = (self.display_manager.matrix.width - 20) // 2 # Center the 20px icon
|
|
||||||
icon_y = 2 # Near the top
|
|
||||||
|
|
||||||
# Clear and draw
|
|
||||||
if force_clear:
|
if force_clear:
|
||||||
self.display_manager.clear()
|
self.display_manager.clear()
|
||||||
|
|
||||||
# Draw icon and text
|
# Calculate layout
|
||||||
self.display_manager.draw_weather_icon(condition, icon_x, icon_y, size=16)
|
display_width = self.display_manager.matrix.width
|
||||||
|
display_height = self.display_manager.matrix.height
|
||||||
|
|
||||||
|
# Draw main temperature in large format
|
||||||
|
temp_text = f"{temp}°"
|
||||||
self.display_manager.draw_text(
|
self.display_manager.draw_text(
|
||||||
text,
|
temp_text,
|
||||||
y=icon_y + 18, # Position text below icon
|
x=display_width // 4,
|
||||||
|
y=display_height // 2 - 4,
|
||||||
|
color=self.COLORS['highlight'],
|
||||||
small_font=False
|
small_font=False
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Draw weather icon
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
# Update cache
|
# Update cache
|
||||||
self.last_temp = temp
|
self.last_temp = temp
|
||||||
self.last_condition = condition
|
self.last_condition = condition
|
||||||
|
|
||||||
def display_hourly_forecast(self, scroll_amount: 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 information."""
|
"""Display scrolling hourly forecast with improved layout."""
|
||||||
if not self.hourly_forecast:
|
if not self.hourly_forecast:
|
||||||
self.get_weather() # This will also update forecasts
|
self.get_weather()
|
||||||
if not self.hourly_forecast:
|
if not self.hourly_forecast:
|
||||||
return
|
return
|
||||||
|
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
# Only update if forced or enough time has passed (100ms minimum between updates)
|
|
||||||
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
|
||||||
|
|
||||||
@@ -194,125 +223,160 @@ class WeatherManager:
|
|||||||
if force_clear:
|
if force_clear:
|
||||||
self.display_manager.clear()
|
self.display_manager.clear()
|
||||||
|
|
||||||
# Calculate base positions
|
# 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 # Each forecast takes half the width
|
forecast_width = display_width // 2
|
||||||
icon_size = 12 # Slightly smaller icons for better fit
|
|
||||||
|
|
||||||
# Create a new image for this frame
|
# Create header
|
||||||
self.display_manager.image = Image.new('RGB', (self.display_manager.matrix.width, self.display_manager.matrix.height))
|
header_text = "HOURLY"
|
||||||
self.display_manager.draw = ImageDraw.Draw(self.display_manager.image)
|
self.display_manager.draw_text(
|
||||||
|
header_text,
|
||||||
|
y=1,
|
||||||
|
color=self.COLORS['highlight'],
|
||||||
|
small_font=True
|
||||||
|
)
|
||||||
|
|
||||||
# Create the forecast display
|
# Draw separator line
|
||||||
|
self.display_manager.draw.line(
|
||||||
|
[(0, 8), (display_width, 8)],
|
||||||
|
fill=self.COLORS['separator']
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create forecasts
|
||||||
for i, forecast in enumerate(self.hourly_forecast):
|
for i, forecast in enumerate(self.hourly_forecast):
|
||||||
# Calculate x position with scrolling
|
x_pos = display_width - scroll_position + (i * forecast_width)
|
||||||
x_pos = display_width - scroll_amount + (i * forecast_width)
|
|
||||||
|
|
||||||
# Only draw if the forecast would be visible
|
|
||||||
if x_pos < -forecast_width or x_pos > display_width:
|
if x_pos < -forecast_width or x_pos > display_width:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Draw icon at top
|
# Draw time
|
||||||
icon_x = x_pos + (forecast_width - icon_size) // 2
|
|
||||||
icon_y = 2
|
|
||||||
self.display_manager.draw_weather_icon(forecast['condition'], icon_x, icon_y, size=icon_size)
|
|
||||||
|
|
||||||
# Draw hour below icon
|
|
||||||
hour_text = forecast['hour']
|
|
||||||
hour_y = icon_y + icon_size + 2
|
|
||||||
self.display_manager.draw_text(
|
self.display_manager.draw_text(
|
||||||
hour_text,
|
forecast['hour'],
|
||||||
x=x_pos + forecast_width // 2, # Center in section
|
x=x_pos + forecast_width // 2,
|
||||||
y=hour_y,
|
y=10,
|
||||||
|
color=self.COLORS['text'],
|
||||||
small_font=True
|
small_font=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# Draw temperature at bottom
|
# Draw icon
|
||||||
|
icon_x = x_pos + (forecast_width - self.ICON_SIZE['medium']) // 2
|
||||||
|
icon_y = 14
|
||||||
|
self.display_manager.draw_weather_icon(
|
||||||
|
forecast['condition'],
|
||||||
|
icon_x,
|
||||||
|
icon_y,
|
||||||
|
size=self.ICON_SIZE['medium']
|
||||||
|
)
|
||||||
|
|
||||||
|
# Draw temperature
|
||||||
temp_text = f"{forecast['temp']}°"
|
temp_text = f"{forecast['temp']}°"
|
||||||
temp_y = display_height - 8 # 8 pixels from bottom
|
|
||||||
self.display_manager.draw_text(
|
self.display_manager.draw_text(
|
||||||
temp_text,
|
temp_text,
|
||||||
x=x_pos + forecast_width // 2, # Center in section
|
x=x_pos + forecast_width // 2,
|
||||||
y=temp_y,
|
y=display_height - 8,
|
||||||
|
color=self.COLORS['text'],
|
||||||
small_font=True
|
small_font=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# Draw separator line if not last forecast
|
# Draw separator line
|
||||||
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.display_manager.draw.line(
|
||||||
[(sep_x, 0), (sep_x, display_height)],
|
[(sep_x, 8), (sep_x, display_height)],
|
||||||
fill=(64, 64, 64) # Dim gray line
|
fill=self.COLORS['separator']
|
||||||
)
|
)
|
||||||
|
|
||||||
# Update the display
|
|
||||||
self.display_manager.update_display()
|
self.display_manager.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:
|
||||||
"""Display 3-day forecast information."""
|
"""Display daily forecast with improved layout and visual hierarchy."""
|
||||||
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()
|
current_time = time.time()
|
||||||
# Only update if forced or enough time has passed
|
|
||||||
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
|
||||||
|
|
||||||
# Create new image for this frame
|
|
||||||
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)
|
|
||||||
|
|
||||||
# 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 # Width for each day
|
section_width = display_width // 3
|
||||||
icon_size = 12 # Smaller icons for better fit
|
|
||||||
|
# Create header
|
||||||
|
header_text = "3-DAY FORECAST"
|
||||||
|
self.display_manager.draw_text(
|
||||||
|
header_text,
|
||||||
|
y=1,
|
||||||
|
color=self.COLORS['highlight'],
|
||||||
|
small_font=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# Draw separator line
|
||||||
|
self.display_manager.draw.line(
|
||||||
|
[(0, 8), (display_width, 8)],
|
||||||
|
fill=self.COLORS['separator']
|
||||||
|
)
|
||||||
|
|
||||||
for i, day in enumerate(self.daily_forecast):
|
for i, day in enumerate(self.daily_forecast):
|
||||||
# Calculate base x position for this section
|
|
||||||
x_base = i * section_width
|
x_base = i * section_width
|
||||||
|
|
||||||
# Draw day name at top (e.g., "MON")
|
# Draw day name
|
||||||
day_text = day['date'].upper()
|
day_text = day['date'].upper()
|
||||||
self.display_manager.draw_text(
|
self.display_manager.draw_text(
|
||||||
day_text,
|
day_text,
|
||||||
x=x_base + section_width // 2, # Center in section
|
x=x_base + section_width // 2,
|
||||||
y=2, # Near top
|
y=10,
|
||||||
|
color=self.COLORS['text'],
|
||||||
small_font=True
|
small_font=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# Draw weather icon in middle
|
# Draw weather icon
|
||||||
icon_x = x_base + (section_width - icon_size) // 2
|
icon_x = x_base + (section_width - self.ICON_SIZE['medium']) // 2
|
||||||
icon_y = (display_height - icon_size) // 2
|
icon_y = 14
|
||||||
self.display_manager.draw_weather_icon(
|
self.display_manager.draw_weather_icon(
|
||||||
day['condition'],
|
day['condition'],
|
||||||
icon_x,
|
icon_x,
|
||||||
icon_y,
|
icon_y,
|
||||||
size=icon_size
|
size=self.ICON_SIZE['medium']
|
||||||
)
|
)
|
||||||
|
|
||||||
# Draw temperature at bottom (e.g., "45°/65°")
|
# Draw temperatures with different colors for high/low
|
||||||
temp_text = f"{day['temp_low']}°/{day['temp_high']}°"
|
low_text = f"{day['temp_low']}°"
|
||||||
|
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(
|
||||||
temp_text,
|
low_text,
|
||||||
x=x_base + section_width // 2, # Center in section
|
x=low_x,
|
||||||
y=display_height - 8, # 8 pixels from bottom
|
y=temp_y,
|
||||||
|
color=self.COLORS['temp_low'],
|
||||||
|
small_font=True
|
||||||
|
)
|
||||||
|
self.display_manager.draw_text(
|
||||||
|
high_text,
|
||||||
|
x=high_x,
|
||||||
|
y=temp_y,
|
||||||
|
color=self.COLORS['temp_high'],
|
||||||
small_font=True
|
small_font=True
|
||||||
)
|
)
|
||||||
|
|
||||||
# Draw separator line if not last day
|
# 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.display_manager.draw.line(
|
||||||
[(sep_x, 0), (sep_x, display_height)],
|
[(sep_x, 8), (sep_x, display_height)],
|
||||||
fill=(64, 64, 64) # Dim gray line
|
fill=self.COLORS['separator']
|
||||||
)
|
)
|
||||||
|
|
||||||
# Update the display
|
|
||||||
self.display_manager.update_display()
|
self.display_manager.update_display()
|
||||||
self.last_draw_time = current_time
|
self.last_draw_time = current_time
|
||||||
Reference in New Issue
Block a user