Add some last-resort exception handling

This commit is contained in:
Nahuel Lofeudo 2023-10-08 15:27:35 +01:00
parent 35d0261682
commit 9f87527d9a
1 changed files with 48 additions and 42 deletions

90
main.py
View File

@ -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: def update_screen(config: Config, updates: list[ArrivalTime]) -> None:
""" Repaint the screen with the new arrival times """ """ Repaint the screen with the new arrival times """
updates = updates[0:LINE_COUNT] # take the first X lines try:
for line_num, update in enumerate(updates): updates = updates[0:LINE_COUNT] # take the first X lines
# Find what color we need to use for the ETA for line_num, update in enumerate(updates):
time_to_walk = update.due_in_minutes - (config.minutes_to_stop(update.stop_id) or 0) # Find what color we need to use for the ETA
lcd_color = None time_to_walk = update.due_in_minutes - (config.minutes_to_stop(update.stop_id) or 0)
if time_to_walk > 5: lcd_color = None
lcd_color = COLOR_LCD_GREEN if time_to_walk > 5:
elif time_to_walk > 1: lcd_color = COLOR_LCD_GREEN
lcd_color = COLOR_LCD_AMBER elif time_to_walk > 1:
else: lcd_color = COLOR_LCD_AMBER
lcd_color = COLOR_LCD_RED else:
lcd_color = COLOR_LCD_RED
# Draw the line # Draw the line
write_entry( write_entry(
line = line_num, line = line_num,
route = update.route_id, route = update.route_id,
destination = update.destination, destination = update.destination,
time_left = 'Due' if update.isDue() else update.due_in_str(), time_left = 'Due' if update.isDue() else update.due_in_str(),
time_color = lcd_color, time_color = lcd_color,
text_color = COLOR_LCD_GREEN if update.is_added else COLOR_LCD_AMBER text_color = COLOR_LCD_GREEN if update.is_added else COLOR_LCD_AMBER
) )
# Add the current time to the bottom line # Add the current time to the bottom line
datetime_text = "Current time: " + datetime.today().strftime("%d/%m/%Y %H:%M") datetime_text = "Current time: " + datetime.today().strftime("%d/%m/%Y %H:%M")
write_line(5, datetime_text) write_line(5, datetime_text)
except Exception as e:
print("Error updating screen: ", str(e))
def clear_screen() -> None: def clear_screen() -> None:
""" Clear screen """ """ Clear screen """
@ -148,29 +151,32 @@ def main():
# Main event loop # Main event loop
running = True running = True
while running: while running:
# Pygame event handling begins try:
if pygame.event.peek(): # Pygame event handling begins
for e in pygame.event.get(): if pygame.event.peek():
if e.type == pygame.QUIT: for e in pygame.event.get():
running = False if e.type == pygame.QUIT:
elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_ESCAPE:
running = False running = False
pygame.display.flip() elif e.type == pygame.KEYDOWN:
# Pygame event handling ends if e.key == pygame.K_ESCAPE:
running = False
pygame.display.flip()
# Pygame event handling ends
# Display update begins # Display update begins
schedule.run_pending() schedule.run_pending()
if update_queue.qsize() > 0: if update_queue.qsize() > 0:
clear_screen() clear_screen()
updates = update_queue.get() updates = update_queue.get()
update_screen(config, updates) update_screen(config, updates)
pygame.display.flip() pygame.display.flip()
gc.collect() gc.collect()
# Display update ends # Display update ends
sleep(0.2) sleep(0.2)
except Exception as e:
print("Exception in main loop: ", str(e))
pygame.quit() pygame.quit()
exit(0) exit(0)