View Single Post
  #5   Spotlight this post!  
Unread 10-07-2013, 18:54
sergioCorral842's Avatar
sergioCorral842 sergioCorral842 is offline
College Student
FRC #0842 (Falcon Robotics)
Team Role: Alumni
 
Join Date: Aug 2011
Rookie Year: 2012
Location: Phoenix, Arizona
Posts: 161
sergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to behold
Re: Reading one Joystick (PS1/PS2) using slimDX and C#

Hello,
I used Slim DX and C# to create a scouting system that used ps3 controllers to change several different numbers. Here is the part of my code that gets all the usb joysticks connected to the computer.

Code:
namespace FalconScoutingSoftware
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            GetSticks();
            Sticks = GetSticks();
            stick1 = stick;
            timer1.Enabled = true;
        }

        DirectInput Input = new DirectInput();
        SlimDX.DirectInput.Joystick stick;
        Joystick[] Sticks;
        Joystick stick1;
   
        //Thumstick variables.
        int yValue = 0;
        int xValue = 0;
        int zValue = 0;
        int rotationZValue = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            Joystick[] joystick = GetSticks();
        }

        public Joystick[] GetSticks()
        {

            List<SlimDX.DirectInput.Joystick> sticks = new List<SlimDX.DirectInput.Joystick>(); // Creates the list of joysticks connected to the computer via USB.
            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // Creates a joystick for each game device in USB Ports
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    // Gets the joysticks properties and sets the range for them.
                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                    }

                    // Adds how ever many joysticks are connected to the computer into the sticks list.
                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                }
            }
            return sticks.ToArray();
        }

        //Creates the StickHandlingLogic Method which takes all the joysticks in the sticks List and puts them into a timer.
        public void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < Sticks.Length; i++)
            {
                StickHandlingLogic(Sticks[i], i);
            }
        }
   }
}
This will be the method that is called by the timer every time it ticks:
Code:
        void StickHandlingLogic(Joystick stick, int id)
        {
            // Creates an object from the class JoystickState.
            JoystickState state = new JoystickState();

            state = stick.GetCurrentState(); //Gets the state of the joystick
            
            //These are for the thumbstick readings
            yValue = -state.Y;
            xValue = state.X;
            zValue = state.Z;
            rotationZValue = -state.RotationZ;

            bool[] buttons = state.GetButtons(); // Stores the number of each button on the gamepad into the bool[] butons.
            //Here is an example on how to use this for the joystick in the first index of the array list
            if(id == 0)
            {
                  // This is when button 0 of the gamepad is pressed, the label will change. Button 0 should be the square button.
                  if(buttons[0])
                  {
                        label1.Text = "Worked";
                  }
                  
            }
       }
__________________
Team 842 Falcon Robotics: 2011-2015 (President, Head Programmer, Drive Team)
Arizona Regional Winners: 2012-2014 (Thanks 610, 1212, 1726, 3944, 2486, 2403)
Curie Division Semi-Finalists: 2014 (Thanks 1311, 2013, 2928)
Las Vegas Semi-Finalists: 2015 (Thanks 3495, 4415)
Curie Division Quarter-Finalists: 2015 (Thanks 107, 228, 649)

2015 - 2016: Programming, Drive Team, and Strategy Mentor for Team 3187, The Titans

Studying Computer Science- Software Engineering at ASU (Class of 2019)