Author Topic: Reading USB joysticks . . .  (Read 9780 times)

0 Members and 1 Guest are viewing this topic.

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Reading USB joysticks . . .
« 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
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Reading USB joysticks . . .
« Reply #1 on: April 03, 2008 »
use GETJOYSTICK

Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Reading USB joysticks . . .
« Reply #2 on: April 03, 2008 »
Is it really that simple?
Thanks rain_storm - didn't even know that command existed????!?!?!?

DrewPee
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Reading USB joysticks . . .
« Reply #3 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

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17422
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Reading USB joysticks . . .
« Reply #4 on: April 03, 2008 »
I think you'll need to ;

#include "windows.bi"

In the beginning of your program first though :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline BadMrBox

  • ZX 81
  • *
  • Posts: 22
  • Karma: 0
    • View Profile
    • Ragnarok Games
Re: Reading USB joysticks . . .
« Reply #5 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.
Bringer of Apocalypse

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Reading USB joysticks . . .
« Reply #6 on: April 04, 2008 »
Cheers guys!
I will have a play and let you know how i get on . . .  :whisper:

Drew
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Reading USB joysticks . . .
« Reply #7 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!!
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17422
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Reading USB joysticks . . .
« Reply #8 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.

Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Reading USB joysticks . . .
« Reply #9 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

Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Reading USB joysticks . . .
« Reply #10 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
« Last Edit: April 05, 2008 by ninogenio »
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Reading USB joysticks . . .
« Reply #11 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
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk

Offline Merick

  • Atari ST
  • ***
  • Posts: 113
  • Karma: 7
    • View Profile
Re: Reading USB joysticks . . .
« Reply #12 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

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17422
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Reading USB joysticks . . .
« Reply #13 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 :)
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Dr_D

  • Atari ST
  • ***
  • Posts: 151
  • Karma: 29
    • View Profile
Re: Reading USB joysticks . . .
« Reply #14 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()
The Dr. is INsane!!!

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17422
  • Karma: 499
  • evil/good
    • View Profile
    • My Homepage
Re: Reading USB joysticks . . .
« Reply #15 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..
Shockwave ^ Codigos
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
Re: Reading USB joysticks . . .
« Reply #16 on: April 11, 2008 »
do you have a usb xbox 360 pad shockwave? cause i can point you to some win32 drivers for it.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Reading USB joysticks . . .
« Reply #17 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
Challenge Trophies Won:

Offline DrewPee

  • I Toast Therefore I am
  • Pentium
  • *****
  • Posts: 563
  • Karma: 25
  • Eat Cheese - It's good for you!
    • View Profile
    • Retro Computer Museum
Re: Reading USB joysticks . . .
« Reply #18 on: April 11, 2008 »
I second Jim, but also thanks to everybody who has posted.
This place really is fantastic.

Regards
Andy (DrewPee)
DrewPee
aka Falcon of The Lost Boyz (Amiga)
Ex-Amiga Coder and Graphic Designer
Administrator of > www.retrocomputermuseum.co.uk