コード例 #1
0
        static ParseUpdateResult parseUpdate(JoystickUpdate update, PartialJoystickState state)
        {
            JoystickButtons btn  = (JoystickButtons)update.Offset;
            string          data = btn.ToString();

            switch (btn)
            {
            case JoystickButtons.X:
            case JoystickButtons.A:
            case JoystickButtons.B:
            case JoystickButtons.Y:
            case JoystickButtons.LB:
            case JoystickButtons.RB:
            case JoystickButtons.LT:
            case JoystickButtons.RT:
            case JoystickButtons.BACK:
            case JoystickButtons.START:
            case JoystickButtons.LJ:
            case JoystickButtons.RJ:
                data += " " + (update.Value == 128 ? 1 : 0);
                break;

            case JoystickButtons.POV:     //Top, Bottom, Left, Right
                double val = update.Value / 100;
                val   = ((val > 180.0) && (val < 360.0)) ? val - 360.0 : val;
                val   = (update.Value == -1) ? -1 : val;
                data += " " + ((update.Value == 0) || (update.Value == 4500) || (update.Value == 31500) ? 1 : 0) + " "
                        + ((update.Value == 18000) || (update.Value == 13500) || (update.Value == 22500) ? 1 : 0) + " "
                        + ((update.Value == 27000) || (update.Value == 22500) || (update.Value == 31500) ? 1 : 0) + " "
                        + ((update.Value == 9000) || (update.Value == 4500) || (update.Value == 13500) ? 1 : 0) + " " + (val).ToString();
                break;

            case JoystickButtons.LX:
                state.lx = deadband(((update.Value / 65535.0) - 0.5) * 2.0, 0.05);
                data     = "L " + calcJoystick(state.lx, state.ly);
                break;

            case JoystickButtons.LY:
                state.ly = deadband(((update.Value / 65535.0) - 0.5) * -2.0, 0.05);
                data     = "L " + calcJoystick(state.lx, state.ly);
                break;

            case JoystickButtons.RX:
                state.rx = deadband(((update.Value / 65535.0) - 0.5) * 2.0, 0.05);
                data     = "R " + calcJoystick(state.rx, state.ry);
                break;

            case JoystickButtons.RY:
                state.ry = deadband(((update.Value / 65535.0) - 0.5) * -2.0, 0.05);
                data     = "R " + calcJoystick(state.rx, state.ry);
                break;

            default:
                data = update.ToString();
                break;
            }
            return(new ParseUpdateResult(state, data));
        }
コード例 #2
0
 public ParseUpdateResult(PartialJoystickState state, string update)
 {
     this.state  = state;
     this.update = update;
 }
コード例 #3
0
        static void Main(string[] args)
        {
            /** BLUETOOTH INIT **/

            //Check to make sure BT is running
            new BluetoothClient();

            /** CONTROLLER INIT **/

            // Initialize DirectInput
            var directInput = new DirectInput();

            // Find a Joystick Guid
            var joystickGuid = Guid.Empty;

            foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad,
                                                                  DeviceEnumerationFlags.AllDevices))
            {
                joystickGuid = deviceInstance.InstanceGuid;
            }

            // If Gamepad not found, look for a Joystick
            if (joystickGuid == Guid.Empty)
            {
                foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick,
                                                                      DeviceEnumerationFlags.AllDevices))
                {
                    joystickGuid = deviceInstance.InstanceGuid;
                }
            }

            // If Joystick not found, throws an error
            if (joystickGuid == Guid.Empty)
            {
                Console.WriteLine("No joystick/Gamepad found.");
                Console.ReadKey();
                Environment.Exit(1);
            }

            // Instantiate the joystick
            var joystick = new Joystick(directInput, joystickGuid);

            Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);

            // Set BufferSize in order to use buffered data.
            joystick.Properties.BufferSize = 128;

            // Acquire the joystick
            joystick.Acquire();

            // Poll events from joystick
            PartialJoystickState partialState = new PartialJoystickState();

            partialState.lx = 0.0;
            partialState.ly = 0.0;
            partialState.rx = 0.0;
            partialState.ry = 0.0;
            while (true)
            {
                try
                {
                    joystick.Poll();
                    var updates = joystick.GetBufferedData();
                    //JoystickState state = joystick.GetCurrentState();
                    foreach (JoystickUpdate update in updates)
                    {
                        ParseUpdateResult result = parseUpdate(update, partialState);
                        partialState = result.state;
                        Console.WriteLine(result.update);
                    }
                } catch (Exception e)
                {
                    Console.WriteLine("Error reading from joystick.");
                    joystick.Unacquire();
                    joystick.Acquire();
                }
            }
        }