A non working GPIO gamepad with uinput
Posted: Thu Dec 04, 2014 10:25 pm
Hello !
I'm trying to build an arcade cabinet with Piplay! (A raspbian based OS with emulators)
Nice to write, hard to do when you're a noob...
Well, I tried to wire a joystick and a few buttons to GPIOs and write a python script using Uinput to emulate a keyboard.
But.... It's not working... I mean, keys are working in the terminal but not on the GUI...
Please can anyone have a look at it?
Thanks by advance !
I'm trying to build an arcade cabinet with Piplay! (A raspbian based OS with emulators)
Nice to write, hard to do when you're a noob...
Well, I tried to wire a joystick and a few buttons to GPIOs and write a python script using Uinput to emulate a keyboard.
But.... It's not working... I mean, keys are working in the terminal but not on the GUI...
Please can anyone have a look at it?
Code: Select all
#!/usr/bin/env python
import uinput
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(04, GPIO.IN) # UP
GPIO.setup(17, GPIO.IN) # DOWN
GPIO.setup(27, GPIO.IN) # LEFT
GPIO.setup(22, GPIO.IN) # RIGHT
GPIO.setup(23, GPIO.IN) # BTN1
GPIO.setup(24, GPIO.IN) # BTN2
events = (
uinput.KEY_UP,
uinput.KEY_DOWN,
uinput.KEY_LEFT,
uinput.KEY_RIGHT,
uinput.KEY_ENTER,
uinput.KEY_ESC,
)
device = uinput.Device(events)
# Bools to keep track of movement
UP = False
DOWN = False
LEFT = False
RIGHT = False
BOUTON1 = False
BOUTON2 = False
while True:
if (not UP) and (not GPIO.input(4)):
UP = True
device.emit(uinput.KEY_UP, 1)
if UP and GPIO.input(4):
UP = False
device.emit(uinput.KEY_UP, 0)
if (not DOWN) and (not GPIO.input(17)):
DOWN = True
device.emit(uinput.KEY_DOWN, 1)
if DOWN and GPIO.input(17):
DOWN = False
device.emit(uinput.KEY_DOWN, 0)
if (not LEFT) and (not GPIO.input(27)):
LEFT = True
device.emit(uinput.KEY_RIGHT, 1)
if LEFT and GPIO.input(27):
LEFT = False
device.emit(uinput.KEY_RIGHT, 0)
if (not RIGHT) and (not GPIO.input(22)):
RIGHT = True
device.emit(uinput.KEY_RIGHT, 1)
if RIGHT and GPIO.input(22):
RIGHT = False
device.emit(uinput.KEY_RIGHT, 0)
if (not BOUTON1) and (not GPIO.input(23)):
BOUTON1 = True
device.emit(uinput.KEY_ENTER, 1)
if BOUTON1 and GPIO.input(23):
BOUTON1 = False
device.emit(uinput.KEY_ENTER, 0)
if (not BOUTON2) and (not GPIO.input(24)):
BOUTON2 = True
device.emit(uinput.KEY_ESC, 1)
if BOUTON2 and GPIO.input(24):
BOUTON2 = False
device.emit(uinput.KEY_ESC, 0)