public DirectInputManager() { var directInput = new DirectInput(); // Prefer a Driving device but make do with fallback to a Joystick if we have to var deviceInstance = FindAttachedDevice(directInput, DeviceType.Driving); if (null == deviceInstance) deviceInstance = FindAttachedDevice(directInput, DeviceType.Joystick); if (null == deviceInstance) throw new Exception("No Driving or Joystick devices attached."); joystickType = (DeviceType.Driving == deviceInstance.Type ? JoystickTypes.Driving : JoystickTypes.Joystick); // A little debug spew is often good for you Log.Spew("First Suitable Device Selected \"" + deviceInstance.InstanceName + "\":"); Log.Spew("\tProductName: " + deviceInstance.ProductName); Log.Spew("\tType: " + deviceInstance.Type); Log.Spew("\tSubType: " + deviceInstance.Subtype); // Data for both Driving and Joystick device types is received via Joystick joystick = new Joystick(directInput, deviceInstance.InstanceGuid); IntPtr consoleWindowHandle = GetConsoleWindow(); if (IntPtr.Zero != consoleWindowHandle) { CooperativeLevel cooperativeLevel = CooperativeLevel.Background | CooperativeLevel.Nonexclusive; Log.Spew("Console window cooperative level: " + cooperativeLevel); if (!joystick.SetCooperativeLevel(consoleWindowHandle, cooperativeLevel).IsSuccess) throw new Exception("Failed to set cooperative level for DirectInput device in console window."); } var result = joystick.Acquire(); if (!result.IsSuccess) throw new Exception("Failed to acquire DirectInput device."); Log.Spew("Joystick acquired."); }
/// <summary> /// Creates a new instance of a <see cref = "Device" /> object that can manage a single PPJoy virtual device. /// </summary> /// <param name = "lptNum">LPT number that the device is attached to. /// A value of Zero specifies a virtual device.</param> /// <param name = "type"><see cref = "JoystickTypes">JoystickType</see> of the device.</param> /// <param name = "subType"><see cref = "JoystickSubTypes">JoystickSubType</see> of this device.</param> /// <param name = "productId">Product ID associated with the device.</param> /// <param name = "vendorId">Vendor ID associated with the device.</param> /// <param name = "unitNum">Unit number of the device.</param> internal Device(int lptNum, JoystickTypes type, JoystickSubTypes subType, int productId, int vendorId, int unitNum) { if (_lptNum < 0) { throw new ArgumentOutOfRangeException(nameof(lptNum)); } _lptNum = lptNum; _joyType = type; SetSubtype(subType); _productId = productId; _vendorId = vendorId; SetUnitNumber(unitNum); }
public DirectInputManager() { var directInput = new DirectInput(); // Prefer a Driving device but make do with fallback to a Joystick if we have to var deviceInstance = FindAttachedDevice(directInput, DeviceType.Driving); if (null == deviceInstance) { deviceInstance = FindAttachedDevice(directInput, DeviceType.Joystick); } if (null == deviceInstance) { throw new Exception("No Driving or Joystick devices attached."); } joystickType = (DeviceType.Driving == deviceInstance.Type ? JoystickTypes.Driving : JoystickTypes.Joystick); // A little debug spew is often good for you Log.Spew("First Suitable Device Selected \"" + deviceInstance.InstanceName + "\":"); Log.Spew("\tProductName: " + deviceInstance.ProductName); Log.Spew("\tType: " + deviceInstance.Type); Log.Spew("\tSubType: " + deviceInstance.Subtype); // Data for both Driving and Joystick device types is received via Joystick joystick = new Joystick(directInput, deviceInstance.InstanceGuid); IntPtr consoleWindowHandle = GetConsoleWindow(); if (IntPtr.Zero != consoleWindowHandle) { CooperativeLevel cooperativeLevel = CooperativeLevel.Background | CooperativeLevel.Nonexclusive; Log.Spew("Console window cooperative level: " + cooperativeLevel); if (!joystick.SetCooperativeLevel(consoleWindowHandle, cooperativeLevel).IsSuccess) { throw new Exception("Failed to set cooperative level for DirectInput device in console window."); } } var result = joystick.Acquire(); if (!result.IsSuccess) { throw new Exception("Failed to acquire DirectInput device."); } Log.Spew("Joystick acquired."); }
public Home() { DataContext = this; Loaded += MainWindow_Loaded; InitializeComponent(); //Set up time to be used for timing user responses _timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) }; _timer.Tick += _timer_Tick; _timer.Start(); // Initialize history array to all 0's for (int i = 0; i < historySize; i++) { wheelHistory[i] = 0; } try { //Check if a serial input device is connected, if so open serial input either way set initial values to null try { //null values here are simply for intitialization prior to input sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived); sp.Open(); ArduinoSerialMsg = string.Format("PVT Results = Null"); PVTResults = string.Format("PVT Results = NULL ms |"); PVTAverage = string.Format("Average response time of: NULL ms over 0 tests."); ArduinoSerialMsg = string.Format("Left Sensor = NULL | Right Sensor = NULL | Button Status = NULL"); } catch (System.IO.IOException e) { //null values here will persist due to a lack of input ArduinoSerialMsg = string.Format("PVT Results = Null"); PVTResults = string.Format("PVT Results = NULL ms |"); PVTAverage = string.Format("Average response time of: NULL ms over 0 tests."); ArduinoSerialMsg = string.Format("Left Sensor = NULL | Right Sensor = NULL | Button Status = NULL"); MessageBox.Show("Arduino not connected!"); } } catch (System.NullReferenceException en) { //pop up message box to let user know there is no input MessageBox.Show("I/O not connected"); } var directInput = new DirectInput(); //Get steering wheel input //Check if there is input from a Driving device, which is preferred, if not, fallback to a Joystick. If neither throw an exception. var deviceInstance = FindAttachedDevice(directInput, DeviceType.Driving); if (null == deviceInstance) { deviceInstance = FindAttachedDevice(directInput, DeviceType.Joystick); } if (null == deviceInstance) { //throw new Exception("No Driving or Joystick devices attached.") MessageBox.Show("Wheel is not connected"); } else { //store whether the input is coming from a driving wheel or a joystick joystickType = (DeviceType.Driving == deviceInstance.Type ? JoystickTypes.Driving : JoystickTypes.Joystick); // Information about input driving device for debugging Console.WriteLine("First Suitable Device Selected \"" + deviceInstance.InstanceName + "\":"); Console.WriteLine("\tProductName: " + deviceInstance.ProductName); Console.WriteLine("\tType: " + deviceInstance.Type); Console.WriteLine("\tSubType: " + deviceInstance.Subtype); // Data for both Driving and Joystick devices is received via Joystick type joystick = new Joystick(directInput, deviceInstance.InstanceGuid); var result = joystick.Acquire(); //start retrieving data from the driving input foreach (DeviceObjectInstance deviceObject in joystick.GetObjects()) { if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0) { joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-5000, 5000); } } if (!result.IsSuccess) { throw new Exception("Failed to acquire DirectInput device."); } Console.WriteLine("Joystick acquired."); } }