/// <summary>
        /// Button press check with debounce
        /// </summary>
        /// <param name="buttonno"></param>
        /// <returns></returns>
        bool getButtonState(JoyButton but, int buttonno)
        {
            byte[] buts = state.GetButtons();

            // button down
            bool ans = buts[buttonno] > 0 && buttonpressed[buttonno] == 0; // press check + debounce

            if (ans)
            {
                ButtonDown(but);
            }

            // button up
            ans = buts[buttonno] == 0 && buttonpressed[buttonno] > 0;
            if (ans)
            {
                ButtonUp(but);
            }

            buttonpressed[buttonno] = buts[buttonno]; // set only this button

            return(ans);
        }
Пример #2
0
        int servoPos     = 90;   //temporary

        void stickHandle(Joystick stick, int id)
        {
            JoystickState state = new JoystickState();

            state = stick.GetCurrentState();
            x     = state.X;
            y     = state.Y;
            z     = state.Z;
            int zPwr      = (z / 82) + 1100;//these are NOT arbitrarily created numbers
            int xPwr      = (x / 82) + 1100;
            int yPwr      = (y / 82) + 1100;
            int rightTrig = (state.RotationY / 164) + 1500;
            int leftTrig  = 1500 - (state.RotationX / 164);

            bool[] buttons = state.GetButtons();

            Invoke(new MethodInvoker(delegate
            {
                debug.Text = "X: " + xPwr;
            }));

            if (tcpclnt != null)
            {
                if (tcpclnt.Connected)
                {
                    String str = ((yPwr > 1550 && yPwr < highDeadBand)? divider + "f" + yPwr : "") + (yPwr <1450 && yPwr> lowDeadBand ? divider + "f" + yPwr : "") +
                                 (xPwr > 1550 && xPwr < highDeadBand ? divider + "l" + xPwr : "") + (xPwr <1450 && xPwr> lowDeadBand ? divider + "l" + xPwr : "") +
                                 (buttons[3] ? divider + "c0001" : "") + (buttons[1] ? divider + "v0001" : "") +
                                 (buttons[10] ? divider + "r0000":"") +
                                 (buttons[4] ? divider + "x0100":"") + (buttons[5] ? divider + "x-100":"") +
                                 (buttons[2] ? divider + ",0001" : "") + (buttons[0] ? divider + ".0001" : "") +//servo, adding this many degrees to position
                                 (zPwr > 1550 || zPwr < 1450 ? divider + "z" + zPwr : "") +
                                 checkForBuzzer(buttons[13]) +
                                 (rightTrig > 1501 ? divider + "h" + rightTrig : "") + (leftTrig < 1499 ? divider + "h" + leftTrig : "");
                    str += checkForStop(xPwr, yPwr, zPwr);
                    if (str.Length > 0)
                    {
                        str += divider + "\n";
                        ASCIIEncoding asen = new ASCIIEncoding();
                        Invoke(new MethodInvoker(delegate
                        {
                            //debug.Text = asen.GetString(asen.GetBytes(str));
                        }));
                        byte[] ba = asen.GetBytes(str);
                        stream.Write(ba, 0, ba.Length);
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Time to update things again.
        /// </summary>
        void HandleJoystick()
        {
            // Get the state of the device.
            JoystickState jss = joystick.CurrentJoystickState;

            byte[] button       = jss.GetButtons();
            bool   isButtonDown = false;

            if (button[0] != 0)
            {
                // Button 1 pressed
                isButtonDown = true;

                // Heading
                drawArgs.WorldCamera.RotationYawPitchRoll(
                    Angle.Zero,
                    Angle.Zero,
                    Angle.FromRadians(-jss.X * RotationFactor));

                // Zoom in/out
                double altitudeDelta = ZoomFactor * drawArgs.WorldCamera.Altitude * (float)jss.Y / AxisRange;
                drawArgs.WorldCamera.Altitude += altitudeDelta;
            }

            if (button[1] != 0)
            {
                // Button 2 pressed
                isButtonDown = true;

                // Bank
                drawArgs.WorldCamera.Bank += Angle.FromRadians(jss.X * RotationFactor);

                // Tilt
                drawArgs.WorldCamera.Tilt += Angle.FromRadians(jss.Y * RotationFactor);
            }


            if (!isButtonDown)
            {
                // Normal rotation
                double scaling = 0.2 * RotationFactor * drawArgs.WorldCamera.ViewRange.Radians;

                // Up/Down, Left/Right
                drawArgs.WorldCamera.RotationYawPitchRoll(
                    Angle.FromRadians(jss.X * scaling),
                    Angle.FromRadians(-jss.Y * scaling),
                    Angle.Zero);
            }
        }
Пример #4
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));
        }
Пример #5
0
        void stickHandle(Joystick stick, int id)
        {
            JoystickState state = new JoystickState();

            state = stick.GetCurrentState();

            yValue = state.Y;
            xValue = state.X;
            zValue = state.Z;

            //Sending Joystick data over USB2UART
            //MouseMove(xValue, yValue);
            if (ComPort.IsOpen)
            {
                SendJoystickData(xValue, yValue);
                bool[] buttons = state.GetButtons();

                //if (id == 0)
                {
                    for (int i = 0; i < buttons.Length; i++)
                    {
                        if (buttons[i])
                        {
                            SendButtonsData(i);
                            //MessageBox.Show(String.Format("Button '{0}' pressed", i));
                        }
                    }
                    //    if (buttons[0])
                    //    {
                    //        Do stuff
                    //        if (mouseClicked == false)
                    //        {
                    //            mouse_event(MOUSEEVENT_LEFTDOWN, 0, 0, 0, 0);
                    //            mouseClicked = true;
                    //        }
                    //    }
                    //    else
                    //    {
                    //        if (mouseClicked == true)
                    //        {
                    //            mouse_event(MOUSEEVENT_LEFTUP, 0, 0, 0, 0);
                    //            mouseClicked = false;

                    //        }
                    //    }
                }
            }
        }
Пример #6
0
        /// <summary>
        /// Update the properties of button and axis positions.
        /// </summary>
        public void UpdateStatus()
        {
            int repeat = 20;

            int valueXsum  = 0;
            int valueYsum  = 0;
            int valueZsum  = 0;
            int valueRysum = 0;

            for (int i = 0; i < repeat; i++)
            {
                Poll();

                valueXsum  += state.X;
                valueYsum  += state.Y;
                valueZsum  += state.Z;
                valueRysum += state.Ry;
            }
            valueXsum  /= repeat;
            valueYsum  /= repeat;
            valueZsum  /= repeat;
            valueRysum /= repeat;

            axisX  = valueXsum;
            axisY  = valueYsum;
            axisZ  = valueZsum;
            axisRy = state.Ry;


            roll     = Convert.ToInt32(Map(AxisX, (decimal)rollMin, (decimal)rollMax, -90, 90));
            pitch    = Convert.ToInt32(Map(AxisY, (decimal)pitchMin, (decimal)pitchMax, -90, 90));
            throttle = Convert.ToInt32(Map(AxisZ, (decimal)throttleMin, (decimal)throttleMax, 1050, 1900));
            yaw      = Convert.ToInt32(Map(AxisRy, (decimal)yawMin, (decimal)rollMax, -180, 180));



            // 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 a = 0;

            foreach (byte button in jsButtons)
            {
                buttons[a] = button >= 128;
                a++;
            }
        }
Пример #7
0
        public void ReadPaddle()
        {
            paddleDevice.Poll();
            jsState = paddleDevice.CurrentJoystickState;
            byte[] contacts = jsState.GetButtons();
            int    thisDit  = contacts[0];
            int    thisDah  = contacts[1];

            if (thisDit != lastDit | (thisDah != lastDah))
            {
                lastDit = thisDit;
                lastDah = thisDah;
                int[] bauds = { thisDit, thisDah };
                OnChanged(bauds);
            }
        }
Пример #8
0
            }//test

            public bool BindsPressed(Joystick stickk)
            {
                JoystickState state1 = new JoystickState();

                state1  = stickk.GetCurrentState();
                buttons = state1.GetButtons();

                for (int i = 0; i < 7; ++i)
                {
                    if (binds[i] == true)
                    {
                        return(true);
                    }
                }
                return(false);
            }
Пример #9
0
        /// <summary>
        /// Loop though all buttons on the gamepad an see which one is pressed
        /// </summary>
        /// <returns>The pressed button</returns>
        public int GetPressedButton()
        {
            int buttonIndex = -1;

            _state = GetState();
            var buttons = new bool[_state.GetButtons().Length];

            for (int i = 0; i < buttons.Length; i++)
            {
                if (_state.IsPressed(i))
                {
                    buttonIndex = i;
                }
            }
            return(buttonIndex);
        }
        private void RefreshState()
        {
            while (true)
            {
                _state = _joystick.GetCurrentState();

                if (_isimeToExit == true)
                {
                    _joystick.Unacquire();
                    _joystick.Dispose();
                    return;
                }

                var btns = _state.GetButtons();
                if (_isXBoxGamePad == false)
                {
                    _speedX = Convert(_state.RotationZ, true);
                    _speedY = Convert(_state.Z, false);
                }
                else
                {
                    _speedX = Convert(_state.RotationX);
                    _speedY = Convert(_state.RotationY, true);
                }
                _kickUp.SetState(btns[_kickUpIndex]);
                _kickForward.SetState(btns[_kickForwardIndex]);
                _kickerEnable.SetFixationState(btns[_kickerEnableIndex]);
                _dribbler.SetFixationState(btns[_dribblerIndex]);
                setPovButtons();
                _speedR = Convert(_state.X);

                var footBot = new Footbot()
                {
                    SpeedX             = _speedX,
                    SpeedY             = _speedY,
                    SpeedR             = -_speedR,
                    SpeedDribbler      = _speedDribbler,
                    DribblerEnable     = _dribbler.GetState(),
                    KickerVoltageLevel = _voltageLevel,
                    KickerChargeEnable = _kickerEnable.GetState(),
                    KickUp             = _kickUp.GetState(),
                    KickForward        = _kickForward.GetState()
                };
                ValueComposed?.Invoke(this, footBot);
                Thread.Sleep(100);
            }
        }
Пример #11
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);
        }
Пример #12
0
        public override void Poll()
        {
            JoystickState state = Joystick.GetCurrentState();

            //Straight forward for PS Controllers
            //Just convert to -400 to 400 scale.
            X         = (state.X / 82) - 400;
            Y         = (state.Y / 82) - 400;
            Z         = (state.Z / 82) - 400;
            RotationX = (state.RotationX / 163);
            RotationY = (state.RotationY / 163);
            RotationZ = (state.RotationZ / 82) - 400;
            PointOfViewControllers = state.GetPointOfViewControllers();
            Buttons = state.GetButtons();

            //add more as we need more
        }
        protected override void ProcessController()
        {
            ControllerState newState = new ControllerState();
            JoystickState joyState = new JoystickState();

            if (this.padsList == null || !this.padsList.Any())
            {
                this.padsList = this.GetGamePads();
                if (!this.padsList.Any()) return;
            }

            if (this.device == null)
            {
                this.device = this.padsList[0];
            }

            if (this.pad == null)
            {
                this.Acquire();
            }

            if (this.pad == null || this.pad.Poll().IsFailure || this.pad.GetCurrentState(ref joyState).IsFailure)
            {
                newState.Active = false;
            }
            else
            {
                //var objs = pad.GetObjects();
                newState.Active = true;
                newState.X = (joyState.X / 5000d);
                newState.Y = (joyState.Y / 5000d);
                newState.Buttons = joyState.GetButtons();
            }

            if (!newState.Equals(this.State))
            {

                this.State = newState;

                if (this.OnUpdate != null)
                {
                    this.OnUpdate.Invoke(this.State);
                }
            }
        }
Пример #14
0
        public void PollChanges(iGameClient GameCli)
        {
            //polls changes in this controller from the previous poll and raises
            //appropriate events in iGameClient depending on the differences.
            if (!haspolled)
            {
                haspolled=true;
                ourdevice.Poll();
                laststate = ourdevice.CurrentJoystickState;
                return;

            }
            laststate = ourdevice.CurrentJoystickState;
            ourdevice.Poll();

            byte[] prevbuttons = laststate.GetButtons();
            byte[] currbuttons = ourdevice.CurrentJoystickState.GetButtons();
        }
Пример #15
0
        void StickHandlerLogic(Joystick js)
        {
            //Enumerate a snapshot of the joystick
            JoystickState state = new JoystickState();

            tab_mate.Acquire();
            state = tab_mate.GetCurrentState();

            bool[] buttons = state.GetButtons();

            for (int button_num = 0; button_num < buttons.Length; button_num++)
            {
                if (buttons[button_num])
                {
                    Console.WriteLine("Button " + button_num + " was pressed!");
                }
            }
        }
Пример #16
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            jState = sJoy.State;
            double angX = 0;
            double angY = 0;
            double angZ = 0;

            bool[] Jbuttons = jState.GetButtons();

            if (Math.Abs(jState.X - calX) > threshold)
            {
                angX = ((int)(jState.X - calX - (Math.Sign(jState.X) * 5))) * (Math.PI / 6) * factor;
            }
            if (Math.Abs(jState.Y - calY) > threshold)
            {
                angY = ((int)(jState.Y - calY - (Math.Sign(jState.X) * 5))) * (Math.PI / 6) * factor;
            }
            if (Math.Abs(jState.Z - calZ) > threshold)
            {
                angZ = ((int)(jState.Z - calZ - (Math.Sign(jState.Z) * 5))) * (Math.PI / 6) * factor;
            }
            if ((Math.Abs(jState.X - calX) > threshold) || (Math.Abs(jState.Y - calY) > threshold) || (Math.Abs(jState.Z - calZ) > threshold))
            {
                label1.Text  = "X=" + Math.Round(180 * angX / Math.PI, 3).ToString() + "°";
                label1.Text += " Y=" + Math.Round(180 * angY / Math.PI, 3).ToString() + "°";
                label1.Text += " Z=" + Math.Round(1 - angZ / 4, 3).ToString();
                if ((angX != 0) || (angY != 0) || (angZ != 0))
                {
                    if (Jbuttons[1])    //translate
                    {
                        modelView.TranslateBy(angX / 40, -angY / 40);
                    }
                    else if (Jbuttons[0])    //zoom
                    {
                        modelView.ZoomByFactor(1 - angY / 4);
                    }
                    else
                    {
                        modelView.RotateAboutCenter(angY, angX);
                        modelView.RollBy(-angZ);
                    }
                }
            }
        }
Пример #17
0
        private void timesUp(object sender, EventArgs e)
        {
            Poll();
            richTextBox2.Text = "Test Window";
            int RightStickY = state.Rz;
            int RightStickX = state.Z;
            int LeftStickX  = state.X;
            int LeftStickY  = state.Y;

            // Display axes values
            RightStickYY.Text = RightStickY.ToString();
            RightStickXX.Text = RightStickX.ToString();
            LeftStickXX.Text  = LeftStickX.ToString();
            LeftStickYY.Text  = LeftStickY.ToString();

            // Check Buttons
            byte[] buttons          = state.GetButtons();
            int    i                = 0;
            bool   anyButtonPressed = false;

            foreach (byte but in buttons)
            {
                i++;
                if (but >= 128)
                {
                    LabelButton.ForeColor = Color.Red;
                    LabelButton.Text      = "Button " + i + " pressed";
                    anyButtonPressed      = true;
                }
            }
            if (anyButtonPressed == false)
            {
                LabelButton.ForeColor = Color.White;
                LabelButton.Text      = "No buttons pressed";
            }
            // JsendCommandTest(); // temp (Testing purpose)

            // Send commands via serial (USB)
            if (USBport.IsOpen == true)//(buttonConnectSerial.Text == "Disconnect (USB Port)")
            // It means Serial is connected
            {
                JsendCommand();
            }
        }
Пример #18
0
        void UpdateUI()
        {
            if (joystick == null)
            {
                createDeviceButton.Text = "Create Device";
            }
            else
            {
                createDeviceButton.Text = "Release Device";
            }

            string strText = null;

            label_X.Text = state.X.ToString(CultureInfo.CurrentCulture);
            label_Y.Text = state.Y.ToString(CultureInfo.CurrentCulture);
            label_Z.Text = state.Z.ToString(CultureInfo.CurrentCulture);

            label_XRot.Text = state.RotationX.ToString(CultureInfo.CurrentCulture);
            label_YRot.Text = state.RotationY.ToString(CultureInfo.CurrentCulture);
            label_ZRot.Text = state.RotationZ.ToString(CultureInfo.CurrentCulture);

            int[] slider = state.GetSliders();

            label_S0.Text = slider[0].ToString(CultureInfo.CurrentCulture);
            label_S1.Text = slider[1].ToString(CultureInfo.CurrentCulture);

            int[] pov = state.GetPointOfViewControllers();

            label_P0.Text = pov[0].ToString(CultureInfo.CurrentCulture);
            label_P1.Text = pov[1].ToString(CultureInfo.CurrentCulture);
            label_P2.Text = pov[2].ToString(CultureInfo.CurrentCulture);
            label_P3.Text = pov[3].ToString(CultureInfo.CurrentCulture);

            bool[] buttons = state.GetButtons();

            for (int b = 0; b < buttons.Length; b++)
            {
                if (buttons[b])
                {
                    strText += b.ToString("00 ", CultureInfo.CurrentCulture);
                }
            }
            label_ButtonList.Text = strText;
        }
Пример #19
0
        void stickHandle(Joystick stick, int id)
        {
            JoystickState state = new JoystickState();

            state = stick.GetCurrentState();

            xValue = state.X;
            yValue = state.Y;
            zValue = state.Z;

            xRotValue = state.RotationX;
            yRotValue = state.RotationY;
            zRotValue = state.RotationZ;

            bool[] buttons     = state.GetButtons();
            int[]  pointOfView = state.GetPointOfViewControllers();
            if (id == 0)
            {
                if (buttons[0])
                {
                    if (MouseClicked = false)
                    {
                        mouse_event(MOUSEEVENT_LEFTDOWN, 0, 0, 0, 0);
                        MouseClicked = true;
                    }
                }
                else
                {
                    if (MouseClicked == true)
                    {
                        mouse_event(MOUSEEVENT_LEFTUP, 0, 0, 0, 0);
                        MouseClicked = false;
                    }
                }
                label1.Text = pointOfView[0] + "";

                checkBox1.Text = buttons[0] + "";
                checkBox2.Text = buttons[1] + "";
                checkBox3.Text = buttons[2] + "";
                checkBox4.Text = buttons[3] + "";
                checkBox5.Text = buttons[4] + "";
                checkBox6.Text = buttons[5] + "";
            }
        }
Пример #20
0
        void T_Elapsed(object sender, ElapsedEventArgs e)
        {
            Dispatcher.Invoke(() =>
            {
                var result = _joystick.Poll();
                _state = _joystick.GetCurrentState();
                Button.Content = result.Name;
            for (var I = 0; I < 8; I++)
            {
                if (_state.GetButtons()[I])
                    Button.Content = "Found SaveButton " + I;
            }
            X.Value = _state.X;
            Y.Value = -_state.Y;
            R.Value = _state.AngularVelocityX;
            Z.Value = -_state.Z;

            });
        }
Пример #21
0
        private bool CheckJoystickInput()
        {
            if (joystick.Acquire().IsSuccess)
            {
                joystickState = joystick.GetCurrentState();
                if (joystickState == null)
                {
                    return(false);
                }

                bool[] buttons = joystickState.GetButtons();
                for (int button = 0; button < buttons.Length; button++)
                {
                    if (buttons[button])
                    {
                        _inputName = button.ToString();
                        return(true);
                    }

                    if (joystickState.X > 0xC000)
                    {
                        _inputName = "X+";
                        return(true);
                    }
                    else if (joystickState.X < 0x4000)
                    {
                        _inputName = "X-";
                        return(true);
                    }
                    else if (joystickState.Y > 0xC000)
                    {
                        _inputName = "Y+";
                        return(true);
                    }
                    else if (joystickState.Y < 0x4000)
                    {
                        _inputName = "Y-";
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #22
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;
        }
Пример #23
0
        void stickHandle2(Joystick stick, int id)
        {
            JoystickState state = new JoystickState();

            state = stick.GetCurrentState();



            bool[] buttons = state.GetButtons();

            if (id == 0)
            {
                if (buttons[0])
                {
                    if (mouseClicked == false)
                    {
                        mouse_event(MOUSEEVENT_LEFTDOWN, 0, 0, 0, 0);
                        mouseClicked = true;
                        xClick1      = Cursor.Position.X;
                        yClick1      = Cursor.Position.Y;
                    }
                }
                else
                {
                    if (mouseClicked)
                    {
                        mouse_event(MOUSEEVENT_LEFTUP, 0, 0, 0, 0);
                        mouseClicked = false;
                    }
                }
                if (buttons[6])
                {
                    counter++;
                    textBox1.Text = counter.ToString();
                }
                if (buttons[9])
                {
                    counter--;
                    textBox1.Text = counter.ToString();
                }
            }
        }
Пример #24
0
        public override void Update(TimeSpan elapsedTime)
        {
            buttonDown = -1;

            if (getInputData)
            {
                getInputData = false;
            }
            else
            {
                return;
            }

            if (joystick != null)
            {
                if (joystick.Acquire().IsFailure ||
                    joystick.Poll().IsFailure)
                {
                    return;
                }

                state = joystick.GetCurrentState();

                if (Result.Last.IsFailure)
                {
                    return;
                }

                joystickIsReady = true;

                bool[] buttons = state.GetButtons();

                for (int i = 0; i < buttons.Length; i++)
                {
                    if (buttons[i])
                    {
                        buttonDown = i;
                        break;
                    }
                }
            }
        }
Пример #25
0
        public void UpdateStatus()
        {
            Poll();

            Axis_X    = state.X;
            Axis_Y    = state.Y;
            Axis_Z    = state.Z;
            Axis_RotZ = state.Rz;

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

            int i = 0;

            foreach (byte button in jsButtons)
            {
                buttons[i] = button >= 128;
                i++;
            }
        }
Пример #26
0
        private string ButtonComboAsString(JoystickState state)
        {
            byte[] buttons = state.GetButtons();
            int    button  = 0;
            string res     = "";

            // button combos
            string sep = "";

            foreach (byte b in buttons)
            {
                if (0 != (b & 0x80))
                {
                    res += sep + button.ToString("00");
                    sep  = ",";
                }
                button++;
            }
            return(res);
        }
Пример #27
0
        public void UpdateStatus()
        {
            Poll();

            int[] extraAxis = state.GetSlider();

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

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

            int i = 0;

            foreach (byte button in jsButtons)
            {
                buttons[i] = button >= 128;
                i++;
            }
        }
Пример #28
0
 //Metoda odpowiedzialna za przekazanie danych do wątku głównego (interfejsu graficznego)
 private void SetValues(int x, int y)
 {
     //Za pomocą delegaty przekazujemy dane z naszego wątku pobierającego stan joysticka do wątku głównego
     this.Invoke((MethodInvoker) delegate
     {
         //Ustawiamy współrzędne textboxów
         xTextBox.Text = x.ToString();
         yTextBox.Text = y.ToString();
         //Przekazujemy również stan przycisku
         if (state.GetButtons()[0] >= 128)
         {
             buttonClickedTextBox.BackColor = Color.Green;
             buttonValue = true;
         }
         else
         {
             buttonClickedTextBox.BackColor = Color.Red;
             buttonValue = false;
         }
     });
 }
Пример #29
0
        public void UpdateStatus()
        {
            Poll();

            int[] extraAxis = state.GetSlider();

            Xaxis = state.X;  //Stuur
            Yaxis = state.Y;  //Gas
            Zaxis = state.Rz; //Z,Rx,Ry is niets Rz is rem

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

            int i = 0;

            foreach (byte button in jsButtons)
            {
                buttons[i] = button >= 128;
                i++;
            }
        }
Пример #30
0
        internal void updateJoystick()
        {
            //Get Joystick State.
            JoystickState state = joystickDevice.CurrentJoystickState;

            previousJoystickState = currentJoystickState;
            //Capture Buttons.
            byte[] buttons = state.GetButtons();
            for (int i = 0; i < buttons.Length; i++)
            {
                //Console.Out.Write(buttons[i]);
                if (buttons[i] > 0)
                {
                    currentJoystickState[i] = true;
                }
                else
                {
                    currentJoystickState[i] = false;
                }
            }
        }
Пример #31
0
        public SWInputStates GetState()
        {
            JoystickState state = _controller.CurrentJoystickState;

            inputStates.SetGear("First", state.GetButtons()[8]);
            inputStates.SetGear("Second", state.GetButtons()[9]);
            inputStates.SetGear("Third", state.GetButtons()[10]);
            inputStates.SetGear("Fourth", state.GetButtons()[11]);
            inputStates.SetGear("Fifth", state.GetButtons()[12]);
            inputStates.SetGear("Sixth", state.GetButtons()[13]);
            inputStates.SetGear("Reverse", state.GetButtons()[14]);

            inputStates.Accelerator = state.Y;
            inputStates.Brake       = state.Rz;
            inputStates.Steering    = state.X;
            inputStates.Clutch      = state.GetSlider()[1];

            return(inputStates);
        }
        /// <summary>Generates events for the changes between two states</summary>
        /// <param name="previous">Previous state for the comparison</param>
        /// <param name="current">Current state for the comparison</param>
        private void generateEvents(JoystickState previous, JoystickState current)
        {
            bool haveExtendedEventSubscribers = HaveExtendedEventSubscribers;

            if (!HaveEventSubscribers && !haveExtendedEventSubscribers)
            {
                return;
            }

            bool[] previousButtons = previous.GetButtons();
            bool[] currentButtons  = current.GetButtons();

            if (haveExtendedEventSubscribers)
            {
                generateAllEvents(previousButtons, currentButtons);
            }
            else
            {
                generateStandardEventsOnly(previousButtons, currentButtons);
            }
        }
Пример #33
0
        public List<int> GetButtons()
        {
            if (Pad.Acquire().IsFailure)
                return null;

            if (Pad.Poll().IsFailure)
                return null;

            State = Pad.GetCurrentState();
            if (Result.Last.IsFailure)
                return null;

            var answer = new List<int>();

            bool[] buttons = State.GetButtons();
            for (int b = 0; b < buttons.Length; b++)
            {
                if (buttons[b])
                    answer.Add(b);
            }
            return answer;
        }
Пример #34
0
        private bool CheckInput(JoystickState state, int index)
        {
            if (state == null)
                return false;

            var buttons = state.GetButtons();
            for (var button = 0; button < buttons.Length; button++)
            {
                if (buttons[button])
                {
                    _inputName = "Joystick" + index + "." + button;
                    return true;
                }

                if (state.X > 0xC000)
                {
                    _inputName = "Joystick" + index + ".X+";
                    return true;
                }
                else if (state.X < 0x4000)
                {
                    _inputName = "Joystick" + index + ".X-";
                    return true;
                }
                else if (state.Y > 0xC000)
                {
                    _inputName = "Joystick" + index + ".Y+";
                    return true;
                }
                else if (state.Y < 0x4000)
                {
                    _inputName = "Joystick" + index + ".Y-";
                    return true;
                }
            }
            return false;
        }
 private bool ReactToDigitalEvents(List<ButtonMapping> mappingsForDevice, JoystickState state)
 {
     byte[] buttonStates = state.GetButtons();
     bool atLeastOneChange = false;
     for (int i = 0; i < mappingsForDevice.Count; i++)
     {
         byte bs = buttonStates[i];
         if (bs >= 128)
         {
             if (!mappingsForDevice[i].Pressed)
             {
                 mappingsForDevice[i].Pressed = true;
                 atLeastOneChange = true;
                 // TODO: carry out configured _pressed_ action, if appropriate
             }
         }
         else
         {
             if (mappingsForDevice[i].Pressed)
             {
                 mappingsForDevice[i].Pressed = false;
                 atLeastOneChange = true;
                 // TODO: carry out configured _release_ action, if appropriate
             }
         }
     }
     return atLeastOneChange;
 }
Пример #36
0
		public void ReadPaddle()
		{
			paddleDevice.Poll();
			jsState = paddleDevice.CurrentJoystickState;
			byte[] contacts = jsState.GetButtons();
			int thisDit = contacts[0];
			int thisDah = contacts[1];
			if(thisDit != lastDit | (thisDah != lastDah))
			{
				lastDit = thisDit;
				lastDah = thisDah;
				int[] bauds = {thisDit, thisDah};
				OnChanged(bauds);
			}			
		}
Пример #37
0
        private bool CheckJoystickInput()
        {
            if (joystick.Acquire().IsSuccess)
            {
                joystickState = joystick.GetCurrentState();
                if (joystickState == null)
                    return false;

                bool[] buttons = joystickState.GetButtons();
                for (int button = 0; button < buttons.Length; button++)
                {
                    if (buttons[button])
                    {
                        _inputName = button.ToString();
                        return true;
                    }

                    if (joystickState.X > 0xC000)
                    {
                        _inputName = "X+";
                        return true;
                    }
                    else if (joystickState.X < 0x4000)
                    {
                        _inputName = "X-";
                        return true;
                    }
                    else if (joystickState.Y > 0xC000)
                    {
                        _inputName = "Y+";
                        return true;
                    }
                    else if (joystickState.Y < 0x4000)
                    {
                        _inputName = "Y-";
                        return true;
                    }
                }
            }
            return false;
        }
Пример #38
0
        public override void Update(TimeSpan elapsedTime)
        {
            buttonDown = -1;

            if (getInputData)
                getInputData = false;
            else
                return;

            if (joystick != null)
            {
                if (joystick.Acquire().IsFailure ||
                    joystick.Poll().IsFailure)
                    return;

                state = joystick.GetCurrentState();

                if (Result.Last.IsFailure)
                    return;

                joystickIsReady = true;

                bool[] buttons = state.GetButtons();

                for (int i = 0; i < buttons.Length; i++)
                {
                    if (buttons[i])
                    {
                        buttonDown = i;
                        break;
                    }
                }
            }
        }
    /// <summary>
    ///   Initializes a new game pad state from a DirectInput joystick state
    /// </summary>
    /// <param name="converter">DirectInput converter used to fill the state</param>
    /// <param name="joystickState">
    ///   Joystick state from which the game pad state will be built
    /// </param>
    internal GamePadState(
      DirectInputConverter converter, ref JoystickState joystickState
      )
    {
      // Take over the joystick's axes
      {
        this.AvailableAxes = converter.AvailableAxes;
        DirectInputConverter.IAxisReader[] axisReaders = converter.AxisReaders;

        if (axisReaders[0] != null)
        {
          this.X = axisReaders[0].GetValue(ref joystickState);
        }
        else
        {
          this.X = 0.0f;
        }
        if (axisReaders[1] != null)
        {
          this.Y = -axisReaders[1].GetValue(ref joystickState);
        }
        else
        {
          this.Y = 0.0f;
        }
        if (axisReaders[2] != null)
        {
          this.Z = axisReaders[2].GetValue(ref joystickState);
        }
        else
        {
          this.Z = 0.0f;
        }

        if (axisReaders[3] != null)
        {
          this.VelocityX = axisReaders[3].GetValue(ref joystickState);
        }
        else
        {
          this.VelocityX = 0.0f;
        }
        if (axisReaders[4] != null)
        {
          this.VelocityY = -axisReaders[4].GetValue(ref joystickState);
        }
        else
        {
          this.VelocityY = 0.0f;
        }
        if (axisReaders[5] != null)
        {
          this.VelocityZ = axisReaders[5].GetValue(ref joystickState);
        }
        else
        {
          this.VelocityZ = 0.0f;
        }

        if (axisReaders[6] != null)
        {
          this.AccelerationX = axisReaders[6].GetValue(ref joystickState);
        }
        else
        {
          this.AccelerationX = 0.0f;
        }
        if (axisReaders[7] != null)
        {
          this.AccelerationY = -axisReaders[7].GetValue(ref joystickState);
        }
        else
        {
          this.AccelerationY = 0.0f;
        }
        if (axisReaders[8] != null)
        {
          this.AccelerationZ = axisReaders[8].GetValue(ref joystickState);
        }
        else
        {
          this.AccelerationZ = 0.0f;
        }

        if (axisReaders[9] != null)
        {
          this.ForceX = axisReaders[9].GetValue(ref joystickState);
        }
        else
        {
          this.ForceX = 0.0f;
        }
        if (axisReaders[10] != null)
        {
          this.ForceY = -axisReaders[10].GetValue(ref joystickState);
        }
        else
        {
          this.ForceY = 0.0f;
        }
        if (axisReaders[11] != null)
        {
          this.ForceZ = axisReaders[11].GetValue(ref joystickState);
        }
        else
        {
          this.ForceZ = 0.0f;
        }

        if (axisReaders[12] != null)
        {
          this.RotationX = axisReaders[12].GetValue(ref joystickState);
        }
        else
        {
          this.RotationX = 0.0f;
        }
        if (axisReaders[13] != null)
        {
          this.RotationY = -axisReaders[13].GetValue(ref joystickState);
        }
        else
        {
          this.RotationY = 0.0f;
        }
        if (axisReaders[14] != null)
        {
          this.RotationZ = axisReaders[14].GetValue(ref joystickState);
        }
        else
        {
          this.RotationZ = 0.0f;
        }

        if (axisReaders[15] != null)
        {
          this.AngularVelocityX = axisReaders[15].GetValue(ref joystickState);
        }
        else
        {
          this.AngularVelocityX = 0.0f;
        }
        if (axisReaders[16] != null)
        {
          this.AngularVelocityY = -axisReaders[16].GetValue(ref joystickState);
        }
        else
        {
          this.AngularVelocityY = 0.0f;
        }
        if (axisReaders[17] != null)
        {
          this.AngularVelocityZ = axisReaders[17].GetValue(ref joystickState);
        }
        else
        {
          this.AngularVelocityZ = 0.0f;
        }

        if (axisReaders[18] != null)
        {
          this.AngularAccelerationX = axisReaders[18].GetValue(ref joystickState);
        }
        else
        {
          this.AngularAccelerationX = 0.0f;
        }
        if (axisReaders[19] != null)
        {
          this.AngularAccelerationY = -axisReaders[19].GetValue(ref joystickState);
        }
        else
        {
          this.AngularAccelerationY = 0.0f;
        }
        if (axisReaders[20] != null)
        {
          this.AngularAccelerationZ = axisReaders[20].GetValue(ref joystickState);
        }
        else
        {
          this.AngularAccelerationZ = 0.0f;
        }

        if (axisReaders[21] != null)
        {
          this.TorqueX = axisReaders[21].GetValue(ref joystickState);
        }
        else
        {
          this.TorqueX = 0.0f;
        }
        if (axisReaders[22] != null)
        {
          this.TorqueY = -axisReaders[22].GetValue(ref joystickState);
        }
        else
        {
          this.TorqueY = 0.0f;
        }
        if (axisReaders[23] != null)
        {
          this.TorqueZ = axisReaders[23].GetValue(ref joystickState);
        }
        else
        {
          this.TorqueZ = 0.0f;
        }
      }

      // Take over the joystick's buttons
      {
        this.ButtonCount = converter.ButtonCount;
        bool[] buttonPressed = joystickState.GetButtons();

        this.buttonState1 = 0;
        for (int index = 0; index < Math.Min(64, ButtonCount); ++index)
        {
          if (buttonPressed[index])
          {
            this.buttonState1 |= (1UL << index);
          }
        }

        this.buttonState2 = 0;
        for (int index = 0; index < (ButtonCount - 64); ++index)
        {
          if (buttonPressed[index + 64])
          {
            this.buttonState2 |= (1UL << index);
          }
        }
      }

      // Take over the joystick's sliders
      {
        this.AvailableSliders = converter.AvailableSliders;
        DirectInputConverter.ISliderReader[] sliderReaders = converter.SliderReaders;

        if (sliderReaders[0] != null)
        {
          this.Slider1 = sliderReaders[0].GetValue(ref joystickState);
        }
        else
        {
          this.Slider1 = 0.0f;
        }
        if (sliderReaders[1] != null)
        {
          this.Slider2 = sliderReaders[1].GetValue(ref joystickState);
        }
        else
        {
          this.Slider2 = 0.0f;
        }

        if (sliderReaders[2] != null)
        {
          this.VelocitySlider1 = sliderReaders[2].GetValue(ref joystickState);
        }
        else
        {
          this.VelocitySlider1 = 0.0f;
        }
        if (sliderReaders[3] != null)
        {
          this.VelocitySlider2 = sliderReaders[3].GetValue(ref joystickState);
        }
        else
        {
          this.VelocitySlider2 = 0.0f;
        }

        if (sliderReaders[4] != null)
        {
          this.AccelerationSlider1 = sliderReaders[4].GetValue(ref joystickState);
        }
        else
        {
          this.AccelerationSlider1 = 0.0f;
        }
        if (sliderReaders[5] != null)
        {
          this.AccelerationSlider2 = sliderReaders[5].GetValue(ref joystickState);
        }
        else
        {
          this.AccelerationSlider2 = 0.0f;
        }

        if (sliderReaders[6] != null)
        {
          this.ForceSlider1 = sliderReaders[6].GetValue(ref joystickState);
        }
        else
        {
          this.ForceSlider1 = 0.0f;
        }
        if (sliderReaders[7] != null)
        {
          this.ForceSlider2 = sliderReaders[7].GetValue(ref joystickState);
        }
        else
        {
          this.ForceSlider2 = 0.0f;
        }
      }

      // Take over the joystick's Point-of-View controllers
      {
        this.PovCount = converter.PovCount;

        int[] povs = null;
        if (this.PovCount >= 1)
        {
          povs = joystickState.GetPointOfViewControllers();
          this.Pov1 = povs[0];
        }
        else
        {
          this.Pov1 = -1;
        }
        if (this.PovCount >= 2)
        {
          this.Pov2 = povs[1];
        }
        else
        {
          this.Pov2 = -1;
        }
        if (this.PovCount >= 3)
        {
          this.Pov3 = povs[2];
        }
        else
        {
          this.Pov3 = -1;
        }
        if (this.PovCount >= 4)
        {
          this.Pov4 = povs[3];
        }
        else
        {
          this.Pov4 = -1;
        }
      }
    }
 private void procesar(int idJoystick, JoystickState estado)
 {
     try
     {
         bool[] botones = estado.GetButtons();
         if (botones.Length >= 10)
         {
             if (botones[0] == true)
             {
                 procesarBoton(idJoystick, 0);
                 index = idJoystick;
                 return;
             }
             if (botones[1] == true)
             {
                 procesarBoton(idJoystick, 1);
                 index = idJoystick;
                 return;
             }
             if (botones[2] == true)
             {
                 procesarBoton(idJoystick, 2);
                 index = idJoystick;
                 return;
             }
             if (botones[3] == true)
             {
                 procesarBoton(idJoystick, 3);
                 index = idJoystick;
                 return;
             }
             if (botones[4] == true)
             {
                 procesarBoton(idJoystick, 4);
                 index = idJoystick;
                 return;
             }
             if (botones[5] == true)
             {
                 procesarBoton(idJoystick, 5);
                 index = idJoystick;
                 return;
             }
             if (botones[6] == true)
             {
                 procesarBoton(idJoystick, 6);
                 index = idJoystick;
                 return;
             }
             if (botones[7] == true)
             {
                 procesarBoton(idJoystick, 7);
                 index = idJoystick;
                 return;
             }
             if (botones[8] == true)
             {
                 procesarBoton(idJoystick, 8);
                 index = idJoystick;
                 return;
             }
             if (botones[9] == true)
             {
                 procesarBoton(idJoystick, 9);
                 index = idJoystick;
                 return;
             }
             int sx = estado.X;
             int sy = estado.Y;
             if (sx == -1000)
             {
                 procesarBoton(idJoystick, 13);
                 index = idJoystick;
                 return;
             }
             else if (sx == 1000)
             {
                 procesarBoton(idJoystick, 11);
                 index = idJoystick;
                 return;
             }
             if (sy == -1000)
             {
                 procesarBoton(idJoystick, 10);
                 index = idJoystick;
                 return;
             }
             else if (sy == 1000)
             {
                 procesarBoton(idJoystick, 12);
                 index = idJoystick;
                 return;
             }
         }
     }
     catch (Exception ex)
     {
     }
 }
Пример #41
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;
    }
Пример #42
0
        /// <summary>
        /// Handler stavu gamepadu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gamepadHandle(object sender, EventArgs e)
        {
            if (enabled)
            {
                try
                {
                    gamepad.Acquire();
                }
                catch (DirectInputException)
                {
                    dinput.Dispose();
                    periodicChecker.Dispose();
                    errorObserver();
                }
                JoystickState stateNow = new JoystickState();
                stateNow = gamepad.GetCurrentState();
                bool[] buttons = stateNow.GetButtons();
                if ((Math.Abs(state.stickDirectMoveX - stateNow.X) > sensitivityJoystick || Math.Abs(state.stickDirectMoveY - stateNow.Y) > sensitivityJoystick) && stickDirectMoveObserver != null)
                {
                    state.stickDirectMoveX = stateNow.X;
                    state.stickDirectMoveY = stateNow.Y;
                    int x = state.stickDirectMoveX;
                    int y = state.stickDirectMoveY;
                    if (Math.Abs(x) < 10 && Math.Abs(y) < 10)
                    {
                        x = 0;
                        y = 0;
                    }
                    stickDirectMoveObserver(x, y);
                }

                if (state.moveDown != buttons[0] && buttonMoveDownObserver != null)
                {
                    buttonMoveDownObserver(buttons[0]);
                    state.moveDown = buttons[0];
                }

                if (state.moveUp != buttons[3] && buttonMoveUpObserver != null)
                {
                    buttonMoveUpObserver(buttons[3]);
                    state.moveUp = buttons[3];
                }

                if (state.narrow != buttons[2] && buttonNarrowObserver != null)
                {
                    buttonNarrowObserver(buttons[2]);
                    state.narrow = buttons[2];
                }

                if (state.widen != buttons[1] && buttonWidenObserver != null)
                {
                    buttonWidenObserver(buttons[1]);
                    state.widen = buttons[1];
                }

                if (state.defaultPosition != buttons[7] && buttonDefaultPositionObserver != null)
                {
                    buttonDefaultPositionObserver(buttons[7]);
                    state.defaultPosition = buttons[7];
                }

                if (state.rotateLeft != buttons[8] && buttonRotateLeftObserver != null)
                {
                    buttonRotateLeftObserver(buttons[8]);
                    state.rotateLeft = buttons[8];
                }

                if (state.rotateRight != buttons[9] && buttonRotateRightObserver != null)
                {
                    buttonRotateRightObserver(buttons[9]);
                    state.rotateRight = buttons[9];
                }

                if (state.stop != buttons[10] && buttonStopObserver != null)
                {
                    buttonStopObserver(buttons[10]);
                    state.stop = buttons[10];
                }
            }
        }
    private string ButtonComboAsString(JoystickState state)
    {
      byte[] buttons = state.GetButtons();
      int button = 0;
      string res = "";

      // button combos
      string sep = "";
      foreach (byte b in buttons)
      {
        if (0 != (b & 0x80))
        {
          res += sep + button.ToString("00");
          sep = ",";
        }
        button++;
      }
      return res;
    }
    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);
      }
    }
Пример #45
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);
      }
    }
Пример #46
0
        private void procesar(Joystick stick)
        {
            try
            {
                JoystickState state = new JoystickState();
                state = stick.GetCurrentState();

                string nombre = stick.Information.InstanceName; //instance name
                lblNombre.Text = nombre;

                bool[] botones = state.GetButtons();
                if (botones.Length >= 10)
                {
                    if (botones[0] == true)
                    {
                        boton0.Image = muestraActivado.Image;
                    }
                    if (botones[1] == true)
                    {
                        boton1.Image = muestraActivado.Image;
                    }
                    if (botones[2] == true)
                    {
                        boton2.Image = muestraActivado.Image;
                    }
                    if (botones[3] == true)
                    {
                        boton3.Image = muestraActivado.Image;
                    }
                    if (botones[4] == true)
                    {
                        boton4.Image = muestraActivado.Image;
                    }
                    if (botones[5] == true)
                    {
                        boton5.Image = muestraActivado.Image;
                    }
                    if (botones[6] == true)
                    {
                        boton6.Image = muestraActivado.Image;
                    }
                    if (botones[7] == true)
                    {
                        boton7.Image = muestraActivado.Image;
                    }
                    if (botones[8] == true)
                    {
                        boton8.Image = muestraActivado.Image;
                    }
                    if (botones[9] == true)
                    {
                        boton9.Image = muestraActivado.Image;
                    }
                    int sx = state.X;
                    int sy = state.Y;
                    if (sx == -1000)
                    {
                        boton13.Image = muestraActivado.Image;
                    }
                    else if (sx == 1000)
                    {
                        boton11.Image = muestraActivado.Image;
                    }
                    if (sy == -1000)
                    {
                        boton10.Image = muestraActivado.Image;
                    }
                    else if (sy == 1000)
                    {
                        boton12.Image = muestraActivado.Image;
                    }
                }
                else
                {
                    MessageBox.Show("El dispositivo no es del tipo adecuado o está dañado.", "Dispositivo incorrecto", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            { 
            }
        }
Пример #47
0
        public static void UpdateJoystick(int joystick, ref string pressedButton)
        {
            try
            {
                pressedButton = "";
                state = joysticks[joystick].CurrentJoystickState;
                buttons = state.GetButtons(); // Capture buttons state.

                for (int i = 0; i < buttonPressed[joystick].Length; i++)
                {
                    if (buttons[i] != 0)
                    {
                        if (buttonPressed[joystick][i] == false)
                        {
                            pressedButton += "Btn" + i + ":";
                            buttonPressed[joystick][i] = true;
                        }
                    }
                    else
                        buttonPressed[joystick][i] = false; // release button
                }
            }
            catch (Exception e)
            {
                ImportExport.LogMessage("UpdateJoystick ... " + e.ToString(), true);
            }
        }
Пример #48
0
        private void ComputeInputs()
        {
            // Pull from joystick
            if (joystick != null)
            {
                joystick.Poll();
                state = joystick.GetCurrentState();
                pov_state = (POV_HAT_DIR)state.GetPointOfViewControllers()[0];
                button_state = state.GetButtons();
                control_x = state.X;
                control_y = state.Y;
                control_z = state.RotationZ;
                control_throttle = state.GetSliders()[0];
                if (control_x > 0 && control_x < JOYSTICK_XY_DEADBAND) { control_x = 0; }
                if (control_x < 0 && control_x > -JOYSTICK_XY_DEADBAND) { control_x = 0; }
                if (control_y > 0 && control_y < JOYSTICK_XY_DEADBAND) { control_y = 0; }
                if (control_y < 0 && control_y > -JOYSTICK_XY_DEADBAND) { control_y = 0; }
                if (control_z > 0 && control_z < JOYSTICK_Z_DEADBAND) { control_z = 0; }
                if (control_z < 0 && control_z > -JOYSTICK_Z_DEADBAND) { control_z = 0; }
            }
            else
            {
                state = new JoystickState();
                button_state = new bool[] { false, false, false, false };
                control_x = 0;
                control_y = 0;
                pov_state = POV_HAT_DIR.NONE;
            }

            // Turret
            switch (pov_state)
            {
                case POV_HAT_DIR.NW:
                    turret_vert_pwr = TURRET_VERT_SPEED_SLOW;
                    turret_vert_fwd = true;// down
                    turret_horiz_pwr = TURRET_VERT_SPEED_SLOW;
                    turret_horiz_fwd = true;// left
                    break;
                case POV_HAT_DIR.N:
                    turret_vert_pwr = TURRET_VERT_SPEED_MED;
                    turret_vert_fwd = true;// down
                    turret_horiz_pwr = 0;
                    turret_horiz_fwd = false;// right
                    break;
                case POV_HAT_DIR.NE:
                    turret_vert_pwr = TURRET_VERT_SPEED_SLOW;
                    turret_vert_fwd = true;// down
                    turret_horiz_pwr = TURRET_VERT_SPEED_SLOW;
                    turret_horiz_fwd = false;// right
                    break;
                case POV_HAT_DIR.E:
                    turret_vert_pwr = 0;
                    turret_vert_fwd = true;// down
                    turret_horiz_pwr = TURRET_VERT_SPEED_MED;
                    turret_horiz_fwd = false;// right
                    break;
                case POV_HAT_DIR.SE:
                    turret_vert_pwr = TURRET_VERT_SPEED_SLOW;
                    turret_vert_fwd = false;// up
                    turret_horiz_pwr = TURRET_VERT_SPEED_SLOW;
                    turret_horiz_fwd = false;// right
                    break;
                case POV_HAT_DIR.S:
                    turret_vert_pwr = TURRET_VERT_SPEED_MED;
                    turret_vert_fwd = false;// up
                    turret_horiz_pwr = 0;
                    turret_horiz_fwd = false;// right
                    break;
                case POV_HAT_DIR.SW:
                    turret_vert_pwr = TURRET_VERT_SPEED_SLOW;
                    turret_vert_fwd = false;// up
                    turret_horiz_pwr = TURRET_VERT_SPEED_SLOW;
                    turret_horiz_fwd = true;// left
                    break;
                case POV_HAT_DIR.W:
                    turret_vert_pwr = 0;
                    turret_vert_fwd = true;// down
                    turret_horiz_pwr = TURRET_VERT_SPEED_MED;
                    turret_horiz_fwd = true;// left
                    break;

                default:
                    turret_vert_pwr = 0;
                    turret_horiz_pwr = 0;
                    break;
            }

            // Normal weapons
            /*if (button_state[0])
            {
                weapon1_fwd = true;
                weapon1_pwr = 255;
            }
            else
            {
                weapon1_fwd = true;
                weapon1_pwr = 0;
            }
            if (button_state[1])
            {
                weapon2_fwd = false;
                weapon2_pwr = 255;
            }
            else
            {
                weapon2_fwd = false;
                weapon2_pwr = 0;
            }*/

            // Lighting unit
            int light_power = (int)((-control_throttle + 1000.0) * (255.0 / 2000.0));
            switch (comboBoxLightType.SelectedIndex)
            {
                case 0: // white
                default:
                    weapon1_fwd = false;
                    weapon1_pwr = light_power;
                    weapon2_pwr = 0;
                    break;
                case 1: // IR
                    weapon2_fwd = false;
                    weapon2_pwr = light_power;
                    weapon1_pwr = 0;
                    break;
                case 2: // UV
                    weapon1_fwd = true;
                    weapon1_pwr = light_power;
                    weapon2_pwr = 0;
                    break;
                case 3: // Laser
                    weapon2_fwd = true;
                    weapon2_pwr = light_power;
                    weapon1_pwr = 0;
                    break;
            }

            // Button control (overrides joystick)
            if (DateTime.Now < w_button_effective_until)
            { control_y = -BUTTON_POWER; }
            else if (DateTime.Now < s_button_effective_until)
            { control_y = BUTTON_POWER; }
            if (DateTime.Now < a_button_effective_until)
            { control_x = -BUTTON_POWER; }
            else if (DateTime.Now < d_button_effective_until)
            { control_x = BUTTON_POWER; }
        }
        public void PollGamePad()
        {
            if (mGamepad != null)
            {
                mGamepad.Poll();
                try
                {
                    mGamePadState = mGamepad.CurrentJoystickState;

                    this.XInput.Content = mGamePadState.X;
                    this.YInput.Content = mGamePadState.Y;

                    if (mThumbStick == 0)
                        this.EngagementSlider.Value = mGamePadState.Y * -1;
                    else
                        this.EngagementSlider.Value = mGamePadState.Rz * -1;


                    if (mButtonState == ButtonState.NONE)
                    {
                        //Capture Buttons.
                        byte[] buttons = mGamePadState.GetButtons();

                        if (buttons[1] != 0)
                        {
                            mButtonState = ButtonState.BUTTON1PRESSED;
                            mCleared = false;

                        }
                        else if (buttons[2] != 0)
                        {
                            mButtonState = ButtonState.BUTTON2PRESSED;
                            mCleared = false;

                        }
                        else if (buttons[3] != 0)
                        {
                            mButtonState = ButtonState.BUTTON3PRESSED;
                            mCleared = false;

                        }
                        else if (buttons[4] != 0)
                        {
                            mButtonState = ButtonState.BUTTON4PRESSED;
                            mCleared = false;

                        }
                        else if (buttons[5] != 0)
                        {
                            mButtonState = ButtonState.BUTTONLBPRESSED;
                            mCleared = false;

                        }
                        else if (buttons[6] != 0)
                        {
                            mButtonState = ButtonState.BUTTONLTPRESSED;
                            mCleared = false;

                        }
                        else if (buttons[7] != 0)
                        {
                            mButtonState = ButtonState.BUTTONRTPRESSED;
                            mCleared = false;

                        }
                        else if (buttons[8] != 0)
                        {
                            mButtonState = ButtonState.BUTTONRBPRESSED;
                            mCleared = false;

                        }
                        else
                        { 
                            mCleared = true;
                        }

                    }
                }
                catch(Exception ex)
                {
                    IsConnected = ConnectedState.NONE;
                    mGamepad = null;
                }
            }
            
        }
Пример #50
0
 protected override double _GetValue(JoystickState joystickState)
 {
     return joystickState.GetButtons()[ButtonIndex] ?
         (int)Position.MaxValue :
         (int)Position.MinValue;
 }
Пример #51
0
        //end of WTF

        //Updating values from joystick
        public void update()
        {
            Joystick stick = sticks[0];
            state = stick.GetCurrentState();

            for (int i = 0; i < 1; i++) //i indicating which joystick to update
            {
                //Update axes values
                axis[0] = state.AccelerationX; //Ax
                axis[1] = state.AccelerationY; //Ay
                axis[2] = state.AccelerationZ; //Az
                axis[3] = state.AngularAccelerationX; //aAx
                axis[4] = state.AngularAccelerationY; //aAy
                axis[5] = state.AngularAccelerationZ; //aAz
                axis[6] = state.AngularVelocityX; //aVx
                axis[7] = state.AngularVelocityY; //aVy
                axis[8] = state.AngularVelocityZ; //aVz
                axis[9] = state.ForceX; //Fx
                axis[10] = state.ForceY; //Fy
                axis[11] = state.ForceZ; //Fz
                axis[12] = state.RotationX; //Rx
                axis[13] = state.RotationY; //Ry
                axis[14] = state.RotationZ; //Rz
                axis[15] = state.TorqueX; //Tx
                axis[16] = state.TorqueY; //Ty
                axis[17] = state.TorqueZ; //Tz
                axis[18] = state.VelocityX; //Vx
                axis[19] = state.VelocityY; //Vy
                axis[20] = state.VelocityZ; //Vz
                axis[21] = state.X; //x
                axis[22] = state.Y; //y
                axis[23] = state.Z; //z

                //Update button values
                buttons[i] = state.GetButtons();

            }

        }
 /// <summary>
 /// Loop though all buttons on the gamepad an see which one is pressed
 /// </summary>
 /// <returns>The pressed button</returns>
 public int GetPressedButton()
 {
     int buttonIndex = -1;
     _state = GetState();
     var buttons = new bool[_state.GetButtons().Length];
     for (int i = 0; i < buttons.Length; i++)
     {
         if (_state.IsPressed(i))
         {
             buttonIndex = i;
         }
     }
     return buttonIndex;
 }
Пример #53
0
        private void CheckButtonPresses(JoystickState state, Int32 gamepadIndex)
        {
            if (state.Y < 0x4000)
                _viewModel.AddGamepadBinding(-1, gamepadIndex, GetFocusedButton());

            if (state.Y > 0xC000)
                _viewModel.AddGamepadBinding(-2, gamepadIndex, GetFocusedButton());

            if (state.X < 0x4000)
                _viewModel.AddGamepadBinding(-3, gamepadIndex, GetFocusedButton());

            if (state.X > 0xC000)
                _viewModel.AddGamepadBinding(-4, gamepadIndex, GetFocusedButton());

            Int32 buttonIndex = 0;

            for (buttonIndex = 0; buttonIndex < state.GetButtons().Count(); buttonIndex++)
            {
                if (state.GetButtons()[buttonIndex])
                {
                    _viewModel.AddGamepadBinding(-4, gamepadIndex, GetFocusedButton());
                }
            }
        }
        private void UpdateButtons(JoystickState joystickState)
        {
            var buttons = joystickState.GetButtons();

            if (null == lastReportedButtons)
            {
                // We don't raise any events on the first call to this method.
                lastReportedButtons = buttons;
                return;
            }

            if (buttons.Length != lastReportedButtons.Length)
                throw new Exception("Button array lengths differ. This is unexpected.");

            for (int i = 0; i < buttons.Length; i++)
            {
                // Only report this button if state has flipped (on vs off).
                if (buttons[i] != lastReportedButtons[i])
                {
                    CarButtonEventArgs e = CreateCarButtonEventArgs(i, buttons[i]);
                    Log.Spew("Button " + i + ": " + (buttons[i] ? "On" : "Off") + (null==e ? " [not tracked]" : ""));

                    if (null != e)
                        OnButtonStateChanged(e);
                }
            }

            lastReportedButtons = buttons;
        }
Пример #55
0
        void StickHandlingLogic(Joystick stick, int id)
        {
            //Console.WriteLine("here2");
            // Creates an object from the class JoystickState.
            JoystickState state = new JoystickState();

            state = stick.GetCurrentState(); //Gets the state of the joystick
            //Console.WriteLine(state);
            //These are for the thumbstick readings
            yValue = -state.Y;
            xValue = state.X;
            zValue = state.Z;
            rotationZValue = state.RotationZ;
            rotationXValue = state.RotationY;
            rotationYValue = state.RotationX;

            int th = 0;
            int[] z = state.GetSliders();
            th= z[0];
            if (z[0] == 0 && isFirst)
            {
                th = 0;

            }
            else
            {
                if(isFirst) isFirst = false;
                if (th >= 0)
                {
                    th = 50 - th / 2;
                }
                else
                {
                    th = -th / 2 + 50;
                }
            }

            Console.Write("thrust = " + th);

            Console.WriteLine(" x = " + xValue + " y = " + yValue + " z = " + zValue + " rot x = " + rotationXValue + " rot y = " + rotationYValue + " rot Z = " + rotationZValue);

            bool[] buttons = state.GetButtons(); // Stores the number of each button on the gamepad into the bool[] butons.
               // Console.WriteLine("# of button = " + buttons.Length);
            //Here is an example on how to use this for the joystick in the first index of the array list
            if (id == 0)
            {
                // This is when button 0 of the gamepad is pressed, the label will change. Button 0 should be the square button.
                if (buttons[0])
                {

                }

            }
        }