aboutsummaryrefslogtreecommitdiff
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
parentfeat: Add Pygame menu (diff)
downloadsplitscreen-duo-bdcc991a5a3bec5e136be97b14de8e51939f3652.tar.xz
splitscreen-duo-bdcc991a5a3bec5e136be97b14de8e51939f3652.zip
feat: Add serial interface
-rw-r--r--justfile12
-rw-r--r--pyproject.toml1
-rw-r--r--requirements-dev.lock2
-rw-r--r--requirements.lock2
-rw-r--r--src/splitscreen_duo/__init__.py4
-rw-r--r--src/splitscreen_duo/menu.py16
-rw-r--r--src/splitscreen_duo/serial.py21
7 files changed, 49 insertions, 9 deletions
diff --git a/justfile b/justfile
index 621ae68..dfff4c9 100644
--- a/justfile
+++ b/justfile
@@ -1,12 +1,14 @@
default:
just --list
-create_serial_port:
- socat -d -d pty,raw,echo=0 pty,raw,echo=0 &
+create_serial_devices:
+ socat -d -d pty,raw,echo=0,link=/tmp/primary-serial pty,raw,echo=0,link=/tmp/secondary-serial &> /dev/null &
-kill_serial_port:
+kill_serial_devices:
pkill socat
run:
- DEVELOPMENT=1 INSTANCE=primary rye run splitscreen_duo &
- DEVELOPMENT=1 INSTANCE=secondary rye run splitscreen_duo &
+ just create_serial_devices
+ SERIAL_DEVICE=/tmp/primary-serial DEVELOPMENT=1 INSTANCE=primary rye run splitscreen_duo &
+ SERIAL_DEVICE=/tmp/secondary-serial DEVELOPMENT=1 INSTANCE=secondary rye run splitscreen_duo
+ just kill_serial_devices
diff --git a/pyproject.toml b/pyproject.toml
index f074f88..d81b3e1 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -9,6 +9,7 @@ authors = [
]
dependencies = [
"pygame>=2.6.1",
+ "pyserial>=3.5",
]
readme = "README.md"
requires-python = ">= 3.8"
diff --git a/requirements-dev.lock b/requirements-dev.lock
index 5a0b09c..c1bd1f5 100644
--- a/requirements-dev.lock
+++ b/requirements-dev.lock
@@ -12,3 +12,5 @@
-e file:.
pygame==2.6.1
# via splitscreen-duo
+pyserial==3.5
+ # via splitscreen-duo
diff --git a/requirements.lock b/requirements.lock
index 5a0b09c..c1bd1f5 100644
--- a/requirements.lock
+++ b/requirements.lock
@@ -12,3 +12,5 @@
-e file:.
pygame==2.6.1
# via splitscreen-duo
+pyserial==3.5
+ # via splitscreen-duo
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
+