moved around structure

This commit is contained in:
2023-05-11 14:08:22 -04:00
parent ca9406aab8
commit bbf549c024
6 changed files with 7 additions and 7 deletions

View File

@@ -0,0 +1,56 @@
"""
Module used to fake the RPi.GPIO module so that
the hardware can be run without the actual hardware.
"""
# Constants used by RPi.GPIO
BOARD = 0
IN = 0
FALLING = 0
def setmode(new_mode):
"""
Fake setmode function.
:param new_mode:
"""
pass
def setup(channel, mode, initial=None, pull_up_down=None):
"""
Fake setup function.
:param channel:
:param mode:
:param initial:
:param pull_up_down:
"""
pass
def add_event_detect(channel, edge_type, callback=None, bouncetime=0):
"""
Fake function to add a non-existent event detect.
:param channel:
:param edge_type:
:param callback:
:param bouncetime:
"""
pass
def event_detected(channel):
"""
Fake function to detect an event. Always returns false.
:param channel:
:return:
"""
return False
def cleanup(channel=None):
"""
Fake cleanup function.
:param channel:
"""
pass

View File

@@ -0,0 +1,71 @@
"""
Module that helps with the handling of taking inputs from the GPIO pins on the Raspberry
Pi and converting them to events to be used in other places (pygame, etc.)
"""
import importlib.util
try:
importlib.util.find_spec('RPi.GPIO')
import RPi.GPIO as GPIO
except ImportError:
import pocket_friends.game_files.io_helpers.fake_gpio as GPIO
class Constants:
"""
Contains the constants used by the HAT to read in buttons
"""
buttons = {
'a': 31, # A button
'b': 29, # B button
'j_i': 7, # Joystick in
'j_u': 11, # Joystick up
'j_d': 15, # Joystick down
'j_l': 13, # Joystick left
'j_r': 16 # Joystick right
}
class GPIOHandler:
"""
Class to handle the GPIO inputs from the buttons.
"""
@staticmethod
def setup():
"""
Primes the GPIO pins for reading the inputs of the buttons.
"""
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Constants.buttons.get('a'), GPIO.IN)
GPIO.setup(Constants.buttons.get('b'), GPIO.IN)
GPIO.setup(Constants.buttons.get('j_i'), GPIO.IN)
GPIO.setup(Constants.buttons.get('j_u'), GPIO.IN)
GPIO.setup(Constants.buttons.get('j_d'), GPIO.IN)
GPIO.setup(Constants.buttons.get('j_l'), GPIO.IN)
GPIO.setup(Constants.buttons.get('j_r'), GPIO.IN)
GPIO.add_event_detect(Constants.buttons.get('a'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('b'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('j_i'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('j_u'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('j_d'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('j_l'), GPIO.FALLING)
GPIO.add_event_detect(Constants.buttons.get('j_r'), GPIO.FALLING)
@staticmethod
def teardown():
"""
Cleans up the GPIO handler.
"""
GPIO.cleanup()
@staticmethod
def get_press(button):
"""
Returns true if a button has moved from not pressed to pressed.
:param button: button to be detected
:return: True if the button is has been pressed, False otherwise
"""
return GPIO.event_detected(button)

View File

@@ -1,6 +1,6 @@
import pygame
import importlib.util
from pocket_friends.hardware.gpio_handler import Constants, GPIOHandler
from pocket_friends.game_files.io_helpers.gpio_handler import Constants, GPIOHandler
class InputHandler:
@@ -11,7 +11,7 @@ class InputHandler:
import RPi.GPIO as GPIO
self.on_hardware = True
except ImportError:
import pocket_friends.development.FakeGPIO as GPIO
import pocket_friends.game_files.io_helpers.fake_gpio as GPIO
self.on_hardware = False
self.last_input_tick = 0

View File

@@ -1,6 +1,6 @@
import pygame
from . import sprites
from pocket_friends.hardware.gpio_handler import Constants
from pocket_friends.game_files.io_helpers.gpio_handler import Constants
from ..io_helpers.input_handler import InputHandler