From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Reading USB joystick?

Solved!
Go to solution

LabView has a VI to read a USB Joystick/game controller.  Is there any support for doing this in CVI?

 

I don't need anything more complex than polling the X & Y positions and detecting button presses.

 

Google found me some MSDN info about "Raw Device Input" but when I try to register for WM_INPUT messages with:

RAWINPUTDEVICE Rid[2];

Rid[0].usUsagePage = 0x01;
Rid[0].usUsage = 0x05;
Rid[0].dwFlags = 0; // adds game pad
Rid[0].hwndTarget = 0;

Rid[1].usUsagePage = 0x01;
Rid[1].usUsage = 0x04;
Rid[1].dwFlags = 0; // adds joystick
Rid[1].hwndTarget = 0;

if (RegisterRawInputDevices(Rid, 2, sizeof(Rid[0])) == FALSE) {
//registration failed. Call GetLastError for the cause of the error
}

 

My compile fails as my SDK installation seems to be missing something as including windows.h is not enough and the RAWINPUTDEVICE typedef and RegisterRawInputDevices prototypes are missing.

 

--wally.

 

0 Kudos
Message 1 of 9
(7,634 Views)
Solution
Accepted by topic author wally_666

a quick search in the MSDN for RegisterRawInputDevices() shows:

Minimum DLL Version user32.dll
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows XP

 

that means that you need to add user32.lib to your project and it won't run unless you have Windows XP or newer. the Windows SDK which comes with CVI 8 dates from Windows 2000, so it will not define those functions: you will need to either update to CVI 9 or download a recent Windows SDK from the microsoft website.

 

however, have a look at this section of the Windows SDK which describes a more supported (and older) way of using joysticks. some functions in this section are as old as windows 95.

Message 2 of 9
(7,626 Views)

Hey Wally - 

 

These functions are only available on Windows XP and later.  Because I assume you will be using Windows XP and later, you can get around this by adding the following line to the predefined macros section of your build options:

 

/D_WIN32_WINNT=0x0501

 

NickB09-25, 10_52_56.png

 

NickB

National Instruments  

 

@dummy_decoy  - you just beat me to it.  Just FYI though, user32 is one of the libs that CVI will automatically link in, and so does not need to be explicitly added to the project.

Message Edited by nickb on 09-25-2009 11:09 AM
Message 3 of 9
(7,625 Views)
@nickb: you are right about _WIN32_WINNT, i forgot about it and it can be a cause of great pain. but i would suggest that if you define _WIN32_WINNT, you should also define WINVER, using the same value for the two.
0 Kudos
Message 4 of 9
(7,618 Views)

"however, have a look at this section of the Windows SDK which describes a more supported (and older) way of using joysticks"

 

 

Sometimes the old ways are the best!

 

I can't believe how poorly documented this stuff is, going off on tangents about mind numbing detail for such a trivial thing.

 

It really is as simple as I thought it should be, just the documents do their best to obscure it!

 

I've only tested it with two USB joysticks (Logitech Extreme 3D Pro & Saitek Cyborg EVO Force) but it should work with any USB joystick that supports the HID joystick class using the built-in driver. (I'm using windows 2000)

 

Declare:

    double x,y,z

    JOYINFO joyinfo;

 

Poll:   
    joyGetPos(JOYSTICKID1,&joyinfo);

 

Use:

    x=((int)joyinfo.wXpos-32768)/65536.0         // -1.0=full scale left, 1.0=full scale right, 0 = center

 

Now if you need multiple joysticks or fancy features or more than about 1% position accuracy, you'll have to hassle with calibration issues and maybe learn other interface methods, but I just dropped this into a timer callback that was calling NIDAQ to read X & Y voltages for an analog (expensive!) joystick and am good to go!

 

And if there is no USB joystick plugged it it just reads minus full scale (unconnected analog inputs usually float to plus or minus full scale). If you need error handling its easy enough to add since status is returned.

 

Thanks guys!

 --wally.

 

0 Kudos
Message 5 of 9
(7,607 Views)

I just tried a USB Logitech Precision game controller.  This is not a joystick, but it falls in the same HID class driver model.  It has  two-axis "rocker" switch (along with 10 other pushbutton switches) that maps to the X-Y joystick axis and delivers plus and minus full scale when pushed.  This allows a quick 1 of 4 or 1 of 8 response like you might want to use to signal orientation in a Landolt C visual acuity test or duplicate an advanced joystick's "coolie hat" switch response.

 

The 10 buttons (each has a molded label, number 1 thru 10) map as joyinfo.wButtons which set a bit 0 thru 9 reading as values 1 through 512 when pressed.  And if you manage to get multiple buttons pressed and held between polls you read the OR of the bits -- buttons 4 and 6 pressed read as 8 + 32 or 40, (0x18).  Simple, logical, and couldn't be easier to use. 

 

I suspect if it shows up in ControlPanel->GamingOptions without installing third party drivers, you can use it with this very simple method.

 

--wally.

 

Message Edited by wally_666 on 09-25-2009 03:00 PM
Message Edited by wally_666 on 09-25-2009 03:01 PM
0 Kudos
Message 6 of 9
(7,602 Views)

Has anyone updated the Joystick.fp to support all the Extreme 3D Pro buttons? Is the source available?

 

Thanks this is a good start for a novice like me.

0 Kudos
Message 7 of 9
(5,289 Views)

Hi johnboy529,

 

I'd recommend creating a new thread for this question--you'll get more exposure and it's more likely that somebody familiar with these Windows functions and/or the Extreme 3D Pro will be able to answer.

 

Cheers,

Tom D.
Staff Software Engineer
NI
0 Kudos
Message 8 of 9
(5,261 Views)

i am using Extreme 3D Pro Joystick with cvi 9.0 , my issue is that i am using joystick.fp instrument driver to read from the joystick, the issue i am facing is this that joystick.fp only allows me to use 4 buttons at most, if i want to use more butttons or if i want to change location of button number i am unable to do so kindly guide me in this regard, i will be thankful to you. i have already read your contribution on the fourm.

0 Kudos
Message 9 of 9
(5,109 Views)