aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZoltan Szabatin <[email protected]>2025-02-25 16:12:49 -0800
committerZoltan Szabatin <[email protected]>2025-02-25 16:12:49 -0800
commitbdcc991a5a3bec5e136be97b14de8e51939f3652 (patch)
tree9433cf8c24a447d88ec19077c4a2966ef511783c /src
parentfeat: Add Pygame menu (diff)
downloadsplitscreen-duo-bdcc991a5a3bec5e136be97b14de8e51939f3652.tar.xz
splitscreen-duo-bdcc991a5a3bec5e136be97b14de8e51939f3652.zip
feat: Add serial interface
Diffstat (limited to 'src')
-rw-r--r--src/splitscreen_duo/__init__.py4
-rw-r--r--src/splitscreen_duo/menu.py16
-rw-r--r--src/splitscreen_duo/serial.py21
3 files changed, 37 insertions, 4 deletions
diff --git a/src/splitscreen_duo/__init__.py b/src/splitscreen_duo/__init__.py
index 4be2b6f..84c86f9 100644
--- a/src/splitscreen_duo/__init__.py
+++ b/src/splitscreen_duo/__init__.py
@@ -9,11 +9,11 @@ logger = logging.getLogger(__name__)
def main() -> int:
logging.basicConfig(
level=(
- logging.DEBUG if os.environ.get("DEVELOPMENT", "").lower() else logging.INFO
+ logging.DEBUG if os.getenv("DEVELOPMENT", "").lower() else logging.INFO
)
)
print("The Dual Screen Console")
- logger.info(f"Running as {os.environ.get("INSTANCE", "unknown")}")
+ logger.info(f"Running as {os.getenv("INSTANCE", "unknown")}")
menu.main_loop()
return 0
diff --git a/src/splitscreen_duo/menu.py b/src/splitscreen_duo/menu.py
index 489a75c..de51a47 100644
--- a/src/splitscreen_duo/menu.py
+++ b/src/splitscreen_duo/menu.py
@@ -2,9 +2,14 @@ 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("SplitScreen Duo Menu")
+pygame.display.set_caption(f"SplitScreen Duo Menu ({INSTANCE})")
WIDTH, HEIGHT = (
pygame.display.Info().current_w // 2,
@@ -13,13 +18,15 @@ WIDTH, HEIGHT = (
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
FONT = pygame.font.Font(None, 36)
-IS_DEVELOPMENT_MODE = os.environ.get("DEVELOPMENT", "").lower()
+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 draw_menu():
@@ -42,6 +49,10 @@ def main_loop():
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()
@@ -59,6 +70,7 @@ def main_loop():
sys.exit()
else:
print(OPTIONS[selected_index])
+ serial.write(json.dumps({"command": 0, "action": action}).encode("utf-8"))
elif action == "QUIT":
pygame.quit()
sys.exit()
diff --git a/src/splitscreen_duo/serial.py b/src/splitscreen_duo/serial.py
new file mode 100644
index 0000000..bc9caec
--- /dev/null
+++ b/src/splitscreen_duo/serial.py
@@ -0,0 +1,21 @@
+import serial
+
+
+class Serial:
+ def __init__(self, port, baudrate=115200):
+ self.port = port
+ self.baudrate = baudrate
+ self.serial = serial.Serial(port, baudrate, timeout=0.1)
+
+ def read(self):
+ return self.serial.read()
+
+ def write(self, data):
+ self.serial.write(data)
+
+ def readline(self):
+ return self.serial.readline()
+
+ def in_waiting(self):
+ return self.serial.in_waiting
+