Add some comments and reformat code

This commit is contained in:
Nahuel Lofeudo 2022-10-08 16:11:17 +01:00
parent e914a0630e
commit 34b80fd248
1 changed files with 21 additions and 5 deletions

26
main.py
View File

@ -36,12 +36,16 @@ dublinbus_client = DublinBusSoapClient(stops=STOPS, update_queue=update_queue, u
def get_line_offset(line: int) -> int: def get_line_offset(line: int) -> int:
""" Calculate the Y offset within the display for a given text line """
global font global font
return line * (font.get_height() + INTER_LINE_SPACE) return line * (font.get_height() + INTER_LINE_SPACE)
def write_entry(line: int, def write_entry(line: int,
route: str = '', destination: str = '', time_left: str = '', route: str = '', destination: str = '', time_left: str = '',
time_color: Color = COLOR_LCD, text_color: Color = COLOR_LCD): time_color: Color = COLOR_LCD, text_color: Color = COLOR_LCD):
""" Draws on the screen buffer an entry corresponding to an arrival time. """
# Step 1: Render the fragments # Step 1: Render the fragments
route_img = font.render(route[0:4], True, text_color) route_img = font.render(route[0:4], True, text_color)
destination_img = font.render(destination[0:21], True, text_color) destination_img = font.render(destination[0:21], True, text_color)
@ -53,6 +57,7 @@ def write_entry(line: int,
window.blit(destination_img, dest=(XOFFSET_DESTINATION, vertical_offset)) window.blit(destination_img, dest=(XOFFSET_DESTINATION, vertical_offset))
window.blit(time_left_img, dest=(XOFFSEET_TIME_LEFT, vertical_offset)) window.blit(time_left_img, dest=(XOFFSEET_TIME_LEFT, vertical_offset))
def update_screen(updates: list[ArrivalTime]) -> None: def update_screen(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 updates = updates[0:LINE_COUNT] # take the first X lines
@ -65,11 +70,14 @@ def update_screen(updates: list[ArrivalTime]) -> None:
time_color=COLOR_LCD time_color=COLOR_LCD
) )
def clear_screen() -> None: def clear_screen() -> None:
""" Clear screen """
pygame.draw.rect(surface=window, color=COLOR_BACKGROUND, width=0, rect=(0, 0, window.get_width(), window.get_height())) pygame.draw.rect(surface=window, color=COLOR_BACKGROUND, width=0, rect=(0, 0, window.get_width(), window.get_height()))
def init_screen(width: int, height: int) -> pygame.Surface: def init_screen(width: int, height: int) -> pygame.Surface:
""" Creates a Surface to draw on, with the given size, using either X11/Wayland (desktop) or directfb (no desktop) """ """ Create a Surface to draw on, with the given size, using either X11/Wayland (desktop) or directfb (no desktop) """
drivers = ['x11', 'directfb'] drivers = ['x11', 'directfb']
for driver in drivers: for driver in drivers:
print(f'Trying driver {driver}') print(f'Trying driver {driver}')
@ -83,11 +91,14 @@ def init_screen(width: int, height: int) -> pygame.Surface:
return window return window
raise Exception('No suitable video driver found!') raise Exception('No suitable video driver found!')
def main(): def main():
""" Main function """
global font global font
global window global window
""" Main method. Initialise graph """ """ Main method. Initialise graphics context """
pygame.init() pygame.init()
window = init_screen(1920, 720) window = init_screen(1920, 720)
pygame.font.init() pygame.font.init()
@ -101,6 +112,7 @@ def main():
# Main event loop # Main event loop
running = True running = True
while running: while running:
# Pygame event handling begins
if pygame.event.peek(): if pygame.event.peek():
for e in pygame.event.get(): for e in pygame.event.get():
print(e) print(e)
@ -109,15 +121,19 @@ def main():
elif e.type == pygame.KEYDOWN: elif e.type == pygame.KEYDOWN:
if e.key == pygame.K_ESCAPE: if e.key == pygame.K_ESCAPE:
running = False running = False
#end event handling pygame.display.flip()
# Pygame event handling ends
# Display update begins
if update_queue.qsize() > 0: if update_queue.qsize() > 0:
clear_screen() clear_screen()
updates = update_queue.get() updates = update_queue.get()
update_screen(updates) update_screen(updates)
pygame.display.flip() pygame.display.flip()
# Display update ends
sleep(0.2) sleep(0.2)
pygame.quit() pygame.quit()
exit(0) exit(0)