updated docstrings to follow google styling, changed checking of hardware to only happen once
This commit is contained in:
parent
f56f775541
commit
65365adc3d
@ -3,9 +3,6 @@ import json
|
||||
|
||||
|
||||
class SaveData:
|
||||
"""
|
||||
Class that represents the save data of the game.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
|
@ -1,6 +1,6 @@
|
||||
"""
|
||||
Module used to fake the RPi.GPIO module so that
|
||||
the program can be run without the actual hardware.
|
||||
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
|
||||
@ -12,7 +12,11 @@ FALLING = 0
|
||||
def setmode(new_mode):
|
||||
"""
|
||||
Fake setmode function.
|
||||
:param new_mode:
|
||||
Args:
|
||||
new_mode:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
@ -20,10 +24,14 @@ def setmode(new_mode):
|
||||
def setup(channel, mode, initial=None, pull_up_down=None):
|
||||
"""
|
||||
Fake setup function.
|
||||
:param channel:
|
||||
:param mode:
|
||||
:param initial:
|
||||
:param pull_up_down:
|
||||
Args:
|
||||
channel:
|
||||
mode:
|
||||
initial:
|
||||
pull_up_down:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
@ -31,10 +39,14 @@ def setup(channel, mode, initial=None, pull_up_down=None):
|
||||
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:
|
||||
Args:
|
||||
channel:
|
||||
edge_type:
|
||||
callback:
|
||||
bouncetime:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
pass
|
||||
|
||||
@ -42,8 +54,11 @@ def add_event_detect(channel, edge_type, callback=None, bouncetime=0):
|
||||
def event_detected(channel):
|
||||
"""
|
||||
Fake function to detect an event. Always returns false.
|
||||
:param channel:
|
||||
:return:
|
||||
Args:
|
||||
channel:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
return False
|
||||
|
||||
@ -51,6 +66,10 @@ def event_detected(channel):
|
||||
def cleanup(channel=None):
|
||||
"""
|
||||
Fake cleanup function.
|
||||
:param channel:
|
||||
Args:
|
||||
channel:
|
||||
|
||||
Returns:
|
||||
|
||||
"""
|
||||
pass
|
||||
|
@ -1,15 +1,19 @@
|
||||
"""
|
||||
Handle inputs from the GPIO pins on the Raspberry Pi and converting them to events to be used in other places (pygame, etc.)
|
||||
"""
|
||||
"""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
|
||||
|
||||
try:
|
||||
importlib.util.find_spec('RPi.GPIO')
|
||||
import RPi.GPIO as GPIO
|
||||
ON_HARDWARE = True
|
||||
except ImportError:
|
||||
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 = {
|
||||
@ -24,9 +28,7 @@ BUTTONS = {
|
||||
|
||||
|
||||
def setup():
|
||||
"""
|
||||
Primes the GPIO pins for reading the inputs of the buttons.
|
||||
"""
|
||||
"""Prime the GPIO pins for reading the inputs of the buttons."""
|
||||
GPIO.setmode(GPIO.BOARD)
|
||||
|
||||
GPIO.setup(BUTTONS.get('a'), GPIO.IN)
|
||||
@ -47,16 +49,17 @@ def setup():
|
||||
|
||||
|
||||
def teardown():
|
||||
"""
|
||||
Cleans up the GPIO handler.
|
||||
"""
|
||||
"""Clean up the GPIO handler."""
|
||||
GPIO.cleanup()
|
||||
|
||||
|
||||
def get_press(button):
|
||||
"""
|
||||
Returns true if a button has changed from not pressed to pressed.
|
||||
:param button: button to be detected
|
||||
:return: True if the button is has been pressed, False otherwise
|
||||
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)
|
||||
|
@ -1,5 +1,4 @@
|
||||
import pygame
|
||||
import importlib.util
|
||||
import pocket_friends.game_files.io.gpio_handler as gpio_handler
|
||||
|
||||
|
||||
@ -7,36 +6,40 @@ class InputHandler:
|
||||
"""
|
||||
Class that is implemented into surfaces in order to control the
|
||||
pressing of buttons on both the real hardware and on a keyboard.
|
||||
|
||||
Attributes:
|
||||
clock (pygame.time.Clock): Pygame clock used for input time calculations.
|
||||
last_input_tick (int): The tick that the last input was registered on.
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, pygame_clock):
|
||||
"""
|
||||
Create a InputHandler object using a given Pygame clock.
|
||||
|
||||
Args:
|
||||
pygame_clock (pygame.time.Clock): A pygame clock to use as the clock for input time calculations.
|
||||
|
||||
"""
|
||||
self.clock = pygame_clock
|
||||
|
||||
# If not on actual hardware, fake the GPIO in order to get it working correctly.
|
||||
try:
|
||||
importlib.util.find_spec('RPi.GPIO')
|
||||
import RPi.GPIO as GPIO
|
||||
self.on_hardware = True
|
||||
except ImportError:
|
||||
import pocket_friends.game_files.io.fake_gpio as GPIO
|
||||
self.on_hardware = False
|
||||
|
||||
self.last_input_tick = 0
|
||||
|
||||
def create_event(self, pressed_button):
|
||||
"""
|
||||
Creates a pygame event with a given keyboard code
|
||||
:param pressed_button:
|
||||
Create a pygame event given a GPIO code and post it to the pygame event handler.
|
||||
Args:
|
||||
pressed_button (int): The GPIO code to be registered and pressed.
|
||||
|
||||
"""
|
||||
# Register a button click so long as the last button click happened no less than two frames ago
|
||||
if pygame.time.get_ticks() - self.last_input_tick > self.clock.get_time() * 2 or not self.on_hardware:
|
||||
if pygame.time.get_ticks() - self.last_input_tick > self.clock.get_time() * 2 or not gpio_handler.ON_HARDWARE:
|
||||
pygame.event.post(pygame.event.Event(pygame.KEYDOWN, {'key': pressed_button}))
|
||||
pygame.event.post(pygame.event.Event(pygame.KEYUP, {'key': pressed_button}))
|
||||
self.last_input_tick = pygame.time.get_ticks()
|
||||
|
||||
def handle_gpio(self):
|
||||
"""
|
||||
Handles getting GPIO button presses and making a pygame event when a press is detected.
|
||||
Handle GPIO events and create events for them.
|
||||
"""
|
||||
for pressed_button in gpio_handler.BUTTONS:
|
||||
code = gpio_handler.BUTTONS.get(pressed_button)
|
||||
@ -45,10 +48,8 @@ class InputHandler:
|
||||
if gpio_handler.get_press(code):
|
||||
self.create_event(code)
|
||||
|
||||
def keyboard_handler(self):
|
||||
"""
|
||||
Simulates key presses to GPIO button presses. Also handles quitting the game.
|
||||
"""
|
||||
def handle_keyboard(self):
|
||||
"""Handle keyboard presses and generate corresponding GPIO codes to create events."""
|
||||
|
||||
# Checks if a corresponding keyboard key has been pressed. If it has, emulate a button press.
|
||||
for keyboard_event in pygame.event.get():
|
||||
@ -73,10 +74,8 @@ class InputHandler:
|
||||
running = False
|
||||
|
||||
def update(self):
|
||||
"""
|
||||
Run the input handler and check for inputs.
|
||||
"""
|
||||
if self.on_hardware:
|
||||
"""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.keyboard_handler()
|
||||
self.handle_keyboard()
|
||||
|
@ -6,7 +6,18 @@ from ..io.input_handler import InputHandler
|
||||
|
||||
|
||||
class Surface(pygame.Surface):
|
||||
"""
|
||||
|
||||
"""
|
||||
def __init__(self, window_size, resources_dir, game_fps, **kwargs):
|
||||
"""
|
||||
|
||||
Args:
|
||||
window_size:
|
||||
resources_dir:
|
||||
game_fps:
|
||||
**kwargs:
|
||||
"""
|
||||
super().__init__(window_size, pygame.SRCALPHA)
|
||||
self.name = 'selection_info'
|
||||
self.running = True
|
||||
|
Loading…
Reference in New Issue
Block a user