Quote from Gowtham on April 14, 2022, 4:30 pm
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