diff options
| author | Zoltan Szabatin <[email protected]> | 2025-03-02 17:41:20 -0800 |
|---|---|---|
| committer | Zoltan Szabatin <[email protected]> | 2025-03-02 17:41:20 -0800 |
| commit | 47ce41308a2c9ff65c8b59d641ddde19134fb167 (patch) | |
| tree | 98ce7f3771dc7a6e5e8bf494090811576907cc06 /src/splitscreen_duo/__init__.py | |
| parent | refactor: Move serial and Pygame definitions up in hierarchy (diff) | |
| download | splitscreen-duo-47ce41308a2c9ff65c8b59d641ddde19134fb167.tar.xz splitscreen-duo-47ce41308a2c9ff65c8b59d641ddde19134fb167.zip | |
refactor: Move serial controller up hierarchy
Diffstat (limited to 'src/splitscreen_duo/__init__.py')
| -rw-r--r-- | src/splitscreen_duo/__init__.py | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/src/splitscreen_duo/__init__.py b/src/splitscreen_duo/__init__.py index 6a93cbd..90d54ab 100644 --- a/src/splitscreen_duo/__init__.py +++ b/src/splitscreen_duo/__init__.py @@ -3,7 +3,7 @@ import os import logging import pygame from .serial import Serial - +import json def main() -> int: logger = logging.getLogger(__name__) @@ -18,7 +18,45 @@ def main() -> int: 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(serial, INSTANCE) + logger.info(f"running as {os.getenv("INSTANCE", "unknown")}") + menu.init_display() + + is_running = True + + while is_running: + if INSTANCE == "secondary" and serial.in_waiting() > 0: + try: + data = serial.readline().decode("utf-8").strip() + + logger.debug(f"received serial data: {data}") + + message = json.loads(data) + + if message.get("action") == "QUIT_ALL": + logger.info("received quit_all command from primary, shutting down") + pygame.quit() + + return 0 + + except json.JSONDecodeError: + logger.error("failed to decode serial message") + + except Exception as e: + logger.error(f"error processing serial data: {e}") + + serial_command = menu.process_events(serial, INSTANCE) + + if serial_command: + serial.write(json.dumps(serial_command).encode("utf-8")) + + if serial_command.get("action") == "QUIT_ALL": + logger.info("primary sent quit_all, shutting down") + pygame.quit() + + return 0 + + menu.draw_menu() + + pygame.quit() return 0 |