Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: DrewPee on April 03, 2008

Title: Reading USB joysticks . . .
Post by: DrewPee on April 03, 2008
Hello again guys!

First off - apologies for not being around for a few weeks (months?). Real life once again takes over and I had lots of stuff to sort out!!

Im in the middle of writing something and I want to be able to read my USB joystick (speedlink pro - like the old competition pro!!! but usb!!!). Can anybody help me with this as I don't know where to start?
Regards
DrewPee
Title: Re: Reading USB joysticks . . .
Post by: rain_storm on April 03, 2008
use GETJOYSTICK
Title: Re: Reading USB joysticks . . .
Post by: DrewPee on April 03, 2008
Is it really that simple?
Thanks rain_storm - didn't even know that command existed????!?!?!?

DrewPee
Title: Re: Reading USB joysticks . . .
Post by: rain_storm on April 03, 2008
no problem it will give a value between -1.0 and 1.0 for the analog sticks but you need to set a tolerence because it wont return 0.0 if the sticks are not being pushed (its usually slightly negative but really small fraction for zero) so use something like if (leftstick < -0.1) or (leftstick > 0.1) ' do something
Title: Re: Reading USB joysticks . . .
Post by: Shockwave on April 03, 2008
I think you'll need to ;

#include "windows.bi"

In the beginning of your program first though :)
Title: Re: Reading USB joysticks . . .
Post by: BadMrBox on April 04, 2008
I think you'll need to ;

#include "windows.bi"

In the beginning of your program first though :)
Nope, not even that.
Title: Re: Reading USB joysticks . . .
Post by: DrewPee on April 04, 2008
Cheers guys!
I will have a play and let you know how i get on . . .  :whisper:

Drew
Title: Re: Reading USB joysticks . . .
Post by: DrewPee on April 04, 2008
Only one problem that I can see by using this command is that you have to use a screen command first! I will be using TinyPTC ?!?!?!??!
Works exactly as I wanted though - my joystick has four buttons . . . and it can read as many as the joystick has! clever stuff!!!

Any ideas on using this on tinyptc?

Drew

ps Thanks again for everybodys comments!!
Title: Re: Reading USB joysticks . . .
Post by: Shockwave on April 04, 2008
Mmm. I thought it was a windows api call :) I am wrong then.

You'd probably be better off sticking to FB's graphics library. I believe it is possible to use some functions in GLUT to read the joystick, so maybe it would be possible to integrate it into the functionality of tinyptc ext though.

It's a tricky one I think.

Title: Re: Reading USB joysticks . . .
Post by: Jim on April 04, 2008
Hi DrewPee,

If you stick with SCREEN version, I can show you how to port it to PTC.  Right now I'm about to go away for the weekend, so I can't type up everything you need to do.

Anyway, this might just work - you'll need windows.bi and mmsystem.bi (or is it winmm.bi).
You'll be using the function
Code: [Select]
JOYINFOEX joystick
joystick.dwSize = SIZE JOYINFOEX
joystick.dwFlags = JOY_RETURNALL
joyGetPosEx(JOYSTICKID1, @joystick)
The JOYINFOEX structure looks like this
Code: [Select]
typedef struct joyinfoex_tag {
    DWORD dwSize;
    DWORD dwFlags;
    DWORD dwXpos;
    DWORD dwYpos;
    DWORD dwZpos;
    DWORD dwRpos;
    DWORD dwUpos;
    DWORD dwVpos;
    DWORD dwButtons;
    DWORD dwButtonNumber;
    DWORD dwPOV;
    DWORD dwReserved1;
    DWORD dwReserved2;
} JOYINFOEX;

From that you ought to be able to pull out the info that you want.

Jim

Title: Re: Reading USB joysticks . . .
Post by: ninogenio on April 05, 2008
cool cheers jim this is usefull to me to and works excellently with my old hacked xbox pad.

Code: [Select]
#include "Windows.bi"
#include "Win\\MmSystem.bi"

Dim As JOYINFOEX joystick
joystick.dwSize = SizeOf(JOYINFOEX)
joystick.dwFlags = JOY_RETURNALL

While Inkey$=""
joyGetPosEx(JOYSTICKID1, @joystick)
'this will get the x pos
Print joystick.dwXpos
'and this will get the y pos
Print joystick.dwYpos

'other swithches that can be used
'dwXpos
'dwYpos
'dwZpos
'dwRpos
'dwUpos
'dwVpos
'dwButtons
'dwButtonNumber
wend
Title: Re: Reading USB joysticks . . .
Post by: DrewPee on April 07, 2008
@Jim - thanks for that - very good!

@ Nino - thanks for your bit of code as well - this will work excellently for what I am trying to do!!!

Drew
Title: Re: Reading USB joysticks . . .
Post by: Merick on April 10, 2008
Heyo, have you actually tried to use getjoystick with tinyptc? The info in the FB docs seems to be outdated, I just did a small edit to the getjoystick demo and it does work without using the screen command:

Code: [Select]
Dim x As Single
Dim y As Single
Dim buttons As Integer
Dim result As Integer
Dim a As Integer

Const JoystickID = 0

'This line checks to see if the joystick is ok.

If GetJoystick(JoystickID,buttons,x,y) Then
    Print "Joystick doesn't exist or joystick error."
    Print
    Print "Press any key to continue."
    Sleep
    End
End If


while not multikey(1)
    result = GetJoystick(JoystickID,buttons,x,y)

    Locate 1,1
    Print ;"result:";result;" x:" ;x;" y:";y;" Buttons:";buttons,"","",""
   
    'This tests to see which buttons from 1 to 27 are pressed.
    For a = 0 To 20 ' changed this to 20  so the list will fit on the console screen
        If (buttons And (1 Shl a)) Then
            Print "Button ";a;" pressed.    "
        Else
            Print "Button ";a;" not pressed."
        End If
    Next a
wend
Title: Re: Reading USB joysticks . . .
Post by: Shockwave on April 10, 2008
I wish I could test that with tinyptc ext but I dont even have a joystick!
Thanks for posting that though Merick, hopefully it can be verified :)
Title: Re: Reading USB joysticks . . .
Post by: Dr_D on April 11, 2008
Hey guys. I just made a little test to confirm it and it does work with fb 0.18.4b  :clap:


Code: [Select]
#include "tinyptc.bi"

const as integer SCR_W = 640
const as integer SCR_H = 480

if ptc_open( "TEST!!!", SCR_W, SCR_H ) = 0 then
    end
end if

dim as uinteger Buffer()
redim Buffer( SCR_W * SCR_H )


dim x as single
dim y as single
dim buttons as integer
dim result as integer
dim a as integer

const JoystickID = 0

'This line checks to see if the joystick is ok.

if getjoystick(JoystickID,buttons,x,y) then
    print "Joystick doesn't exist or joystick error."
    print
    print "Press any key to continue."
    sleep
    end
end if


Do
    result = getjoystick(JoystickID,buttons,x,y)
   
    dim as integer nx = 320+(x*320)
    dim as integer ny = 240+(y*240)

    if nx<0 then nx=0
    if nx>SCR_W-1 then nx=SCR_W-1
    if ny<0 then ny=0
    if ny>SCR_H-1 then ny=SCR_H-1
   
    Buffer ( (ny * SCR_W) + nx ) = &hffffff

    PTC_UPDATE( @Buffer(0) )
Loop until inkey = chr(27)

PTC_CLOSE()
Title: Re: Reading USB joysticks . . .
Post by: Shockwave on April 11, 2008
Cool :)

But never mind tinyptc, that has been outclassed by tinyptc_ext :) I would test it but I have no usb joystick/joypad..
Title: Re: Reading USB joysticks . . .
Post by: ninogenio on April 11, 2008
do you have a usb xbox 360 pad shockwave? cause i can point you to some win32 drivers for it.
Title: Re: Reading USB joysticks . . .
Post by: Jim on April 11, 2008
There's no doubt that tinyptc_ext from rbz with all the great input from the people of these forums is easily the best Freebasic demo coding framework available.  Thanks to rbz for responding to enhancement requests, and thanks to all the dbf forum members for offering their support.

Jim
Title: Re: Reading USB joysticks . . .
Post by: DrewPee on April 11, 2008
I second Jim, but also thanks to everybody who has posted.
This place really is fantastic.

Regards
Andy (DrewPee)