void InitWheel()
    {
        if (inited == null)
        {
            inited = this;
        }
        else
        {
            return;
        }

        if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.Windows)
        {
            try
            {
                DirectInputWrapper.Init();
            }
            catch (DllNotFoundException)
            {
                // in case DirectInput wrapper dll file is not found
                Debug.Log("DirectInput wrapper dll file is not found");
                available = false;
                return;
            }

            for (int i = 0; i < DirectInputWrapper.DevicesCount(); i++)
            {
                if (!DirectInputWrapper.HasForceFeedback(i))
                {
                    continue;
                }
                wheelIndex = i;
                available  = true;
                break;
            }

            if (!available)
            {
                Debug.Log("STEERINGWHEEL: Multiple devices and couldn't find steering wheel device index");
                return;
            }
        }
        else
        {
            // WARNING: Input.GetJoystickNames or GetAxis/Buttons will crash if no valid Joystick is connected
            available = Environment.GetEnvironmentVariable("SDL_GAMECONTROLLERCONFIG") != null;
            // if (available)
            // {
            //     foreach (var joy in Input.GetJoystickNames())
            //     {
            //         Debug.Log($"Available joystick: {joy}, Preconfigured = {Input.IsJoystickPreconfigured(joy)}");
            //     }
            // }
        }
    }
    protected override void Start()
    {
        base.Start();

        debugStyle                  = new GUIStyle();
        debugStyle.fontSize         = 45;
        debugStyle.normal.textColor = Color.white;

        if (inited == null)
        {
            inited = this;
        }
        else
        {
            return;
        }

        DirectInputWrapper.Init();

        bool ff0 = DirectInputWrapper.HasForceFeedback(0);

        if (DirectInputWrapper.DevicesCount() > 1)
        {
            bool ff1 = DirectInputWrapper.HasForceFeedback(1);

            if (ff1 && !ff0)
            {
                wheelIndex = 1;
                pedalIndex = 0;
            }
            else if (ff0 && !ff1)
            {
                wheelIndex = 0;
                pedalIndex = 1;
            }
            else
            {
                //Debug.Log("STEERINGWHEEL: Multiple devices and couldn't find steering wheel device index");
                wheelIndex = 0;
                pedalIndex = 1;
            }
        }

        minBrake  = AppController.Instance.appSettings.minBrake;
        maxBrake  = AppController.Instance.appSettings.maxBrake;
        minGas    = AppController.Instance.appSettings.minGas;
        maxGas    = AppController.Instance.appSettings.maxGas;
        gasAxis   = AppController.Instance.appSettings.gasAxis;
        brakeAxis = AppController.Instance.appSettings.brakeAxis;
        FFBGain   = AppController.Instance.appSettings.FFBMultiplier;
    }
    void Start()
    {
        Debug.Log("init: " + DirectInputWrapper.Init());
        Debug.Log("Count: " + DirectInputWrapper.DevicesCount());

        for (int i = 0; i < DirectInputWrapper.DevicesCount(); i++)
        {
            Debug.Log("Name: " + DirectInputWrapper.GetProductNameManaged(i));
            Debug.Log("FFB: " + DirectInputWrapper.HasForceFeedback(i));
            
            for(int j = 0; j < DirectInputWrapper.GetNumEffects(i); j++)
            {
                Debug.Log("EFFECT: " + DirectInputWrapper.GetEffectNameManaged(i, j));
            }
        }
    }
    public int reportMasterWheel(string wheelName)
    {
        int returnVal = -1;

        for (int i = 0; i < DirectInputWrapper.DevicesCount(); i++)
        {
            if (DirectInputWrapper.GetProductNameManaged(i) == wheelName)
            {
                Debug.Log("We got the FantaTec as a Master now! ");

                returnVal = i;
                break;
            }
            else
            {
                Debug.Log("Number" + i + "is not fanatec, moving on");
            }
        }


        return(returnVal);
    }
    // Use this for initialization
    void Start()
    {
        DirectInputWrapper.Init();

        /*LogitechGSDK.LogiControllerPropertiesData properties = new LogitechGSDK.LogiControllerPropertiesData();
         * properties.wheelRange = 900;
         * properties.forceEnable = true;
         * properties.overallGain = 80;
         * properties.springGain = 80;
         * properties.damperGain = 80;
         * properties.allowGameSettings = true;
         * properties.combinePedals = true;
         * properties.defaultSpringEnabled = false;
         * properties.defaultSpringGain = 0;
         * LogitechGSDK.LogiSetPreferredControllerProperties(properties);
         */

        bool ff0 = DirectInputWrapper.HasForceFeedback(0);

        if (DirectInputWrapper.DevicesCount() > 1)
        {
            bool ff1 = DirectInputWrapper.HasForceFeedback(1);

            if (ff1 && !ff0)
            {
                wheelIndex = 1;
                pedalIndex = 0;
            }
            else if (ff0 && !ff1)
            {
                wheelIndex = 0;
                pedalIndex = 1;
            }
            else
            {
                Debug.Log("STEERINGWHEEL: Multiple devices and couldn't find steering wheel device index");
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        DirectInputWrapper.Update();

        DeviceState rec = DirectInputWrapper.GetStateManaged(wheelIndex);

        //    steerInput = rec.lX / 32768f;
        //      accelInput = rec.rglSlider[0] / -32768f;
        x  = rec.lX;
        y  = rec.lY;
        z  = rec.lZ;
        s0 = rec.rglSlider[0];
        s1 = rec.rglSlider[1];

        Debug.Log("d0 = " + DirectInputWrapper.GetProductNameManaged(wheelIndex));
        Debug.Log("ff0 = " + DirectInputWrapper.HasForceFeedback(wheelIndex));

        if (forceFeedbackPlaying)
        {
            DirectInputWrapper.PlayConstantForce(wheelIndex, constant);
        }


        if (DirectInputWrapper.DevicesCount() > 1)
        {
            Debug.Log("d1 = " + DirectInputWrapper.GetProductNameManaged(pedalIndex));
            Debug.Log("ff1 = " + DirectInputWrapper.HasForceFeedback(pedalIndex));
            DeviceState rec2 = DirectInputWrapper.GetStateManaged(pedalIndex);
            //     accelInput = ((32768 + rec2.lZ) - (rec2.lY + 32768f)) / 65535f;
            x2  = rec2.lX;
            y2  = rec2.lY;
            z2  = rec2.lZ;
            s02 = rec2.rglSlider[0];
            s12 = rec2.rglSlider[1];
        }
    }
Exemple #7
0
    public void OnUpdate()
    {
        if (workingInstance != this || !available || !enable)
        {
            return;
        }

        float accelInput;
        uint  pov;

        byte[] buttons;

        if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.Windows)
        {
            if (DirectInputWrapper.DevicesCount() == 0)
            {
                timeAccumulator += UnityEngine.Time.deltaTime;
                if (timeAccumulator >= 1.0f)
                {
                    Init();
                    timeAccumulator = 0.0f;
                }
            }

            DirectInputWrapper.Update();

            DeviceState state;
            if (!DirectInputWrapper.GetStateManaged(wheelIndex, out state))
            {
                var error = $"Can not get valid device state for steering wheel {wheelIndex}, try reconnect and restart game to fix";
                Debug.Log(error);
                stateFail = error;
                available = false;
                return;
            }
            SteerInput = state.lX / 32768f;
            accelInput = (state.lY - state.lRz) / -32768f;

            if (!useFeedback || !useGroundFeedbackForce)
            {
                DirectInputWrapper.PlayConstantForce(wheelIndex, 0);
                DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.Clamp(Mathf.RoundToInt(damper * forceFeedbackGain), -10000, 10000));
                DirectInputWrapper.PlaySpringForce(wheelIndex, 0, 0, springCoefficient);
            }

            if (useFeedback && (useGroundFeedbackForce || (autonomousBehavior != SteerWheelAutonomousFeedbackBehavior.None)))
            {
                float totalConstForce = 0.0f;
                if (useGroundFeedbackForce)
                {
                    totalConstForce += constant * forceFeedbackGain;
                }

                if (autonomousBehavior != SteerWheelAutonomousFeedbackBehavior.None)
                {
                    totalConstForce += autoforce * autoForceGain;
                }

                //applying effects to steering wheel
                var value = Mathf.RoundToInt(totalConstForce);
                if (value < -100)
                {
                    value = Mathf.Clamp(value, -10000, -800);
                }
                if (value > 100)
                {
                    value = Mathf.Clamp(value, 800, 10000);
                }

                DirectInputWrapper.PlayConstantForce(wheelIndex, Mathf.Clamp(value, -10000, 10000));
                if (useGroundFeedbackForce)
                {
                    DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.Clamp(Mathf.RoundToInt(damper * forceFeedbackGain), -10000, 10000));
                    DirectInputWrapper.PlaySpringForce(wheelIndex, 0, Mathf.Clamp(Mathf.RoundToInt(springSaturation * forceFeedbackGain), 0, 10000), springCoefficient);
                }
            }

            pov     = state.rgdwPOV[0];
            buttons = state.rgbButtons;
        }
        else
        {
            SteerInput = Input.GetAxis("Steering");

            // pedal range is -1 (not pressed) to +1 (pressed)
            // but by default when user has not pressed pedal the Unity reports value 0
            float accel = Input.GetAxis("Acceleration");
            float brake = Input.GetAxis("Braking");
            if (accel != 0.0f)
            {
                accelPressedOnce = true;
            }
            if (brake != 0.0f)
            {
                brakePressedOnce = true;
            }
            if (!accelPressedOnce)
            {
                accel = -1.0f;
            }
            if (!brakePressedOnce)
            {
                brake = -1.0f;
            }
            accelInput = (accel - brake) / 2.0f;

            pov = 0;
            // TODO
            // if (Input.GetButton("SelectUp")) pov = 0;
            // else if (Input.GetButton("SelectRight")) pov = 9000;
            // else if (Input.GetButton("SelectDown")) pov = 18000;
            // else if (Input.GetButton("SelectLeft")) pov = 27000;

            buttons = new byte [32];
            // TODO: here fill buttons according to buttonMapping array above
            buttons[0]  = (byte)(Input.GetButton("TurnOffAutonomousMode") ? 1 : 0);
            buttons[1]  = (byte)(Input.GetButton("ToggleMainCamera") ? 1 : 0);
            buttons[8]  = (byte)(Input.GetButton("ShiftUp") ? 1 : 0);
            buttons[9]  = (byte)(Input.GetButton("ShiftDown") ? 1 : 0);
            buttons[10] = (byte)(Input.GetButton("EngineStartStop") ? 1 : 0);
        }

        if (accelInput >= 0)
        {
            AccelBrakeInput = pedalInput == null ? accelInput : pedalInput.throttleInputCurve.Evaluate(accelInput);
        }
        else
        {
            AccelBrakeInput = pedalInput == null ? accelInput: -pedalInput.brakeInputCurve.Evaluate(-accelInput);
        }

        foreach (var m in buttonMapping)
        {
            if (buttons[m.Key] != 0)
            {
                TriggerDown(m.Value);
            }

            if (oldButtons[m.Key] == 0 && buttons[m.Key] != 0)
            {
                TriggerPress(m.Value);
            }
            else if (oldButtons[m.Key] != 0 && buttons[m.Key] == 0)
            {
                TriggerRelease(m.Value);
            }
        }

        System.Array.Copy(buttons, oldButtons, oldButtons.Length);

        Action <uint, Action <InputEvent> > povAction = (uint value, Action <InputEvent> action) =>
        {
            switch (value)
            {
            case 0:
                action(InputEvent.SELECT_UP);
                break;

            case 9000:
                action(InputEvent.SELECT_RIGHT);
                break;

            case 18000:
                action(InputEvent.SELECT_DOWN);
                break;

            case 27000:
                action(InputEvent.SELECT_LEFT);
                break;

            default:
                break;
            }
        };

        povAction(pov, x => TriggerDown(x));

        if (pov != oldPov)
        {
            povAction(oldPov, x => TriggerRelease(x));
            povAction(pov, x => TriggerPress(x));
        }

        oldPov = pov;
    }
Exemple #8
0
    public override void OnUpdate()
    {
        if (inited != this)
        {
            return;
        }

        //check for SelectLeft/right actions
        if (currentSelectState == SelectState.REST && GetSteerInput() < -selectThreshold)
        {
            currentSelectState = SelectState.LEFT;
            TriggerEvent(EventType.SELECT_CHOICE_LEFT);
        }
        else if (currentSelectState == SelectState.LEFT && GetSteerInput() > -selectThreshold)
        {
            currentSelectState = SelectState.REST;
        }
        else if (currentSelectState == SelectState.REST && GetSteerInput() > selectThreshold)
        {
            currentSelectState = SelectState.RIGHT;
            TriggerEvent(EventType.SELECT_CHOICE_RIGHT);
        }
        else if (currentSelectState == SelectState.RIGHT && GetSteerInput() < selectThreshold)
        {
            currentSelectState = SelectState.REST;
        }

        //Check for Throttle confirm
        if (isConfirmDown && GetAccelBrakeInput() < selectThreshold)
        {
            isConfirmDown = false;
        }
        else if (!isConfirmDown && GetAccelBrakeInput() > selectThreshold)
        {
            isConfirmDown = true;
            if (Time.timeSinceLevelLoad > confirmTimeout)
            {
                TriggerEvent(EventType.SELECT_CHOICE_CONFIRM);
            }
        }



        DirectInputWrapper.Update();

        {
            DeviceState state = DirectInputWrapper.GetStateManaged(wheelIndex);
            steerInput = state.lX / 32768f;
            accelInput = state.rglSlider[0] / -32768f;

            /* x = state.lX;
             * y = state.lY;
             * z = state.lZ;
             * s0 = state.rglSlider[0];
             * s1 = state.rglSlider[1];*/
            if (forceFeedbackPlaying)
            {
                DirectInputWrapper.PlayConstantForce(wheelIndex, Mathf.RoundToInt(constant * FFBGain));
                DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.RoundToInt(damper * FFBGain));
                DirectInputWrapper.PlaySpringForce(wheelIndex, 0, Mathf.RoundToInt(springSaturation * FFBGain), springCoefficient);
            }


            if (DirectInputWrapper.DevicesCount() > 1)
            {
                DeviceState state2 = DirectInputWrapper.GetStateManaged(pedalIndex);
                int         gas    = 0;
                int         brake  = 0;

                /* x2 = state2.lX;
                 * y2 = state2.lY;
                 * z2 = state2.lZ;
                 * s02 = state2.rglSlider[0];
                 * s12 = state2.rglSlider[1];*/

                switch (gasAxis)
                {
                case "X":
                    gas = state2.lX;
                    break;

                case "Y":
                    gas = state2.lY;
                    break;

                case "Z":
                    gas = state2.lZ;
                    break;
                }

                switch (brakeAxis)
                {
                case "X":
                    brake = state2.lX;
                    break;

                case "Y":
                    brake = state2.lY;
                    break;

                case "Z":
                    brake = state2.lZ;
                    break;
                }


                float totalGas   = (maxGas - minGas);
                float totalBrake = (maxBrake - minBrake);

                accelInput = (gas - minGas) / totalGas - (brake - minBrake) / totalBrake;
            }
        }
    }
    protected override void Start()
    {
        base.Start();

        debugStyle                  = new GUIStyle();
        debugStyle.fontSize         = 45;
        debugStyle.normal.textColor = Color.white;

        if (inited == null)
        {
            inited = this;
        }
        else
        {
            return;
        }

        DirectInputWrapper.Init();

        MasterSteeringWheel = false;
        masterIndex         = reportMasterWheel("FANATEC CSL Elite Wheel Base");
        if (masterIndex > -1)
        {
            MasterSteeringWheel = true;
        }


        bool ff0 = DirectInputWrapper.HasForceFeedback(0); /// Thios part mworks for now as the main inputs are always 0 or 1 we need code here though tat jumps over the master wheel shpould it be present

        if (DirectInputWrapper.DevicesCount() > 1)         // steering one and two should be padles and participant steering wheel
        {
            bool ff1 = DirectInputWrapper.HasForceFeedback(1);

            if (ff1 && !ff0)
            {
                wheelIndex = 1;
                pedalIndex = 0;
            }
            else if (ff0 && !ff1)
            {
                wheelIndex = 0;
                pedalIndex = 1;
            }
            else
            {
                Debug.Log("STEERINGWHEEL: Multiple devices and couldn't find steering wheel device index");
            }
        }


        if (MasterSteeringWheel)
        {
            minBrake = AppController.Instance.appSettings.minBrakeFanatec;
            maxBrake = AppController.Instance.appSettings.maxBrakeFanatec;
            minGas   = AppController.Instance.appSettings.minGasFanatec;
            maxGas   = AppController.Instance.appSettings.maxGasFanatec;
        }
        else
        {
            minBrake = AppController.Instance.appSettings.minBrake;
            maxBrake = AppController.Instance.appSettings.maxBrake;
            minGas   = AppController.Instance.appSettings.minGas;
            maxGas   = AppController.Instance.appSettings.maxGas;
        }

        gasAxis   = AppController.Instance.appSettings.gasAxis;
        brakeAxis = AppController.Instance.appSettings.brakeAxis;
        // FWheel = AppController.Instance.appSettings.FantacWheel;
        FFBGain = AppController.Instance.appSettings.FFBMultiplier;
    }
    public override void OnUpdate()
    {
        if (inited != this)
        {
            return;
        }

        //check for SelectLeft/right actions
        if (currentSelectState == SelectState.REST && GetSteerInput() < -selectThreshold)
        {
            currentSelectState = SelectState.LEFT;
            TriggerEvent(EventType.SELECT_CHOICE_LEFT);
        }
        else if (currentSelectState == SelectState.LEFT && GetSteerInput() > -selectThreshold)
        {
            currentSelectState = SelectState.REST;
        }
        else if (currentSelectState == SelectState.REST && GetSteerInput() > selectThreshold)
        {
            currentSelectState = SelectState.RIGHT;
            TriggerEvent(EventType.SELECT_CHOICE_RIGHT);
        }
        else if (currentSelectState == SelectState.RIGHT && GetSteerInput() < selectThreshold)
        {
            currentSelectState = SelectState.REST;
        }

        //Check for Throttle confirm
        if (isConfirmDown && GetAccelBrakeInput() < selectThreshold)
        {
            isConfirmDown = false;
        }
        else if (!isConfirmDown && GetAccelBrakeInput() > selectThreshold)
        {
            isConfirmDown = true;
            if (Time.timeSinceLevelLoad > confirmTimeout)
            {
                TriggerEvent(EventType.SELECT_CHOICE_CONFIRM);
            }
        }



        if (Application.platform != RuntimePlatform.OSXEditor)
        {
            DirectInputWrapper.Update();

            {
                DeviceState state;
                DeviceState slaveState;


                if (MasterSteeringWheel)
                {
                    state         = DirectInputWrapper.GetStateManaged(masterIndex);
                    slaveState    = DirectInputWrapper.GetStateManaged(wheelIndex);
                    slaveSteering = slaveState.lX / 32768f;
                }
                else
                {
                    state = DirectInputWrapper.GetStateManaged(wheelIndex);
                }

                steerInput = state.lX / 32768f;
                accelInput = state.rglSlider [0] / -32768f;


                // Debug.Log("Device One: \tlRx: " + state.lRx + "\tlRy: " + state.lRy + "\tlRz: " + state.lRz + "\tlX: " + state.lX + "\tlY: " + state.lY + "\tlZ: " + state.lZ);


                /* x = state.lX;
                 * y = state.lY;
                 * z = state.lZ;
                 * s0 = state.rglSlider[0];
                 * s1 = state.rglSlider[1];*/
                if (forceFeedbackPlaying)
                {
                    if (MasterSteeringWheel)
                    {
                        DirectInputWrapper.PlayConstantForce(masterIndex, Mathf.RoundToInt(constant * FFBGain));
                        DirectInputWrapper.PlayDamperForce(masterIndex, Mathf.RoundToInt(damper * FFBGain));
                        DirectInputWrapper.PlaySpringForce(masterIndex, 0, Mathf.RoundToInt(springSaturation * FFBGain), springCoefficient);

                        DirectInputWrapper.PlayConstantForce(wheelIndex, Mathf.RoundToInt(slaveConstant * FFBGain));
                        DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.RoundToInt(slaveDamper * FFBGain));
                        DirectInputWrapper.PlaySpringForce(wheelIndex, 0, Mathf.RoundToInt(slaveSpringSaturation * FFBGain), slaveSpringCoefficient);
                    }
                    else
                    {
                        DirectInputWrapper.PlayConstantForce(wheelIndex, Mathf.RoundToInt(constant * FFBGain));
                        DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.RoundToInt(damper * FFBGain));
                        DirectInputWrapper.PlaySpringForce(wheelIndex, 0, Mathf.RoundToInt(springSaturation * FFBGain), springCoefficient);
                    }
                }
                if (DirectInputWrapper.DevicesCount() > 1 || MasterSteeringWheel)
                {
                    int gas   = 0;
                    int brake = 0;
                    if (DirectInputWrapper.DevicesCount() > 1 && !MasterSteeringWheel)
                    {
                        DeviceState state2 = DirectInputWrapper.GetStateManaged(pedalIndex);

                        /* x2 = state2.lX;
                         * y2 = state2.lY;
                         * z2 = state2.lZ;
                         * s02 = state2.rglSlider[0];
                         * s12 = state2.rglSlider[1];*/
                        switch (gasAxis)
                        {
                        case "X":
                            gas = state2.lX;
                            break;

                        case "Y":
                            gas = state2.lY;
                            break;

                        case "Z":
                            gas = state2.lZ;
                            break;
                        }

                        switch (brakeAxis)
                        {
                        case "X":
                            brake = state2.lX;
                            break;

                        case "Y":
                            brake = state2.lY;
                            break;

                        case "Z":
                            brake = state2.lZ;
                            break;
                        }
                    }
                    if (MasterSteeringWheel)
                    {
                        brake = state.lRz;
                        gas   = state.lY;
                    }
                    //Debug.Log(brake.ToString() + " break and gas" + gas.ToString());
                    float totalGas   = (maxGas - minGas);
                    float totalBrake = (maxBrake - minBrake);

                    accelInput = (gas - minGas) / totalGas - (brake - minBrake) / totalBrake;
                }
            }
        }
    }