aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZoltan Szabatin <[email protected]>2025-03-02 17:41:20 -0800
committerZoltan Szabatin <[email protected]>2025-03-02 17:41:20 -0800
commit47ce41308a2c9ff65c8b59d641ddde19134fb167 (patch)
tree98ce7f3771dc7a6e5e8bf494090811576907cc06 /src
parentrefactor: Move serial and Pygame definitions up in hierarchy (diff)
downloadsplitscreen-duo-47ce41308a2c9ff65c8b59d641ddde19134fb167.tar.xz
splitscreen-duo-47ce41308a2c9ff65c8b59d641ddde19134fb167.zip
refactor: Move serial controller up hierarchy
Diffstat (limited to 'src')
-rw-r--r--src/splitscreen_duo/__init__.py44
-rw-r--r--src/splitscreen_duo/menu.py50
2 files changed, 62 insertions, 32 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
diff --git a/src/splitscreen_duo/menu.py b/src/splitscreen_duo/menu.py
index 8962c5d..7934696 100644
--- a/src/splitscreen_duo/menu.py
+++ b/src/splitscreen_duo/menu.py
@@ -10,6 +10,7 @@ BLACK = (0, 0, 0)
IS_DEVELOPMENT_MODE = os.getenv("DEVELOPMENT", "").lower() != ""
OPTIONS = ["Breakout", "Pong v.s. Computer", "Snake", "Quit"]
logger = logging.getLogger(__name__)
+WIDTH = HEIGHT = FONT = screen = selected_index = None
def init_display():
@@ -38,40 +39,31 @@ def draw_menu():
pygame.display.flip()
-def main_loop(serial, instance):
- init_display()
-
+def process_events(serial, instance):
global selected_index
- is_running = True
input_handler = Input(debug=IS_DEVELOPMENT_MODE)
- while is_running:
- draw_menu()
-
- if instance == "secondary":
- if serial.in_waiting() > 0:
- logger.debug(serial.readline().decode("utf-8").strip())
-
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT:
+ return {"command": 0, "action": "QUIT_ALL"}
- action = input_handler.get_input(event)
+ action = input_handler.get_input(event)
- if action == "UP":
- selected_index = (selected_index - 1) % len(OPTIONS)
- elif action == "DOWN":
- selected_index = (selected_index + 1) % len(OPTIONS)
- elif action == "SELECT":
- if OPTIONS[selected_index] == "Quit":
+ if action == "UP":
+ selected_index = (selected_index - 1) % len(OPTIONS)
+ elif action == "DOWN":
+ selected_index = (selected_index + 1) % len(OPTIONS)
+ elif action == "SELECT":
+ if OPTIONS[selected_index] == "Quit":
+ if instance == "primary":
+ return {"command": 0, "action": "QUIT_ALL"}
+ else:
pygame.quit()
sys.exit()
- else:
- serial.write(
- json.dumps({"command": 0, "action": action}).encode("utf-8")
- )
- elif action == "QUIT":
- pygame.quit()
- sys.exit()
+ else:
+ return {"command": 0, "action": OPTIONS[selected_index]}
+ elif action == "QUIT":
+ return {"command": 0, "action": "QUIT_ALL"}
+
+ return None