[Gambas-user] Gambas class to read a joystick

kevinfishburne kevinfishburne at ...1887...
Tue Sep 7 05:19:44 CEST 2010



Rob Kudla wrote:
> 
> Someone on linuxbasic.net asked for a way to read a joystick in 
> Gambas.  I couldn't find anything in the SDL component for that 
> purpose, so I wrote a minimal class that reads from the raw device, 
> parses the data into something useful, and throws events.  I wrote a 
> program to demonstrate it as well.
> 

Referenced from:

http://old.nabble.com/Gambas-class-to-read-a-joystick-td15794466.html#a15797006

Hi Rob. Thanks for the awesome code. It was the only method I could find for
reading a gamepad in GAMBAS. I've since integrated it into my own program
and thought I'd list it here for folks who need to monitor asynchronous
button presses along with stick input.

I've integrated a time stamp comparison for each button so "taps" and
"holds" may be differentiated. I left debug prints where programmers may
insert their own code (myself included). The values are reported from a PS2
"dual shock"-style USB controller, a Logitech "DUAL ACTION" $30 piece of
history. Here it is, for better or worse:

' Gambas module file
' Player input module

PUBLIC pad_device AS Process    ' Device to accept gamepad input.
PUBLIC pad_time AS Integer      ' Current time input occurred.
PUBLIC pad_data AS Short        ' Current input data (stick data, 0=press,
1=release).
PUBLIC pad_type AS Byte         ' Current input type (1 = button, 2 =
stick).
' Current button or axis number.
' (0 - 11 = buttons, 0 = left horizontal, 1 = left verticle, 2 = right
horizontal, 3 = right verticle, 4 = d-pad horizontal, 5 = d-pad verticle).
PUBLIC pad_number AS Byte
PUBLIC button AS Integer[12, 2] ' Button number, old button status (true =
pressed, false = released) and time stamp.
PUBLIC taptime AS Integer = 375 ' Number of milliseconds to elapse after
holding a button for it to become a hold command.

PUBLIC SUB Initialize()

  ' Perform player input initialization.

  ' Set up the gamepad.
  pad_device = EXEC ["cat", "/dev/input/js0"] FOR READ AS "Gamepad"
  WAIT 1

END

PUBLIC SUB Gamepad_READ()

  ' General declarations.
  DIM counter AS Byte

  ' Read new gamepad data.
  READ #pad_device, pad_time, 4
  READ #pad_device, pad_data, 2
  READ #pad_device, pad_type, 1
  READ #pad_device, pad_number, 1

  ' Deal with button mashing.
  IF pad_type = 1 THEN
    ' Button mashing has occurred.
    IF pad_data = 1 THEN
      ' Button was pressed.
      button[pad_number, 0] = TRUE
      button[pad_number, 1] = pad_time
    ELSE
      ' Button was released.
      FOR counter = 0 TO 11
        ' Determine if button was tapped or held.
        IF pad_number = counter THEN
          IF Abs(pad_time - button[counter, 1]) < taptime THEN
            PRINT "tapped", Abs(pad_time - button[counter, 1])
          ELSE
            PRINT "held", Abs(pad_time - button[counter, 1])
          ENDIF
        ENDIF
      NEXT
      ' Preserve command history.
      button[pad_number, 0] = FALSE
      button[pad_number, 1] = pad_time
    ENDIF
  ENDIF
  ' Deal with stick flailing.
  IF pad_type = 2 THEN
    ' Stick flailing has occurred.
    SELECT CASE pad_number
      CASE 0
        ' Left analog, horizontal.
        IF pad_data < 0 THEN PRINT "Left analog: Move left"
        IF pad_data > 0 THEN PRINT "Left analog: Move right"
      CASE 1
        ' Left analog, vertical.
        IF pad_data < 0 THEN PRINT "Left analog: Move up"
        IF pad_data > 0 THEN PRINT "Left analog: Move down"
      CASE 2
        ' Right analog, horizontal.
        IF pad_data < 0 THEN PRINT "Right analog: Move left"
        IF pad_data > 0 THEN PRINT "Right analog: Move right"
      CASE 3
        ' Right analog, vertical.
        IF pad_data < 0 THEN PRINT "Right analog: Move up"
        IF pad_data > 0 THEN PRINT "Right analog: Move down"
      CASE 4
        ' D-pad, horizontal.
        IF pad_data < 0 THEN PRINT "D-pad: Move left"
        IF pad_data > 0 THEN PRINT "D-pad: Move right"
      CASE 5
        ' D-pad, vertical.
        IF pad_data < 0 THEN PRINT "D-pad: Move up"
        IF pad_data > 0 THEN PRINT "D-pad: Move down"
    END SELECT
  ENDIF

  ' Debug info.
  PRINT "pad_time: " & Str$(pad_time), "pad_data: " & Str$(pad_data),
"pad_type: " & Str$(pad_type), "pad_number: " & Str$(pad_number)

END


-----
Kevin Fishburne, Eight Virtues
www:  http://sales.eightvirtues.com http://sales.eightvirtues.com 
e-mail:  mailto:sales at ...1887... sales at ...1887... 
phone: (770) 853-6271
-- 
View this message in context: http://old.nabble.com/Gambas-class-to-read-a-joystick-tp15794466p29639131.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list