commit d0e9b946dfa7ef919ff5e4bf2854e26628bb4b6e Author: SPiNNiX Date: Sat Jun 27 00:12:48 2020 +0200 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..bcb4266 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +Requires python3, python-uinput and RPi.GPIO + +Make sure that user pi can open the uinput device. + +Enable access to it: +sudo groupadd -f uinput +sudo gpasswd -a pi uinput +Open /etc/udev/rules.d/99-input.rules and add: +SUBSYSTEM=="input", GROUP="input", MODE="0660" +KERNEL=="uinput", GROUP="uinput", MODE="0660" \ No newline at end of file diff --git a/gpiopad.py b/gpiopad.py new file mode 100644 index 0000000..a255599 --- /dev/null +++ b/gpiopad.py @@ -0,0 +1,163 @@ +import uinput +import time +import RPi.GPIO as GPIO + +P1_BTN_FIRE1 = 4 +P1_BTN_FIRE2 = 17 +P1_BTN_UP = 27 +P1_BTN_DOWN = 22 +P1_BTN_LEFT = 26 +P1_BTN_RIGHT = 10 +P1_BTN_SELECT = 9 +P1_BTN_START = 11 +P1_BTN_MODE = 14 +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)): # select button pressed + # p1_mode = True + # device_p1.emit(uinput.BTN_MODE, 0) + # if p1_mode and GPIO.input(P1_BTN_SELECT): # select button released + # p1_mode = False + # device_p1.emit(uinput.BTN_MODE, 1) + # Later implementation + 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)