Gambas India Forum

Forum breadcrumbs - You are here:ForumRepository: Source CodesJoystick

Joystick

Do you like this program?
yes
no
Page 1 of 80Next
Quote

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

 

Quote

Incredible all kinds of amazing data.
pay someone to write paper ico white paper writing service top paper writing services custom research paper writing service

Quote

Very good posts. With thanks.
writing an essay for college https://quality-essays.com essay editing service online https://bestonlinepaperwritingservices.com

Quote

Many thanks! I value it!
do my finance homework for me homework 123 do my physics homework online my son will not do his homework

Quote

You actually suggested that perfectly!
essays online to buy buy essay paper

Quote

This is nicely put. !
help writing essays for college essays to write about write my essay for me reviews

Quote

Very good info. Thanks a lot.
juegos de casino online slots casino online stardust online casino

Quote

Thank you. Very good information.
i cant write my college essay i need someone to write a resume for me someone do my essay

Quote

Excellent data. Regards.
automatic essay writer write a research paper term paper writer custom paper writers

Quote

You said it nicely.!
need help writing an essay essay helper essay help online essay writing helper

Page 1 of 80Next