|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Reading multiple joysticks on C# using SlimDX.
Hello,
I am currently working on a scouting system for the upcoming FRC Season and I am having trouble getting more than one joystick to be read. The program receives data from the joysticks (like how many balls were attempted and made) then saves it to notepad. From notepad it can be read using Excel. The program works fine with one controller. Is there a way to do the same thing but with 6 different USB connected joysticks? |
|
#2
|
||||
|
||||
|
Re: Reading multiple joysticks on C# using SlimDX.
I'd be willing to look at your code. If you don't want to post it publicly, PM me.
This should give you a starting place. Code:
public static Joystick[] GetSticks()
{
var sticks = new List<SlimDX.DirectInput.Joystick>();
foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
{
// create the device
try
{
var stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
stick.Acquire();
foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
{
if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
}
sticks.Add(stick);
Console.WriteLine(stick.Information.InstanceName);
}
catch (DirectInputException)
{
}
}
return sticks.ToArray();
}
Last edited by EHaskins : 01-09-2012 at 01:42. Reason: Missed a bracket |
|
#3
|
||||
|
||||
|
Re: Reading multiple joysticks on C# using SlimDX.
I will use that for the starting point thank you and here is the code if anyone is interested.
Code:
public Form1()
{
InitializeComponent();
BootGamePort();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
BootGamePort();
}
Joystick mainScouter;
JoystickState state = new JoystickState();
void BootGamePort()
{
DirectInput dinput = new DirectInput();
foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
{
try
{
if (count == 0)
{
mainScouter = new Joystick(dinput, device.InstanceGuid);
}
count = 1;
break;
}
catch (DirectInputException)
{
}
}
if (mainScouter == null)
{
return;
}
foreach (DeviceObjectInstance deviceObject in mainScouter.GetObjects())
{
if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
mainScouter.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
}
mainScouter.Acquire();
}
public void timer1_Tick(object sender, EventArgs e)
{
if (mainScouter.Acquire().IsFailure)
{
return;
}
if (mainScouter.Poll().IsFailure)
{
return;
}
state = mainScouter.GetCurrentState();
if (Result.Last.IsFailure)
{
return;
}
bool[] buttons = state.GetButtons();
String strText = "";
for (int b = 0; b < buttons.Length; b++)
{
if (buttons[b])
{
strText += b.ToString("00 ", CultureInfo.CurrentCulture);
lblbuttons.Text = strText;
}
}
Last edited by sergioCorral842 : 06-09-2012 at 10:44. |
|
#4
|
||||
|
||||
|
Re: Reading multiple joysticks on C# using SlimDX.
I tested the code that EHaskins provided me and it can read every USB gamepad. I developed code so it can show which buttons are being pressed, but it only works if each gamepad is under a timer. The timer is used to refresh the labels which each button is assigned to change. The problem that I get in the code is in this line:
Code:
SlimDX.DirectInput.Joystick stick = new SlimDX.DirectInput.Joystick(Input. device.InstanceGuild); |
|
#5
|
||||
|
||||
|
Re: Reading multiple joysticks on C# using SlimDX.
First, shouldn't that be a comma after Input you the code you posted. If that's just a typo when copying to CD, there's probably a problem on the line before or after it.
Second, you should do that with a single timer. Something like: Code:
Joystick[] sticks;
void init() {//pageload, or new, or something similar
Sticks = GetSticks();//using my code from previous post
}
void TimerHandler (...){
for (int i = 0; i < sticks.Length; i++){
StickHandlingLogic(sticks[i], i); //replace the identifier with something meaningful to your application.
}
}
void StickhandlingLogic(Joystick stick, int id) { //pass the stick and some type of identifier, since you want to know which users stick this is
//Process the joystick data here
}
|
|
#6
|
||||
|
||||
|
Re: Reading multiple joysticks on C# using SlimDX.
Thanks for the help EHaskins. Now I am able to use all 6 gamepads that I need. Right now, whenever I press a certain button a counter on an integer goes up by one and is displayed on a label like this:
Code:
if (buttons[0])
{
basketsAttempted++;
}
Code:
if (buttons[4] & buttons[0])
{
basketsAttempted--;
}
|
|
#7
|
||||
|
||||
|
Re: Reading multiple joysticks on C# using SlimDX.
Try this. Either one is functionally the same, but I think the second may be more concise.
Code:
if (buttons[4] && buttons[0])
{
basketsAttempted--;
}
else if (buttons[0])
{
basketsAttempted++;
}
OR
if (buttons[0]){
if (buttons[4){
basketsAttempted--;
}
else{
basketsAttempted++;
}
}
|
|
#8
|
|||
|
|||
|
Re: Reading multiple joysticks on C# using SlimDX.
Hey Eric Haskins,
i am trying to program the dance pad programmed for a Dancing Revolution Game, could any one tell how i can take the input from their button..?? because i tried using the serial port input but it didn't worked out. even i used this code but its not returning the data. JoystickState state = new JoystickState(); // JoystickState state = joystick.CurrentJoystickState; ( If i use this and delete the above command then i get a error for Object reference not set to an instance of an object. ( NULL Reference Exception ) //Capture Buttons. byte[] buttons = state.GetButtons(); for (int i = 0; i < buttons.Length; i++) { if (buttons[i] != 0) { info += "Button:" + i + " "; label1.Text = info; } } |
|
#9
|
|||
|
|||
|
Re: Reading multiple joysticks on C# using SlimDX.
Hey Eric whats app..??
i am trying to program the dance pad programmed for a Dancing Revolution Game, could any one tell how i can take the input from their button..?? because i tried using the serial port input but it didn't worked out. even i used this code but its not returning the data. JoystickState state = new JoystickState(); // JoystickState state = joystick.CurrentJoystickState; ( If i use this and delete the above command then i get a error for Object reference not set to an instance of an object. ( NULL Reference Exception ) //Capture Buttons. byte[] buttons = state.GetButtons(); for (int i = 0; i < buttons.Length; i++) { if (buttons[i] != 0) { info += "Button:" + i + " "; label1.Text = info; } } |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|