zockbox-gpiopad/gpiopad.py
2020-06-27 19:53:26 +02:00

163 lines
6.4 KiB
Python

import uinput
import time
import RPi.GPIO as GPIO
P1_BTN_FIRE1 = 2
P1_BTN_FIRE2 = 3
P1_BTN_UP = 4
P1_BTN_DOWN = 17
P1_BTN_LEFT = 27
P1_BTN_RIGHT = 22
P1_BTN_SELECT = 10
P1_BTN_START = 9
P1_BTN_MODE = 11
P2_BTN_FIRE1 = 0
P2_BTN_FIRE2 = 5
P2_BTN_UP = 6
P2_BTN_DOWN = 13
P2_BTN_LEFT = 19
P2_BTN_RIGHT = 26
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(P1_BTN_UP, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P1_BTN_DOWN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P1_BTN_LEFT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P1_BTN_RIGHT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P1_BTN_FIRE1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P1_BTN_FIRE2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P1_BTN_SELECT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P1_BTN_START, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P1_BTN_MODE, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P2_BTN_UP, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P2_BTN_DOWN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P2_BTN_LEFT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P2_BTN_RIGHT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P2_BTN_FIRE1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(P2_BTN_FIRE2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
events_p1 = (
uinput.BTN_SOUTH, uinput.BTN_EAST, uinput.BTN_MODE, uinput.BTN_START, uinput.BTN_SELECT, uinput.ABS_X + (0, 100, 0, 0),
uinput.ABS_Y + (0, 100, 0, 0))
events_p2 = (uinput.BTN_SOUTH, uinput.BTN_EAST, uinput.ABS_X + (0, 100, 0, 0), uinput.ABS_Y + (0, 100, 0, 0))
device_p1 = uinput.Device(events_p1, name="Zockbox1")
device_p2 = uinput.Device(events_p2, name="Zockbox2")
# Bools to keep track of movement
p1_fire1 = False
p1_fire2 = False
p1_start = False
p1_select = False
p1_mode = False
p1_up = False
p1_down = False
p1_left = False
p1_right = False
p2_fire1 = False
p2_fire2 = False
p2_up = False
p2_down = False
p2_left = False
p2_right = False
device_p1.emit(uinput.ABS_X, 50, syn=False)
device_p1.emit(uinput.ABS_Y, 50)
device_p2.emit(uinput.ABS_X, 50, syn=False)
device_p2.emit(uinput.ABS_Y, 50)
while True:
# P1
if (not p1_start) and (not GPIO.input(P1_BTN_START)): # start button pressed
p1_start = True
device_p1.emit(uinput.BTN_START, 1)
if p1_start and GPIO.input(P1_BTN_START): # start button released
p1_start = False
device_p1.emit(uinput.BTN_START, 0)
if (not p1_select) and (not GPIO.input(P1_BTN_SELECT)): # select button pressed
p1_select = True
device_p1.emit(uinput.BTN_SELECT, 1)
if p1_select and GPIO.input(P1_BTN_SELECT): # select button released
p1_select = False
device_p1.emit(uinput.BTN_SELECT, 0)
if (not p1_mode) and (not GPIO.input(P1_BTN_MODE)): # guide button pressed
p1_mode = True
device_p1.emit(uinput.BTN_MODE, 1)
if p1_mode and GPIO.input(P1_BTN_MODE): # guide button released
p1_mode = False
device_p1.emit(uinput.BTN_MODE, 0)
if (not p1_fire1) and (not GPIO.input(P1_BTN_FIRE1)): # Fire1 button pressed
p1_fire1 = True
device_p1.emit(uinput.BTN_SOUTH, 1)
if p1_fire1 and GPIO.input(P1_BTN_FIRE1): # Fire1 button released
p1_fire1 = False
device_p1.emit(uinput.BTN_SOUTH, 0)
if (not p1_fire2) and (not GPIO.input(P1_BTN_FIRE2)): # Fire2 button pressed
p1_fire2 = True
device_p1.emit(uinput.BTN_EAST, 1)
if p1_fire2 and GPIO.input(P1_BTN_FIRE2): # Fire2 button released
p1_fire2 = False
device_p1.emit(uinput.BTN_EAST, 0)
if (not p1_up) and (not GPIO.input(P1_BTN_UP)): # Up button pressed
p1_up = True
device_p1.emit(uinput.ABS_Y, 0) # Zero Y
if p1_up and GPIO.input(P1_BTN_UP): # Up button released
p1_up = False
device_p1.emit(uinput.ABS_Y, 50) # Center Y
if (not p1_down) and (not GPIO.input(P1_BTN_DOWN)): # Down button pressed
p1_down = True
device_p1.emit(uinput.ABS_Y, 100) # Max Y
if p1_down and GPIO.input(P1_BTN_DOWN): # Down button released
p1_down = False
device_p1.emit(uinput.ABS_Y, 50) # Center Y
if (not p1_left) and (not GPIO.input(P1_BTN_LEFT)): # Left button pressed
p1_left = True
device_p1.emit(uinput.ABS_X, 0) # Zero X
if p1_left and GPIO.input(P1_BTN_LEFT): # Left button released
p1_left = False
device_p1.emit(uinput.ABS_X, 50) # Center X
if (not p1_right) and (not GPIO.input(P1_BTN_RIGHT)): # Right button pressed
p1_right = True
device_p1.emit(uinput.ABS_X, 100) # Max X
if p1_right and GPIO.input(P1_BTN_RIGHT): # Right button released
p1_right = False
device_p1.emit(uinput.ABS_X, 50) # Center X
# P2
if (not p2_fire1) and (not GPIO.input(P2_BTN_FIRE1)): # Fire1 button pressed
p2_fire1 = True
device_p2.emit(uinput.BTN_SOUTH, 1)
if p2_fire1 and GPIO.input(P2_BTN_FIRE1): # Fire1 button released
p2_fire1 = False
device_p2.emit(uinput.BTN_SOUTH, 0)
if (not p2_fire2) and (not GPIO.input(P2_BTN_FIRE2)): # Fire2 button pressed
p2_fire2 = True
device_p2.emit(uinput.BTN_EAST, 1)
if p2_fire2 and GPIO.input(P2_BTN_FIRE2): # Fire2 button released
p2_fire2 = False
device_p2.emit(uinput.BTN_EAST, 0)
if (not p2_up) and (not GPIO.input(P2_BTN_UP)): # Up button pressed
p2_up = True
device_p2.emit(uinput.ABS_Y, 0) # Zero Y
if p2_up and GPIO.input(P2_BTN_UP): # Up button released
p2_up = False
device_p2.emit(uinput.ABS_Y, 50) # Center Y
if (not p2_down) and (not GPIO.input(P2_BTN_DOWN)): # Down button pressed
p2_down = True
device_p2.emit(uinput.ABS_Y, 100) # Max Y
if p2_down and GPIO.input(P2_BTN_DOWN): # Down button released
p2_down = False
device_p2.emit(uinput.ABS_Y, 50) # Center Y
if (not p2_left) and (not GPIO.input(P2_BTN_LEFT)): # Left button pressed
p2_left = True
device_p2.emit(uinput.ABS_X, 0) # Zero X
if p2_left and GPIO.input(P2_BTN_LEFT): # Left button released
p2_left = False
device_p2.emit(uinput.ABS_X, 50) # Center X
if (not p2_right) and (not GPIO.input(P2_BTN_RIGHT)): # Right button pressed
p2_right = True
device_p2.emit(uinput.ABS_X, 100) # Max X
if p2_right and GPIO.input(P2_BTN_RIGHT): # Right button released
p2_right = False
device_p2.emit(uinput.ABS_X, 50) # Center X
time.sleep(.02)