Markus Blechschmidt
Markus Blechschmidt

Reputation: 101

Reading joystick values with Python

I want to read the values of an Logitech Logitech Extreme 3D Pro with a Raspberry Pi. I'm using the pygame library

The script:

import pygame
import sys
import time

pygame.joystick.init()

print pygame.joystick.get_count()

_joystick = pygame.joystick.Joystick(0)
_joystick.init()
print _joystick.get_init()
print _joystick.get_id()
print _joystick.get_name()
print _joystick.get_numaxes()
print _joystick.get_numballs()
print _joystick.get_numbuttons()
print _joystick.get_numhats()
print _joystick.get_axis(0)

The ouput:

1
1
0
Logitech Logitech Extreme 3D Pro
4
0
12
SDL_JoystickNumHats value:1:
1
SDL_JoystickGetAxis value:0:
0.0

There are 4 axes and I turned all of them.

I can't find the problem. I already tried using other axes.

Thanks for help.

Upvotes: 2

Views: 22729

Answers (3)

s4mdf0o1
s4mdf0o1

Reputation: 468

I'd rather wait (even more in a thread) for example :

    axes = [ 0.0 ] * your_joystick.get_numaxes()
    buttons = [ False ] * your_joystick.get_numbuttons()

    while self.keep_alive:
        event = pygame.event.wait()
        if event.type == pygame.QUIT:
             self.keep_alive = False
        elif event.type == pygame.JOYAXISMOTION:
            e = event.dict
            axes[e['axis']] = e['value']
        elif event.type in [pygame.JOYBUTTONUP, pygame.JOYBUTTONDOWN ]:
            e = event.dict
            buttons[e['button']] ^= True

Upvotes: 0

user4092994
user4092994

Reputation: 31

I ran into the same problem. You have to write pygame.event.get() in order to read information from the joystick. Otherwise it never updates.

Upvotes: 3

cube
cube

Reputation: 3948

If the problem is that the value is always 0, then try to do pygame.event.pump() before reading the values. I've had a similar problem and this helped.

Upvotes: 3

Related Questions