Ejemplo n.º 1
2
        private static void StartJoystickCapture(CancellationToken token)
        {
            var joystickGuid = Guid.Empty;
            var di = new DirectInput();
            foreach (var device in di.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AttachedOnly))
            {
                joystickGuid = device.InstanceGuid;
                break;
            }
            _joystick = new SharpDX.DirectInput.Joystick(di, joystickGuid);
            _joystick.Acquire();
            _currentThrottle = 0;
            _currentYawTrim = 0;
            while (!token.IsCancellationRequested)
            {
                var state = _joystick.GetCurrentState();

                _currentThrottle = (1000 - GetAnalogStickValue(state.Sliders[0])) / 2;
                Helicopter.Command.MainThrottle = _currentThrottle;

                Helicopter.Command.Pitch = GetAnalogStickValue(state.Y);

                //Helicopter.Command.Yaw = -1 * (GetAnalogStickValue(state.X) + _currentYawTrim); //For n64 controller
                Helicopter.Command.Yaw = -1 * (GetAnalogStickValue(state.RotationZ) + _currentYawTrim);

                var yawtrimChange = GetYawTrimChange(state.Buttons);
                _currentYawTrim += yawtrimChange;
                Helicopter.Command.YawTrim = _currentYawTrim;

                SetConsoleDisplay(state);
                Thread.Sleep(Speed);
            }
        }
    public void Update(GameTime gameTime)
    {
        previousMouseState    = currentMouseState;
        previousKeyboardState = currentKeyboardState;
        previousGamePadState  = (GamePadState[])currentGamePadState.Clone();
        //previousJoyState = currentJoyState;

        currentMouseState    = Mouse.GetState();
        currentKeyboardState = Keyboard.GetState();

        foreach (PlayerIndex index in Enum.GetValues(typeof(PlayerIndex)))
        {
            currentGamePadState[(int)index] = GamePad.GetState(index);
        }

        if (RumbleDuration > 0)
        {
            GamePadVibration(PlayerIndex.One, leftMotor, rightMotor);
            rumbleDuration -= (float)gameTime.ElapsedGameTime.TotalSeconds;
        }

        if (!currentGamePadState[0].IsConnected && enableControllers && joystick == null)
        {
            JoystickPing -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (JoystickPing < 0)
            {
                JoystickPing = JoystickPingDuration;
                var th = new Thread(GenericControllerConnection);
                th.Start();
#if DEBUG
                Console.WriteLine("A new thread has been created!");
#endif
            }
        }
        else if (joystick != null && enableControllers)
        {
            joystick.Poll();
#if DEBUG
            Console.WriteLine("Polling Joystick...");
#endif
            try
            {
                JoystickState state = joystick.GetCurrentState();
                currentJoyState = joystick.GetCurrentState();
                bool[] button = state.Buttons;
                int[]  hats   = state.PointOfViewControllers;
                Console.WriteLine("[{0}]", string.Join(", ", hats));
            }
            catch (Exception)
            {
#if DEBUG
                Console.WriteLine("Oops, the controller disconnected!");
#endif
                joystick = null;
            }
        }
    }
Ejemplo n.º 3
0
 public void RefreshState()
 {
     if (padConnected)
     {
         PadState = Pad.GetCurrentState();
     }
     else
     {
         PadState = new DI.JoystickState();
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Collect the current data from the device
        /// </summary>
        public void GetAxisData(out int x, out int y, out int rz)
        {
            x = 0; y = 0; rz = 0;

            // Make sure there is a valid device.
            if (null == m_device)
            {
                return;
            }

            // Poll the device for info.
            try {
                m_device.Poll( );
            }
            catch (SharpDXException e) {
                if ((e.ResultCode == ResultCode.NotAcquired) || (e.ResultCode == ResultCode.InputLost))
                {
                    // Check to see if either the app needs to acquire the device, or
                    // if the app lost the device to another process.
                    try {
                        // Acquire the device.
                        m_device.Acquire( );
                    }
                    catch (SharpDXException) {
                        // Failed to acquire the device. This could be because the app doesn't have focus.
                        return; // EXIT unaquired
                    }
                }
                else
                {
                    log.Error("Unexpected Poll Exception", e);
                    return; // EXIT see ex code
                }
            }


            // Get the state of the device - retaining the previous state to find the lates change
            m_prevState = m_state;
            try { m_state = m_device.GetCurrentState( ); }
            // Catch any exceptions. None will be handled here,
            // any device re-aquisition will be handled above.
            catch (SharpDXException) {
                return;
            }

            x = m_state.X; y = m_state.Y; rz = m_state.RotationZ;
        }
Ejemplo n.º 5
0
    public static bool GetButton(SharpDX.DirectInput.Joystick joystick, int buttonNumber)
    {
        JoystickState state = new JoystickState();

        state = joystick.GetCurrentState();

        bool[] buttons = state.Buttons;
        return(buttons[buttonNumber] == true);
    }
Ejemplo n.º 6
0
        public override bool Loop()
        {
            if (joystick != null)
            {
                joystick.Poll();

                current_state = joystick.GetCurrentState();


                yaw   = (current_state.X / 1000) - 32;
                pitch = (current_state.Y / 1000) - 32;
                zoom  = (current_state.RotationZ / 1000) - 32;

                if (Math.Abs(yaw) <= 2)
                {
                    yaw = 0;
                }
                if (Math.Abs(pitch) <= 2)
                {
                    pitch = 0;
                }
                if (Math.Abs(zoom) <= 2)
                {
                    zoom = 0;
                }


                ahd_switch = (current_state.Buttons[0] ? 1 : 0) + (current_state.Buttons[1] ? 2 : 0) + (current_state.Buttons[2] ? 3 : 0) + (current_state.Buttons[3] ? 4 : 0) + (current_state.Buttons[5] ? 5 : 0);
                mode_click = (current_state.Buttons[4] ? 1 : 0);
                focus_dir  = (current_state.Buttons[6] ? 1 : 0) + (current_state.Buttons[7] ? 2 : 0);

                if ((yaw != old_yaw) || (pitch != old_pitch) || (zoom != old_zoom) || (ahd_switch != 0) || (mode_click != 0) || (focus_dir != 0))
                {
                    if (Host.cs.connected)
                    {
                        bool x = Host.comPort.doCommand(1, 0, MAVLink.MAV_CMD.DO_CONTROL_VIDEO, -1, focus_dir, mode_click, zoom, pitch, yaw, ahd_switch, false);
                    }
                    old_zoom  = zoom;
                    old_pitch = pitch;
                    old_yaw   = pitch;
                }
            }

            //Must use BeginIvoke to avoid deadlock with OnClose in main form.
            MainV2.instance.BeginInvoke((Action)(() =>
            {
                lab.Text = "y:" + yaw.ToString() + " p:" + pitch.ToString() + " z:" + zoom.ToString();
            }));

            return(true);
        }
Ejemplo n.º 7
0
 private void Poll()
 {
     if (_joystick == null)
     {
         return;
     }
     try
     {
         _joystick.Poll();
         _state = _joystick.GetCurrentState();
     }
     catch (Exception ex)
     {
         MainForm.LogExceptionToFile(ex);
     }
 }
Ejemplo n.º 8
0
 private void Poll()
 {
     if (_joystick == null)
     {
         return;
     }
     try
     {
         _joystick.Poll();
         _state = _joystick.GetCurrentState();
     }
     catch (Exception ex)
     {
         Logger.LogException(ex);
     }
 }
Ejemplo n.º 9
0
        public State Read()
        {
            _joystick.Poll();
            var state = _joystick.GetCurrentState();

            return(new State()
            {
                Axis = new Dictionary <Axis, double>()
                {
                    { Axis.X, TranformAxis(state.X) },
                    { Axis.Y, TranformAxis(state.Y) },
                    { Axis.Z, TranformAxis(state.Z) },
                    { Axis.XRot, TranformAxis(state.RotationX) },
                    { Axis.YRot, TranformAxis(state.RotationY) },
                    { Axis.ZRot, TranformAxis(state.RotationZ) }
                }
            });
        }
Ejemplo n.º 10
0
    public static Axe GetAxis(SharpDX.DirectInput.Joystick joystick)
    {
        if (joystick != null)
        {
            JoystickState state = new JoystickState();

            state = joystick.GetCurrentState();

            Axe axis = new Axe();
            axis.Xaxis = state.X;
            axis.Yaxis = state.Y;
            axis.Zaxis = state.Z;

            return(axis);
        }
        else
        {
            return(null);
        }
    }
        public List <InputDeviceEvent> GetEvents()
        {
            List <InputDeviceEvent> events = new List <InputDeviceEvent>();

            JoystickState js;

            try
            {
                js = stick.GetCurrentState();
            }
            catch
            {
                System.Diagnostics.Debug.WriteLine("Joystick removed!");
                return(null);
            }

            bool[] buttons = js.Buttons;

            for (int i = 0; i < butstate.Length; i++)
            {
                bool s = buttons[i];
                if (s != butstate[i])
                {
                    butstate[i] = s;
                    //System.Diagnostics.Debug.WriteLine("But " + (i + 1) + "=" + s);
                    events.Add(new InputDeviceEvent(this, ButtonBase + i, butstate[i]));
                }
            }

            int[] pov = js.PointOfViewControllers;

            for (int i = 0; i < povvalue.Length; i++)
            {
                if (pov[i] != povvalue[i])
                {
                    if (povvalue[i] != -1 && pov[i] != -1 && pov[i] != povvalue[i])         // if both previous and current is not released, and changed.. generate a fake release event
                    {
                        events.Add(new InputDeviceEvent(this, POVBase + i + 1, false, -1)); // this gives the caller indication that the current state has ended..
                    }
                    povvalue[i] = pov[i];
                    events.Add(new InputDeviceEvent(this, POVBase + i + 1, povvalue[i] != -1, povvalue[i]));
                }
            }

            int[] sliders = js.Sliders;

            for (int i = 0; i < axispresent.Length; i++)
            {
                if (axispresent[i])
                {
                    int value;
                    if (i == (int)Axis.X)
                    {
                        value = js.X;
                    }
                    else if (i == (int)Axis.Y)
                    {
                        value = js.Y;
                    }
                    else if (i == (int)Axis.Z)
                    {
                        value = js.Z;
                    }
                    else if (i == (int)Axis.RX)
                    {
                        value = js.RotationX;
                    }
                    else if (i == (int)Axis.RY)
                    {
                        value = js.RotationY;
                    }
                    else if (i == (int)Axis.RZ)
                    {
                        value = js.RotationZ;
                    }
                    else
                    {
                        value = sliders[i - (int)Axis.U];
                    }

                    if (axisvalue[i] == AxisNullValue)
                    {
                        axisvalue[i] = value;
                    }
                    else
                    {
                        int diff = Math.Abs(value - axisvalue[i]);
                        if (diff >= 5) // don't report min changes
                        {
                            axisvalue[i] = value;
                            if (axisevents)
                            {
                                events.Add(new InputDeviceEvent(this, AxisBase + i, true, value));      // axis is always pressed
                            }
                        }
                    }
                }
            }

            return((events.Count > 0) ? events : null);
        }
Ejemplo n.º 12
0
    private void Awake()
    {
        controllers = new List <SharpDX.DirectInput.Joystick>();
        var joystickGuid = Guid.Empty;
        var di           = new DirectInput();
        IList <DeviceInstance> keyboards = di.GetDevices(SharpDX.DirectInput.DeviceType.Keyboard, DeviceEnumerationFlags.AttachedOnly);

        for (int device = 0; device < keyboards.Count; device++)
        {
            joystickGuid = keyboards[device].InstanceGuid;
            controllers.Add(new Joystick(di, joystickGuid));
        }
        IList <DeviceInstance> gamepads = di.GetDevices(SharpDX.DirectInput.DeviceType.Gamepad, DeviceEnumerationFlags.AttachedOnly);

        for (int device = 0; device < gamepads.Count; device++)
        {
            joystickGuid = gamepads[device].InstanceGuid;
            controllers.Add(new Joystick(di, joystickGuid));
        }
        IList <DeviceInstance> joysticks = di.GetDevices(SharpDX.DirectInput.DeviceType.Joystick, DeviceEnumerationFlags.AttachedOnly);

        for (int device = 0; device < joysticks.Count; device++)
        {
            joystickGuid = joysticks[device].InstanceGuid;
            controllers.Add(new Joystick(di, joystickGuid));
        }
        if (playerNumber <= 2)
        {
            joystick = controllers[0];

            joystick.Acquire();
            if (playerNumber == 1)
            {
                button1 = PlayerPrefs.GetInt("Keyboard #1" + "1", 33);
                button2 = PlayerPrefs.GetInt("Keyboard #1" + "2", 34);
                dreapta = 31;
                stanga  = 29;
                jos     = 30;
                sus     = 16;
            }
            else
            {
                button1 = PlayerPrefs.GetInt("Keyboard #2" + "1", 50);
                button2 = PlayerPrefs.GetInt("Keyboard #2" + "2", 51);
                dreapta = 107;
                stanga  = 106;
                jos     = 109;
                sus     = 104;
            }
        }
        else if (playerNumber <= controllers.Count + 1)
        {
            joystick = controllers[playerNumber - 2];
            joystick.Acquire();
            button1 = PlayerPrefs.GetInt(joystick.Information.ProductName + "1", 0);
            button2 = PlayerPrefs.GetInt(joystick.Information.ProductName + "2", 2);
            Debug.Log(joystick.Properties.InterfacePath);
        }
        FindObjectOfType <PlayerData>().button1[playerNumber] = button1;
        FindObjectOfType <PlayerData>().button2[playerNumber] = button2;
        if (joystick != null)
        {
            defaultAxis = joystick.GetCurrentState().X;
        }
        teamNumber.text = "Team " + (team + 1);
        if (playerNumber >= 10)
        {
            opposite = true;
        }
        teamNumber.gameObject.SetActive(false);
        selected2.gameObject.SetActive(false);
        selected3.gameObject.SetActive(false);
        char1Bg.gameObject.SetActive(false);
        char2Bg.gameObject.SetActive(false);
        char3Bg.gameObject.SetActive(false);
        char1          = characters[0];
        char2          = characters[0];
        char3          = characters[0];
        char1Text.text = char1.ToString();
        char2Text.text = char2.ToString();
        char3Text.text = char3.ToString();
        FindObjectOfType <PlayerData>().playerNames[playerNumber] = char1.ToString() + char2.ToString() + char3.ToString();
    }
        /// <summary>Initialises the joystick.</summary>
        private void InitialiseJoystick()
        {
            List<DI.Joystick> joysticks = GetAttachedJoysticks();

            if(ControllerIndex < joysticks.Count)
            {
                _joystick = joysticks[ControllerIndex];
                _joystickState = _joystick.GetCurrentState();

                System.Diagnostics.Debug.WriteLine("Using joystick: " + _joystick.Information.InstanceName);
            }

            _joystickConnected = (_joystickState != null);
        }
        void Joystick_Poll(object Sender, EventArgs e)
        {
            if (joystickGuid != Guid.Empty)
            {
                if (!directInput.IsDeviceAttached(joystickGuid))
                {
                    joystick.Dispose();
                    joystick     = null;
                    joystickGuid = Guid.Empty;
                    joystickEnable.Dispatcher.Invoke(() =>
                    {
                        joystickEnable.IsChecked = false;
                        joystickEnable.IsEnabled = false;
                    });
                }
            }

            JoystickState state = null;

            if (joystick != null)
            {
                joystick.Poll();
                state = joystick.GetCurrentState();
            }
            if (state != null)
            {
                if (joystic_enabled)
                {
                    if (state.Buttons[6])
                    {
                        btnZoomIn_Click(null, null);
                    }
                    else if (state.Buttons[4])
                    {
                        btnZoomOut_Click(null, null);
                    }

                    if (state.Buttons[2] && !prevCamSwitchState)
                    {
                        prevCamSwitchState = true;
                        if (currentCamera == CameraSource.RGB_CAMERA_SOURCE)
                        {
                            setCameraVideoSource(CameraSource.IR_CAMERA_SOURCE);
                        }
                        else if (currentCamera == CameraSource.IR_CAMERA_SOURCE)
                        {
                            setCameraVideoSource(CameraSource.RGB_CAMERA_SOURCE);
                        }
                    }
                    else if (!state.Buttons[2])
                    {
                        prevCamSwitchState = false;
                    }

                    if (currentMode == GimbalMode.MODE_RATE)
                    {
                        double pitch_cmd = 0.0;
                        double yaw_cmd   = 0.0;
                        if (Math.Abs(state.Y - 32768) > 30)
                        {
                            pitch_cmd = ((state.Y - 32768.0) / 65536.0 * -2.0) * 3.14 / 2;
                        }
                        else
                        {
                            pitch_cmd = 0.0;
                        }
                        if (Math.Abs(state.X - 32768) > 30)
                        {
                            yaw_cmd = ((state.X - 32768.0) / 65536.0 * -2.0) * 3.14 / 2;
                        }
                        else
                        {
                            yaw_cmd = 0.0;
                        }
                        sendGimbalCommand(pitch_cmd, 0.0, yaw_cmd);
                    }
                }
            }
        }
Ejemplo n.º 15
0
 public override IMyJoystickState GetCurrentState()
 {
     joystick.Poll();
     return(new WindowsJoystickState(joystick.GetCurrentState()));
 }