GPIO buttons

This commit is contained in:
ohsqueezy 2022-12-13 16:56:33 -05:00
parent 617edd50e5
commit ac9c5cdf4c
5 changed files with 85 additions and 7 deletions

2
.gitmodules vendored
View File

@ -1,3 +1,3 @@
[submodule "lib/pgfw"]
path = lib/pgfw
url = makar:/var/www/git/pgfw
url = https://git.nugget.fun/nugget/pgfw

26
OPEN-GAME-KMS Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env python3
import sys, os
def ignore_sighup():
"""
Ignore hangup signal (that is thrown when launching from systemd?).
Taken from https://stackoverflow.com/questions/57205271/how-to-display-pygame-framebuffer-using-systemd-service
"""
import signal
def handler(signum, frame):
pass
signal.signal(signal.SIGHUP, handler)
# Change directory to the directory of the program launching the script (usually this script "OPEN-GAME").
if "--go-to-dir" in sys.argv:
os.chdir(os.path.dirname(sys.argv[0]))
# Use the KMS video driver. This works for newer versions of Raspberry Pi with the KMS overlay
# enabled, SDL 2, and Pygame 2.
os.putenv("SDL_VIDEODRIVER", "kmsdrm")
# ignore_sighup()
from electric_sieve.ElectricSieve import ElectricSieve
ElectricSieve().run()

View File

@ -3,6 +3,7 @@
from random import randint, randrange, choice
from time import time
from operator import itemgetter
import RPi.GPIO as GPIO
from pygame import Surface, PixelArray, Rect
from pygame.draw import aalines, polygon
@ -17,12 +18,30 @@ from lib.pgfw.pgfw.extension import render_box
class ElectricSieve(Game):
# The GPIO pins corresponding to the buttons and LED indicators
PIN_BUTTON_UP = 17
PIN_BUTTON_DOWN = 27
PIN_LED_UP = 22
PIN_LED_DOWN = 23
def __init__(self):
self.initialize_gpio()
Game.__init__(self)
self.background = Surface(self.display.screen.get_size())
self.background.fill((255, 80, 190))
self.title.activate()
def initialize_gpio(self):
"""
Set pin numbering mode to GPIO, initialize all buttons to input pullup.
"""
# Use GPIO numbering
GPIO.setmode(GPIO.BCM)
# Set all button pins to pullup
for pin in self.PIN_BUTTON_UP, self.PIN_BUTTON_DOWN:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def set_children(self):
Game.set_children(self)
self.title = Title(self)
@ -112,6 +131,8 @@ class Strip(Sprite):
def __init__(self, parent):
Sprite.__init__(self, parent)
GPIO.add_event_detect(ElectricSieve.PIN_BUTTON_UP, GPIO.BOTH, self.respond)
GPIO.add_event_detect(ElectricSieve.PIN_BUTTON_DOWN, GPIO.BOTH, self.respond)
self.deactivate()
self.display_surface = self.get_display_surface()
self.delegate = self.get_game().delegate
@ -130,12 +151,30 @@ class Strip(Sprite):
pass
def respond(self, event):
if type(event) == int:
pressed = "pressed" if GPIO.input(event) == GPIO.LOW else "released"
print(f"pin {event} {pressed}")
if self.active:
compare = self.delegate.compare
if compare(event, "left") or compare(event, "left", True):
self.hshifts[self.LEFT].active = not event.cancel
elif compare(event, "right") or compare(event, "right", True):
self.hshifts[self.RIGHT].active = not event.cancel
move = False
if type(event) == int:
if event == ElectricSieve.PIN_BUTTON_UP:
direction = self.LEFT
move = True
elif event == ElectricSieve.PIN_BUTTON_DOWN:
direction = self.RIGHT
move = True
active = GPIO.input(event) == GPIO.LOW
else:
compare = self.delegate.compare
if compare(event, "left") or compare(event, "left", True):
direction = self.LEFT
move = True
elif compare(event, "right") or compare(event, "right", True):
direction = self.RIGHT
move = True
active = not event.cancel
if move:
self.hshifts[direction].active = active
def activate(self):
self.active = True

@ -1 +1 @@
Subproject commit 314b722528a65e6f0053f8be1844c2d8818319f4
Subproject commit 24b5d7c6e7c7f17ec09ddb3de2127c092aa6f874

View File

@ -120,3 +120,16 @@
1529983381.92 171 BUG
1586137469.11 247 NUT
1669926143.6577697 5 ---
1670815336.2178595 112 ---
1670815414.8010118 21 ---
1670815999.6434624 147 FLA
1670816044.5578532 24 ---
1670816145.6957958 124 ---
1670816263.0361412 223 GUN
1670816338.4996192 28 ---
1670816435.1543665 105 ---
1670816534.3354058 102 ---
1670816639.158178 146 ---
1670817307.1141152 53 ---
1670819531.720299 203 FLA
1670819619.775536 96 ---