removed gpio modules (rest in peace)

This commit is contained in:
Nicholas Dyer 2023-05-13 10:46:34 -04:00
parent 35ac1cff51
commit 0f69120213
6 changed files with 19 additions and 172 deletions

View File

@ -1,75 +0,0 @@
"""
Dummy module that contains empty functions and variables that exist in RPi.GPIO. This module is to be imported
if importing RPi.GPIO fails.
"""
# Constants used by RPi.GPIO
BOARD = 0
IN = 0
FALLING = 0
def setmode(new_mode):
"""
Fake setmode function.
Args:
new_mode:
Returns:
"""
pass
def setup(channel, mode, initial=None, pull_up_down=None):
"""
Fake setup function.
Args:
channel:
mode:
initial:
pull_up_down:
Returns:
"""
pass
def add_event_detect(channel, edge_type, callback=None, bouncetime=0):
"""
Fake function to add a non-existent event detect.
Args:
channel:
edge_type:
callback:
bouncetime:
Returns:
"""
pass
def event_detected(channel):
"""
Fake function to detect an event. Always returns false.
Args:
channel:
Returns:
"""
return False
def cleanup(channel=None):
"""
Fake cleanup function.
Args:
channel:
Returns:
"""
pass

View File

@ -1,59 +0,0 @@
"""Handle 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
# 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.
#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
# 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():
"""Prime the GPIO pins for reading the inputs of the buttons."""
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():
"""Clean up the GPIO handler."""
GPIO.cleanup()
def get_press(button):
"""
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
"""
return GPIO.event_detected(button)

View File

@ -1,6 +1,4 @@
import pygame
import pocket_friends.game_files.io.gpio_handler as gpio_handler
class InputHandler:
"""
@ -37,17 +35,6 @@ class InputHandler:
pygame.event.post(pygame.event.Event(pygame.KEYUP, {'key': pressed_button}))
self.last_input_tick = pygame.time.get_ticks()
def handle_gpio(self):
"""
Handle GPIO events and create events for them.
"""
for pressed_button in gpio_handler.BUTTONS:
code = gpio_handler.BUTTONS.get(pressed_button)
# Check if a button has been pressed. If it has, create a pygame event for it.
if gpio_handler.get_press(code):
self.create_event(code)
def handle_keyboard(self):
"""Handle keyboard presses and generate corresponding GPIO codes to create events."""
@ -57,25 +44,22 @@ class InputHandler:
running = False
if keyboard_event.type == pygame.KEYDOWN:
if keyboard_event.key == pygame.K_a:
self.create_event(gpio_handler.BUTTONS.get('a'))
self.create_event(pygame.K_a)
if keyboard_event.key == pygame.K_b:
self.create_event(gpio_handler.BUTTONS.get('b'))
if keyboard_event.key == pygame.K_PERIOD:
self.create_event(gpio_handler.BUTTONS.get('j_i'))
self.create_event(pygame.K_b)
if keyboard_event.key == pygame.K_KP_ENTER:
self.create_event(pygame.K_KP_ENTER)
if keyboard_event.key == pygame.K_RIGHT:
self.create_event(gpio_handler.BUTTONS.get('j_r'))
self.create_event(pygame.K_RIGHT)
if keyboard_event.key == pygame.K_LEFT:
self.create_event(gpio_handler.BUTTONS.get('j_l'))
self.create_event(pygame.K_LEFT)
if keyboard_event.key == pygame.K_DOWN:
self.create_event(gpio_handler.BUTTONS.get('j_d'))
self.create_event(pygame.K_DOWN)
if keyboard_event.key == pygame.K_UP:
self.create_event(gpio_handler.BUTTONS.get('j_u'))
self.create_event(pygame.K_UP)
if keyboard_event.key == pygame.K_ESCAPE:
running = False
def update(self):
"""Run either the GPIO handler or the keyboard handler to check for input and create events."""
if gpio_handler.ON_HARDWARE:
self.handle_gpio()
else:
self.handle_keyboard()
self.handle_keyboard()

View File

@ -1,6 +1,5 @@
import pygame
from ..elements import sprites
import pocket_friends.game_files.io.gpio_handler as gpio_handler
from ..io.input_handler import InputHandler
@ -133,15 +132,15 @@ class Surface(pygame.Surface):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == gpio_handler.BUTTONS.get('j_r'):
if event.key == pygame.K_RIGHT:
self.sel_right()
if event.key == gpio_handler.BUTTONS.get('j_l'):
if event.key == pygame.K_LEFT:
self.sel_left()
if event.key == gpio_handler.BUTTONS.get('j_d'):
if event.key == pygame.K_DOWN:
self.sel_down()
if event.key == gpio_handler.BUTTONS.get('j_u'):
if event.key == pygame.K_UP:
self.sel_up()
if event.key == gpio_handler.BUTTONS.get('a'):
if event.key == pygame.K_a:
self.additional_args = {'selected_egg': self.selected_color}
self.next_surface = 'selection_info'
self.running = False

View File

@ -1,6 +1,5 @@
import pygame
from pocket_friends.game_files.io import gpio_handler
from pocket_friends.game_files.io.input_handler import InputHandler
@ -36,6 +35,6 @@ class Surface(pygame.Surface):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == gpio_handler.BUTTONS.get('b'):
if event.key == pygame.K_b:
self.running = False
print('stop')

View File

@ -1,6 +1,5 @@
import pygame
from ..elements import sprites
import pocket_friends.game_files.io.gpio_handler as gpio_handler
from ..io.input_handler import InputHandler
@ -56,17 +55,17 @@ class Surface(pygame.Surface):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == gpio_handler.BUTTONS.get('j_d'):
if event.key == pygame.K_DOWN:
# Scroll down on the info screen.
self.info_text.scroll_down()
if event.key == gpio_handler.BUTTONS.get('j_u'):
if event.key == pygame.K_UP:
# Scroll up on the info screen.
self.info_text.scroll_up()
if event.key == gpio_handler.BUTTONS.get('a'):
if event.key == pygame.K_a:
self.running = False
self.additional_args = {'selected_color': self.selected_egg}
self.next_surface = 'playground'
if event.key == gpio_handler.BUTTONS.get('b'):
if event.key == pygame.K_b:
self.running = False
self.additional_args = {'selected_color': self.selected_egg}
self.next_surface = 'egg_select'