Пример #1
0
        /// <summary>
        /// Update the properties of button and axis positions.
        /// </summary>
        public void UpdateStatus()
        {
            Poll();

            int[]  extraAxis = state.GetSlider();      //Retrieves the throtle axis from joystick
            int [] hatAxis   = state.GetPointOfView(); // Retrieves the hat axis from the joystick
            //Rz Rx X Y Axis1 Axis2

            /*axisA = state.Rz;
             * axisB = state.Rx;
             * axisC = state.X;
             * axisD = state.Y;*/
            axisA = state.X;
            axisB = state.Y;
            //axisC = state.Rz;
            axisC = hatAxis[0];                  //Up and down hat axis.
            axisD = (0x0000ffff - extraAxis[0]); // Subtract value since values are sent from max to low. This will then start from 0 - 65535.
            //axisE = state.Rx;
            axisF = extraAxis[1];

            // not using buttons, so don't take the tiny amount of time it takes to get/parse
            byte[] jsButtons = state.GetButtons();
            buttons = new bool[jsButtons.Length];

            int i = 0;

            foreach (byte button in jsButtons)
            {
                buttons[i] = button >= 20;
                i++;
            }
        }
Пример #2
0
        /// <summary>
        /// Update the properties of button and axis positions.
        /// </summary>
        public void UpdateStatus()
        {
            Poll();

            //..int[] extraAxis = state.GetSlider();
            //Rz Rx X Y Axis1 Axis2
            axisA = state.Rz; //brake
            axisB = state.Rx;
            axisC = state.X;  //wheel
            axisD = state.Y;  //accel
            //..axisE = extraAxis[0];
            //..axisF = extraAxis[1];

            // not using buttons, so don't take the tiny amount of time it takes to get/parse
            byte[] jsButtons = state.GetButtons();
            buttonCount = jsButtons.Length; //..
            buttons     = new bool[jsButtons.Length];

            int i = 0;

            foreach (byte button in jsButtons)
            {
                buttons[i] = button >= 128;
                i++;
            }

            // Point Of View Hat
            pointOfView = state.GetPointOfView();
            slider      = state.GetSlider();
        }
Пример #3
0
        public override Dictionary <String, float> GetAxisValues()
        {
            Dictionary <String, float> axisValues = new Dictionary <String, float>();

            axisValues[Axis.Axis_X.ToString()] = axisValues[Axis.Axis_Y.ToString()] = axisValues[Axis.Axis_Z.ToString()] = axisValues[Axis.Axis_R.ToString()] = axisValues[Axis.Axis_POV_1.ToString()] = 0.0f;

            try
            {
                JoystickState state = device.CurrentJoystickState;
                axisValues[Axis.Axis_X.ToString()]  = GetFloatValue(state.X);
                axisValues[Axis.Axis_Y.ToString()]  = GetFloatValue(state.Y);
                axisValues[Axis.Axis_Z.ToString()]  = GetFloatValue(state.Z);
                axisValues[Axis.Axis_R.ToString()]  = GetFloatValue(state.Rz);
                axisValues[Axis.Axis_Rx.ToString()] = GetFloatValue(state.Rx);
                axisValues[Axis.Axis_Ry.ToString()] = GetFloatValue(state.Ry);

                axisValues[Axis.Axis_POV_1.ToString()] = CalculatePOVValue(state.GetPointOfView()[0]);

                return(axisValues);
            }
            catch (Exception)
            { }

            return(axisValues);
        }
Пример #4
0
        public JoystickStateEventArgs(JoystickState state)
        {
            this.state = state;
            int[] extraAxis = state.GetSlider();

            axisA = state.Rz;
            axisB = state.Rx;
            axisC = state.X;
            axisD = state.Y;
            axisE = state.Z;
            axisF = extraAxis[0];
            // Console.WriteLine(Convert.ToString(axisA));

            byte[] jsButtons = state.GetButtons();
            int    i         = 0;

            buttons = new bool[jsButtons.Length];

            foreach (byte button in jsButtons)
            {
                buttons[i++] = (button == 128);
            }

            int[] jsPOV = state.GetPointOfView();
            pov    = new int[jsPOV.Length];
            pov[0] = jsPOV[0];
        }
Пример #5
0
        private void UpdateUI()
        {
            string strText = null;

            int[] slider = state.GetSlider();
            int[] pov    = state.GetPointOfView();
            // Fill up text with which buttons are pressed
            byte[] buttons = state.GetButtons();
            int    button  = 0;

            foreach (byte b in buttons)
            {
                if (0 != (b & 0x80))
                {
                    strText += button.ToString("00 ");
                }
                button++;
            }
        }
Пример #6
0
        public void UpdateJoystick(VModel Vmodel)
        {
            JoystickState state = joystick.CurrentJoystickState;

            Buttons_Update(state.GetButtons());
            Manipulator_Rotation_Update(state.GetPointOfView());
            SpeedMode_Update(Vmodel);
            Slider_Update(state.GetSlider());
            Main_Joystick_Parameters_Update(state);
        }
Пример #7
0
        void tmr_Tick(object sender, EventArgs e)
        {
            joystick.Poll();
            joystickState   = joystick.CurrentJoystickState;
            joystickButtons = joystickState.GetButtons();
            jPOV0           = joystickState.GetPointOfView();

            //Axis - Rotation
            lblXAxis.Text     = joystickState.X.ToString();
            lblYAxis.Text     = joystickState.Y.ToString();
            lblZAxis.Text     = joystickState.Z.ToString();
            lblXRotation.Text = joystickState.Rx.ToString();
            lblYRotation.Text = joystickState.Ry.ToString();

            //Butonlar
            lblBtn0.Text = joystickButtons[0].ToString();
            lblBtn1.Text = joystickButtons[1].ToString();
            double sayi = 0.01;

            if (joystickButtons[1] == 128)
            {
                if (serialPort1.IsOpen == true)
                {
                    sayi += 0.1;
                    int    tr1Deger = (int)sayi;
                    string deger    = tr1Deger.ToString();
                    serialPort1.Write("D" + deger);
                }
            }
            if (joystickButtons[3] == 128)
            {
                if (serialPort1.IsOpen == true)
                {
                    sayi -= 0.1;
                    int    tr1Deger = (int)sayi;
                    string deger    = tr1Deger.ToString();
                    serialPort1.Write("D" + deger);
                }
            }

            lblBtn2.Text  = joystickButtons[2].ToString();
            lblBtn3.Text  = joystickButtons[3].ToString();
            lblBtn4.Text  = joystickButtons[4].ToString();
            lblBtn5.Text  = joystickButtons[5].ToString();
            lblBtn6.Text  = joystickButtons[6].ToString();
            lblBtn7.Text  = joystickButtons[7].ToString();
            lblBtn8.Text  = joystickButtons[8].ToString();
            lblBtn9.Text  = joystickButtons[9].ToString();
            lblBtn10.Text = joystickButtons[10].ToString();
            lblBtn11.Text = joystickButtons[11].ToString();

            //Point of View
            lblPOV0.Text = jPOV0[0].ToString();
        }
Пример #8
0
        //* -----------------------------------------------------------------------*
        /// <summary>POVの入力状態をベクトルで取得します。</summary>
        ///
        /// <param name="state">入力状態の格納された構造体。</param>
        /// <returns>入力されたPOVのベクトル(-1.0~+1.0)。</returns>
        public static Vector2 getVectorPOV(this JoystickState state)
        {
            Vector2 result = Vector2.Zero;
            int     pov    = state.GetPointOfView()[0];

            if (pov != -1)
            {
                float fRadian = MathHelper.ToRadians(pov * 0.01f) - MathHelper.PiOver2;
                result = new Vector2((float)Math.Cos(fRadian), (float)Math.Sin(fRadian));
            }
            return(result);
        }
        private void DoWork()
        {
            //JoystickEvent je = null;


            while (CloseRequest)
            {
                try
                {
                    WaitHandle.WaitAll(WAIT_FOR);
                    JoystickState xboxState = xboxController.CurrentJoystickState;
                    byte[]        buttons   = xboxState.GetButtons();

                    this.Y            = (buttons[0] == 128);
                    this.B            = (buttons[1] == 128);
                    this.A            = (buttons[2] == 128);
                    this.X            = (buttons[3] == 128);
                    this.WhiteButton  = (buttons[4] == 128);
                    this.BlackButton  = (buttons[5] == 128);
                    this.LeftTrigger  = (buttons[6] == 128);
                    this.RightTrigger = (buttons[7] == 128);
                    this.Back         = (buttons[8] == 128);
                    this.Start        = (buttons[9] == 128);
                    this.LeftStick    = (buttons[10] == 128);
                    this.RightStick   = (buttons[11] == 128);

                    int[] dpad = xboxState.GetPointOfView();

                    this.Up    = (dpad[0] == 0 || dpad[0] == 31500 || dpad[0] == 4500);
                    this.Down  = (dpad[0] == 18000 || dpad[0] == 22500 || dpad[0] == 13500);
                    this.Left  = (dpad[0] == 27000 || dpad[0] == 31500 || dpad[0] == 22500);
                    this.Right = (dpad[0] == 9000 || dpad[0] == 13500 || dpad[0] == 4500);

                    this.X1 = -(((xboxState.X - 1) * 1.0f / 32767.0f) - 1);
                    this.Y1 = -(((xboxState.Y - 1) * 1.0f / 32767.0f) - 1);
                    this.X2 = -(((xboxState.Z - 1) * 1.0f / 32767.0f) - 1);
                    this.Y2 = -(((xboxState.Rz - 1) * 1.0f / 32767.0f) - 1);

                    /* Joystick Gera
                     * this.X1 = -(((xboxState.X - 1) * 1.0f / 32707.0f) - 1);
                     * this.Y1 = -(((xboxState.Y - 1) * 1.0f / 32707.0f) - 1);
                     * this.X2 = -(((xboxState.Z - 1) * 1.0f / 32707.0f) - 1);
                     * this.Y2 = -(((xboxState.Rz - 1) * 1.0f / 32707.0f) - 1);
                     */

                    OnChanged(EventArgs.Empty);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error XBoxController" + e.ToString());
                }
            }
        }
Пример #10
0
        internal static void UpdateState(JoystickState state)
        {
            prevButtons    = currentButtons;
            currentButtons = state.GetButtons();

            prevPov    = currentPov;
            currentPov = state.GetPointOfView();

            PrevStickPos = StickPos;
            StickPos     = new Vector2(ConvertAxisToFloat(state.X), ConvertAxisToFloat(state.Y));

            var slider = state.GetSlider();

            Gyro = new Vector3(ConvertAxisToFloat(slider[0]), ConvertAxisToFloat(slider[1]), ConvertAxisToFloat(state.Rz));
        }
Пример #11
0
        private int updatePOVButtons(byte[] b)
        {
            int pov = state.GetPointOfView()[0];

            switch (pov)
            {
            case 0:
                b[NUM_BASIC_BUTTONS]     = 0x80;
                b[NUM_BASIC_BUTTONS + 1] = 0;
                b[NUM_BASIC_BUTTONS + 2] = 0;
                b[NUM_BASIC_BUTTONS + 3] = 0;
                break;

            case 4500:
            case 9000:
            case 13500:
                b[NUM_BASIC_BUTTONS]     = 0;
                b[NUM_BASIC_BUTTONS + 1] = 0x80;
                b[NUM_BASIC_BUTTONS + 2] = 0;
                b[NUM_BASIC_BUTTONS + 3] = 0;
                break;

            case 18000:
                b[NUM_BASIC_BUTTONS]     = 0;
                b[NUM_BASIC_BUTTONS + 1] = 0;
                b[NUM_BASIC_BUTTONS + 2] = 0x80;
                b[NUM_BASIC_BUTTONS + 3] = 0;
                break;

            case 22500:
            case 27000:
            case 31500:
                b[NUM_BASIC_BUTTONS]     = 0;
                b[NUM_BASIC_BUTTONS + 1] = 0;
                b[NUM_BASIC_BUTTONS + 2] = 0;
                b[NUM_BASIC_BUTTONS + 3] = 0x80;
                break;

            default:
                b[NUM_BASIC_BUTTONS]     = 0;
                b[NUM_BASIC_BUTTONS + 1] = 0;
                b[NUM_BASIC_BUTTONS + 2] = 0;
                b[NUM_BASIC_BUTTONS + 3] = 0;
                break;
            }
            return(pov);
        }
Пример #12
0
        //* -----------------------------------------------------------------------*
        /// <summary>任意のボタンが押されたかどうかを判定します。</summary>
        ///
        /// <param name="pov">POVの操作も判定するかどうか。</param>
        /// <param name="slider">スライダーの操作も判定するかどうか。</param>
        /// <returns>任意のボタンが押された場合、<c>true</c>。</returns>
        public bool isPushAnyKey(bool pov, bool slider)
        {
            JoystickState state = _privateMembers.poll();

            byte[] buttons = state.GetButtons();
            bool   bResult = buttons.Any(button => button != 0);

            if (!bResult && pov)
            {
                bResult = state.GetPointOfView()[0] != -1;
            }
            if (!bResult && slider)
            {
                bResult = state.getVector3Slider().Length() >= analogThreshold;
            }
            return(bResult);
        }
Пример #13
0
        //* -----------------------------------------------------------------------*
        /// <summary>ハット スイッチの角度情報をベクトルに変換します。。</summary>
        ///
        /// <param name="state">最新の入力状態。</param>
        /// <returns>ベクトル。Z軸はダミー値。</returns>
        private Vector3 getVelocityFromPOV(JoystickState state)
        {
            Vector3 result = Vector3.Zero;

            int[] povList = state.GetPointOfView();
            if (povList.Length > 0)
            {
                int pov = povList[0];
                if (pov >= 0)
                {
                    float      angle = MathHelper.ToRadians(pov * 0.01f);
                    Quaternion q     = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, angle);
                    result = Vector3.Transform(-Vector3.UnitY, q);
                }
            }
            return(result);
        }
Пример #14
0
        private void UpdateUI()
        {
            // This function updated the UI with
            // joystick state information.

            string strText = null;

            labelXAxis.Text = state.X.ToString();
            labelYAxis.Text = state.Y.ToString();
            labelZAxis.Text = state.Z.ToString();

            labelXRotation.Text = state.Rx.ToString();
            labelYRotation.Text = state.Ry.ToString();
            labelZRotation.Text = state.Rz.ToString();


            int[] slider = state.GetSlider();

            labelSlider0.Text = slider[0].ToString();
            labelSlider1.Text = slider[1].ToString();

            int[] pov = state.GetPointOfView();

            labelPOV0.Text = pov[0].ToString();
            labelPOV1.Text = pov[1].ToString();
            labelPOV2.Text = pov[2].ToString();
            labelPOV3.Text = pov[3].ToString();

            // Fill up text with which buttons are pressed
            byte[] buttons = state.GetButtons();

            int button = 0;

            foreach (byte b in buttons)
            {
                if (0 != (b & 0x80))
                {
                    strText += button.ToString("00 ");
                }
                button++;
            }
            labelButtons.Text = strText;
        }
Пример #15
0
        public void UpdateStatus()
        {
            Poll();

            int[] extraAxis = state.GetSlider();

            int[] pointOfView = state.GetPointOfView();

            Xaxis    = state.X;
            Yaxis    = state.Y;
            RZaxis   = state.Rz;
            Throttle = extraAxis[0];
            if (pointOfView[0] == -1)
            {
                PointOfView = pointOfView[0];
            }
            else
            {
                PointOfView = pointOfView[0] / 100;
            }


            byte[] jsButtons = state.GetButtons();
            buttons = new bool[jsButtons.Length];

            int i   = 0;
            int cnt = 0;

            foreach (byte button in jsButtons)
            {
                buttons[i] = button >= 128;
                i++;
                if (button != 0)
                {
                    int r = 0;
                    cnt++;
                    r++;
                }
            }
        }
Пример #16
0
        public void UpdateStatus()
        {
            Poll();

            extra = state.GetSlider();

            Xaxis = state.X;
            Yaxis = state.Y;

            byte[] jsButtons = state.GetButtons();
            buttons = new bool[jsButtons.Length];

            // do something with state
            jsPOV = state.GetPointOfView();

            int i = 0;

            foreach (byte button in jsButtons)
            {
                buttons[i] = button >= 128;
                i++;
            }
        }
Пример #17
0
        public int GetAxisValue(string function)
        {
            if (joyStickDevice == null)
            {
                return(0);
            }

            bool inverted = false;

            switch (Settings.GetAxis(function, out inverted))
            {
            case JoyStickAxis.X:
                return(inverted ? -joystickState.X : joystickState.X);

            case JoyStickAxis.Y:
                return(inverted ? -joystickState.Y : joystickState.Y);

            case JoyStickAxis.Z:
                return(inverted ? -joystickState.Z : joystickState.Z);

            case JoyStickAxis.Rx:
                return(inverted ? -joystickState.Rx : joystickState.Rx);

            case JoyStickAxis.Ry:
                return(inverted ? -joystickState.Ry : joystickState.Ry);

            case JoyStickAxis.Rz:
                return(inverted ? -joystickState.Rz : joystickState.Rz);

            case JoyStickAxis.Slider1:
                if (joystickState.GetSlider() != null && joystickState.GetSlider().GetLength(0) > 0)
                {
                    return(inverted ? -joystickState.GetSlider()[0] : joystickState.GetSlider()[0]);
                }
                else
                {
                    return(0);
                }

            case JoyStickAxis.Slider2:
                if (joystickState.GetSlider() != null && joystickState.GetSlider().GetLength(0) > 1)
                {
                    return(inverted ? -joystickState.GetSlider()[1] : joystickState.GetSlider()[1]);
                }
                else
                {
                    return(0);
                }

            case JoyStickAxis.POV1:
                return(inverted ? -joystickState.GetPointOfView()[0] : joystickState.GetPointOfView()[0]);

            case JoyStickAxis.POV2:
                return(inverted ? -joystickState.GetPointOfView()[1] : joystickState.GetPointOfView()[1]);

            case JoyStickAxis.POV3:
                return(inverted ? -joystickState.GetPointOfView()[2] : joystickState.GetPointOfView()[2]);

            case JoyStickAxis.POV4:
                return(inverted ? -joystickState.GetPointOfView()[3] : joystickState.GetPointOfView()[3]);

            default:
                return(0);
            }
        }
Пример #18
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            // sprawdzenie stanu przycisków i osi
            try
            {
                if (_device != null)
                {
                    _device.Poll();
                    JoystickState state = _device.CurrentJoystickState;

                    // zwykłe przyciski
                    for (int i = 0; i < _buttonsStates.Length; i++)
                    {
                        bool b = (state.GetButtons()[i] & 0x80) == 0x80;
                        if (!_buttonsStates[i].HasValue || _buttonsStates[i] != b)
                        {
                            _buttonsStates[i] = b;
                            // znalezenie komórki
                            for (int j = 0; j < dataGridView1.Rows.Count; j++)
                            {
                                if (dataGridView1.Rows[j].Tag is IButton && ((IButton)dataGridView1.Rows[j].Tag).Index == i)
                                {
                                    dataGridView1.Rows[j].Cells[_stanPrzycisku.Index].Style.BackColor = b ? Color.GreenYellow : _stanPrzycisku.DefaultCellStyle.BackColor;
                                    dataGridView1.Rows[j].Cells[_stanPrzycisku.Index].Value           = b.ToString();
                                }
                            }
                        }
                    }

                    // hat
                    for (int i = 0; i < _hatsStates.Length; i++)
                    {
                        if (_hatsStates[i] != state.GetPointOfView()[i])
                        {
                            _hatsStates[i] = state.GetPointOfView()[i];
                            // znalezenie komórki
                            for (int j = 0; j < dataGridView1.Rows.Count; j++)
                            {
                                if (dataGridView1.Rows[j].Tag is IHAT && ((IHAT)dataGridView1.Rows[j].Tag).Index == i)
                                {
                                    dataGridView1.Rows[j].Cells[_stanPrzycisku.Index].Style.BackColor = _hatsStates[i] != -1 ? Color.GreenYellow : _stanPrzycisku.DefaultCellStyle.BackColor;
                                    dataGridView1.Rows[j].Cells[_stanPrzycisku.Index].Value           = _hatsStates[i].ToString();
                                }
                            }
                        }
                    }

                    // osie
                    if (_axes[0] != state.X)
                    {
                        _axes[0] = state.X;
                        SetAxisState(AxisType.X, state.X);
                    }
                    if (_axes[1] != state.Y)
                    {
                        _axes[1] = state.Y;
                        SetAxisState(AxisType.Y, state.Y);
                    }
                    if (_axes[2] != state.Z)
                    {
                        _axes[2] = state.Z;
                        SetAxisState(AxisType.Z, state.Z);
                    }
                    if (_axes[3] != state.Rx)
                    {
                        _axes[3] = state.Rx;
                        SetAxisState(AxisType.RX, state.Rx);
                    }
                    if (_axes[4] != state.Ry)
                    {
                        _axes[4] = state.Ry;
                        SetAxisState(AxisType.RY, state.Ry);
                    }
                    if (_axes[5] != state.Rz)
                    {
                        _axes[5] = state.Rz;
                        SetAxisState(AxisType.RZ, state.Rz);
                    }
                    int[] sliders = state.GetSlider();
                    if (sliders != null && sliders.Length > 0 && _axes[6] != sliders[0])
                    {
                        _axes[6] = sliders[0];
                        SetAxisState(AxisType.EXT1, sliders[0]);
                    }
                    if (sliders != null && sliders.Length > 0 && _axes[7] != sliders[1])
                    {
                        _axes[7] = sliders[1];
                        SetAxisState(AxisType.EXT2, sliders[1]);
                    }
                }
            }
            catch { }
        }
Пример #19
0
 public int GetPOV()
 {
     GetData();
     return((state.GetPointOfView())[0]);
 }
Пример #20
0
        public static joystickaxis getMovingAxis(string name, int threshold)
        {
            self.name = name;
            DeviceList joysticklist = Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly);

            bool found = false;

            Device joystick = null;

            foreach (DeviceInstance device in joysticklist)
            {
                if (device.ProductName == name)
                {
                    joystick = new Device(device.InstanceGuid);
                    found    = true;
                    break;
                }
            }
            if (!found)
            {
                return(joystickaxis.ARx);
            }

            joystick.SetDataFormat(DeviceDataFormat.Joystick);

            joystick.Acquire();

            // CustomMessageBox.Show("Please ensure you have calibrated your joystick in Windows first");

            // need a pause between aquire and poll
            System.Threading.Thread.Sleep(100);

            joystick.Poll();

            System.Threading.Thread.Sleep(50);

            JoystickState obj    = joystick.CurrentJoystickState;
            Hashtable     values = new Hashtable();

            // get the state of the joystick before.
            Type type = obj.GetType();

            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                values[property.Name] = int.Parse(property.GetValue(obj, null).ToString());
            }
            values["Slider1"] = obj.GetSlider()[0];
            values["Slider2"] = obj.GetSlider()[1];
            values["Hatud1"]  = obj.GetPointOfView()[0];
            values["Hatlr2"]  = obj.GetPointOfView()[0];
            values["Custom1"] = 0;
            values["Custom2"] = 0;

            CustomMessageBox.Show("Please move the joystick axis you want assigned to this function after clicking ok");

            DateTime start = DateTime.Now;

            while (start.AddSeconds(10) > DateTime.Now)
            {
                joystick.Poll();
                System.Threading.Thread.Sleep(50);
                JoystickState nextstate = joystick.CurrentJoystickState;

                int[] slider = nextstate.GetSlider();

                int[] hat1 = nextstate.GetPointOfView();

                type       = nextstate.GetType();
                properties = type.GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    //Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null));

                    log.InfoFormat("test name {0} old {1} new {2} ", property.Name, values[property.Name], int.Parse(property.GetValue(nextstate, null).ToString()));
                    log.InfoFormat("{0}  {1} {2}", property.Name, (int)values[property.Name], (int.Parse(property.GetValue(nextstate, null).ToString()) + threshold));
                    if ((int)values[property.Name] > (int.Parse(property.GetValue(nextstate, null).ToString()) + threshold) ||
                        (int)values[property.Name] < (int.Parse(property.GetValue(nextstate, null).ToString()) - threshold))
                    {
                        log.Info(property.Name);
                        joystick.Unacquire();
                        return((joystickaxis)Enum.Parse(typeof(joystickaxis), property.Name));
                    }
                }

                // slider1
                if ((int)values["Slider1"] > (slider[0] + threshold) ||
                    (int)values["Slider1"] < (slider[0] - threshold))
                {
                    joystick.Unacquire();
                    return(joystickaxis.Slider1);
                }

                // slider2
                if ((int)values["Slider2"] > (slider[1] + threshold) ||
                    (int)values["Slider2"] < (slider[1] - threshold))
                {
                    joystick.Unacquire();
                    return(joystickaxis.Slider2);
                }

                // Hatud1
                if ((int)values["Hatud1"] != (hat1[0]))
                {
                    joystick.Unacquire();
                    return(joystickaxis.Hatud1);
                }

                // Hatlr2
                if ((int)values["Hatlr2"] != (hat1[0]))
                {
                    joystick.Unacquire();
                    return(joystickaxis.Hatlr2);
                }
            }

            CustomMessageBox.Show("No valid option was detected");

            return(joystickaxis.None);
        }
Пример #21
0
        private void SendActions(JoystickState state)
        {
            int actionCode = -1;

            //int curAxisValue = 0;
            // todo: timer stuff!!

            // buttons first!
            byte[] buttons = state.GetButtons();
            int    button  = 0;

            // button combos

            /*
             * string sep = "";
             * string pressedButtons = "";
             * foreach (byte b in buttons)
             * {
             * if ((b & 0x80) != 0)
             * {
             *  pressedButtons += sep + button.ToString("00");
             *  sep = ",";
             * }
             * button++;
             * }
             */

            // single buttons
            if (actionCode == -1)
            {
                button = 0;
                bool foundButton = false;
                foreach (byte b in buttons)
                {
                    if (0 != (b & 0x80))
                    {
                        foundButton = true;
                        break;
                    }
                    button++;
                }
                if (foundButton)
                {
                    if ((button >= 0) && (button <= 19))
                    {
                        // don't need no stinkin' enum-constants here....
                        actionCode = 3030 + button;
                    }
                }
            }

            // pov next
            if (actionCode == -1)
            {
                int[] pov = state.GetPointOfView();
                switch (pov[0])
                {
                case 0:
                    actionCode = (int)joyButton.povN;
                    break;

                case 4500:
                    actionCode = (int)joyButton.povNE;
                    break;

                case 9000:
                    actionCode = (int)joyButton.povE;
                    break;

                case 13500:
                    actionCode = (int)joyButton.povSE;
                    break;

                case 18000:
                    actionCode = (int)joyButton.povS;
                    break;

                case 22500:
                    actionCode = (int)joyButton.povSW;
                    break;

                case 27000:
                    actionCode = (int)joyButton.povW;
                    break;

                case 31500:
                    actionCode = (int)joyButton.povNW;
                    break;
                }
            }

            if (actionCode == -1)
            {
                // axes next
                if (Math.Abs(state.X) > AxisLimit)
                {
                    //curAxisValue = state.X;
                    if (state.X > 0)
                    {
                        actionCode = (int)joyButton.axisXUp; // right
                    }
                    else
                    {
                        actionCode = (int)joyButton.axisXDown; // left
                    }
                }
                else if (Math.Abs(state.Y) > AxisLimit)
                {
                    //curAxisValue = state.Y;
                    if (state.Y > 0)
                    {
                        // down
                        actionCode = (int)joyButton.axisYUp;
                    }
                    else
                    {
                        // up
                        actionCode = (int)joyButton.axisYDown;
                    }
                }
                else if (Math.Abs(state.Z) > AxisLimit)
                {
                    //curAxisValue = state.Z;
                    if (state.Z > 0)
                    {
                        actionCode = (int)joyButton.axisZUp;
                    }
                    else
                    {
                        actionCode = (int)joyButton.axisZDown;
                    }
                }
            }

            if (actionCode == -1)
            {
                // rotation
                if (Math.Abs(state.Rx) > AxisLimit)
                {
                    //curAxisValue = state.Rx;
                    if (state.Rx > 0)
                    {
                        actionCode = (int)joyButton.rotationXUp;
                    }
                    else
                    {
                        actionCode = (int)joyButton.rotationXDown;
                    }
                }
                else if (Math.Abs(state.Ry) > AxisLimit)
                {
                    //curAxisValue = state.Ry;
                    if (state.Ry > 0)
                    {
                        actionCode = (int)joyButton.rotationYUp;
                    }
                    else
                    {
                        actionCode = (int)joyButton.rotationYDown;
                    }
                }
                else if (Math.Abs(state.Rz) > AxisLimit)
                {
                    //curAxisValue = state.Rz;
                    if (state.Rz > 0)
                    {
                        actionCode = (int)joyButton.rotationZUp;
                    }
                    else
                    {
                        actionCode = (int)joyButton.rotationZDown;
                    }
                }
            }

            if (actionCode != -1 && _remoteHandler != null)
            {
                string keyCode = TranslateActionCode(actionCode);

                _remoteHandler(Name, keyCode);
            }
        }
Пример #22
0
        /// <summary>
        /// ジョイパッド監視ループ
        /// </summary>
        /// <param name="sender"></param>
        internal static void DI_Loop(object sender)
        {
            // 重複処理防止のためのチェック
            if (nowProcessing == true)
            {
                //System.Diagnostics.Debug.WriteLine(string.Format("{0}: Skip DI_LOOP", DateTime.Now.ToString("HH:mm:ss")));
                return;
            }
            nowProcessing = true;
            try
            {
                // コントローラの状態をポーリングで取得
                // TODO: 例外処理 https://msdn.microsoft.com/ja-jp/library/cc351898.aspx
                DI_joypad.Poll();
                JoystickState state = DI_joypad.CurrentJoystickState;

                // ボタン状態
                // 仮想キーコード (VK_* / System.Windows.Forms.Keys) のマップにいくつか独自定義
                bool[] dState = new bool[256];

                // ボタン状態チェック(GetButtons() で byte[128] が取れるので境界注意)
                byte[] buttons = state.GetButtons();
                for (int i = 0; i < BtnMap.Length; i++)
                {
                    if (buttons[i] > 0)
                    {
                        dState[BtnMap[i]] = true;
                    }
                }

                // 16ボタン
                for (int i = 0; i < 15; i++)
                {
                    ButtonPress[i] = buttons[i] > 0;
                }
                for (int i = 16; i < 32; i++)
                {
                    ButtonPress[i] = false;
                }


                // POV値(十字キー)の取得
                int[] pov = state.GetPointOfView();
                if (pov.Length >= 1)
                {
                    switch (pov[0])
                    {
                    case 0:     // 上
                        ButtonPress[16]    = true;
                        dState[BtnMap[16]] = true;
                        break;

                    case 9000:     // 右
                        ButtonPress[17]    = true;
                        dState[BtnMap[17]] = true;
                        break;

                    case 18000:     // 下
                        ButtonPress[18]    = true;
                        dState[BtnMap[18]] = true;
                        break;

                    case 27000:     // 左
                        ButtonPress[19]    = true;
                        dState[BtnMap[19]] = true;
                        break;

                    default:
                        // case -1: 何も押していない
                        // 斜めは処理しない
                        break;
                    }
                }
                if (pov.Length >= 2)
                {
                    switch (pov[1])
                    {
                    case -1:     // 何も押してない
                        break;

                    case 0:     // 上
                        ButtonPress[20]    = true;
                        dState[BtnMap[20]] = true;
                        break;

                    case 9000:     // 右
                        ButtonPress[21]    = true;
                        dState[BtnMap[21]] = true;
                        break;

                    case 18000:     // 下
                        ButtonPress[22]    = true;
                        dState[BtnMap[22]] = true;
                        break;

                    case 27000:     // 左
                        ButtonPress[23]    = true;
                        dState[BtnMap[23]] = true;
                        break;

                    default:
                        // case -1: 何も押していない
                        // 斜めは処理しない
                        break;
                    }
                }

                // トラブル対策
                // TODO: コンストラクタに戻したい
                if (isInited == false)
                {
                    isInited = true;
                    // デバイス固有情報で初期化する
                    anaBase[0] = state.X;
                    anaBase[1] = state.Y;
                    anaBase[2] = state.Z;
                    anaBase[3] = state.Rx;
                    anaBase[4] = state.Ry;
                    anaBase[5] = state.Rz;
                }

                // アナログ変位取得
                int Lx = state.X - anaBase[0];
                int Ly = state.Y - anaBase[1];
                int Lz = state.Z - anaBase[2];
                int Rx = state.Rx - anaBase[3];
                int Ry = state.Ry - anaBase[4];
                int Rz = state.Rz - anaBase[5];

                // アナログトリガ(デジタル信号扱いする場合があるので先に処理)
                if (Math.Abs(Lz) > AnalogFree)
                {
                    ButtonPress[28] = Lz > 0;
                    ButtonPress[29] = Lz < 0;
                    dState[BtnMap[28 + (Lz > 0 ? 0 : 1)]] = true;
                }
                if (Math.Abs(Rz) > AnalogFree)
                {
                    ButtonPress[30] = Rz > 0;
                    ButtonPress[31] = Rz < 0;
                    dState[BtnMap[30 + (Lz > 0 ? 0 : 1)]] = true;
                }

                if (Math.Abs(Lx) > AnalogFree)
                {
                    ButtonPress[24] = true;
                }
                if (Math.Abs(Ly) > AnalogFree)
                {
                    ButtonPress[25] = true;
                }
                if (Math.Abs(Rx) > AnalogFree)
                {
                    ButtonPress[26] = true;
                }
                if (Math.Abs(Ry) > AnalogFree)
                {
                    ButtonPress[27] = true;
                }

                // 入力イベントを全て止める
                if (AllStop == true)
                {
                    return;
                }

                // アナログ状態チェック
                int[,] aState = new int[4, 3];

                // 右アナログ
                if (Rx * Rx + Ry * Ry > AnalogFree * AnalogFree)
                {
                    if (AnalogMapR == 1 || AnalogMapR == 2)
                    {
                        double rad = Math.Atan2(Ry, Rx);
                        aState[AnalogMapR, 0] = (int)((Rx - (Math.Cos(rad) * AnalogFree)) * (AnalogMapR == 2 ? freeMoveZit : charaMoveZit));
                        aState[AnalogMapR, 1] = (int)((Ry - (Math.Sin(rad) * AnalogFree)) * (AnalogMapR == 2 ? freeMoveZit : charaMoveZit));
                        aState[AnalogMapR, 2] = AnalogMapR;
                    }
                }

                // 左アナログ(右アナログと同じ処理の場合は上書きする)
                if (Lx * Lx + Ly * Ly > AnalogFree * AnalogFree)
                {
                    if (AnalogMapL == 1 || AnalogMapL == 2)
                    {
                        double rad = Math.Atan2(Ly, Lx);
                        aState[AnalogMapL, 0] = (int)((Lx - (Math.Cos(rad) * AnalogFree)) * (AnalogMapL == 2 ? freeMoveZit : charaMoveZit));
                        aState[AnalogMapL, 1] = (int)((Ly - (Math.Sin(rad) * AnalogFree)) * (AnalogMapL == 2 ? freeMoveZit : charaMoveZit));
                        aState[AnalogMapL, 2] = AnalogMapL;
                    }
                }

                // アナログ3(右アナログと同じ処理の場合は上書きする)
                if (Lz * Lz + Rz * Rz > AnalogFree * AnalogFree)
                {
                    if (AnalogMapZ == 1 || AnalogMapZ == 2)
                    {
                        double rad = Math.Atan2(Lz, Rz);
                        aState[AnalogMapZ, 0] = (int)((Lz - (Math.Cos(rad) * AnalogFree)) * (AnalogMapZ == 2 ? freeMoveZit : charaMoveZit));
                        aState[AnalogMapZ, 1] = (int)((Rz - (Math.Sin(rad) * AnalogFree)) * (AnalogMapZ == 2 ? freeMoveZit : charaMoveZit));
                        aState[AnalogMapZ, 2] = AnalogMapZ;
                    }
                }

                // キーボード状態チェック
                if (ImageMap.curSceneName == "@フィールド")
                {
                    if (KeyState[(int)Keys.NumPad8])
                    {
                        dState[38] = true;
                    }
                    if (KeyState[(int)Keys.NumPad6])
                    {
                        dState[39] = true;
                    }
                    if (KeyState[(int)Keys.NumPad2])
                    {
                        dState[40] = true;
                    }
                    if (KeyState[(int)Keys.NumPad4])
                    {
                        dState[37] = true;
                    }
                    if (KeyState[(int)Keys.NumPad7])
                    {
                        dState[61] = true;
                    }                                                       // ←
                    if (KeyState[(int)Keys.NumPad9])
                    {
                        dState[58] = true;
                    }                                                       // ↑
                    if (KeyState[(int)Keys.NumPad3])
                    {
                        dState[60] = true;
                    }                                                       // ↓
                }
                else
                {
                    if (KeyState[(int)Keys.NumPad8])
                    {
                        dState[58] = true;
                    }                                                       // ↑
                    if (KeyState[(int)Keys.NumPad6])
                    {
                        dState[59] = true;
                    }                                                       // →
                    if (KeyState[(int)Keys.NumPad2])
                    {
                        dState[60] = true;
                    }                                                       // ↓
                    if (KeyState[(int)Keys.NumPad4])
                    {
                        dState[61] = true;
                    }                                                       // ←
                }
                if (KeyState[(int)Keys.NumPad0])
                {
                    dState[1] = true;
                }                                                      // 左クリック
                if (KeyState[(int)Keys.NumPad5])
                {
                    dState[2] = true;
                }                                                      // 右クリック
                if (KeyState[(int)Keys.Divide])
                {
                    dState[14] = true;
                }                                                      // 左タブ
                if (KeyState[(int)Keys.Multiply])
                {
                    dState[15] = true;
                }                                                        // 右タブ

                if (KeyState[(int)Keys.Decimal])
                {
                    dState[17] = true;
                }                                                       // Ctrl
                if (KeyState[(int)Keys.NumPad1])
                {
                    dState[17] = true;
                }                                                       // Ctrl

                // 十字キー上下左右 (1:上 / 2:右 / 3:下 / 4:左)
                if (!(lastState[0x3A] || lastState[0x3B] || lastState[0x3C] || lastState[0x3D]))
                {
                    aState[3, 0] = dState[0x3A] ? 1 : dState[0x3B] ? 2 : dState[0x3C] ? 3 : dState[0x3D] ? 4 : 0;
                }
                aState[3, 2] = 8;

                // マウスカーソル移動系の最終値を決定
                for (int i = 1; i < 4; i++)
                {
                    if ((aState[i, 2] != 0) && ((aState[i, 0] != 0) || (aState[i, 1] != 0)))
                    {
                        aState[0, 0] = aState[i, 0];
                        aState[0, 1] = aState[i, 1];
                        aState[0, 2] = aState[i, 2];
                        break;
                    }
                }

                // 現在のカーソル位置を取得
                Point absoluteCurPos;
                w32.GetCursorPos(out absoluteCurPos);

                // ウィンドウ枠内にカーソルがあるか
                bool isMouseInWindow = ScreenCapture.WindowPos.Contains(absoluteCurPos);

                // curPos(クライアント座標) と cur(Windows座標)
                Point clientCurPos = new Point(absoluteCurPos.X - ScreenCapture.WindowPos.Left, absoluteCurPos.Y - ScreenCapture.WindowPos.Top);
                Point targetCurPos = clientCurPos;

                // 移動先確定
                switch (aState[0, 2])
                {
                case 1:
                    // カーソル移動 (左ボタン押し下げ無効)
                    targetCurPos.X = absoluteCurPos.X + aState[0, 0];
                    targetCurPos.Y = absoluteCurPos.Y + aState[0, 1];
                    break;

                case 2:
                    // キャラ移動 (座標指定+左ボタン押し下げ)はフィールドのみ
                    if (ImageMap.curSceneName == "@フィールド")
                    {
                        targetCurPos.X = ScreenCapture.WindowPos.Left + CharaPosition.X + aState[0, 0];
                        targetCurPos.Y = ScreenCapture.WindowPos.Top + CharaPosition.Y + aState[0, 1];
                        isRunning      = true;
                    }
                    break;

                case 8:
                    // 十字キー (左ボタン押し下げ無効)

                    //// 押されなかったことにして次のインターバルに回す
                    //if (bmp == null)
                    //{
                    //    dState[0x3A] = dState[0x3B] = dState[0x3C] = dState[0x3D] = false;
                    //    break;
                    //}
                    targetCurPos = ImageMap.MoveCursor(clientCurPos, aState[0, 0]);

                    // マイナスの値の場合はホイール信号が必要(正負反転)
                    if (targetCurPos.X < 0)
                    {
                        dState[0x0A]    = true;
                        targetCurPos.X *= -1;     // これ以降、マウスを移動してはいけない
                    }
                    else if (targetCurPos.Y < 0)
                    {
                        dState[0x0B]    = true;
                        targetCurPos.Y *= -1;     // これ以降、マウスを移動してはいけない
                    }
                    else
                    {
                        targetCurPos.X += ScreenCapture.WindowPos.Left;
                        targetCurPos.Y += ScreenCapture.WindowPos.Top;
                    }
                    break;

                default:
                    break;
                }

                // 移動中(左ボタン押し下げ)の終了判定
                if (isRunning == true)
                {
                    isRunning = (aState[0, 2] == 2);
                    dState[1] = isRunning;
                }

                // ウィンドウがアクティブでない場合はフリーカーソル移動のみ許可
                bool isWindowActive = ScreenCapture.IsWindowActive;
                if (isWindowActive == false)
                {
                    if (aState[0, 2] == 1)
                    {
                        // ホイール時は入らない (aState[0, 2] == 8)
                        if (targetCurPos != clientCurPos)
                        {
                            WinSendInput.MouseMove(targetCurPos.X, targetCurPos.Y);
                        }
                    }
                    return;
                }

                // この時点でウィンドウがアクティブであることが保障される

                // 移動するべきであればカーソルを移動(ホイールの場合は動かさない)
                bool isMoved = false;
                if (targetCurPos != clientCurPos)
                {
                    // ホイール時は処理してはいけない
                    if (dState[0x0A] == false && dState[0x0B] == false)
                    {
                        WinSendInput.MouseMove(targetCurPos.X, targetCurPos.Y);
                        isMoved = true;
                    }
                }

                // ウィンドウの外にカーソルがある場合は全ての処理を行わない
                if (isMouseInWindow == false)
                {
                    // 0x07 画面キャプチャは取りたい?
                    if (dState[0x07] == true && lastState[0x07] == false)
                    {
                        SnapShot     = true;
                        lastState[7] = true;
                    }
                    return;
                }

                // この時点でウィンドウがアクティブでマウスがウィンドウ枠内に存在することが保障される

                for (int i = 0; i < dState.Length; i++)
                {
                    if (lastState[i] != dState[i])
                    {
                        switch (i)
                        {
                        case 0x01:     // マウス左ボタン
                        case 0x02:     // マウス右ボタン
                        case 0x04:     // マウス中ボタン
                        case 0x05:     // マウス XBUTTON1
                        case 0x06:     // マウス XBUTTON2
                        case 0x0A:     // ホイール上 (独自定義)
                        case 0x0B:     // ホイール下 (独自定義)
                            if (dState[i] == true)
                            {
                                WinSendInput.MouseEvent(i, true, (i == 0x0A ? targetCurPos.X : 0));
                            }
                            else
                            {
                                WinSendInput.MouseEvent(i, false, (i == 0x0B ? targetCurPos.Y : 0));
                            }
                            break;

                        case 0x07:     // 画面キャプチャ命令
                            if (dState[i] == true)
                            {
                                SnapShot = true;
                            }
                            break;

                        case 0x0E:     // 左タブ (独自定義)
                        case 0x0F:     // 右タブ (独自定義)
                            if (dState[i] == true)
                            {
                                if (isWindowActive && isMoved == false)
                                {
                                    // タブ切り替える場所に移動してクリックして戻ってくる
                                    Point tabPos = ImageMap.TabSelect(clientCurPos, i == 0x0F);

                                    if (clientCurPos != tabPos)
                                    {
                                        if (tabPos.X > 0)
                                        {
                                            WinSendInput.MouseMove(ScreenCapture.WindowPos.Left + tabPos.X, ScreenCapture.WindowPos.Top + tabPos.Y);
                                            WinSendInput.MouseEvent(1, true, 0);
                                            WinSendInput.MouseEvent(1, false, 0);
                                        }
                                        else
                                        {
                                            tabPos.X *= -1;
                                            WinSendInput.MouseMove(ScreenCapture.WindowPos.Left + tabPos.X, ScreenCapture.WindowPos.Top + tabPos.Y);
                                            WinSendInput.MouseEvent(1, true, 0);
                                            WinSendInput.MouseEvent(1, false, 0);
                                            // タブエリア外から飛んできた場合は戻る
                                            System.Threading.Thread.Sleep(200);
                                            WinSendInput.MouseMove(ScreenCapture.WindowPos.Left + clientCurPos.X, ScreenCapture.WindowPos.Top + clientCurPos.Y);
                                        }
                                    }
                                }
                            }
                            break;

                        default:
                            if (USE_KEYS[i] == true)
                            {
                                if (dState[i] == true)
                                {
                                    WinSendInput.DirectKey(i, true);
                                }
                                else
                                {
                                    WinSendInput.DirectKey(i, false);
                                    lastState[i] = false;
                                }
                            }
                            break;
                        }
                    }
                    lastState[i] = dState[i];
                }
            }
            catch (Exception)
            {
                //System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
            finally
            {
                // 排他フラグ下げる
                nowProcessing = false;
            }
        }
Пример #23
0
        /// <summary>
        /// Updates the rcoverride values and controls the mode changes
        /// </summary>
        void mainloop()
        {
            while (enabled)
            {
                try
                {
                    System.Threading.Thread.Sleep(50);
                    //joystick stuff
                    joystick.Poll();
                    state = joystick.CurrentJoystickState;
                    int[] slider = state.GetSlider();

                    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);
                        ushort roll  = pickchannel(1, JoyChannels[1].axis, false, JoyChannels[1].expo);
                        ushort pitch = pickchannel(2, JoyChannels[2].axis, false, JoyChannels[2].expo);

                        if (getJoystickAxis(1) != Joystick.joystickaxis.None)
                        {
                            MainV2.comPort.MAV.cs.rcoverridech1 = (ushort)(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 = (ushort)(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);
                    }

                    //UAS
                    if (JoyChannels[1].reverse)
                    {
                        MainV2.comPort.MAV.cs.controlleroverridech1 = (ushort)(65535 - state.X);
                    }
                    else
                    {
                        MainV2.comPort.MAV.cs.controlleroverridech1 = (ushort)state.X;
                    }
                    if (JoyChannels[2].reverse)
                    {
                        MainV2.comPort.MAV.cs.controlleroverridech2 = (ushort)(65535 - state.Y);
                    }
                    else
                    {
                        MainV2.comPort.MAV.cs.controlleroverridech2 = (ushort)state.Y;
                    }
                    if (JoyChannels[3].reverse)
                    {
                        MainV2.comPort.MAV.cs.controlleroverridech3 = (ushort)(65535 - state.GetSlider()[0]);
                    }
                    else
                    {
                        MainV2.comPort.MAV.cs.controlleroverridech1 = (ushort)state.GetSlider()[0];
                    }
                    if (JoyChannels[4].reverse)
                    {
                        MainV2.comPort.MAV.cs.controlleroverridech4 = (ushort)(65535 - state.Rz);
                    }
                    else
                    {
                        MainV2.comPort.MAV.cs.controlleroverridech4 = (ushort)state.Rz;
                    }

                    int pov = state.GetPointOfView()[0];

                    switch (pov)
                    {
                    case 0:     //PITCH UP
                        MainV2.comPort.MAV.cs.trim_y += 20;
                        break;

                    case 9000:     //ROLL RIGHT
                        MainV2.comPort.MAV.cs.trim_x -= 20;
                        break;

                    case 18000:     //PITCH DOWN
                        MainV2.comPort.MAV.cs.trim_y -= 20;
                        break;

                    case 27000:     //ROLL LEFT
                        MainV2.comPort.MAV.cs.trim_x += 20;
                        break;

                    default:
                        break;
                    }

                    foreach (JoyButton but in JoyButtons)
                    {
                        if (but.buttonno != -1 && getButtonState(but.buttonno))
                        {
                            string mode = but.mode;
                            MainV2.instance.BeginInvoke((System.Windows.Forms.MethodInvoker) delegate()
                            {
                                try
                                {
                                    MainV2.comPort.setMode(mode);
                                }
                                catch { System.Windows.Forms.CustomMessageBox.Show("Failed to change Modes"); }
                            });
                        }
                    }

                    //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 (Exception ex) { log.Info("Joystick thread error " + ex.ToString()); } // so we cant fall out
            }
        }
Пример #24
0
        private void Joystick_timer_Tick(object sender, EventArgs e)
        {
            if (joystick_exist)
            {
                try
                {
                    //Get Mouse State.

                    //Get
                    JoystickState state = joystick.CurrentJoystickState;

                    byte[] buttons = state.GetButtons();
                    int[]  pov     = state.GetPointOfView();

                    //Output

                    int joy_x  = state.X;
                    int joy_y  = -state.Y;
                    int joy_z  = state.Z;
                    int joy_rz = -state.Rz;
                    //buttons[0] !=0;
                    //buttons[1] != 0;
                    //buttons[2] != 0;
                    //buttons[3] != 0;
                    //buttons[4] != 0;
                    bool btn_6 = buttons[5] != 0;
                    //buttons[6] != 0;
                    //buttons[7] != 0;
                    //buttons[8] != 0;
                    //buttons[9] != 0;
                    //buttons[10] != 0;
                    //buttons[11] != 0;
                    //pov[0];
                    //                    listBox2.Items.Add("X:"+joy_x.ToString()+"Y:"+joy_y.ToString()+"Z:"+joy_z.ToString()+"RZ:"+joy_rz.ToString());

                    int    deadband = 120;
                    double gain     = 127.0 / 1000;
                    if (btn_6)
                    {
                        gain *= 2;//high speed
                    }
                    else
                    {
                        gain *= 1;//high speed
                    }
                    if ((-deadband < joy_y) && (joy_y < deadband))
                    {
                        joy_y = 0;
                    }
                    if (joy_y < 0)
                    {
                        joy_y = joy_y + deadband;
                    }
                    if (joy_y > 0)
                    {
                        joy_y = joy_y - deadband;
                    }
                    if ((-deadband < joy_z) && (joy_z < deadband))
                    {
                        joy_z = 0;
                    }
                    if (joy_z < 0)
                    {
                        joy_z = joy_z + deadband;
                    }
                    if (joy_z > 0)
                    {
                        joy_z = joy_z - deadband;
                    }
                }
                catch
                {
                    joystick_exist    = false;
                    checkBox1.Checked = false;
                    //listBox2.Items.Add("Fail to Read Joystick.");
                    return;
                }
            }
        }
Пример #25
0
        public void UpdateJoystick(VModel Vmodel)
        {
            JoystickState state = joystick.CurrentJoystickState;//M

            //Capture Position.C>
            Model.vGM.button_data1 = 0;
            Model.vGM.button_data2 = 0;
            byte[] buttons  = state.GetButtons();
            int[]  sligterP = state.GetSlider();
            int[]  viewP    = state.GetPointOfView();
            for (int i = 0; i < 12; i++)
            {
                if (buttons[i] != 0)
                {
                    Buttons[i] = 1;
                }
                else
                {
                    Buttons[i] = 0;
                }
            }
            //C<
            //костыль для изменения кнопок блютуза и инвертирования камер
            int change = Buttons[0];

            Buttons[0]  = Buttons[1];
            Buttons[1]  = change;
            change      = Buttons[3];
            Buttons[3]  = Buttons[11];
            Buttons[11] = change;

            for (int i = 0; i <= 7; i++)//C>
            {
                if ((Buttons[i] == 1))
                {
                    Model.vGM.button_data1 = (sbyte)(((int)Model.vGM.button_data1) | (1 << i));
                }
            }
            for (int i = 8; i < 12; i++)
            {
                if ((Buttons[i] == 1))
                {
                    Model.vGM.button_data2 = (sbyte)(((int)Model.vGM.button_data2) | (1 << (i - 8)));
                }
            }

            if (viewP[2] == 0)
            {
                viewP[0] = 0;
            }
            Model.vGM.manipulator_p = (sbyte)viewP[0];
            if (Model.vGM.manipulator_p == (sbyte)40)
            {
                Model.vGM.manipulator_p = (sbyte)1;
            }
            if (Model.vGM.manipulator_p == (sbyte)120)
            {
                Model.vGM.manipulator_p = (sbyte)3;
            }
            if (Model.vGM.manipulator_p == (sbyte)80)
            {
                Model.vGM.manipulator_p = (sbyte)2;
            }
            if (Model.vGM.manipulator_p == (sbyte)0)
            {
                Model.vGM.manipulator_p = (sbyte)4;
            }
            if (Model.vGM.manipulator_p == (sbyte)-1)
            {
                Model.vGM.manipulator_p = 0;
            }

            if (Buttons[6] == 1)
            {
                Vmodel.SpeedMode = "1";
                SpeedK           = 0.25;
            }
            if (Buttons[5] == 1)
            {
                Vmodel.SpeedMode = "2";
                SpeedK           = 0.5;
            }
            if (Buttons[4] == 1)
            {
                Vmodel.SpeedMode = "3";
                SpeedK           = 1.0;
            }

            Model.vGM.axisY_p = (sbyte)Math.Round(state.X * SpeedK);
            Model.vGM.axisX_p = (sbyte)Math.Round(state.Y * SpeedK * -1);
            Model.vGM.axisW_p = (sbyte)Math.Round(state.Z * SpeedK);
            Model.vGM.axisZ_p = (sbyte)Math.Round(state.Rz * SpeedK);

            if (sligterP[0] > -50 && sligterP[0] < 50)
            {
                sligterP[0] = 0;
            }
            Model.vGM.slighter_p = (sbyte)(sligterP[0]);//V<
        }
Пример #26
0
    private void SendActions(JoystickState state)
    {
      int actionCode = -1;
      int actionParam = -1;
      int curAxisValue = 0;
      // todo: timer stuff!!

      // buttons first!
      byte[] buttons = state.GetButtons();
      int button = 0;
      string pressedButtons = "";

      // button combos
      string sep = "";
      foreach (byte b in buttons)
      {
        if (0 != (b & 0x80))
        {
          pressedButtons += sep + button.ToString("00");
          sep = ",";
        }
        button++;
      }

      if ((ButtonComboKill != "") && (ButtonComboKill == pressedButtons))
      {
        if (null != _lastProc)
        {
          actionCode = (int)joyButton.comboKillProcess;
          actionParam = _lastProc.Id;
        }
      }
      else if ((ButtonComboClose != "") && (ButtonComboClose == pressedButtons))
      {
        if (null != _lastProc)
        {
          actionCode = (int)joyButton.comboCloseProcess;
          actionParam = _lastProc.Id;
        }
      }

      // single buttons
      if (actionCode == -1)
      {
        button = 0;
        bool foundButton = false;
        foreach (byte b in buttons)
        {
          if (0 != (b & 0x80))
          {
            foundButton = true;
            break;
          }
          button++;
        }
        if (foundButton)
        {
          if ((button >= 0) && (button <= 19))
          {
            // don't need no stinkin' enum-constants here....
            actionCode = 3030 + button;
          }
        }
      }

      // pov next
      if (actionCode == -1)
      {
        int[] pov = state.GetPointOfView();
        switch (pov[0])
        {
          case 0:
            {
              actionCode = (int)joyButton.povN;
              break;
            }
          case 9000:
            {
              actionCode = (int)joyButton.povE;
              break;
            }
          case 18000:
            {
              actionCode = (int)joyButton.povS;
              break;
            }
          case 27000:
            {
              actionCode = (int)joyButton.povW;
              break;
            }
        }
      }

      if (actionCode == -1)
      {
        // axes next
        if (Math.Abs(state.X) > _axisLimit)
        {
          curAxisValue = state.X;
          if (state.X > 0)
          {
            actionCode = (int)joyButton.axisXUp; // right
          }
          else
          {
            actionCode = (int)joyButton.axisXDown; // left
          }
        }
        else if (Math.Abs(state.Y) > _axisLimit)
        {
          curAxisValue = state.Y;
          if (state.Y > 0)
          {
            // down
            actionCode = (int)joyButton.axisYUp;
          }
          else
          {
            // up
            actionCode = (int)joyButton.axisYDown;
          }
        }
        else if (Math.Abs(state.Z) > _axisLimit)
        {
          curAxisValue = state.Z;
          if (state.Z > 0)
          {
            actionCode = (int)joyButton.axisZUp;
          }
          else
          {
            actionCode = (int)joyButton.axisZDown;
          }
        }
      }

      if (actionCode == -1)
      {
        // rotation
        if (Math.Abs(state.Rx) > _axisLimit)
        {
          curAxisValue = state.Rx;
          if (state.Rx > 0)
          {
            actionCode = (int)joyButton.rotationXUp;
          }
          else
          {
            actionCode = (int)joyButton.rotationXDown;
          }
        }
        else if (Math.Abs(state.Ry) > _axisLimit)
        {
          curAxisValue = state.Ry;
          if (state.Ry > 0)
          {
            actionCode = (int)joyButton.rotationYUp;
          }
          else
          {
            actionCode = (int)joyButton.rotationYDown;
          }
        }
        else if (Math.Abs(state.Rz) > _axisLimit)
        {
          curAxisValue = state.Rz;
          if (state.Rz > 0)
          {
            actionCode = (int)joyButton.rotationZUp;
          }
          else
          {
            actionCode = (int)joyButton.rotationZDown;
          }
        }
      }

      if (VerifyAction(actionCode, curAxisValue))
      {
        Log.Info("mapping action {0}", actionCode);
        _inputHandler.MapAction(actionCode, actionParam);
      }
    }
Пример #27
0
    private string GetStateAsText(JoystickState state)
    {
      string strText = string.Empty;

      string joyState = string.Format("Axis    : {0:+0000;-0000} / {1:+0000;-0000} / {2:+0000;-0000}\n", state.X,
                                      state.Y, state.Z);
      joyState += string.Format("Rotation: {0:+0000;-0000} / {1:+0000;-0000} / {2:+0000;-0000}\n\n", state.Rx, state.Ry,
                                state.Rz);

      int[] slider = state.GetSlider();
      joyState += string.Format("Slider  : 0: {0:+0000;-0000} 1: {1:+0000;-0000}\n\n", slider[0], slider[1]);

      int[] pov = state.GetPointOfView();
      switch (pov[0])
      {
        case 0:
          {
            joyState += string.Format("POV     : North\n");
            break;
          }
        case 4500:
          {
            joyState += string.Format("POV     : NorthEast\n");
            break;
          }
        case 9000:
          {
            joyState += string.Format("POV     : East\n");
            break;
          }
        case 13500:
          {
            joyState += string.Format("POV     : SouthEast\n");
            break;
          }
        case 18000:
          {
            joyState += string.Format("POV     : South\n");
            break;
          }
        case 22500:
          {
            joyState += string.Format("POV     : SouthWest\n");
            break;
          }
        case 27000:
          {
            joyState += string.Format("POV     : West\n");
            break;
          }
        case 31500:
          {
            joyState += string.Format("POV     : NorthWest\n");
            break;
          }
        default:
          {
            break;
          }
      }

      // Fill up text with which buttons are pressed
      byte[] buttons = state.GetButtons();

      int button = 0;
      foreach (byte b in buttons)
      {
        if (0 != (b & 0x80))
        {
          strText += button.ToString("00 ");
        }
        button++;
      }
      if (strText != string.Empty)
      {
        joyState += "Buttons : " + strText;
      }
      return joyState;
    }
Пример #28
0
        private void SendActions(JoystickState state)
        {
            int actionCode   = -1;
            int actionParam  = -1;
            int curAxisValue = 0;

            // todo: timer stuff!!

            // buttons first!
            byte[] buttons        = state.GetButtons();
            int    button         = 0;
            string pressedButtons = "";

            // button combos
            string sep = "";

            foreach (byte b in buttons)
            {
                if (0 != (b & 0x80))
                {
                    pressedButtons += sep + button.ToString("00");
                    sep             = ",";
                }
                button++;
            }

            if ((ButtonComboKill != "") && (ButtonComboKill == pressedButtons))
            {
                if (null != _lastProc)
                {
                    actionCode  = (int)joyButton.comboKillProcess;
                    actionParam = _lastProc.Id;
                }
            }
            else if ((ButtonComboClose != "") && (ButtonComboClose == pressedButtons))
            {
                if (null != _lastProc)
                {
                    actionCode  = (int)joyButton.comboCloseProcess;
                    actionParam = _lastProc.Id;
                }
            }

            // single buttons
            if (actionCode == -1)
            {
                button = 0;
                bool foundButton = false;
                foreach (byte b in buttons)
                {
                    if (0 != (b & 0x80))
                    {
                        foundButton = true;
                        break;
                    }
                    button++;
                }
                if (foundButton)
                {
                    if ((button >= 0) && (button <= 19))
                    {
                        // don't need no stinkin' enum-constants here....
                        actionCode = 3030 + button;
                    }
                }
            }

            // pov next
            if (actionCode == -1)
            {
                int[] pov = state.GetPointOfView();
                switch (pov[0])
                {
                case 0:
                {
                    actionCode = (int)joyButton.povN;
                    break;
                }

                case 9000:
                {
                    actionCode = (int)joyButton.povE;
                    break;
                }

                case 18000:
                {
                    actionCode = (int)joyButton.povS;
                    break;
                }

                case 27000:
                {
                    actionCode = (int)joyButton.povW;
                    break;
                }
                }
            }

            if (actionCode == -1)
            {
                // axes next
                if (Math.Abs(state.X) > _axisLimit)
                {
                    curAxisValue = state.X;
                    if (state.X > 0)
                    {
                        actionCode = (int)joyButton.axisXUp; // right
                    }
                    else
                    {
                        actionCode = (int)joyButton.axisXDown; // left
                    }
                }
                else if (Math.Abs(state.Y) > _axisLimit)
                {
                    curAxisValue = state.Y;
                    if (state.Y > 0)
                    {
                        // down
                        actionCode = (int)joyButton.axisYUp;
                    }
                    else
                    {
                        // up
                        actionCode = (int)joyButton.axisYDown;
                    }
                }
                else if (Math.Abs(state.Z) > _axisLimit)
                {
                    curAxisValue = state.Z;
                    if (state.Z > 0)
                    {
                        actionCode = (int)joyButton.axisZUp;
                    }
                    else
                    {
                        actionCode = (int)joyButton.axisZDown;
                    }
                }
            }

            if (actionCode == -1)
            {
                // rotation
                if (Math.Abs(state.Rx) > _axisLimit)
                {
                    curAxisValue = state.Rx;
                    if (state.Rx > 0)
                    {
                        actionCode = (int)joyButton.rotationXUp;
                    }
                    else
                    {
                        actionCode = (int)joyButton.rotationXDown;
                    }
                }
                else if (Math.Abs(state.Ry) > _axisLimit)
                {
                    curAxisValue = state.Ry;
                    if (state.Ry > 0)
                    {
                        actionCode = (int)joyButton.rotationYUp;
                    }
                    else
                    {
                        actionCode = (int)joyButton.rotationYDown;
                    }
                }
                else if (Math.Abs(state.Rz) > _axisLimit)
                {
                    curAxisValue = state.Rz;
                    if (state.Rz > 0)
                    {
                        actionCode = (int)joyButton.rotationZUp;
                    }
                    else
                    {
                        actionCode = (int)joyButton.rotationZDown;
                    }
                }
            }

            if (VerifyAction(actionCode, curAxisValue))
            {
                Log.Info("mapping action {0}", actionCode);
                _inputHandler.MapAction(actionCode, actionParam);
            }
        }
Пример #29
0
        /// <summary>
        /// Updates the rcoverride values and controls the mode changes
        /// </summary>
        void mainloop()
        {
            while (enabled)
            {
                try
                {
                    System.Threading.Thread.Sleep(50);
                    //joystick stuff
                    joystick.Poll();
                    state = joystick.CurrentJoystickState;
                    int[] slider = state.GetSlider();

                    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);
                        ushort roll = pickchannel(1, JoyChannels[1].axis, false, JoyChannels[1].expo);
                        ushort pitch = pickchannel(2, JoyChannels[2].axis, false, JoyChannels[2].expo);

                        if (getJoystickAxis(1) != Joystick.joystickaxis.None)
                            MainV2.comPort.MAV.cs.rcoverridech1 = (ushort)(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 = (ushort)(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);

                    //UAS
                    if (elevons)
                    {
                        if (JoyChannels[1].reverse && elevRev1)
                            MainV2.comPort.MAV.cs.controlleroverridech1 = (ushort)(65535 - (((int)state.X + (int)(65535 - state.Y)) / 2));
                        else if(JoyChannels[1].reverse)
                            MainV2.comPort.MAV.cs.controlleroverridech1 = (ushort)(65535 - ((int)state.X + (int)state.Y) / 2);
                        else if(elevRev1)
                            MainV2.comPort.MAV.cs.controlleroverridech1 = (ushort)(((int)state.X + (int)(65535 - state.Y)) / 2);
                        else
                            MainV2.comPort.MAV.cs.controlleroverridech1 = (ushort)(((int)state.X + (int)state.Y) / 2);

                        if (JoyChannels[2].reverse && elevRev2)
                            MainV2.comPort.MAV.cs.controlleroverridech2 = (ushort)(65535 - (((int)state.X + (int)(65535 - state.Y)) / 2));
                        else if (JoyChannels[2].reverse)
                            MainV2.comPort.MAV.cs.controlleroverridech2 = (ushort)(65535 - ((int)state.X + (int)state.Y) / 2);
                        else if (elevRev2)
                            MainV2.comPort.MAV.cs.controlleroverridech2 = (ushort)(((int)state.X + (int)(65535 - state.Y)) / 2);
                        else
                            MainV2.comPort.MAV.cs.controlleroverridech2 = (ushort)(((int)state.X + (int)state.Y) / 2);
                    }
                    else
                    {
                        if (JoyChannels[1].reverse)
                            MainV2.comPort.MAV.cs.controlleroverridech1 = (ushort)(65535 - state.X);
                        else
                            MainV2.comPort.MAV.cs.controlleroverridech1 = (ushort)state.X;
                        if (JoyChannels[2].reverse)
                            MainV2.comPort.MAV.cs.controlleroverridech2 = (ushort)(65535 - state.Y);
                        else
                            MainV2.comPort.MAV.cs.controlleroverridech2 = (ushort)state.Y;
                    }
                    if (JoyChannels[3].reverse)
                        MainV2.comPort.MAV.cs.controlleroverridech3 = (ushort)(65535 - state.GetSlider()[0]);
                    else
                        MainV2.comPort.MAV.cs.controlleroverridech1 = (ushort)state.GetSlider()[0];
                    if (JoyChannels[4].reverse)
                        MainV2.comPort.MAV.cs.controlleroverridech4 = (ushort)(65535 - state.Rz);
                    else
                        MainV2.comPort.MAV.cs.controlleroverridech4 = (ushort)state.Rz;

                    int pov = state.GetPointOfView()[0];

                    switch (pov)
                    {
                        case 0: //PITCH UP
                            MainV2.comPort.MAV.cs.trim_y += 20;
                            break;
                        case 9000: //ROLL RIGHT
                            MainV2.comPort.MAV.cs.trim_x -= 20;
                            break;
                        case 18000: //PITCH DOWN
                            MainV2.comPort.MAV.cs.trim_y -= 20;
                            break;
                        case 27000: //ROLL LEFT
                            MainV2.comPort.MAV.cs.trim_x += 20;
                            break;
                        default:
                            break;
                    }

                    foreach (JoyButton but in JoyButtons)
                    {
                        if (but.buttonno != -1 && getButtonState(but.buttonno))
                        {
                            string mode = but.mode;
                            MainV2.instance.BeginInvoke((System.Windows.Forms.MethodInvoker)delegate()
                            {
                                try
                                {
                                    MainV2.comPort.setMode(mode);

                                }
                                catch { System.Windows.Forms.CustomMessageBox.Show("Failed to change Modes"); }
                            });
                        }
                    }

                    //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 (Exception ex) { log.Info("Joystick thread error "+ex.ToString()); } // so we cant fall out
            }
        }
Пример #30
0
        private string GetStateAsText(JoystickState state)
        {
            string strText = string.Empty;

            string joyState = string.Format("Axis    : {0:+0000;-0000} / {1:+0000;-0000} / {2:+0000;-0000}\n", state.X,
                                            state.Y, state.Z);

            joyState += string.Format("Rotation: {0:+0000;-0000} / {1:+0000;-0000} / {2:+0000;-0000}\n\n", state.Rx, state.Ry,
                                      state.Rz);

            int[] slider = state.GetSlider();
            joyState += string.Format("Slider  : 0: {0:+0000;-0000} 1: {1:+0000;-0000}\n\n", slider[0], slider[1]);

            int[] pov = state.GetPointOfView();
            switch (pov[0])
            {
            case 0:
            {
                joyState += string.Format("POV     : North\n");
                break;
            }

            case 4500:
            {
                joyState += string.Format("POV     : NorthEast\n");
                break;
            }

            case 9000:
            {
                joyState += string.Format("POV     : East\n");
                break;
            }

            case 13500:
            {
                joyState += string.Format("POV     : SouthEast\n");
                break;
            }

            case 18000:
            {
                joyState += string.Format("POV     : South\n");
                break;
            }

            case 22500:
            {
                joyState += string.Format("POV     : SouthWest\n");
                break;
            }

            case 27000:
            {
                joyState += string.Format("POV     : West\n");
                break;
            }

            case 31500:
            {
                joyState += string.Format("POV     : NorthWest\n");
                break;
            }

            default:
            {
                break;
            }
            }

            // Fill up text with which buttons are pressed
            byte[] buttons = state.GetButtons();

            int button = 0;

            foreach (byte b in buttons)
            {
                if (0 != (b & 0x80))
                {
                    strText += button.ToString("00 ");
                }
                button++;
            }
            if (strText != string.Empty)
            {
                joyState += "Buttons : " + strText;
            }
            return(joyState);
        }
Пример #31
0
 public int getHatSwitchDirection()
 {
     return((state.GetPointOfView())[0]);
 }
    private void SendActions(JoystickState state)
    {
      int actionCode = -1;

      //int curAxisValue = 0;
      // todo: timer stuff!!

      // buttons first!
      byte[] buttons = state.GetButtons();
      int button = 0;

      // button combos
      /*
      string sep = "";
      string pressedButtons = "";
      foreach (byte b in buttons)
      {
        if ((b & 0x80) != 0)
        {
          pressedButtons += sep + button.ToString("00");
          sep = ",";
        }
        button++;
      }
      */

      // single buttons
      if (actionCode == -1)
      {
        button = 0;
        bool foundButton = false;
        foreach (byte b in buttons)
        {
          if (0 != (b & 0x80))
          {
            foundButton = true;
            break;
          }
          button++;
        }
        if (foundButton)
        {
          if ((button >= 0) && (button <= 19))
          {
            // don't need no stinkin' enum-constants here....
            actionCode = 3030 + button;
          }
        }
      }

      // pov next
      if (actionCode == -1)
      {
        int[] pov = state.GetPointOfView();
        switch (pov[0])
        {
          case 0:
            actionCode = (int)joyButton.povN;
            break;
          case 4500:
            actionCode = (int)joyButton.povNE;
            break;
          case 9000:
            actionCode = (int)joyButton.povE;
            break;
          case 13500:
            actionCode = (int)joyButton.povSE;
            break;
          case 18000:
            actionCode = (int)joyButton.povS;
            break;
          case 22500:
            actionCode = (int)joyButton.povSW;
            break;
          case 27000:
            actionCode = (int)joyButton.povW;
            break;
          case 31500:
            actionCode = (int)joyButton.povNW;
            break;
        }
      }

      if (actionCode == -1)
      {
        // axes next
        if (Math.Abs(state.X) > AxisLimit)
        {
          //curAxisValue = state.X;
          if (state.X > 0)
          {
            actionCode = (int)joyButton.axisXUp; // right
          }
          else
          {
            actionCode = (int)joyButton.axisXDown; // left
          }
        }
        else if (Math.Abs(state.Y) > AxisLimit)
        {
          //curAxisValue = state.Y;
          if (state.Y > 0)
          {
            // down
            actionCode = (int)joyButton.axisYUp;
          }
          else
          {
            // up
            actionCode = (int)joyButton.axisYDown;
          }
        }
        else if (Math.Abs(state.Z) > AxisLimit)
        {
          //curAxisValue = state.Z;
          if (state.Z > 0)
          {
            actionCode = (int)joyButton.axisZUp;
          }
          else
          {
            actionCode = (int)joyButton.axisZDown;
          }
        }
      }

      if (actionCode == -1)
      {
        // rotation
        if (Math.Abs(state.Rx) > AxisLimit)
        {
          //curAxisValue = state.Rx;
          if (state.Rx > 0)
          {
            actionCode = (int)joyButton.rotationXUp;
          }
          else
          {
            actionCode = (int)joyButton.rotationXDown;
          }
        }
        else if (Math.Abs(state.Ry) > AxisLimit)
        {
          //curAxisValue = state.Ry;
          if (state.Ry > 0)
          {
            actionCode = (int)joyButton.rotationYUp;
          }
          else
          {
            actionCode = (int)joyButton.rotationYDown;
          }
        }
        else if (Math.Abs(state.Rz) > AxisLimit)
        {
          //curAxisValue = state.Rz;
          if (state.Rz > 0)
          {
            actionCode = (int)joyButton.rotationZUp;
          }
          else
          {
            actionCode = (int)joyButton.rotationZDown;
          }
        }
      }

      if (actionCode != -1 && _remoteHandler != null)
      {
        string keyCode = TranslateActionCode(actionCode);

        _remoteHandler(Name, keyCode);
      }
    }