From 9f87527d9a2ec8ddd862faeb0901f5879486e387 Mon Sep 17 00:00:00 2001 From: Nahuel Lofeudo Date: Sun, 8 Oct 2023 15:27:35 +0100 Subject: [PATCH] Add some last-resort exception handling --- main.py | 90 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 42 deletions(-) diff --git a/main.py b/main.py index 87c24ba..97fc90a 100755 --- a/main.py +++ b/main.py @@ -72,31 +72,34 @@ def write_line(line: int, text: str, text_color: Color = COLOR_LCD_AMBER): def update_screen(config: Config, updates: list[ArrivalTime]) -> None: """ Repaint the screen with the new arrival times """ - updates = updates[0:LINE_COUNT] # take the first X lines - for line_num, update in enumerate(updates): - # Find what color we need to use for the ETA - time_to_walk = update.due_in_minutes - (config.minutes_to_stop(update.stop_id) or 0) - lcd_color = None - if time_to_walk > 5: - lcd_color = COLOR_LCD_GREEN - elif time_to_walk > 1: - lcd_color = COLOR_LCD_AMBER - else: - lcd_color = COLOR_LCD_RED + try: + updates = updates[0:LINE_COUNT] # take the first X lines + for line_num, update in enumerate(updates): + # Find what color we need to use for the ETA + time_to_walk = update.due_in_minutes - (config.minutes_to_stop(update.stop_id) or 0) + lcd_color = None + if time_to_walk > 5: + lcd_color = COLOR_LCD_GREEN + elif time_to_walk > 1: + lcd_color = COLOR_LCD_AMBER + else: + lcd_color = COLOR_LCD_RED - # Draw the line - write_entry( - line = line_num, - route = update.route_id, - destination = update.destination, - time_left = 'Due' if update.isDue() else update.due_in_str(), - time_color = lcd_color, - text_color = COLOR_LCD_GREEN if update.is_added else COLOR_LCD_AMBER - ) + # Draw the line + write_entry( + line = line_num, + route = update.route_id, + destination = update.destination, + time_left = 'Due' if update.isDue() else update.due_in_str(), + time_color = lcd_color, + text_color = COLOR_LCD_GREEN if update.is_added else COLOR_LCD_AMBER + ) - # Add the current time to the bottom line - datetime_text = "Current time: " + datetime.today().strftime("%d/%m/%Y %H:%M") - write_line(5, datetime_text) + # Add the current time to the bottom line + datetime_text = "Current time: " + datetime.today().strftime("%d/%m/%Y %H:%M") + write_line(5, datetime_text) + except Exception as e: + print("Error updating screen: ", str(e)) def clear_screen() -> None: """ Clear screen """ @@ -148,29 +151,32 @@ def main(): # Main event loop running = True while running: - # Pygame event handling begins - if pygame.event.peek(): - for e in pygame.event.get(): - if e.type == pygame.QUIT: - running = False - elif e.type == pygame.KEYDOWN: - if e.key == pygame.K_ESCAPE: + try: + # Pygame event handling begins + if pygame.event.peek(): + for e in pygame.event.get(): + if e.type == pygame.QUIT: running = False - pygame.display.flip() - # Pygame event handling ends + elif e.type == pygame.KEYDOWN: + if e.key == pygame.K_ESCAPE: + running = False + pygame.display.flip() + # Pygame event handling ends - # Display update begins - schedule.run_pending() - if update_queue.qsize() > 0: - clear_screen() - updates = update_queue.get() - update_screen(config, updates) + # Display update begins + schedule.run_pending() + if update_queue.qsize() > 0: + clear_screen() + updates = update_queue.get() + update_screen(config, updates) - pygame.display.flip() - gc.collect() - # Display update ends + pygame.display.flip() + gc.collect() + # Display update ends - sleep(0.2) + sleep(0.2) + except Exception as e: + print("Exception in main loop: ", str(e)) pygame.quit() exit(0)