2023-05-12 11:20:00 -04:00
|
|
|
"""Handle inputs from the GPIO pins on the Raspberry Pi and converting them to events to be used in other places (
|
|
|
|
pygame, etc.)"""
|
2023-05-11 17:59:06 -04:00
|
|
|
import importlib.util
|
|
|
|
|
|
|
|
# If the RPi.GPIO module is not found (aka the program is not running on a Pi), import the fake
|
|
|
|
# GPIO module instead to prevent a crash.
|
2023-05-12 11:20:00 -04:00
|
|
|
|
2023-05-12 23:36:21 -04:00
|
|
|
#ON_HARDWARE = None # Flag to tell other methods if the program is running on hardware or not
|
|
|
|
import pocket_friends.game_files.io.fake_gpio as GPIO
|
|
|
|
ON_HARDWARE = False
|
2023-05-11 17:59:06 -04:00
|
|
|
|
|
|
|
# Dictionary of all the buttons used and what their corresponding GPIO codes are
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def setup():
|
2023-05-12 11:20:00 -04:00
|
|
|
"""Prime the GPIO pins for reading the inputs of the buttons."""
|
2023-05-11 17:59:06 -04:00
|
|
|
GPIO.setmode(GPIO.BOARD)
|
|
|
|
|
|
|
|
GPIO.setup(BUTTONS.get('a'), GPIO.IN)
|
|
|
|
GPIO.setup(BUTTONS.get('b'), GPIO.IN)
|
|
|
|
GPIO.setup(BUTTONS.get('j_i'), GPIO.IN)
|
|
|
|
GPIO.setup(BUTTONS.get('j_u'), GPIO.IN)
|
|
|
|
GPIO.setup(BUTTONS.get('j_d'), GPIO.IN)
|
|
|
|
GPIO.setup(BUTTONS.get('j_l'), GPIO.IN)
|
|
|
|
GPIO.setup(BUTTONS.get('j_r'), GPIO.IN)
|
|
|
|
|
|
|
|
GPIO.add_event_detect(BUTTONS.get('a'), GPIO.FALLING)
|
|
|
|
GPIO.add_event_detect(BUTTONS.get('b'), GPIO.FALLING)
|
|
|
|
GPIO.add_event_detect(BUTTONS.get('j_i'), GPIO.FALLING)
|
|
|
|
GPIO.add_event_detect(BUTTONS.get('j_u'), GPIO.FALLING)
|
|
|
|
GPIO.add_event_detect(BUTTONS.get('j_d'), GPIO.FALLING)
|
|
|
|
GPIO.add_event_detect(BUTTONS.get('j_l'), GPIO.FALLING)
|
|
|
|
GPIO.add_event_detect(BUTTONS.get('j_r'), GPIO.FALLING)
|
|
|
|
|
|
|
|
|
|
|
|
def teardown():
|
2023-05-12 11:20:00 -04:00
|
|
|
"""Clean up the GPIO handler."""
|
2023-05-11 17:59:06 -04:00
|
|
|
GPIO.cleanup()
|
|
|
|
|
|
|
|
|
|
|
|
def get_press(button):
|
|
|
|
"""
|
2023-05-12 11:20:00 -04:00
|
|
|
Checks if a given button code has been pressed and returns a bool depending on the state.
|
|
|
|
Args:
|
|
|
|
button: button to be detected
|
|
|
|
|
|
|
|
Returns: True if the button is has been pressed, False otherwise
|
|
|
|
|
2023-05-11 17:59:06 -04:00
|
|
|
"""
|
|
|
|
return GPIO.event_detected(button)
|