mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-10 21:03:01 +00:00
troubleshooting weather forecast
weather forecast error resolution
This commit is contained in:
@@ -24,7 +24,8 @@ class DisplayManager:
|
|||||||
if not DisplayManager._initialized:
|
if not DisplayManager._initialized:
|
||||||
self.config = config
|
self.config = config
|
||||||
logger.info("Initializing DisplayManager with config: %s", config)
|
logger.info("Initializing DisplayManager with config: %s", config)
|
||||||
self._setup_matrix() # This now sets self.matrix and self.font
|
self._setup_matrix()
|
||||||
|
self._load_fonts()
|
||||||
DisplayManager._initialized = True
|
DisplayManager._initialized = True
|
||||||
|
|
||||||
def _setup_matrix(self):
|
def _setup_matrix(self):
|
||||||
@@ -111,7 +112,22 @@ class DisplayManager:
|
|||||||
self.draw.rectangle((0, 0, self.matrix.width, self.matrix.height), fill=(0, 0, 0))
|
self.draw.rectangle((0, 0, self.matrix.width, self.matrix.height), fill=(0, 0, 0))
|
||||||
self.update_display()
|
self.update_display()
|
||||||
|
|
||||||
def draw_text(self, text: str, x: int = None, y: int = None, color: tuple = (255, 255, 255), force_clear: bool = False):
|
def _load_fonts(self):
|
||||||
|
"""Load fonts for different text sizes."""
|
||||||
|
try:
|
||||||
|
# Load regular font (size 14 for better readability)
|
||||||
|
self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 14)
|
||||||
|
# Load small font (size 8 for compact display)
|
||||||
|
self.small_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 8)
|
||||||
|
logger.info("Fonts loaded successfully")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error loading fonts: {e}")
|
||||||
|
# Fallback to default bitmap font if TTF loading fails
|
||||||
|
self.font = ImageFont.load_default()
|
||||||
|
self.small_font = self.font
|
||||||
|
|
||||||
|
def draw_text(self, text: str, x: int = None, y: int = None, color: tuple = (255, 255, 255),
|
||||||
|
force_clear: bool = False, small_font: bool = False):
|
||||||
"""Draw text on the display with automatic centering."""
|
"""Draw text on the display with automatic centering."""
|
||||||
if force_clear:
|
if force_clear:
|
||||||
self.clear()
|
self.clear()
|
||||||
@@ -120,6 +136,9 @@ class DisplayManager:
|
|||||||
self.image = Image.new('RGB', (self.matrix.width, self.matrix.height))
|
self.image = Image.new('RGB', (self.matrix.width, self.matrix.height))
|
||||||
self.draw = ImageDraw.Draw(self.image)
|
self.draw = ImageDraw.Draw(self.image)
|
||||||
|
|
||||||
|
# Select font based on small_font parameter
|
||||||
|
font = self.small_font if small_font else self.font
|
||||||
|
|
||||||
# Split text into lines if it contains newlines
|
# Split text into lines if it contains newlines
|
||||||
lines = text.split('\n')
|
lines = text.split('\n')
|
||||||
|
|
||||||
@@ -131,7 +150,7 @@ class DisplayManager:
|
|||||||
edge_padding = 2 # Minimum padding from display edges
|
edge_padding = 2 # Minimum padding from display edges
|
||||||
|
|
||||||
for line in lines:
|
for line in lines:
|
||||||
bbox = self.draw.textbbox((0, 0), line, font=self.font)
|
bbox = self.draw.textbbox((0, 0), line, font=font)
|
||||||
line_width = bbox[2] - bbox[0]
|
line_width = bbox[2] - bbox[0]
|
||||||
line_height = bbox[3] - bbox[1]
|
line_height = bbox[3] - bbox[1]
|
||||||
line_heights.append(line_height)
|
line_heights.append(line_height)
|
||||||
@@ -161,8 +180,8 @@ class DisplayManager:
|
|||||||
# Ensure y coordinate stays within bounds
|
# Ensure y coordinate stays within bounds
|
||||||
current_y = max(edge_padding, min(current_y, self.matrix.height - line_heights[i] - edge_padding))
|
current_y = max(edge_padding, min(current_y, self.matrix.height - line_heights[i] - edge_padding))
|
||||||
|
|
||||||
logger.info(f"Drawing line '{line}' at position ({line_x}, {current_y})")
|
# Draw the text (removed logging to reduce spam)
|
||||||
self.draw.text((line_x, current_y), line, font=self.font, fill=color)
|
self.draw.text((line_x, current_y), line, font=font, fill=color)
|
||||||
|
|
||||||
# Calculate next line position
|
# Calculate next line position
|
||||||
current_y += line_heights[i] + padding
|
current_y += line_heights[i] + padding
|
||||||
|
|||||||
@@ -150,18 +150,32 @@ class WeatherManager:
|
|||||||
if not weather_data:
|
if not weather_data:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
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']
|
||||||
|
|
||||||
# Draw temperature text and weather icon
|
# Only update display if forced or data changed
|
||||||
text = f"{temp}°F"
|
if force_clear or not hasattr(self, 'last_temp') or temp != self.last_temp or condition != self.last_condition:
|
||||||
icon_x = (self.display_manager.matrix.width - 20) // 2 # Center the 20px icon
|
# Draw temperature text and weather icon
|
||||||
icon_y = 2 # Near the top
|
text = f"{temp}°F"
|
||||||
self.display_manager.draw_text_with_icons(
|
icon_x = (self.display_manager.matrix.width - 20) // 2 # Center the 20px icon
|
||||||
text,
|
icon_y = 2 # Near the top
|
||||||
icons=[(condition, icon_x, icon_y)],
|
|
||||||
force_clear=force_clear
|
# Clear and draw
|
||||||
)
|
if force_clear:
|
||||||
|
self.display_manager.clear()
|
||||||
|
|
||||||
|
# Draw icon and text
|
||||||
|
self.display_manager.draw_weather_icon(condition, icon_x, icon_y, size=16)
|
||||||
|
self.display_manager.draw_text(
|
||||||
|
text,
|
||||||
|
y=icon_y + 18, # Position text below icon
|
||||||
|
small_font=False
|
||||||
|
)
|
||||||
|
|
||||||
|
# Update cache
|
||||||
|
self.last_temp = temp
|
||||||
|
self.last_condition = condition
|
||||||
|
|
||||||
def display_hourly_forecast(self, scroll_amount: int = 0, force_clear: bool = False) -> None:
|
def display_hourly_forecast(self, scroll_amount: int = 0, force_clear: bool = False) -> None:
|
||||||
"""Display scrolling hourly forecast information."""
|
"""Display scrolling hourly forecast information."""
|
||||||
|
|||||||
Reference in New Issue
Block a user