mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-04-13 05:53:00 +00:00
fix: Refine live sports state handling to prevent flicker
This commit is contained in:
@@ -407,23 +407,21 @@ class DisplayController:
|
|||||||
|
|
||||||
# --- Live Game Handling ---
|
# --- Live Game Handling ---
|
||||||
if has_live_games:
|
if has_live_games:
|
||||||
manager_to_display = None
|
target_mode = "" # The mode we intend to display this iteration
|
||||||
mode_to_display_str = ""
|
needs_state_update = False
|
||||||
|
|
||||||
is_currently_live = self.current_display_mode.endswith('_live')
|
is_currently_live = self.current_display_mode.endswith('_live')
|
||||||
|
|
||||||
if not is_currently_live:
|
if not is_currently_live:
|
||||||
# Switching INTO live mode from a non-live mode
|
# Switching INTO live mode from a non-live mode
|
||||||
logger.info(f"Switching into LIVE mode: {live_sport_type}_live")
|
target_mode = f"{live_sport_type}_live" # Use highest priority sport
|
||||||
mode_to_display_str = f"{live_sport_type}_live"
|
needs_state_update = True
|
||||||
self.last_switch = current_time # Reset timer on first switch into live
|
logger.info(f"Switching into LIVE mode: {target_mode}")
|
||||||
self.force_clear = True
|
|
||||||
else:
|
else:
|
||||||
# Already in a live mode, check for rotation
|
# Already in a live mode, check timer for rotation
|
||||||
if current_time - self.last_switch > self.get_current_duration():
|
if current_time - self.last_switch > self.get_current_duration():
|
||||||
# Timer expired, check for rotation possibility
|
# Timer expired, check for rotation possibility
|
||||||
active_live_sports = []
|
active_live_sports = []
|
||||||
# Define priority order for rotation check (can be adjusted)
|
|
||||||
priority_order = ['soccer', 'nhl', 'nba', 'mlb']
|
priority_order = ['soccer', 'nhl', 'nba', 'mlb']
|
||||||
for sport in priority_order:
|
for sport in priority_order:
|
||||||
live_attr = f"{sport}_live"
|
live_attr = f"{sport}_live"
|
||||||
@@ -436,47 +434,63 @@ class DisplayController:
|
|||||||
current_index = active_live_sports.index(current_sport)
|
current_index = active_live_sports.index(current_sport)
|
||||||
next_index = (current_index + 1) % len(active_live_sports)
|
next_index = (current_index + 1) % len(active_live_sports)
|
||||||
next_sport = active_live_sports[next_index]
|
next_sport = active_live_sports[next_index]
|
||||||
logger.info(f"Rotating live sports: {self.current_display_mode} -> {next_sport}_live")
|
target_mode = f"{next_sport}_live"
|
||||||
mode_to_display_str = f"{next_sport}_live"
|
if target_mode != self.current_display_mode:
|
||||||
self.last_switch = current_time # Reset timer for the new sport's duration
|
needs_state_update = True
|
||||||
self.force_clear = True
|
logger.info(f"Rotating live sports: {self.current_display_mode} -> {target_mode}")
|
||||||
|
else:
|
||||||
|
# Target is same as current, reset timer but no mode change needed
|
||||||
|
self.last_switch = current_time
|
||||||
|
self.force_clear = False
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# Current mode wasn't in active list? Default to highest priority active
|
|
||||||
logger.warning(f"Could not find current live mode {self.current_display_mode} in active list {active_live_sports}. Defaulting.")
|
logger.warning(f"Could not find current live mode {self.current_display_mode} in active list {active_live_sports}. Defaulting.")
|
||||||
mode_to_display_str = f"{active_live_sports[0]}_live"
|
target_mode = f"{active_live_sports[0]}_live"
|
||||||
self.last_switch = current_time
|
needs_state_update = True # Force update to default
|
||||||
self.force_clear = True
|
|
||||||
else:
|
else:
|
||||||
# Timer expired, but only one or zero active live sports, just reset timer for current
|
# Timer expired, but only one/zero active sports. Stay on current or switch if needed.
|
||||||
self.last_switch = current_time
|
if active_live_sports:
|
||||||
mode_to_display_str = self.current_display_mode # Stay on the same mode
|
target_mode = f"{active_live_sports[0]}_live"
|
||||||
self.force_clear = False # No need to force clear if staying
|
else: # No live sports anymore? Fallback handled later.
|
||||||
|
target_mode = self.current_display_mode # Tentatively stay
|
||||||
|
|
||||||
|
if target_mode != self.current_display_mode:
|
||||||
|
needs_state_update = True # Switch to the single active one if needed
|
||||||
|
else:
|
||||||
|
# Still on the same single live sport, just reset its timer
|
||||||
|
self.last_switch = current_time
|
||||||
|
self.force_clear = False # No visual change
|
||||||
else:
|
else:
|
||||||
# Timer has not expired, continue with current live mode
|
# Timer has not expired, continue with current live mode
|
||||||
mode_to_display_str = self.current_display_mode
|
target_mode = self.current_display_mode
|
||||||
self.force_clear = False
|
needs_state_update = False # No state change needed
|
||||||
|
|
||||||
# --- Set the manager and update display mode string ---
|
# --- Update State if Required ---
|
||||||
if mode_to_display_str:
|
if needs_state_update and target_mode:
|
||||||
self.current_display_mode = mode_to_display_str
|
self.current_display_mode = target_mode
|
||||||
sport_type = mode_to_display_str.replace('_live', '')
|
self.last_switch = current_time # Reset timer whenever mode changes
|
||||||
manager_attr = f"{sport_type}_live"
|
self.force_clear = True
|
||||||
if hasattr(self, manager_attr):
|
elif not target_mode and is_currently_live:
|
||||||
manager_to_display = getattr(self, manager_attr)
|
# We were live, but target couldn't be determined (e.g., all games ended)
|
||||||
else:
|
# Let execution fall through to regular mode rotation logic
|
||||||
logger.error(f"Could not find manager attribute {manager_attr}")
|
has_live_games = False
|
||||||
has_live_games = False # Fallback to non-live rotation
|
logger.info("Exiting live mode as no target live mode determined.")
|
||||||
else:
|
|
||||||
# This case should ideally not be reached if has_live_games is True
|
# --- Select Manager and Display ---
|
||||||
logger.warning("In live game handling but no mode_to_display determined.")
|
manager_to_display = None
|
||||||
has_live_games = False # Fallback
|
if has_live_games: # Check again in case we bailed above
|
||||||
|
current_sport_type = self.current_display_mode.replace('_live', '')
|
||||||
|
manager_attr = f"{current_sport_type}_live"
|
||||||
|
if hasattr(self, manager_attr):
|
||||||
|
manager_to_display = getattr(self, manager_attr)
|
||||||
|
else:
|
||||||
|
logger.error(f"Could not find manager attribute {manager_attr} for current mode {self.current_display_mode}")
|
||||||
|
has_live_games = False # Fallback
|
||||||
|
|
||||||
# --- Display ---
|
|
||||||
if manager_to_display:
|
if manager_to_display:
|
||||||
manager_to_display.display(force_clear=self.force_clear)
|
manager_to_display.display(force_clear=self.force_clear)
|
||||||
self.force_clear = False # Reset clear flag after display
|
self.force_clear = False # Reset clear flag after display
|
||||||
continue # Skip regular mode display logic if live game was shown
|
continue # Skip regular mode display logic if live game was shown
|
||||||
# else handled by falling back to non-live rotation below
|
# else: Fall through to regular rotation
|
||||||
|
|
||||||
# --- Regular Mode Rotation (only if NO live games OR fallback from live) ---
|
# --- Regular Mode Rotation (only if NO live games OR fallback from live) ---
|
||||||
if not has_live_games:
|
if not has_live_games:
|
||||||
@@ -576,7 +590,7 @@ class DisplayController:
|
|||||||
# Continue to next iteration after error
|
# Continue to next iteration after error
|
||||||
|
|
||||||
# Small sleep to prevent high CPU usage
|
# Small sleep to prevent high CPU usage
|
||||||
#time.sleep(self.update_interval)
|
#time.sleep(self.update_interval)
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
logger.info("Display controller stopped by user")
|
logger.info("Display controller stopped by user")
|
||||||
|
|||||||
Reference in New Issue
Block a user