예제 #1
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;
        }
    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;
            }
        }
    }
예제 #3
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);
        }
예제 #4
0
 public void Pull()
 {
     if (value != 1000000)
     {
         // Poll events from joystick
         joystick.Poll();
         var datas = joystick.GetBufferedData();
         foreach (var state in datas)
         {
             key   = Convert.ToString(state.Offset);
             value = state.Value;
         }
     }
 }
예제 #5
0
        public SharpDX.DirectInput.Joystick AcquireJoystick(string name)
        {
            joystick = getJoyStickByName(name);

            if (joystick == null)
            {
                return(null);
            }

            joystick.Acquire();

            joystick.Poll();

            return(joystick);
        }
예제 #6
0
        public override bool AcquireJoystick(string name)
        {
            joystick = getJoyStickByNameInternal(name);

            if (joystick == null)
            {
                return(false);
            }

            joystick.Acquire();

            joystick.Poll();

            return(true);
        }
예제 #7
0
 private void Poll()
 {
     if (_joystick == null)
     {
         return;
     }
     try
     {
         _joystick.Poll();
         _state = _joystick.GetCurrentState();
     }
     catch (Exception ex)
     {
         Logger.LogException(ex);
     }
 }
예제 #8
0
 private void Poll()
 {
     if (_joystick == null)
     {
         return;
     }
     try
     {
         _joystick.Poll();
         _state = _joystick.GetCurrentState();
     }
     catch (Exception ex)
     {
         MainForm.LogExceptionToFile(ex);
     }
 }
예제 #9
0
        public void readInput()
        {
            if (gamepadDevice == null)
            {
                return;
            }

            SharpDX.DirectInput.Joystick joystick = new SharpDX.DirectInput.Joystick(directInput, gamepadDevice.InstanceGuid);
            joystick.Properties.BufferSize = 128;

            joystick.Acquire();
            while (true)
            {
                if (joystick.IsDisposed)
                {
                    return;
                }
                joystick.Poll();
                var datas = joystick.GetBufferedData();
                foreach (var state in datas)
                {
                    if (state.Offset.ToString().Equals("X"))
                    {
                        valueX = state.Value;
                        Console.WriteLine(valueX);
                    }
                    else if (state.Offset.ToString().Equals("Y"))
                    {
                        valueY = state.Value;
                    }
                    else if (state.Offset.ToString().Equals("Buttons0"))
                    {
                        switch (state.Value)
                        {
                        case 0: isButtonClicked = false; break;

                        case 128: isButtonClicked = true; break;
                        }
                    }
                    else if (state.Offset.ToString().Equals("Z"))
                    {
                        valueTriggers = state.Value;
                    }
                }
            }
        }
예제 #10
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) }
                }
            });
        }
        public void Start()
        {
            foreach (var port in MainV2.Comports)
            {
                foreach (var MAV in port.MAVlist)
                {
                    DG.Drones.Add(new Drone()
                    {
                        MavState = MAV
                    });
                }
            }

            foreach (var device in directInput.GetDevices(DeviceClass.GameControl, DeviceEnumerationFlags.AttachedOnly))
            {
                var joystick = new SharpDX.DirectInput.Joystick(directInput, device.InstanceGuid);

                joystick.Acquire();

                joystick.Poll();

                Console.WriteLine(joystick.Information.ProductName);

                Joysticks.Add(joystick);
            }

            if (threadrun == true)
            {
                threadrun = false;
                return;
            }


            new System.Threading.Thread(mainloop)
            {
                IsBackground = true
            }.Start();

            DG.CurrentMode = DroneGroup.Mode.idle;
        }
예제 #12
0
 public override IMyJoystickState GetCurrentState()
 {
     joystick.Poll();
     return(new WindowsJoystickState(joystick.GetCurrentState()));
 }
예제 #13
0
    void Update()
    {
        if (FindObjectOfType <PlayerData>() != null)
        {
            if (joystick != null)
            {
                bool ret = false;
                if (bot == false)
                {
                    try
                    {
                        joystick.Poll();
                    }
                    catch
                    {
                        joystick = FindObjectOfType <PlayerData>().controllers[playerNumber];
                        try
                        {
                            joystick.Poll();
                        }
                        catch
                        {
                            FindObjectOfType <PlayerData>().pendingControllers[playerNumber] = true;
                        }
                        button1      = FindObjectOfType <PlayerData>().button1[playerNumber];
                        button2      = FindObjectOfType <PlayerData>().button2[playerNumber];
                        ret          = true;
                        color1Index  = 0;
                        color2Index  = 0;
                        color1.color = colors[0];
                        color2.color = colors[0];
                        FindObjectOfType <PlayerData>().color1[playerNumber] = color1.color;
                        FindObjectOfType <PlayerData>().color2[playerNumber] = color2.color;
                        UI.SetActive(false);
                        joined = false;
                        botObj.SetActive(true);
                        FindObjectOfType <PlayerData>().players.Remove(playerNumber);
                        return;
                    }
                }
                if (ret)
                {
                    return;
                }
            }
            else
            {
                if (FindObjectOfType <PlayerData>().controllers[playerNumber] != null)
                {
                    joystick = FindObjectOfType <PlayerData>().controllers[playerNumber];
                    try
                    {
                        joystick.Poll();
                    }
                    catch
                    {
                        FindObjectOfType <PlayerData>().pendingControllers[playerNumber] = true;
                    }
                    button1 = FindObjectOfType <PlayerData>().button1[playerNumber];
                    button2 = FindObjectOfType <PlayerData>().button2[playerNumber];
                }
            }
        }

        timeTillScene += Time.deltaTime;
        if (joystick != null)
        {
            if (CustomControls.GetButton(joystick, button1))
            {
                Action1();
            }
            else
            {
                CancelAction1();
            }
            if (CustomControls.GetButton(joystick, button2))
            {
                Action2();
            }
            else
            {
                CancelAction2();
            }
            if (keyboard)
            {
                if (CustomControls.GetButton(joystick, dreapta))
                {
                    if (axis == false)
                    {
                        axis = true;
                        if (opposite == false)
                        {
                            SelectDreapta();
                        }
                        else
                        {
                            SelectStanga();
                        }
                    }
                }
                else if (CustomControls.GetButton(joystick, stanga))
                {
                    if (axis == false)
                    {
                        axis = true;
                        if (opposite == false)
                        {
                            SelectStanga();
                        }
                        else
                        {
                            SelectDreapta();
                        }
                    }
                }
                else
                {
                    axis = false;
                }
            }
            else
            {
                if (CustomControls.GetAxis(joystick).Xaxis > 40000)
                {
                    if (axis == false)
                    {
                        axis = true;
                        if (opposite == false)
                        {
                            SelectDreapta();
                        }
                        else
                        {
                            SelectStanga();
                        }
                    }
                }
                else if (CustomControls.GetAxis(joystick).Xaxis < 20000)
                {
                    if (axis == false)
                    {
                        axis = true;
                        if (opposite == false)
                        {
                            SelectStanga();
                        }
                        else
                        {
                            SelectDreapta();
                        }
                    }
                }
                else
                {
                    axis = false;
                }
            }
        }
    }
예제 #14
0
        public SharpDX.DirectInput.Joystick AcquireJoystick(string name)
        {
            joystick = getJoyStickByName(name);

            if (joystick == null)
                return null;

            //joystick.SetDataFormat(DeviceDataFormat.Joystick);

            joystick.Acquire();

            System.Threading.Thread.Sleep(500);

            joystick.Poll();

            return joystick;
        }
        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);
                    }
                }
            }
        }
예제 #16
0
        public void Update()
        {
            if (dxJoystick == null)
            {
                return;
            }

            // Poll events from joystick
            dxJoystick.Poll();
            var data = dxJoystick.GetBufferedData();

            foreach (var state in data)
            {
                switch (state.Offset)
                {
                // left stick: [0, 65535] = [left max, right max]
                case JoystickOffset.X:
                    axis[0] = Math.Min(Math.Max(-1.0, ((double)(state.Value - 32768) / (double)32768)), 1.0);
                    break;

                // left stick: [0, 65535] = [up max, down max]
                case JoystickOffset.Y:
                    axis[1] = Math.Min(Math.Max(-1.0, ((double)(state.Value - 32768) / (double)32768)), 1.0);
                    break;

                // left stick rotation : [0, 65535] = [up max, down max]
                case JoystickOffset.RotationZ:
                    axis[2] = Math.Min(Math.Max(-1.0, ((double)(state.Value - 32768) / (double)32768)), 1.0);
                    break;

                // left throttle: [32768, 65535] = [neutral, full]
                case JoystickOffset.Sliders0:
                    axis[3] = -Math.Min(Math.Max(-1.0, ((double)(state.Value - 32768) / (double)32768)), 1.0);
                    break;

                // A button: [zero, nonzero] = [release, push]
                case JoystickOffset.Buttons0:
                    button[0] = (state.Value != 0);
                    break;

                // B
                case JoystickOffset.Buttons1:
                    button[1] = (state.Value != 0);
                    break;

                // X
                case JoystickOffset.Buttons2:
                    button[2] = (state.Value != 0);
                    break;

                // Y
                case JoystickOffset.Buttons3:
                    button[3] = (state.Value != 0);
                    break;

                // L
                case JoystickOffset.Buttons4:
                    button[4] = (state.Value != 0);
                    break;

                // R
                case JoystickOffset.Buttons5:
                    button[5] = (state.Value != 0);
                    break;

                    //case JoystickOffset.Sliders0:
                    //    param.MotorGainX = 1.0f - (float)state.Value / (2 << 16);
                    //    param.MotorGainX *= param.SliderGain;
                    //    param.MotorGainY = param.MotorGainX;
                    //    break;
                    //case JoystickOffset.PointOfViewControllers0:
                    //    switch (state.Value)
                    //    {
                    //        case -1:
                    //            poVCtrl0 = Direction.Stop;
                    //            break;
                    //        case 0:
                    //            poVCtrl0 = Direction.Up;
                    //            break;
                    //        case 9000:
                    //            poVCtrl0 = Direction.Right;
                    //            break;
                    //        case 18000:
                    //            poVCtrl0 = Direction.Down;
                    //            break;
                    //        case 27000:
                    //            poVCtrl0 = Direction.Left;
                    //            break;
                    //    }
                    //
                    //    if (poVCtrl0 == Direction.Stop)
                    //    {
                    //        outZ = 0;
                    //    }
                    //    break;
                    //case JoystickOffset.Buttons0:
                    //    stop = true;
                    //    break;
                    //case JoystickOffset.Buttons6:
                    //    MotorClient.Send("SHUTDOWN\n");
                    //    break;
                }

                //switch (poVCtrl0)
                //{
                //    case Direction.Up:
                //        outZ += 0.25;
                //        break;
                //    case Direction.Down:
                //        outZ -= 0.25;
                //        break;
                //}
            }
        }
예제 #17
0
        /// <summary>
        /// Updates the rcoverride values and controls the mode changes
        /// </summary>
        void mainloop()
        {
            while (enabled && joystick != null && !joystick.IsDisposed)
            {
                try
                {
                    System.Threading.Thread.Sleep(50);
                    if (joystick.IsDisposed)
                    {
                        return;
                    }
                    //joystick stuff
                    joystick.Poll();
                    state = joystick.CurrentJoystickState();

                    //Console.WriteLine(state);

                    if (getNumberPOV() > 0)
                    {
                        int pov = getHatSwitchDirection();

                        if (pov != -1)
                        {
                            int angle = pov / 100;

                            //0 = down = 18000
                            //0 = up = 0

                            // 0
                            if (angle > 270 || angle < 90)
                            {
                                hat1 += 500;
                            }
                            // 180
                            if (angle > 90 && angle < 270)
                            {
                                hat1 -= 500;
                            }
                            // 90
                            if (angle > 0 && angle < 180)
                            {
                                hat2 += 500;
                            }
                            // 270
                            if (angle > 180 && angle < 360)
                            {
                                hat2 -= 500;
                            }
                        }
                    }

                    if (elevons)
                    {
                        //g.channel_roll.set_pwm(BOOL_TO_SIGN(g.reverse_elevons) * (BOOL_TO_SIGN(g.reverse_ch2_elevon) * int(ch2_temp - elevon2_trim) - BOOL_TO_SIGN(g.reverse_ch1_elevon) * int(ch1_temp - elevon1_trim)) / 2 + 1500);
                        //g.channel_pitch.set_pwm(                                 (BOOL_TO_SIGN(g.reverse_ch2_elevon) * int(ch2_temp - elevon2_trim) + BOOL_TO_SIGN(g.reverse_ch1_elevon) * int(ch1_temp - elevon1_trim)) / 2 + 1500);
                        short roll  = pickchannel(1, JoyChannels[1].axis, false, JoyChannels[1].expo);
                        short pitch = pickchannel(2, JoyChannels[2].axis, false, JoyChannels[2].expo);

                        if (getJoystickAxis(1) != Joystick.joystickaxis.None)
                        {
                            MainV2.comPort.MAV.cs.rcoverridech1 =
                                (short)
                                (BOOL_TO_SIGN(JoyChannels[1].reverse) * ((int)(pitch - 1500) - (int)(roll - 1500)) / 2 +
                                 1500);
                        }
                        if (getJoystickAxis(2) != Joystick.joystickaxis.None)
                        {
                            MainV2.comPort.MAV.cs.rcoverridech2 =
                                (short)
                                (BOOL_TO_SIGN(JoyChannels[2].reverse) * ((int)(pitch - 1500) + (int)(roll - 1500)) / 2 +
                                 1500);
                        }
                    }
                    else
                    {
                        if (getJoystickAxis(1) != Joystick.joystickaxis.None)
                        {
                            MainV2.comPort.MAV.cs.rcoverridech1 = pickchannel(1, JoyChannels[1].axis,
                                                                              JoyChannels[1].reverse, JoyChannels[1].expo);
                        }
                        //(ushort)(((int)state.Rz / 65.535) + 1000);
                        if (getJoystickAxis(2) != Joystick.joystickaxis.None)
                        {
                            MainV2.comPort.MAV.cs.rcoverridech2 = pickchannel(2, JoyChannels[2].axis,
                                                                              JoyChannels[2].reverse, JoyChannels[2].expo);
                        }
                        //(ushort)(((int)state.Y / 65.535) + 1000);
                    }
                    if (getJoystickAxis(3) != Joystick.joystickaxis.None)
                    {
                        MainV2.comPort.MAV.cs.rcoverridech3 = pickchannel(3, JoyChannels[3].axis, JoyChannels[3].reverse,
                                                                          JoyChannels[3].expo); //(ushort)(1000 - ((int)slider[0] / 65.535) + 1000);
                    }
                    if (getJoystickAxis(4) != Joystick.joystickaxis.None)
                    {
                        MainV2.comPort.MAV.cs.rcoverridech4 = pickchannel(4, JoyChannels[4].axis, JoyChannels[4].reverse,
                                                                          JoyChannels[4].expo); //(ushort)(((int)state.X / 65.535) + 1000);
                    }
                    if (getJoystickAxis(5) != Joystick.joystickaxis.None)
                    {
                        MainV2.comPort.MAV.cs.rcoverridech5 = pickchannel(5, JoyChannels[5].axis, JoyChannels[5].reverse,
                                                                          JoyChannels[5].expo);
                    }
                    if (getJoystickAxis(6) != Joystick.joystickaxis.None)
                    {
                        MainV2.comPort.MAV.cs.rcoverridech6 = pickchannel(6, JoyChannels[6].axis, JoyChannels[6].reverse,
                                                                          JoyChannels[6].expo);
                    }
                    if (getJoystickAxis(7) != Joystick.joystickaxis.None)
                    {
                        MainV2.comPort.MAV.cs.rcoverridech7 = pickchannel(7, JoyChannels[7].axis, JoyChannels[7].reverse,
                                                                          JoyChannels[7].expo);
                    }
                    if (getJoystickAxis(8) != Joystick.joystickaxis.None)
                    {
                        MainV2.comPort.MAV.cs.rcoverridech8 = pickchannel(8, JoyChannels[8].axis, JoyChannels[8].reverse,
                                                                          JoyChannels[8].expo);
                    }

                    // disable button actions when not connected.
                    if (MainV2.comPort.BaseStream.IsOpen)
                    {
                        DoJoystickButtonFunction();
                    }

                    //Console.WriteLine("{0} {1} {2} {3}", MainV2.comPort.MAV.cs.rcoverridech1, MainV2.comPort.MAV.cs.rcoverridech2, MainV2.comPort.MAV.cs.rcoverridech3, MainV2.comPort.MAV.cs.rcoverridech4);
                }
                catch (SharpDX.SharpDXException ex)
                {
                    log.Error(ex);
                    clearRCOverride();
                    MainV2.instance.Invoke((System.Action)
                                           delegate { CustomMessageBox.Show("Lost Joystick", "Lost Joystick"); });
                    return;
                }
                catch (Exception ex)
                {
                    log.Info("Joystick thread error " + ex.ToString());
                } // so we cant fall out
            }
        }