Adapter with auto-reconnect for the EliteGamepad
Example #1
0
        /// <summary>
        /// The function to do processing for the timer
        /// </summary>
        /// <param name="state">The object holding the state to be used by the timer..</param>
        private void TimerCallback(object state)
        {
            if (!Monitor.TryEnter(this.timerLock))
            {
                return;
            }

            try
            {
                if ((this.gamepad == null && !EliteGamepadAdapter.TryCreate(out this.gamepad)) || !this.gamepad.IsReady)
                {
                    return;
                }

                var reading = this.gamepad.GetCurrentUnmappedReading();
                if (this.ShouldSkipGamepadProcessing(reading))
                {
                    return;
                }

                this.WriteGamepadReadings(reading);
                this.ProcessGamepadReadings(reading);
            }
            catch (Exception)
            {
                // Do nothing, gamepad could be uninitialized
            }
            finally
            {
                Monitor.Exit(this.timerLock);
            }
        }
Example #2
0
        /// <summary>
        /// Tries to create a gamepad.
        /// </summary>
        /// <param name="gamepad">The gamepad.</param>
        /// <returns>True if a gamepad was created successfully; otherwise, false.</returns>
        public static bool TryCreate(out EliteGamepadAdapter gamepad)
        {
            EliteGamepad.CheckForDriverLoaded();
            var underlying = EliteGamepad.EliteGamepads.FirstOrDefault();
            if (underlying == null)
            {
                gamepad = null;
                return false;
            }

            gamepad = new EliteGamepadAdapter(underlying);
            return true;
        }
Example #3
0
        /// <summary>
        /// Tries the create.
        /// </summary>
        /// <param name="gamepadFriendlyName">ID of the game pad trying to be created.</param>
        /// <param name="gamepad">The gamepad.</param>
        /// <returns>True if a gamepad was created successfully; otherwise, false.</returns>
        public static bool TryCreate(string gamepadFriendlyName, out EliteGamepadAdapter gamepad)
        {
            EliteGamepad.CheckForDriverLoaded();
            var underlying = EliteGamepad.EliteGamepads.FirstOrDefault(g => g.FriendlyName == gamepadFriendlyName);

            if (underlying == null)
            {
                gamepad = null;
                return(false);
            }

            gamepad = new EliteGamepadAdapter(underlying);
            return(true);
        }
Example #4
0
        /// <summary>
        /// Tries to create a gamepad.
        /// </summary>
        /// <param name="gamepad">The gamepad.</param>
        /// <returns>True if a gamepad was created successfully; otherwise, false.</returns>
        public static bool TryCreate(out EliteGamepadAdapter gamepad)
        {
            EliteGamepad.CheckForDriverLoaded();
            var underlying = EliteGamepad.EliteGamepads.FirstOrDefault();

            if (underlying == null)
            {
                gamepad = null;
                return(false);
            }

            var config = underlying.GetConfiguration(underlying.CurrentSlotId);

            gamepad = new EliteGamepadAdapter(underlying);
            return(true);
        }
Example #5
0
        /// <summary>
        /// The function to do processing for the timer
        /// </summary>
        /// <param name="state">The object holding the state to be used by the timer..</param>
        private void TimerCallback(object state)
        {
            if (!Monitor.TryEnter(this.timerLock))
            {
                return;
            }

            try
            {
                if ((this.gamepad == null && !EliteGamepadAdapter.TryCreate(out this.gamepad)) || !this.gamepad.IsReady)
                {
                    return;
                }

                var reading = this.gamepad.GetCurrentUnmappedReading();
                if (this.ShouldSkipGamepadProcessing(reading))
                {
                    return;
                }

                this.WriteGamepadReadings(reading);
                this.ProcessGamepadReadings(reading);


                {
                    // Vibrations
                    Windows.Gaming.Input.Gamepad          gamepad = Windows.Gaming.Input.Gamepad.Gamepads.First();
                    Windows.Gaming.Input.GamepadVibration vibration;

                    EliteGamepadConfiguration config = this.gamepad.gamepad.GetConfiguration(this.gamepad.CurrentSlotId);

                    vibration.LeftMotor    = 0.0;
                    vibration.LeftTrigger  = 0.0;
                    vibration.RightMotor   = 0.0;
                    vibration.RightTrigger = 0.0;

                    // Set the vibration
                    // Left Trigger
                    if (reading.LeftTrigger != 0.0)
                    {
                        if (isVibrationEnabled == true)
                        {
                            // Normalize our data base on the set interval
                            double value = normalizeTriggerValue(reading.LeftTrigger, config.LeftTrigger.Min / 10, config.LeftTrigger.Max / 10);

                            // Set the vibration Based on the normalized value AND trigger scale factor!
                            vibration.LeftTrigger = value * config.ScaleFactorLeftTriggerMotor / 100.0f;
                            gamepad.Vibration     = vibration;
                        }
                    }
                    else
                    {
                        vibration.LeftTrigger = 0.0;
                        gamepad.Vibration     = vibration;
                    }

                    // Right Trigger
                    if (reading.RightTrigger != 0.0)
                    {
                        if (isVibrationEnabled == true)
                        {
                            // Normalize our data base on the set interval
                            double value = normalizeTriggerValue(reading.RightTrigger, config.RightTrigger.Min / 10, config.RightTrigger.Max / 10);

                            // Set the vibration Based on the normalized value AND trigger scale factor!
                            vibration.RightTrigger = value * config.ScaleFactorRightTriggerMotor / 100.0f;
                            gamepad.Vibration      = vibration;
                        }
                    }
                    else
                    {
                        vibration.RightTrigger = 0.0;
                        gamepad.Vibration      = vibration;
                    }
                }
            }
            catch (Exception)
            {
                // Do nothing, gamepad could be uninitialized
            }
            finally
            {
                Monitor.Exit(this.timerLock);
            }
        }