diff options
| author | Zoltan Szabatin <[email protected]> | 2025-03-02 17:32:55 -0800 |
|---|---|---|
| committer | Zoltan Szabatin <[email protected]> | 2025-03-02 17:33:19 -0800 |
| commit | e32619cfdf5103625e91a67b3b5e175cef43f662 (patch) | |
| tree | e254698693f96fa7861399ce6ab32b7ea1391abd /src | |
| parent | style(src): Format files (diff) | |
| download | splitscreen-duo-e32619cfdf5103625e91a67b3b5e175cef43f662.tar.xz splitscreen-duo-e32619cfdf5103625e91a67b3b5e175cef43f662.zip | |
refactor: Move serial and Pygame definitions up in hierarchy
Diffstat (limited to 'src')
| -rw-r--r-- | src/splitscreen_duo/__init__.py | 13 | ||||
| -rw-r--r-- | src/splitscreen_duo/menu.py | 39 |
2 files changed, 29 insertions, 23 deletions
diff --git a/src/splitscreen_duo/__init__.py b/src/splitscreen_duo/__init__.py index aa0aeda..6a93cbd 100644 --- a/src/splitscreen_duo/__init__.py +++ b/src/splitscreen_duo/__init__.py @@ -1,17 +1,24 @@ from . import menu import os import logging +import pygame +from .serial import Serial -logger = logging.getLogger(__name__) +def main() -> int: + logger = logging.getLogger(__name__) + pygame.init() -def main() -> int: + serial = Serial(os.getenv("SERIAL_DEVICE", "/dev/null"), 115200) + INSTANCE = os.getenv("INSTANCE", "Unknown") + + pygame.display.set_caption(f"SplitScreen Duo Menu ({INSTANCE})") logging.basicConfig( level=(logging.DEBUG if os.getenv("DEVELOPMENT", "").lower() else logging.INFO) ) print("The Dual Screen Console") logger.info(f"Running as {os.getenv("INSTANCE", "unknown")}") - menu.main_loop() + menu.main_loop(serial, INSTANCE) return 0 diff --git a/src/splitscreen_duo/menu.py b/src/splitscreen_duo/menu.py index d552dab..8962c5d 100644 --- a/src/splitscreen_duo/menu.py +++ b/src/splitscreen_duo/menu.py @@ -2,33 +2,31 @@ import sys import pygame import os from .input import Input -from .serial import Serial import logging import json -INSTANCE = os.getenv("INSTANCE", "Unknown") - -pygame.init() -pygame.display.set_caption(f"SplitScreen Duo Menu ({INSTANCE})") - -WIDTH, HEIGHT = ( - pygame.display.Info().current_w // 2, - pygame.display.Info().current_h // 2, -) WHITE = (255, 255, 255) BLACK = (0, 0, 0) -FONT = pygame.font.Font(None, 36) IS_DEVELOPMENT_MODE = os.getenv("DEVELOPMENT", "").lower() != "" OPTIONS = ["Breakout", "Pong v.s. Computer", "Snake", "Quit"] -selected_index = 0 -screen = pygame.display.set_mode( - (WIDTH, HEIGHT) if IS_DEVELOPMENT_MODE else (0, 0), - 0 if IS_DEVELOPMENT_MODE else pygame.FULLSCREEN, -) -serial = Serial(os.getenv("SERIAL_DEVICE", "/dev/null"), 115200) logger = logging.getLogger(__name__) +def init_display(): + global WIDTH, HEIGHT, FONT, screen, selected_index + + WIDTH, HEIGHT = ( + pygame.display.Info().current_w // 2, + pygame.display.Info().current_h // 2, + ) + FONT = pygame.font.Font(None, 36) + screen = pygame.display.set_mode( + (WIDTH, HEIGHT) if IS_DEVELOPMENT_MODE else (0, 0), + 0 if IS_DEVELOPMENT_MODE else pygame.FULLSCREEN, + ) + selected_index = 0 + + def draw_menu(): screen.fill(WHITE) @@ -40,7 +38,9 @@ def draw_menu(): pygame.display.flip() -def main_loop(): +def main_loop(serial, instance): + init_display() + global selected_index is_running = True @@ -49,7 +49,7 @@ def main_loop(): while is_running: draw_menu() - if INSTANCE == "secondary": + if instance == "secondary": if serial.in_waiting() > 0: logger.debug(serial.readline().decode("utf-8").strip()) @@ -69,7 +69,6 @@ def main_loop(): pygame.quit() sys.exit() else: - print(OPTIONS[selected_index]) serial.write( json.dumps({"command": 0, "action": action}).encode("utf-8") ) |