From 51165f5af9fa8f05ba06b50a29b9fc1f10f3fde3 Mon Sep 17 00:00:00 2001 From: frank Date: Wed, 30 Nov 2022 19:57:01 -0500 Subject: [PATCH] optionally set SDL video driver to KMS, set GPIO thread to daemon mode --- .gitignore | 1 + NS.py | 22 +++++++++++----------- OPEN-GAME | 7 ++++++- config | 8 ++++---- gpio.py | 2 ++ 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index e06445a..5e7a9e7 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ include/ lib/python3.9/ pyvenv.cfg lib/python* +stat/ diff --git a/NS.py b/NS.py index 48b577a..383581a 100644 --- a/NS.py +++ b/NS.py @@ -27,7 +27,6 @@ from pygame.mixer import Sound from pygame.image import load, fromstring from pygame.transform import rotate, flip, scale, smoothscale from pygame.time import get_ticks -from pygame.font import Font from pygame.draw import aalines, lines from pygame.gfxdraw import aapolygon, arc, polygon, aaellipse, ellipse, filled_ellipse, filled_circle from pygame.locals import * @@ -191,7 +190,7 @@ class NS(Game, Animation): }, "input": { - "bool": "serial" + "bool": ["serial", "pi"] }, "display": { @@ -243,9 +242,10 @@ class NS(Game, Animation): # Initialize GPIO interface gpio.initialize_gpio() - # Launch a separate thread for reading the GPIO (and allowing its custom delays/sleeps) + # Launch a separate thread for reading the GPIO (and allowing its custom delays/sleeps). Use the daemon flag to force + # exit when the main thread is killed (by a sigterm from systemctl stop) (?). self.gpio_kill = False - self.gpio_thread = Thread(target=self.read_gpio) + self.gpio_thread = Thread(target=self.read_gpio, daemon=True) self.gpio_thread.start() # init Arduino @@ -1114,7 +1114,7 @@ class Title(Animation): if ii == 0 or ii == 8: y = 20 - font = Font(self.get_resource(Dialogue.FONT_PATH), 18) + font = pygame.font.Font(self.get_resource(Dialogue.FONT_PATH), 18) # Parse both strings and score objects if isinstance(entry, NS.Score): @@ -1269,7 +1269,7 @@ class Dialogue(Animation): self.avatar.location.center = self.avatar_box.location.center def set_name(self, text): - font = Font(self.get_resource(self.FONT_PATH), self.FONT_SIZE) + font = pygame.font.Font(self.get_resource(self.FONT_PATH), self.FONT_SIZE) self.name = Sprite(self) self.name.add_frame(font.render(text, True, self.TEXT_COLOR).convert_alpha()) self.name.location.midleft = self.name_box.location.left + 5, self.name_box.location.centery @@ -1298,7 +1298,7 @@ class Dialogue(Animation): self.name_box.update() self.name.update() self.text_box.update() - font = Font(self.get_resource(self.FONT_PATH), self.FONT_SIZE) + font = pygame.font.Font(self.get_resource(self.FONT_PATH), self.FONT_SIZE) message = Sprite(self) lines = self.full_text[:self.text_index].split("\n") frame = Surface((self.text_box.location.w - 10, 30 * len(lines)), SRCALPHA) @@ -1330,7 +1330,7 @@ class SkipPrompt(GameChild): self.buttons[-1].location.centery) left += self.buttons[-1].location.width + AdvancePrompt.BUTTON_SPACING self.text = Sprite(self) - font = Font(self.get_resource(Dialogue.FONT_PATH), 18) + font = pygame.font.Font(self.get_resource(Dialogue.FONT_PATH), 18) self.text.add_frame(font.render("TO SKIP", True, (0, 0, 0)).convert_alpha()) self.text.location.midleft = ( self.buttons[2].location.right + 5, @@ -2807,7 +2807,7 @@ class Countdown(GameChild): def __init__(self, parent): GameChild.__init__(self, parent) dsr = self.get_display_surface().get_rect() - font = Font(self.get_resource(Dialogue.FONT_PATH), 76) + font = pygame.font.Font(self.get_resource(Dialogue.FONT_PATH), 76) self.heading = Sprite(self) self.heading.add_frame(font.render("CONTINUE?", True, (0, 0, 0), (255, 255, 255)).convert_alpha()) self.heading.location.midtop = dsr.centerx, 50 @@ -3141,8 +3141,8 @@ class Ending(Animation): Animation.__init__(self, parent) self.slime_bag = Chemtrails(self) self.tony_avatar = load(self.get_resource("Introduction_tony_avatar.png")).convert() - self.time_font = Font(self.get_resource("rounded-mplus-1m-bold.ttf"), 64) - self.rank_font = Font(self.get_resource("rounded-mplus-1m-bold.ttf"), 26) + self.time_font = pygame.font.Font(self.get_resource("rounded-mplus-1m-bold.ttf"), 64) + self.rank_font = pygame.font.Font(self.get_resource("rounded-mplus-1m-bold.ttf"), 26) self.register(self.start, self.start_wipe) self.register(self.append_sword, interval=1500) self.swords = [] diff --git a/OPEN-GAME b/OPEN-GAME index 0729806..ac1f414 100755 --- a/OPEN-GAME +++ b/OPEN-GAME @@ -29,11 +29,16 @@ if "--go-to-dir" in sys.argv: # Use the framebuffer display (for Raspberry Pi). This only works with Pygame 1.9.6 (and SDL 1.2). if "--fb" in sys.argv: - import os os.putenv("SDL_VIDEODRIVER", "fbcon") os.putenv("SDL_FBDEV", "/dev/fb0") ignore_sighup() +# Use the KMS video driver. This works for newer versions of Raspberry Pi with the KMS overlay +# enabled, SDL 2, and Pygame 2. +if "--kms" in sys.argv: + os.putenv("SDL_VIDEODRIVER", "kmsdrm") + ignore_sighup() + from NS import NS NS().run() diff --git a/config b/config index 209f6e0..afafbd6 100644 --- a/config +++ b/config @@ -22,7 +22,7 @@ data-exclude = local/, *.pyc, .git*, README, build/, dist/, *.egg-info, *.py, MA [display] caption = Scrapeboard -show-framerate = no +show-framerate = yes dimensions = 800, 450 fullscreen = no attract-gif-alpha = 1.0 @@ -60,9 +60,9 @@ volume = 1.0 [input] buffer = 0 arduino-port = /dev/ttyACM0 -serial = False -pi = True -confirm-quit = False +serial = no +pi = no +confirm-quit = no [time] timer-max-time = 10000 diff --git a/gpio.py b/gpio.py index 666fcf9..a22fca6 100644 --- a/gpio.py +++ b/gpio.py @@ -10,6 +10,8 @@ # inputs on the Raspberry Pi for detecting the pads in Scrapeboard. # # When run as a script, it prints connections detected between the input GPIO pins. +# +# Original algorithm by Dr. Clement Shimizu is translated from Arduino code in serial2.ino import time, itertools, argparse import RPi.GPIO as GPIO