fussoir1er
Posts: 6
Joined: Sun Apr 06, 2014 11:22 pm

A non working GPIO gamepad with uinput

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?

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)

Thanks by advance !

User avatar
Gomez
Posts: 40
Joined: Mon Dec 15, 2014 3:42 am
Location: Porto, Portugal

Re: A non working GPIO gamepad with uinput

Sun Jan 11, 2015 4:15 pm

necroing ... did you ever solve this issue? I am running into the same problem.

Thanks

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: A non working GPIO gamepad with uinput

Sun Jan 11, 2015 6:20 pm

The OP hasn't logged on to the forum since 15/12/2014.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “Python”