aboutsummaryrefslogtreecommitdiff
path: root/src/splitscreen_duo/__init__.py
blob: 8233caffcd7fd3d036de1b0605f75a2dc97d766f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from .game import Game
from . import menu
from .command import Command
import os
import logging
import pygame
from .serial import Serial
import json
from .games import benchmark, snake


def main() -> int:
    logger = logging.getLogger(__name__)

    pygame.init()

    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.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("command") == Command.QUIT.value:
                    logger.info("received quit command from primary, shutting down")
                    pygame.quit()

                    return 0
                elif message.get("command") == Command.SELECT_GAME.value:
                    if message.get("value") == Game.BENCHMARK.value:
                        logger.info("received benchmark game selection from primary")

                        game_result = benchmark.main_loop(menu.screen, serial)

                        if (
                            game_result
                            and game_result.get("command") == Command.QUIT.value
                        ):
                            pygame.quit()

                            return 0
                    elif message.get("value") == Game.SNAKE.value:
                        logger.info("received snake game selection from primary")

                        game_result = snake.main_loop(menu.screen, serial, INSTANCE)

                        if (
                            game_result
                            and game_result.get("command") == Command.QUIT.value
                        ):
                            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("command") == Command.QUIT.value:
                logger.info("primary sent quit, shutting down")
                pygame.quit()

                return 0
            elif serial_command.get("command") == Command.SELECT_GAME.value:
                if serial_command.get("value") == Game.BENCHMARK.value:
                    logger.info("starting benchmark game")

                    game_result = benchmark.main_loop(menu.screen, serial)

                    if game_result and game_result.get("command") == Command.QUIT.value:
                        pygame.quit()

                        return 0
                elif serial_command.get("value") == Game.SNAKE.value:
                    logger.info("starting snake game")

                    game_result = snake.main_loop(menu.screen, serial, INSTANCE)

                    if game_result and game_result.get("command") == Command.QUIT.value:
                        pygame.quit()

                        return 0

        menu.draw_menu()

    pygame.quit()

    return 0