/********* * Filename: ClESkyController.cs * Description: ARCHA Project - E-Sky EK2-0406A Mode 2 (Left Throttle) Controller Helper Class * Author: Marc Vieira Cardinal * Creation Date: October 11, 2008 * Revision Date: November 09, 2008 * Web: http://blog.ncode.ca */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.DirectX.DirectInput; namespace ARCHAGroundStation { class ClESkyController { private Device joystickDevice; //The directx joystick device handle private JoystickState state; //Last polled state private ClESkyController() //Private constructor { } public ClESkyController(System.Windows.Forms.Form p_parentForm) //Main and only valid constructor { //Find all the GameControl devices that are attached. DeviceList gameControllerList = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly); //Check that we have at least one device. if (gameControllerList.Count > 0) { //This loop will iterate though all the joystick devices and find one that has a name that begins with "PPM" while (gameControllerList.MoveNext()) { DeviceInstance deviceInstance = (DeviceInstance)gameControllerList.Current; if (deviceInstance.ProductName.Substring(0, 3) == "PPM") { // create a device from this controller. joystickDevice = new Device(deviceInstance.InstanceGuid); joystickDevice.SetCooperativeLevel(p_parentForm, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive); break; } } } //Did we find a ppm device? if (joystickDevice == null) throw new Exception("ERROR: PPM device not found!"); //Tell DirectX that this is a Joystick. joystickDevice.SetDataFormat(DeviceDataFormat.Joystick); //Finally, acquire the device. joystickDevice.Acquire(); //Find the capabilities of the joystick DeviceCaps cps = joystickDevice.Caps; //Number of Axes System.Console.WriteLine("Joystick Axis: " + cps.NumberAxes); //Number of Buttons System.Console.WriteLine("Joystick Buttons: " + cps.NumberButtons); //Number of PoV hats System.Console.WriteLine("Joystick PoV hats: " + cps.NumberPointOfViews); } //Getter for the Roll joystick public int GetRoll() { return state.X; } //Getter for the Pitch joystick public int GetPitch() { return state.Y; } //Getter for the Yaw joystick public int GetYaw() { return state.Ry; } //Getter for the Throttle joystick public int GetThrottle() { return state.Z; } //Called by an external heartbeat process, this will update the State variable with the current joystick state public void Poll() { try { //Poll the joystick joystickDevice.Poll(); //Update the joystick state variable state = joystickDevice.CurrentJoystickState; } catch (Exception err) { //An error has occured... System.Console.WriteLine(err.Message); } } } }