PDA

View Full Version : Programming for a Gameport


Ian W.
10-27-2002, 09:54 PM
Yet another crazy idea from team 810. this time, instead of the serial port, we need help with the gameport. my friend found out how to hook up two joy sticks to the gameport, and has some crazy idea for a program. i can't say what it is, for that will ruin the surprise.

basically, what i need is a simple way (prefereablly just a console app) that can read the gameport. something similar to the serial white paper (the author's name escapes me at the moment) would help a lot. we do know how to make it work if we were to use two serial ports, but we're not sure if that's possible (i think it is, but i can't be sure).

so, if anyone has any information on how to use the gameport in a C++ program, or how to set up a joystick on a serial port (i'm not sure how you would read back analog values), i'd be very thankful.

rbayer
10-28-2002, 01:46 AM
OK, you've got a few options. Let me know which one appeals to you and I'll write another paper (the serial one was mine).

Option 1: DirectX. Advantages: Industry standard. Can support more axes/buttons than the next method. Disadvantages: a lot of overhead for setting up DirectInput, only works under Windows

Option 2: Assembly!!!!!! Yup, you can do it straight from assembly. Advantages: none of the DirectX stuff, can be used w/o windows. Disadvantages: it's assembly. 'nuf said.

In all honesty, assembly isn't really that bad, especially if you have someone tell you exactly what to write in your code--it's the method I'd recomend for a simple project like this.

However, as I said before DirectInput is the industry standard nowadays, so if you want to learn it for the pure sake of learning some introductory DirectX, I'd be happy to write a paper on that too.

As for doing it through a serial port, it just isn't going to happen. A joystick uses a different pin for each axis/set of buttons, but a serial port has only one pin for data.

My Linux box just died a slow, painful death with lots of pretty colors, so I'm off to remedy that (and maybe sleep?), but let me know which you'd prefer.

--Rob

(I could have the assembly one done in about 20 mins. The DirectInput might take a few days)

Ian W.
10-28-2002, 08:10 PM
assembly or directx?

i was kind of hoping there'd be a nice happy thing like the serial port (the readfile thing). i finally understand that now :).

i'm mostly a C++/PBASIC programmer, but i guess i could learn either assembly or directx (isn't that a library extension onto C++ or something like that?). one thing though, i believe like the dashboard program i made, it will be creating a text file that is read into a flash program. that means there needs to be enough resources to let the flash program work (which there should be either way if we use a good computer, but i believe we'll be using this at school, and well, the school computers pretty much suck :p). if you can write something up in assembly in 20 minutes, then it never hurts to try. if you can find the directx stuff it'd be interesting to try, but don't kill yourself trying to do it. and thanks for the help ;).

mtaman02
10-28-2002, 09:26 PM
i second that motion that school computers suck. Board of Ed in NYC stinks all together and needs to start chippin and gettin us robotics schools something decent to work with other then the old 486's / penitium 100's and pros etc. how does a 486 make it in the year 2002 must create thread on this. cya

Joe Ross
10-28-2002, 09:31 PM
I believe that a program using assembly will not work in any NT based operating system(2000 and XP). I could be wrong, though.

rbayer
10-28-2002, 10:33 PM
Well, I tried the assembly stuff I had lying around and it worked fine under DOS but gave a BSOD under Windows. I know I've asked this before, but what compiler are you using? If it's VC++, I think I have a fairly painless solution for you. Otherwise, things could get difficult. Also, do you normally use the Win32 SDK (with Windows, WndProc, etc)? If not, this could get very interesting very fast.

If you want to see some DirectInput code, it's included in RoboEmu 1.03(?) and higher in order to allow binding to real joysticks.

rbayer
10-28-2002, 11:43 PM
OK, I finally figured this out w/o DirectX or assembly! Here's the code you need:


#include <windows.h>
#include <mmsystem.h>
....

JOYINFOEX joyInfo1;
MMRESULT err;

ZeroMemory(&joyInfo1, sizeof(JOYINFOEX));
joyInfo1.dwSize=sizeof(JOYINFOEX);
joyInfo1.dwFlags=JOY_RETURNALL;

err=joyGetPosEx(JOYSTICKID1, &joyInfo1);
if(err!=JOYERR_NOERROR){
switch(err){
case MMSYSERR_NODRIVER:
//no driver installed
break;
case MMSYSERR_INVALPARAM:
//invalid parameter passed
break;
case MMSYSERR_BADDEVICEID:
//joystick ID is invalid
break;
case JOYERR_UNPLUGGED:
//joystick is not pluged in
break;
}
}


At this point, the joyInfo1 struct contains everything you could ever want. Here's a quick rundown of the important fields:

dwXpos: the x pos
dwYpos: the y pos
dwZpos: the z pos
dwButtons: All 32 buttons

In order to access an individual button, use the JOY_BUTTON1, JOY_BUTTON2, etc as a bit mask. I.e:

bool button1=(joyInfo1.dwButtons & JOY_BUTTON1 > 0 ? 1 : 0)

In order to use this stuff, you'll have to tell your compiler to link in the winmm.lib library.

Calibration comes from the Gaming Options control panel, but you'll probably want to check the values yourself just to make sure.

Finally, to access joystick #2, replace JOYSTICKID1 with JOYSTICKID2 in the joyGetPosEx function.


Anyway, I hope this helps. Let me know if something goes wrong or if you want some more detail/additional options. Alternatively, check out the MSDN library entry for JOYINFOEX.

rbayer
10-30-2002, 09:37 PM
Anybody had a chance to try this yet? If it works on other peoples' systems, I'll write up a formal white paper, but I want to make sure it's going to work first. Anyway, let me know if you have questions or want more info.

Ian W.
11-01-2002, 12:56 PM
I haven't had a chance to try it yet because I'm in the middle of installing gentoo on my computer, but i should be able to give it a whirl sometime this weekend. If it works i'll tell you, but i'll need a bit of time to try it out. thanks for the help.