Discussion:
Joystick help
(too old to reply)
Earl Colby Pottinger
2005-08-21 22:51:43 UTC
Permalink
I can seem to access the joystick under BeOS properly.
If I open as an unenhanced joystick, I have problems detecting the pots'
correct values. But worse in enhanced mode the moment I press a button I
lose the 4 axis.

Here is my sample code:

if (Display == 1006) { // Read the game port info, open the
GamePort/Joystick in non-enhanced mode.
SetFontSize(12); X=20.0; Y=20.0; MovePenTo(BPoint(X,Y)); // Nice font
size, set cursor position.
JoyStick = new BJoystick(); // Create a joystick object.

Device_Count = JoyStick->CountDevices(); sprintf(TextBuffer,"Number of
gameport devices found--> %d ",(int)Device_Count); DrawString(TextBuffer);
Y=Y+15.0; MovePenTo(BPoint(X,Y));
for (int n=0; n<Device_Count; n++) {
JoyStick->GetDeviceName(n, JoyStick_Name); // Get name of GamePort.
sprintf(TextBuffer,"Device name of gameport %d -->
%s",n,JoyStick_Name); DrawString(TextBuffer); Y=Y+15.0;
MovePenTo(BPoint(X,Y)); // Print names of gameport devices.
JoyStick->Open(JoyStick_Name,false); // Open port in unenhanced mode.
JoyStick->GetControllerName(&Control_Name);
sprintf(TextBuffer,"Controller name of gameport -->
%s",Control_Name.String()); DrawString(TextBuffer); Y=Y+25.0;
MovePenTo(BPoint(X,Y)); // Print names of gameport controllers.
JoyStick->Close(); }

status_t Status = JoyStick->GetDeviceName(0, JoyStick_Name); // Get name
of first gameport device.
if (Status == B_OK) {
sprintf(TextBuffer,"Opened first game port in enhanced mode. Device
name of gameport read --> %s ",JoyStick_Name); DrawString(TextBuffer);
Y=Y+15.0; MovePenTo(BPoint(X,Y));

Status = JoyStick->Open(JoyStick_Name,false); // Open GamePort Object
as unenhanced.
if (Status >= B_OK) { // Positive value means successful open.
for (int n=0; n<20; n++) {
JoyStick->Update(); // Read Joystick status.
// JoyStick->horizontal first analog pot input (0-4095) on the
joystick.
// JoyStick->vertical second analog pot input (0-4095) on the
joystick.
// JoyStick->button1 first button found on the joystick.
// JoyStick->button2 second button found on the joystick.
sprintf(TextBuffer,"Joystick Horizontal --> %4d Vertical -->
%4d Button 1 --> %d Button 2 --> %d",
10000+JoyStick->horizontal, 10000+JoyStick->vertical,
(int)JoyStick->button1, (int)JoyStick->button2);
DrawString(TextBuffer); Y=Y+15.0; MovePenTo(BPoint(X,Y)); //
print Joystick information.
if (Timing) { // Wait till next valid time slice to do sampling.
TimeBase = TimeBase + Timing; snooze_until(TimeBase,
B_SYSTEM_TIMEBASE); } }
JoyStick->Close(); // Close GamePort.
}else{
DrawString("First joystick on gameport would not open in unenhanced
mode."); Y=Y+25.0; MovePenTo(BPoint(X,Y)); } }
delete JoyStick; SetFontSize(12); } // Delete BJoystick object. Restore
font size

if (Display == 1007) { // Read the game port info, open the
GamePort/Joystick in enhanced mode.
SetFontSize(12); X=20.0; Y=20.0; MovePenTo(BPoint(X,Y)); // Nice font
size, set cursor position.
JoyStick = new BJoystick(); // Create a joystick object.

Device_Count = JoyStick->CountDevices(); sprintf(TextBuffer,"Number of
gameport devices found--> %d ",(int)Device_Count); DrawString(TextBuffer);
Y=Y+15.0; MovePenTo(BPoint(X,Y));
for (int n=0; n<Device_Count; n++) {
JoyStick->GetDeviceName(n, JoyStick_Name); // Get name of GamePort.
sprintf(TextBuffer,"Device name of gameport %d -->
%s",n,JoyStick_Name); DrawString(TextBuffer); Y=Y+15.0;
MovePenTo(BPoint(X,Y)); // Print names of gameport devices.
JoyStick->Open(JoyStick_Name,true); // Open port in unenhanced mode.
JoyStick->GetControllerName(&Control_Name);
sprintf(TextBuffer,"Controller name of gameport -->
%s",Control_Name.String()); DrawString(TextBuffer); Y=Y+25.0;
MovePenTo(BPoint(X,Y)); // Print names of gameport controllers.
JoyStick->Close(); }

status_t Status = JoyStick->GetDeviceName(0, JoyStick_Name); // Get name
of first gameport device.
if (Status == B_OK) {
sprintf(TextBuffer,"Opened first game port in enhanced mode. Device
name of gameport read --> %s ",JoyStick_Name); DrawString(TextBuffer);
Y=Y+15.0; MovePenTo(BPoint(X,Y));

Status = JoyStick->Open(JoyStick_Name,true); // Open GamePort Object
and Open.
if (Status >= B_OK) { // Positive value means successful open.
Stick_Count = JoyStick->CountSticks();
sprintf(TextBuffer,"Gameport/JoyStick Sticks Count--> %d ",
(int)Stick_Count); DrawString(TextBuffer); Y=Y+15.0; MovePenTo(BPoint(X,Y));
Hat_Count = JoyStick->CountHats();
sprintf(TextBuffer,"Gameport/JoyStick Hats Count--> %d ", (int)Hat_Count);
DrawString(TextBuffer); Y=Y+15.0; MovePenTo(BPoint(X,Y));
Button_Count = JoyStick->CountButtons();
sprintf(TextBuffer,"Gameport/JoyStick Buttons Count--> %d ",
(int)Button_Count); DrawString(TextBuffer); Y=Y+15.0; MovePenTo(BPoint(X,Y));
Axis_Count = JoyStick->CountAxes();
sprintf(TextBuffer,"Gameport/JoyStick Axis Count--> %d ",
(int)Axis_Count); DrawString(TextBuffer); Y=Y+15.0; MovePenTo(BPoint(X,Y));
// int16 Pot_Values[9]; // same as ? =>
int16* axes; axes = (int16 *) malloc(sizeof(int16) * Axis_Count);
// Create array to recieve axis values.
for (int n=0; n<20; n++) {
JoyStick->Update();
Button_Values = JoyStick->ButtonValues();
JoyStick->GetAxisValues(axes,0); // Read Joystick status.
sprintf(TextBuffer,"Joystick 1 Buttons, Axis(1-4)--> %d %d %d
%d %d",(int)Button_Values,
(int)axes[0],(int)axes[1],(int)axes[2],(int)axes[3]);
DrawString(TextBuffer); Y=Y+15.0; MovePenTo(BPoint(X,Y)); }
JoyStick->Close(); // Close GamePort.
}else{
DrawString("Joystick would not open in enhanced mode."); Y=Y+15.0;
MovePenTo(BPoint(X,Y)); } }
delete JoyStick; SetFontSize(12); } // Delete BJoystick object. Restore
font size
--
I make public email sent to me! Hydrogen Peroxide Rockets, OpenBeos,
SerialTransfer 3.0, RAMDISK, BoatBuilding, DIY TabletPC. What happened to
the time? http://webhome.idirect.com/~earlcp
Pete
2005-09-10 19:26:30 UTC
Permalink
Sorry for the not-very-prompt response, but things have been pretty
slow here, so I haven't been visiting much... (:-/)
Not sure how much help I can be anyway, but I *have* written code
for the joystick that seems to work.

Unfortunately, it's for the MusicWeaver, so I use it mostly on my
ol' BeBox (!), which doesn't support enhanced mode at all. I did,
however, also write a version for x86 (using enhanced mode) which
I've used occasionally without apparent trouble.

It was several years ago, though, so it's difficult to immediately
check your code. I'll try to plug in a stick to my x86 machine and
see if I see any glitches.
Post by Earl Colby Pottinger
I can seem to access the joystick under BeOS properly.
If I open as an unenhanced joystick, I have problems detecting the pots'
correct values. But worse in enhanced mode the moment I press a button I
lose the 4 axis.
What do you mean by "4 axis"? Does the stick yoo're using have more
than 2? I've only ever used my code with a standard 2-axis, 3-button
one.

-- Pete --
--
============================================================================
The address in the header is a Spam Bucket -- don't bother replying to it...
(If you do need to email, replace the account name with my true name.)
============================================================================
Pete
2005-09-11 07:13:56 UTC
Permalink
Post by Pete
Not sure how much help I can be anyway, but I *have* written code
for the joystick that seems to work.
Unfortunately, it's for the MusicWeaver, so I use it mostly on my
ol' BeBox (!), which doesn't support enhanced mode at all. I did,
however, also write a version for x86 (using enhanced mode) which
I've used occasionally without apparent trouble.
It was several years ago, though, so it's difficult to immediately
check your code. I'll try to plug in a stick to my x86 machine and
see if I see any glitches.
Well, I did do some checking, and things seem to work (on my Intel box)
for me. I see, though, from your code that you *do* have four axes on
your stick; the two I have don't seem to give any trouble, but I guess
it's quite probable there's aglitch in the driver...

As for unenhanced mode, though, you *do* remember that the stick has
to be 'calibrated' before use, do you? If I try to use it uncalibrated
I see much the same garbage as you do. After calibration I use much
the same sort of read loop you do, and it gives good values.

I give the user a "CALIBRATE' button. The first time it's clicked,
it records the values at the 'zero' position of the stick. Clicked
again, the user then moves the stick to all the 'maxima', which are
recorded. A final click ends the calibration and the scaling is calculated
using the zero offsets and the extreme values (actually maximum and
minimum).

In enhance mode, no calibration is needed [Why? No idea...], and for
my two axes it seems to work fine -- buttons don't interfere.

Any help?
-- Pete --
--
============================================================================
The address in the header is a Spam Bucket -- don't bother replying to it...
(If you do need to email, replace the account name with my true name.)
============================================================================
Loading...