Sabtu, 22 Oktober 2011

Interfacing Joystick and DirectX using WPF

In my company project, I have to interface the joystick to PC OS Window XP as Mission Control to control some actuator. In this task I use the DirectX Managed Library as tool and the WPF form as User Interfaces (UI). The version of Windows DirectX is 9. Then, the used joystick is consisted of three axis movement and one actuator button.

The First step of this task is adding the library Microsoft.DirectX.dll and Microsoft.DirectX.DirectInput.dll to application references. The location of library can be gotten in the Micrsoft.NET folder under the Windows drive. Importantly to notice that this code is just for DirectX 9 version because we haven’t tested to other version. Next, you can declare the library in the directive part like below,

using Microsoft.DirectX;


using Microsoft.DirectX.DirectInput;

Then make the function for initialize the Joystick input. Write the code like this,
private bool InitDirectInput(System.Windows.Window window)

        {

            foreach (DeviceInstance instance in

                Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly))

            {

                _applicationDevice = new Device(instance.InstanceGuid);

                break;

            }

            if ((_applicationDevice == null))

            {

                throw new DirectXException("Unable to initialize the joystick device.", new Exception(

                    "No joystick or compatible joystick was was found attached to the computer."));

            }

            _applicationDevice.SetDataFormat(DeviceDataFormat.Joystick);

            foreach (DeviceObjectInstance d in _applicationDevice.Objects)

            {

                if (0 != (d.ObjectId & System.Convert.ToInt32(DeviceObjectTypeFlags.Axis)))

                {

                    _applicationDevice.Properties.SetRange(ParameterHow.ById, d.ObjectId,

                        new InputRange(_intRangeMin, _intRangeMax));

                }

            }

            return true;

        }

The code will return the true value of the joystick is connected but it will give the reverse value or false value that indicates that joystick is not connected. Also, you can change the variable _intRangeMin and _intRangeMax to be the integer value you want for example 0 and 10000.

Next step, you have to write the function to get the current state of joystick. This is the code
public void GetData()

        {

            if (null == _applicationDevice)

            {

                return;

            }

            try

            {

                _applicationDevice.Poll();

            }

            catch (InputException inputex)

            {

                if (inputex is NotAcquiredException | inputex is InputLostException)

                {

                    try

                    {

                        _applicationDevice.Acquire();

                    }

                    catch

                    {

                        return;

                    }

                }

            }

            try

            {

                _state = _applicationDevice.CurrentJoystickState;

            }

            catch

            {

                return;

            }

        }

Of course, you have to declare the _state as the public variable in part before. So if you have written all of those code in some class or not you can access the and get that value of joystick current state.  You can use the timer to trigger the event to get the code which each event you write the accessing the joystick state like below,
            js.GetData();

            string strText = null;

            labelXAxis.Content = _applicationDevice.CurrentJoystickState.X.ToString();

            labelYAxis.Content = _applicationDevice.CurrentJoystickState.Y.ToString();

            labelZAxis.Content = _applicationDevice.CurrentJoystickState.Z.ToString();

            byte[] buttons = _applicationDevice.CurrentJoystickState.GetButtons();

            for (int x = 1; x <= 12; x++)

            {

                if (js.ButtonPressed(x) == true)

                {

                    strText += x + " ";

                }

            }

            labelButton.Content = strText;

Note that js is the class object which contains the code I have explained before. This code will write to each label component in window WPF and write the button pressed to other label component too. If you need the complete code, you can sen the request to me. I hope this can be helps.

Tidak ada komentar:

Posting Komentar