0

I have a BU0836X Joystick Interface board which I want to use to read analog Joystick values. http://www.leobodnar.com/shop/index.php?main_page=product_info&products_id=180

On the computer there is a python script running capturing the joystick values using pygame's joystick module.

However, it's possible to get all the information from the board, such as the name, number of axes, number of buttons, etc. Also the supplied calibration tool works perfectly fine.

The only problem I am experiencing is that I can't read the axis data, which perfectly works with any standard gamepad of course. Is there a workaround to get the board up and running in a pygame enviroment?

Here is the super simple script i used for testing:

import pygame

pygame.joystick.init()

while True:

   joystick_count = pygame.joystick.get_count()

   for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init()
        name = joystick.get_name()
        axes = joystick.get_numaxes()
        hats = joystick.get_numhats()
        button = joystick.get_numbuttons()
        joy = joystick.get_axis(0)
        print (name,joy)

The output is:

('BU0836X Interface', 0.0)
user3603948
  • 153
  • 3
  • 12

1 Answers1

1

Sometimes this problem happens when you don´t make any calls to some of Pygames event queue functions in each frame of your game, as the Documentation states.

If you are not using any other event functions in your main game, you should call pygame.event.pump() to allow Pygame to handle internal actions, such as joystick information.

Try the following updated code:

while True:
   pygame.event.pump() #allow Pygame to handle internal actions
   joystick_count = pygame.joystick.get_count()

   for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init()

        name = joystick.get_name()
        axes = joystick.get_numaxes()
        hats = joystick.get_numhats()
        button = joystick.get_numbuttons()

        joy = joystick.get_axis(0)

        print(name, joy)

An alternative would be to wait for events generated by your joystick (such as JOYAXISMOTION) by using the pygame.event.get() function for instance:

while True:
    #get events from the queue
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT:
            pygame.quit()

        if event.type == pygame.JOYAXISMOTION and event.axis == 0:
            print(event.value)

   joystick_count = pygame.joystick.get_count()

   for i in range(joystick_count):
        joystick = pygame.joystick.Joystick(i)
        joystick.init() #event queue will receive events from your Joystick 

Hope this helps a little bit :)

elegent
  • 3,857
  • 3
  • 23
  • 36
  • thank you VERY MUCH! it works now! though I had to add `pygame.init()` otherwise i got a pygame.error: video system not initialized – user3603948 Jun 02 '15 at 21:42
  • @user3603948: I am glad I could help you :) Oh yes you are right, `pygame.init()` is necessary anyway. Did you test the second way I mentioned? – elegent Jun 04 '15 at 08:42
  • I didn't test the second way since the first works, but I will test it soon too. thanks again! – user3603948 Jun 04 '15 at 16:49