Page 1 of 1

Simultaneously use both push buttons and joystick

Posted: Thu Jul 02, 2015 4:58 pm
by Davespice
Hi folks
Okay so I just wanted to post some example code here to help those of you who are writing games for Tim Peake to play. Perhaps you want to use the joystick to move and the GPIO push buttons to fire?

Information on using the joystick can be found here and on the push buttons here (you'll need to have built that breadboard set up in order to test this code).

The problem you'll have is that you need to use something like pygame to consume events from the joystick, but this isn't neatly compatible with how GPIO button detection works. Marrying the two together could be a bit of a pain. However there is a clever trick you can do which is demonstrated by the code below.

You hook up the GPIO edge detection as per the examples given but you then make it write phantom key press events to the Linux event queue. That way you can receive the events from both joystick and push buttons in pygame at the same time!

To use this you need to install a python module called evdev.

Code: Select all

sudo pip-3.2 install evdev
Also note that the joystick is rotated clockwise through 90 degrees when in flight configuration, so UP on the joystick is actually RIGHT. So I've compensated for this in the code where I test for the key codes.

The code is below.

Code: Select all

import time
import RPi.GPIO as GPIO
from evdev import uinput, ecodes as e
import pygame

print("Press Escape to quit")

from pygame.locals import *
from astro_pi import AstroPi

ap = AstroPi()

pygame.display.set_mode((640, 480))

UP = 26
DOWN = 13
LEFT = 20
RIGHT = 19
A = 16
B = 21  

running = True

keys = {}  # use a dictionary to map GPIO nos to key events

keys[UP] = e.KEY_U
keys[DOWN] = e.KEY_D
keys[LEFT] = e.KEY_L
keys[RIGHT] = e.KEY_R
keys[A] = e.KEY_A
keys[B] = e.KEY_B

ui = uinput.UInput()

def button_pressed(button):
    global ui
    key = keys[button]
    key_state = not GPIO.input(button)
    ui.write(e.EV_KEY, key, key_state) 


for pin in [UP, DOWN, LEFT, RIGHT, A, B]:
    GPIO.setup(pin, GPIO.IN, GPIO.PUD_UP)
    GPIO.add_event_detect(pin, GPIO.BOTH, callback=button_pressed, bouncetime=50)

while running:
    for event in pygame.event.get():
        if event.type == KEYUP:
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                running = False
            if event.key == pygame.K_DOWN:
            elif event.key == pygame.K_UP:
            elif event.key == pygame.K_LEFT:
            elif event.key == pygame.K_RIGHT:
            elif event.key == pygame.K_RETURN:
            elif event.key == pygame.K_u:
            elif event.key == pygame.K_d:
            elif event.key == pygame.K_l:
            elif event.key == pygame.K_r:
            elif event.key == pygame.K_a:
            elif event.key == pygame.K_b:


Re: Simultaneously use both push buttons and joystick

Posted: Thu Mar 24, 2016 7:25 pm
by konbert
This is nice, but an astro-pi is headless and this code will not work without screen because of pygame.
Is there a solution without using pygame?