コード例 #1
0
ファイル: SuperNESandNES.cs プロジェクト: retrospy/RetroSpy
        public static ControllerStateEventArgs ReadFromPacketAtari52001(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length != BUTTONS_ATARI5200.Length)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS_ATARI5200.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS_ATARI5200[i]))
                {
                    continue;
                }

                state.SetButton(BUTTONS_ATARI5200[i], packet[i] == 0x00);
            }

            state.SetAnalog("x", (((packet[17] >> 4) | (packet[18])) - 128.0f) / -128.0f, (packet[17] >> 4) | (packet[18]));
            state.SetAnalog("y", atari5200_y, atari5200_yRaw);

            return(state.Build());
        }
コード例 #2
0
ファイル: Nintendo64.cs プロジェクト: sk84uhlivin/RetroSpy
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            var state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS [i]))
                {
                    continue;
                }
                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            float x = readStick(SignalTool.readByte(packet, BUTTONS.Length));
            float y = readStick(SignalTool.readByte(packet, BUTTONS.Length + 8));

            state.SetAnalog("stick_x", x);
            state.SetAnalog("stick_y", y);

            SignalTool.SetMouseProperties(x, y, state);

            return(state.Build());
        }
コード例 #3
0
ファイル: NeoGeo.cs プロジェクト: edru/RetroSpy
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            float lstick_x = 0;
            float lstick_y = 0;

            if (packet[3] != 0x00)
            {
                lstick_x = 1;
            }
            else if (packet[8] != 0x00)
            {
                lstick_x = -1;
            }

            if (packet[4] != 0x00)
            {
                lstick_y = 1;
            }
            else if (packet[9] != 0x00)
            {
                lstick_y = -1;
            }

            if (lstick_y != 0 || lstick_x != 0)
            {
                // point on the unit circle at the same angle
                double radian = Math.Atan2(lstick_y, lstick_x);
                float  x1     = (float)Math.Cos(radian);
                float  y1     = (float)Math.Sin(radian);

                // Don't let magnitude exceed the unit circle
                if (Math.Sqrt(Math.Pow(lstick_x, 2) + Math.Pow(lstick_y, 2)) > 1.0)
                {
                    lstick_x = x1;
                    lstick_y = y1;
                }
            }

            state.SetAnalog("lstick_x", lstick_x);
            state.SetAnalog("lstick_y", lstick_y);

            return(state.Build());
        }
コード例 #4
0
ファイル: VFlash.cs プロジェクト: retrospy/RetroSpy
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length == PACKET_SIZE)
            {
                ControllerStateBuilder state = new ControllerStateBuilder();

                for (int i = 0; i < BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS[i], packet[i] != 0x00);
                }

                state.SetAnalog("lstick_x", ReadStick(packet[15]), packet[15]);
                state.SetAnalog("lstick_y", ReadStick(packet[16]), packet[16]);

                return(state.Build());
            }

            return(null);
        }
コード例 #5
0
ファイル: Nintendo64.cs プロジェクト: retrospy/RetroSpy
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            float x = ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length));
            float y = ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 8));

            state.SetAnalog("stick_x", x, SignalTool.ReadByte(packet, BUTTONS.Length));
            state.SetAnalog("stick_y", y, SignalTool.ReadByte(packet, BUTTONS.Length + 8));

            SignalTool.SetMouseProperties(x, y, SignalTool.ReadByte(packet, BUTTONS.Length), SignalTool.ReadByte(packet, BUTTONS.Length + 8), state);

            return(state.Build());
        }
コード例 #6
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            if (packet[4] != 5)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            state.SetAnalog("paddle", ReadPaddle(packet[2]), packet[2]);
            state.SetAnalog("paddle2", SecondPaddle, SecondPaddleRaw);
            state.SetButton("fire2", SecondButton);
            state.SetButton("fire", packet[1] != 0x00);

            return(state.Build());
        }
コード例 #7
0
ファイル: MouseTester.cs プロジェクト: sk84uhlivin/RetroSpy
        void tick(object sender, EventArgs e)
        {
            var outState = new ControllerStateBuilder();

            if (ticks % 18 == 0)
            {
                buttonsOn = buttonsOn ? false : true;
            }

            ticks++;

            if (!inCenter && (theta == 0 || theta == 90 || theta == 180 || theta == 270))
            {
                inCenter = true;
            }
            else if (inCenter)
            {
                inCenter = false;
            }

            float x = 0;
            float y = 0;

            if (!inCenter)
            {
                x      = (float)Math.Cos(theta * Math.PI / 180);
                y      = (float)Math.Sin(theta * Math.PI / 180);
                theta  = theta + 5;
                theta %= 360;
            }
            outState.SetButton("left", buttonsOn);
            outState.SetButton("middle", buttonsOn);
            outState.SetButton("right", buttonsOn);
            outState.SetButton("wired-1", buttonsOn);
            outState.SetButton("wired-2", buttonsOn);
            outState.SetButton("a", buttonsOn);
            outState.SetButton("b", buttonsOn);
            outState.SetButton("c", buttonsOn);
            outState.SetButton("start", buttonsOn);
            outState.SetButton("thumb", buttonsOn);
            outState.SetButton("scroll_up", buttonsOn);
            outState.SetButton("scroll_down", buttonsOn);

            outState.SetButton("mouse_center", true);

            outState.SetAnalog("stick_x", x);
            outState.SetAnalog("stick_y", y);

            if (ControllerStateChanged != null)
            {
                ControllerStateChanged(this, outState.Build());
            }
        }
コード例 #8
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            var str = System.Text.Encoding.Default.GetString(packet, 0, 40);

            byte[] binaryPacket = new byte[20];

            try
            {
                for (int i = 0; i < str.Length; i += 2)
                {
                    binaryPacket[i / 2] = Convert.ToByte(str.Substring(i, 2), 16);
                }
            }
            catch (Exception)
            {
                return(null);
            }

            var outState = new ControllerStateBuilder();

            for (int i = 0; i < 8; ++i)
            {
                outState.SetButton(BUTTONS[i], (binaryPacket[2] & (1 << i)) != 0);
            }

            for (int i = 4; i < 12; ++i)
            {
                outState.SetButton(ANALOG_BUTTONS[i - 4], binaryPacket[i] > 0);
                outState.SetAnalog(ANALOG_BUTTONS[i - 4], ReadTrigger(binaryPacket[i]));
            }

            int j = 0;

            for (int i = 0; i < 4; ++i)
            {
                short val = binaryPacket[12 + j];
                val += (short)(binaryPacket[13 + j] << 8);
                outState.SetAnalog(STICKS[i], ReadStick(val));
                j += 2;
            }

            return(outState.Build());
        }
コード例 #9
0
ファイル: Paddles.cs プロジェクト: sk84uhlivin/RetroSpy
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }
            if (packet[4] != 5)
            {
                return(null);
            }

            var state = new ControllerStateBuilder();

            state.SetAnalog("paddle", readPaddle(packet[2]));
            state.SetAnalog("paddle2", SecondPaddle);
            state.SetButton("fire2", SecondButton);
            state.SetButton("fire", (packet[1] != 0x00));

            return(state.Build());
        }
コード例 #10
0
ファイル: SignalTool.cs プロジェクト: retrospy/RetroSpy
        public static void GenerateFakeStick(ControllerStateBuilder state, string xname, string yname, bool up, bool down, bool left, bool right)
        {
            float x = 0;
            float y = 0;

            if (right)
            {
                x = 1;
            }
            else if (left)
            {
                x = -1;
            }

            if (up)
            {
                y = 1;
            }
            else if (down)
            {
                y = -1;
            }

            if (y != 0 || x != 0)
            {
                // point on the unit circle at the same angle
                double radian = Math.Atan2(y, x);
                float  x1     = (float)Math.Cos(radian);
                float  y1     = (float)Math.Sin(radian);

                // Don't let magnitude exceed the unit circle
                if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) > 1.0)
                {
                    x = x1;
                    y = y1;
                }
            }

            state.SetAnalog(xname, x, 0);
            state.SetAnalog(yname, y, 0);
        }
コード例 #11
0
        public static void FakeAnalogStick(byte up, byte down, byte left, byte right, ControllerStateBuilder state, string xName, string yName)
        {
            float x = 0;
            float y = 0;

            if (right != 0x00)
            {
                x = 1;
            }
            else if (left != 0x00)
            {
                x = -1;
            }

            if (up != 0x00)
            {
                y = 1;
            }
            else if (down != 0x00)
            {
                y = -1;
            }

            if (y != 0 || x != 0)
            {
                // point on the unit circle at the same angle
                double radian = Math.Atan2(y, x);
                float  x1     = (float)Math.Cos(radian);
                float  y1     = (float)Math.Sin(radian);

                // Don't let magnitude exceed the unit circle
                if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) > 1.0)
                {
                    x = x1;
                    y = y1;
                }
            }

            state.SetAnalog(xName, x);
            state.SetAnalog(yName, y);
        }
コード例 #12
0
ファイル: XInputReader.cs プロジェクト: sk84uhlivin/RetroSpy
        void tick (object sender, EventArgs e)
        {
            var state = new XInputState ();
            if (XInputDLL.XInputGetState (_id, ref state) > 0) {
                if (ControllerDisconnected != null) ControllerDisconnected (this, EventArgs.Empty);
                Finish ();
                return;
            }

            var outState = new ControllerStateBuilder ();
            
            outState.SetButton ("a", (state.wButtons & 0x1000) != 0);
            outState.SetButton ("b", (state.wButtons & 0x2000) != 0);
            outState.SetButton ("x", (state.wButtons & 0x4000) != 0);
            outState.SetButton ("y", (state.wButtons & 0x8000) != 0);
            outState.SetButton ("up", (state.wButtons & 0x0001) != 0);
            outState.SetButton ("down", (state.wButtons & 0x0002) != 0);
            outState.SetButton ("left", (state.wButtons & 0x0004) != 0);
            outState.SetButton ("right", (state.wButtons & 0x0008) != 0);
            outState.SetButton ("start", (state.wButtons & 0x0010) != 0);
            outState.SetButton ("back", (state.wButtons & 0x0020) != 0);
            outState.SetButton ("l3", (state.wButtons & 0x0040) != 0);
            outState.SetButton ("r3", (state.wButtons & 0x0080) != 0);
            outState.SetButton ("l", (state.wButtons & 0x0100) != 0);
            outState.SetButton ("r", (state.wButtons & 0x0200) != 0);

            outState.SetAnalog ("lstick_x", (float)state.sThumbLX / 32768);
            outState.SetAnalog ("lstick_y", (float)state.sThumbLY / 32768);
            outState.SetAnalog ("rstick_x", (float)state.sThumbRX / 32768);
            outState.SetAnalog ("rstick_y", (float)state.sThumbRY / 32768);
            outState.SetAnalog ("trig_l", (float)state.bLeftTrigger / 255);
            outState.SetAnalog ("trig_r", (float)state.bRightTrigger / 255);

            if (ControllerStateChanged != null) ControllerStateChanged (this, outState.Build ());
        }
コード例 #13
0
ファイル: GameCube.cs プロジェクト: sk84uhlivin/RetroSpy
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            var state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS [i]))
                {
                    continue;
                }
                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            state.SetAnalog("lstick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length)));
            state.SetAnalog("lstick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 8)));
            state.SetAnalog("cstick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length + 16)));
            state.SetAnalog("cstick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 24)));
            state.SetAnalog("trig_l", readTrigger(SignalTool.readByte(packet, BUTTONS.Length + 32)));
            state.SetAnalog("trig_r", readTrigger(SignalTool.readByte(packet, BUTTONS.Length + 40)));

            return(state.Build());
        }
コード例 #14
0
        void tick(object sender, EventArgs e)
        {
            Random rand     = new Random();
            var    outState = new ControllerStateBuilder();

            if (ticks % 18 == 0)
            {
                buttonsOn = buttonsOn ? false : true;
            }

            if (ticks % 36 == 0)
            {
                visable = visable ? false : true;
            }

            ticks++;

            double x1 = rand.NextDouble();
            double y1 = rand.NextDouble();
            double x2 = rand.NextDouble();
            double y2 = rand.NextDouble();

            double x3 = rand.NextDouble();
            double y3 = rand.NextDouble();
            double x4 = rand.NextDouble();
            double y4 = rand.NextDouble();


            if (visable)
            {
                if (buttonsOn)
                {
                    outState.SetAnalog("touchpad_x3", (float)x3);
                    outState.SetAnalog("touchpad_y3", (float)y3);
                    outState.SetAnalog("touchpad_x4", (float)x4);
                    outState.SetAnalog("touchpad_y4", (float)y4);
                }
                else
                {
                    outState.SetAnalog("touchpad_x1", (float)x1);
                    outState.SetAnalog("touchpad_y1", (float)y1);
                    outState.SetAnalog("touchpad_x2", (float)x2);
                    outState.SetAnalog("touchpad_y2", (float)y2);
                }
            }
            if (ControllerStateChanged != null)
            {
                ControllerStateChanged(this, outState.Build());
            }
        }
コード例 #15
0
        static public ControllerState ReadFromPacket_Atari5200_1(byte[] packet)
        {
            if (packet.Length != BUTTONS_ATARI5200.Length)
            {
                return(null);
            }

            var state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS_ATARI5200.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS_ATARI5200[i]))
                {
                    continue;
                }
                state.SetButton(BUTTONS_ATARI5200[i], packet[i] == 0x00);
            }

            state.SetAnalog("x", (((packet[17] >> 4) | (packet[18])) - 128.0f) / -128.0f);
            state.SetAnalog("y", atari5200_y);

            return(state.Build());
        }
コード例 #16
0
ファイル: XboxReader.cs プロジェクト: sk84uhlivin/RetroSpy
        public static void ProcessPacketWorker(object data)
        {
            XboxReader reader = ((XboxReader)data);

            while (!reader.inShutdown)
            {
                if (reader.packetsToBeProcessed.Count > 0)
                {
                    reader.packetsToBeProcessed.TryDequeue(out USBPacket packet);

                    var outState = new ControllerStateBuilder();

                    for (int i = 0; i < 8; ++i)
                    {
                        outState.SetButton(BUTTONS[i], (packet.Packet[3] & (1 << i)) != 0);
                    }

                    for (int i = 5; i < 13; ++i)
                    {
                        outState.SetButton(ANALOG_BUTTONS[i - 5], packet.Packet[i] > 0);
                        outState.SetAnalog(ANALOG_BUTTONS[i - 5], ReadTrigger(packet.Packet[i]));
                    }

                    int j = 0;
                    for (int i = 0; i < 4; ++i)
                    {
                        short val = packet.Packet[13 + j];
                        val += (short)(packet.Packet[14 + j] << 8);
                        outState.SetAnalog(STICKS[i], ReadStick(val));
                        j += 2;
                    }

                    reader.ControllerStateChanged?.Invoke(reader, outState.Build());
                }
            }
        }
コード例 #17
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

            for (int i = 0; i < 16; ++i)
            {
                polishedPacket[i] = (byte)((packet[i] == 0x31) ? 1 : 0);
            }

            for (int i = 0; i < 2; ++i)
            {
                packet[16 + i] = 0;
                for (byte j = 0; j < 8; ++j)
                {
                    polishedPacket[16 + i] |= (byte)((packet[16 + (i * 8 + j)] == 0x30 ? 0 : 1) << j);
                }
            }

            short[] sticks = new short[4];

            for (int i = 0; i < 4; ++i)
            {
                sticks[i] = 0;
                for (byte j = 0; j < 16; ++j)
                {
                    sticks[i] |= (short)((packet[32 + (i * 16 + j)] == 0x30 ? 0 : 1) << j);
                }
            }

            var outState = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }
                outState.SetButton(BUTTONS[i], polishedPacket[i] != 0x00);
            }

            outState.SetAnalog("trig_l", readTrigger(polishedPacket[16]));
            outState.SetAnalog("trig_r", readTrigger(polishedPacket[17]));

            outState.SetAnalog("rstick_x", readStick(sticks[2]));
            outState.SetAnalog("rstick_y", readStick(sticks[3]));
            outState.SetAnalog("lstick_x", readStick(sticks[0]));
            outState.SetAnalog("lstick_y", readStick(sticks[1]));

            return(outState.Build());
        }
コード例 #18
0
ファイル: GamepadReader.cs プロジェクト: retrospy/RetroSpy
        private void Tick(object sender, EventArgs e)
        {
            try
            {
                _joystick.Poll();
            }
            catch (SharpDXException)
            {
                Finish();
                ControllerDisconnected?.Invoke(this, EventArgs.Empty);
                return;
            }

            ControllerStateBuilder outState = new ControllerStateBuilder();
            JoystickState          state    = _joystick.GetCurrentState();

            for (int i = 0; i < _joystick.Capabilities.ButtonCount; ++i)
            {
                outState.SetButton("b" + i.ToString(CultureInfo.CurrentCulture), state.Buttons[i]);
            }

            int[] pov = state.PointOfViewControllers;

            outState.SetButton("up", false);
            outState.SetButton("right", false);
            outState.SetButton("down", false);
            outState.SetButton("left", false);

            if (pov != null && pov.Length > 0 && pov[0] >= 0)
            {
                outState.SetButton("up", pov[0] > OctantAngle(6) || pov[0] < OctantAngle(1));
                outState.SetButton("right", pov[0] > OctantAngle(0) && pov[0] < OctantAngle(3));
                outState.SetButton("down", pov[0] > OctantAngle(2) && pov[0] < OctantAngle(5));
                outState.SetButton("left", pov[0] > OctantAngle(4) && pov[0] < OctantAngle(7));
            }
            else if (_joystick.Information.ProductName == "8Bitdo SF30 Wireless Controller") // For SN30
            {
                outState.SetButton("up", (float)state.Y / RANGE == -1.0);
                outState.SetButton("down", (float)state.Y / RANGE == 1.0);
                outState.SetButton("left", (float)state.X / RANGE == -1.0);
                outState.SetButton("right", (float)state.X / RANGE == 1.0);
            }

            outState.SetAnalog("x", (float)state.X / RANGE, state.X);
            outState.SetAnalog("y", (float)state.Y / RANGE, state.Y);
            outState.SetAnalog("z", (float)state.Z / RANGE, state.Z);
            outState.SetAnalog("rx", (float)state.RotationX / RANGE, state.RotationX);
            outState.SetAnalog("ry", (float)state.RotationY / RANGE, state.RotationY);
            outState.SetAnalog("rz", (float)state.RotationZ / RANGE, state.RotationZ);

            ControllerStateChanged?.Invoke(this, outState.Build());
        }
コード例 #19
0
ファイル: GamepadReader.cs プロジェクト: sk84uhlivin/RetroSpy
        void tick(object sender, EventArgs e)
        {
            try
            { _joystick.Poll(); }
            catch (Exception)
            {
                Finish();
                if (ControllerDisconnected != null)
                {
                    ControllerDisconnected(this, EventArgs.Empty);
                }
                return;
            }

            var outState = new ControllerStateBuilder();
            var state    = _joystick.GetCurrentState();

            for (int i = 0; i < _joystick.Capabilities.ButtonCount; ++i)
            {
                outState.SetButton("b" + i.ToString(), state.Buttons[i]);
            }

            int[] pov = state.PointOfViewControllers;

            outState.SetButton("up", false);
            outState.SetButton("right", false);
            outState.SetButton("down", false);
            outState.SetButton("left", false);

            if (pov != null && pov.Length > 0 && pov[0] >= 0)
            {
                outState.SetButton("up", pov[0] > octantAngle(6) || pov[0] < octantAngle(1));
                outState.SetButton("right", pov[0] > octantAngle(0) && pov[0] < octantAngle(3));
                outState.SetButton("down", pov[0] > octantAngle(2) && pov[0] < octantAngle(5));
                outState.SetButton("left", pov[0] > octantAngle(4) && pov[0] < octantAngle(7));
            }

            outState.SetAnalog("x", (float)state.X / RANGE);
            outState.SetAnalog("y", (float)state.Y / RANGE);
            outState.SetAnalog("z", (float)state.Z / RANGE);
            outState.SetAnalog("rx", (float)state.RotationX / RANGE);
            outState.SetAnalog("ry", (float)state.RotationY / RANGE);
            outState.SetAnalog("rz", (float)state.RotationZ / RANGE);

            if (ControllerStateChanged != null)
            {
                ControllerStateChanged(this, outState.Build());
            }
        }
コード例 #20
0
ファイル: SignalTool.cs プロジェクト: retrospy/RetroSpy
        private static void SetMouseProperties(float x, float y, int xRaw, int yRaw, ControllerStateBuilder state,
                                               float maxCircleSize, SlidingWindow window, string prefix)
        {
            window.windowX[window.windowPositionX] = x;
            window.windowPositionX += 1;
            window.windowPositionX %= 3;

            window.windowY[window.windowPositionY] = y;
            window.windowPositionY += 1;
            window.windowPositionY %= 3;

            y = MiddleOfThree(window.windowY[0], window.windowY[1], window.windowY[2]);
            x = MiddleOfThree(window.windowX[0], window.windowX[1], window.windowX[2]);

            float y1 = y;
            float x1 = x;

            if (y != 0 || x != 0)
            {
                // Direction shows around the unit circle
                double radian = Math.Atan2(y, x);
                x1 = maxCircleSize * (float)Math.Cos(radian);
                y1 = maxCircleSize * (float)Math.Sin(radian);

                // Don't let magnitude exceed the unit circle
                if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) > maxCircleSize)
                {
                    x = x1;
                    y = y1;
                }
            }

            state.SetAnalog(prefix + "mouse_center_x", 0, 0);
            state.SetAnalog(prefix + "mouse_center_y", 0, 0);
            state.SetAnalog(prefix + "mouse_direction_x", x1, xRaw);
            state.SetAnalog(prefix + "mouse_direction_y", y1, yRaw);
            state.SetAnalog(prefix + "mouse_magnitude_x", x, xRaw);
            state.SetAnalog(prefix + "mouse_magnitude_y", y, yRaw);
        }
コード例 #21
0
        public static void SetMouseProperties(float x, float y, ControllerStateBuilder state, float maxCircleSize = 1.0f)
        {
            windowX[windowPositionX] = x;
            windowPositionX         += 1;
            windowPositionX          = (windowPositionX % 3);

            windowY[windowPositionY] = y;
            windowPositionY         += 1;
            windowPositionY          = (windowPositionY % 3);

            y = middleOfThree(windowY[0], windowY[1], windowY[2]);
            x = middleOfThree(windowX[0], windowX[1], windowX[2]);

            float y1 = y;
            float x1 = x;

            if (y != 0 || x != 0)
            {
                // Direction shows around the unit circle
                double radian = Math.Atan2(y, x);
                x1 = maxCircleSize * (float)Math.Cos(radian);
                y1 = maxCircleSize * (float)Math.Sin(radian);

                // Don't let magnitude exceed the unit circle
                if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) > maxCircleSize)
                {
                    x = x1;
                    y = y1;
                }
            }

            state.SetAnalog("mouse_center_x", 0);
            state.SetAnalog("mouse_center_y", 0);
            state.SetAnalog("mouse_direction_x", x1);
            state.SetAnalog("mouse_direction_y", y1);
            state.SetAnalog("mouse_magnitude_x", x);
            state.SetAnalog("mouse_magnitude_y", y);
        }
コード例 #22
0
ファイル: XInputReader.cs プロジェクト: retrospy/RetroSpy
        private void Tick(object sender, EventArgs e)
        {
            XInputState state = new XInputState();

            if (NativeMethods.XInputGetState(_id, ref state) > 0)
            {
                ControllerDisconnected?.Invoke(this, EventArgs.Empty);
                Finish();
                return;
            }

            ControllerStateBuilder outState = new ControllerStateBuilder();

            outState.SetButton("a", (state.wButtons & 0x1000) != 0);
            outState.SetButton("b", (state.wButtons & 0x2000) != 0);
            outState.SetButton("x", (state.wButtons & 0x4000) != 0);
            outState.SetButton("y", (state.wButtons & 0x8000) != 0);
            outState.SetButton("up", (state.wButtons & 0x0001) != 0);
            outState.SetButton("down", (state.wButtons & 0x0002) != 0);
            outState.SetButton("left", (state.wButtons & 0x0004) != 0);
            outState.SetButton("right", (state.wButtons & 0x0008) != 0);
            outState.SetButton("start", (state.wButtons & 0x0010) != 0);
            outState.SetButton("back", (state.wButtons & 0x0020) != 0);
            outState.SetButton("l3", (state.wButtons & 0x0040) != 0);
            outState.SetButton("r3", (state.wButtons & 0x0080) != 0);
            outState.SetButton("l", (state.wButtons & 0x0100) != 0);
            outState.SetButton("r", (state.wButtons & 0x0200) != 0);

            outState.SetAnalog("lstick_x", (float)state.sThumbLX / 32768, state.sThumbLX);
            outState.SetAnalog("lstick_y", (float)state.sThumbLY / 32768, state.sThumbLY);
            outState.SetAnalog("rstick_x", (float)state.sThumbRX / 32768, state.sThumbRX);
            outState.SetAnalog("rstick_y", (float)state.sThumbRY / 32768, state.sThumbRY);
            outState.SetAnalog("trig_l", (float)state.bLeftTrigger / 255, state.bLeftTrigger);
            outState.SetAnalog("trig_r", (float)state.bRightTrigger / 255, state.bRightTrigger);

            ControllerStateChanged?.Invoke(this, outState.Build());
        }
コード例 #23
0
        public static void ProcessPacketWorker(object data)
        {
            WiiReaderV1 reader = ((WiiReaderV1)data);

            while (!reader.inShutdown)
            {
                if (reader.packetsToBeProcessed.Count > 0)
                {
                    reader.packetsToBeProcessed.TryDequeue(out I2CPacket packet);


                    // Check the address
                    if (((packet.Data[0] & 0xff) >> 1) != 0x52)
                    {
                        continue;
                    }

                    // read or write
                    bool isWrite = (packet.Data[0] & 0x01) == 0;
                    if (isWrite)
                    {
                        byte data1 = (byte)(packet.Data[1] & 0xFF);
                        if (packet.Length == 2 && data1 == 0)
                        {
                            reader.isControllerPoll = true;
                        }
                        else if (packet.Length == 3 && (byte)(data1 & 0xFF) == 0xF0 && (byte)(packet.Data[2] & 0xFF) == 0x55)
                        {
                            reader.isEncrypted = false;
                        }
                        else if (packet.Length == 2 && (byte)(data1 & 0xFF) == 0xFA)
                        {
                            reader.isControllerID = true;
                        }
                        else if ((data1 == 0x40 && packet.Length == 8) || (data1 == 0x46 && packet.Length == 8) || (data1 == 0x4C && packet.Length == 6))
                        {
                            if (data1 == 0x40)
                            {
                                for (int j = 0; j < 6; j++)
                                {
                                    reader.wm_rand[9 - j] = (byte)(packet.Data[2 + j] & 0xff);
                                }
                            }
                            else if (data1 == 0x46)
                            {
                                for (int j = 6; j < 10; j++)
                                {
                                    reader.wm_rand[9 - j] = (byte)(packet.Data[2 + (j - 6)] & 0xff);
                                }
                                for (int j = 0; j < 2; j++)
                                {
                                    reader.wm_key[5 - j] = (byte)(packet.Data[6 + j] & 0xff);
                                }
                            }
                            else if (data1 == 0x4C)
                            {
                                for (int j = 2; j < 6; j++)
                                {
                                    reader.wm_key[5 - j] = (byte)(packet.Data[j] & 0xff);
                                }

                                // generate decryption once all data is loaded
                                reader.wm_gentabs();
                                reader.isEncrypted = true;
                            }
                        }
                    }
                    else
                    {
                        if (reader.isControllerID && packet.Length == 7)
                        {
                            if ((packet.Data[packet.Length - 2] & 0xff) == 0 && (packet.Data[packet.Length - 1] & 0xff) == 0)
                            {
                                reader.controllerType = 0;
                            }
                            else if ((packet.Data[packet.Length - 2] & 0xff) == 1 && (packet.Data[packet.Length - 1] & 0xff) == 1)
                            {
                                reader.controllerType = 1;
                            }
                            else
                            {
                                reader.controllerType = 2;
                            }

                            reader.isControllerID = false;
                        }
                        else if (reader.isControllerPoll && (packet.Length == 7 || packet.Length == 9))
                        {
                            for (int i = 1; i < packet.Length; ++i)
                            {
                                if (reader.isEncrypted)
                                {
                                    packet.UnencryptedData[i - 1] = (byte)((byte)((packet.Data[i] & 0xff) ^ reader.wm_sb[(i - 1) % 8]) + reader.wm_ft[(i - 1) % 8]);
                                }
                                else
                                {
                                    packet.UnencryptedData[i - 1] = (byte)(packet.Data[i] & 0xff);
                                }
                            }

                            if (reader.controllerType == 0)  // Nunchuck
                            {
                                var outState = new ControllerStateBuilder();

                                byte   stickX = packet.UnencryptedData[0];
                                byte   stickY = packet.UnencryptedData[1];
                                ushort aX     = (ushort)((packet.UnencryptedData[2] << 2) | ((packet.UnencryptedData[5] & 0b0000000000001100) >> 2));
                                ushort ay     = (ushort)((packet.UnencryptedData[3] << 2) | ((packet.UnencryptedData[5] & 0b0000000000110000) >> 2));
                                ushort az     = (ushort)((packet.UnencryptedData[4] << 2) | ((packet.UnencryptedData[5] & 0b0000000011000000) >> 2));

                                outState.SetButton("c", (packet.UnencryptedData[5] & 0b00000010) == 0);
                                outState.SetButton("z", (packet.UnencryptedData[5] & 0b00000001) == 0);

                                outState.SetAnalog("stick_x", (stickX - 128.0f) / 128.0f);
                                outState.SetAnalog("stick_y", (stickY - 128.0f) / 128.0f);

                                outState.SetButton("disconnect", false);

                                reader.ControllerStateChanged?.Invoke(reader, outState.Build());
                            }
                            else if (reader.controllerType == 1) // Classic Controller
                            {
                                var outState = new ControllerStateBuilder();

                                byte rightTrigger = (byte)(packet.UnencryptedData[3] & 0b00011111);
                                byte leftTrigger  = (byte)(((packet.UnencryptedData[3] & 0b11100000) >> 5) | ((packet.UnencryptedData[2] & 0b01100000) >> 2));

                                byte leftX = (byte)(packet.UnencryptedData[0] & 0b00111111);
                                byte leftY = (byte)(packet.UnencryptedData[1] & 0b00111111);

                                byte rightX = (byte)(((packet.UnencryptedData[2] & 0b10000000) >> 7) | ((packet.UnencryptedData[1] & 0b11000000) >> 5) | ((packet.UnencryptedData[0] & 0b11000000) >> 3));
                                byte rightY = (byte)(packet.UnencryptedData[2] & 0b00011111);

                                outState.SetButton("up", (packet.UnencryptedData[5] & ~0xFE) == 0);
                                outState.SetButton("right", (packet.UnencryptedData[4] & ~0x7F) == 0);
                                outState.SetButton("down", (packet.UnencryptedData[4] & ~0xBF) == 0);
                                outState.SetButton("left", (packet.UnencryptedData[5] & ~0xFD) == 0);

                                outState.SetButton("b", (packet.UnencryptedData[5] & ~0xBF) == 0);
                                outState.SetButton("a", (packet.UnencryptedData[5] & ~0xEF) == 0);
                                outState.SetButton("y", (packet.UnencryptedData[5] & 0b00100000) == 0);
                                outState.SetButton("x", (packet.UnencryptedData[5] & 0b00001000) == 0);

                                outState.SetButton("select", (packet.UnencryptedData[4] & ~0xEF) == 0);
                                outState.SetButton("home", (packet.UnencryptedData[4] & 0b00001000) == 0);
                                outState.SetButton("start", (packet.UnencryptedData[4] & ~0xFB) == 0);

                                outState.SetButton("l", (packet.UnencryptedData[4] & 0b00100000) == 0);
                                outState.SetButton("r", (packet.UnencryptedData[4] & 0b00000010) == 0);

                                outState.SetAnalog("l_trig", leftTrigger / 31.0f);
                                outState.SetAnalog("r_trig", rightTrigger / 31.0f);

                                outState.SetButton("zl", (packet.UnencryptedData[5] & 0b10000000) == 0);
                                outState.SetButton("zr", (packet.UnencryptedData[5] & 0b00000100) == 0);

                                outState.SetAnalog("lstick_x", (leftX - 32.0f) / 32.0f);
                                outState.SetAnalog("lstick_y", (leftY - 32.0f) / 32.0f);
                                outState.SetAnalog("rstick_x", (rightX - 15.0f) / 15.0f);
                                outState.SetAnalog("rstick_y", (rightY - 15.0f) / 15.0f);

                                outState.SetButton("disconnect", false);

                                reader.ControllerStateChanged?.Invoke(reader, outState.Build());
                            }
                            else
                            {
                                var outState = new ControllerStateBuilder();

                                outState.SetButton("disconnect", true);

                                reader.ControllerStateChanged?.Invoke(reader, outState.Build());
                            }
                        }
                    }
                }
            }
        }
コード例 #24
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

            for (int i = 0; i < 20; ++i)
            {
                polishedPacket[i] = (byte)((packet[PACKET_HEADER + i] == 0x31) ? 1 : 0);
            }

            for (int i = 0; i < 14; ++i)
            {
                polishedPacket[20 + i] = 0;
                for (byte j = 0; j < 8; ++j)
                {
                    polishedPacket[20 + i] |= (byte)((packet[PACKET_HEADER + 20 + (i * 8) + j] == 0x30 ? 0 : 1) << j);
                }
            }

            ControllerStateBuilder outState = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                outState.SetButton(BUTTONS[i], polishedPacket[i] != 0x00);
            }

            outState.SetAnalog("rstick_x", ReadStick(polishedPacket[22]), polishedPacket[22]);
            outState.SetAnalog("rstick_y", ReadStick(polishedPacket[23]), polishedPacket[23]);
            outState.SetAnalog("lstick_x", ReadStick(polishedPacket[20]), polishedPacket[20]);
            outState.SetAnalog("lstick_y", ReadStick(polishedPacket[21]), polishedPacket[21]);

            outState.SetAnalog("l_trig", ReadStick(polishedPacket[24]), polishedPacket[24]);
            outState.SetAnalog("r_trig", ReadStick(polishedPacket[25]), polishedPacket[25]);

            int touchpad_x1 = (polishedPacket[27] << 8) | polishedPacket[26];
            int touchpad_y1 = (polishedPacket[29] << 8) | polishedPacket[28];
            int touchpad_x2 = (polishedPacket[31] << 8) | polishedPacket[30];
            int touchpad_y2 = (polishedPacket[33] << 8) | polishedPacket[32];

            if (polishedPacket[18] == 1)     // touch
            {
                if (polishedPacket[17] == 1) // click
                {
                    outState.SetAnalog("touchpad_x3", ReadTouchPad(touchpad_x1, 1920), touchpad_x1);
                    outState.SetAnalog("touchpad_y3", ReadTouchPad(touchpad_y1, 943), touchpad_y1);
                }
                else
                {
                    outState.SetAnalog("touchpad_x1", ReadTouchPad(touchpad_x1, 1920), touchpad_x1);
                    outState.SetAnalog("touchpad_y1", ReadTouchPad(touchpad_y1, 943), touchpad_y1);
                }
            }

            if (polishedPacket[19] == 1)     // touch
            {
                if (polishedPacket[17] == 1) // click
                {
                    outState.SetAnalog("touchpad_x4", ReadTouchPad(touchpad_x2, 1920), touchpad_x2);
                    outState.SetAnalog("touchpad_y4", ReadTouchPad(touchpad_y2, 943), touchpad_y2);
                }
                else
                {
                    outState.SetAnalog("touchpad_x2", ReadTouchPad(touchpad_x2, 1920), touchpad_x2);
                    outState.SetAnalog("touchpad_y2", ReadTouchPad(touchpad_y2, 943), touchpad_y2);
                }
            }

            return(outState.Build());
        }
コード例 #25
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length >= PACKET_SIZE)
            {
                ControllerStateBuilder state = new ControllerStateBuilder();

                for (int i = 0; i < BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS[i], packet[i] != 0x00);
                }
                float x = 0;
                float y = 0;

                if (packet[3] != 0x00)
                {
                    x = 1;
                }
                else if (packet[2] != 0x00)
                {
                    x = -1;
                }

                if (packet[0] != 0x00)
                {
                    y = 1;
                }
                else if (packet[1] != 0x00)
                {
                    y = -1;
                }

                if (y != 0 || x != 0)
                {
                    // point on the unit circle at the same angle
                    double radian = Math.Atan2(y, x);
                    float  x1     = (float)Math.Cos(radian);
                    float  y1     = (float)Math.Sin(radian);

                    // Don't let magnitude exceed the unit circle
                    if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) > 1.0)
                    {
                        x = x1;
                        y = y1;
                    }
                }

                state.SetAnalog("x", x, 0);
                state.SetAnalog("y", y, 0);

                return(state.Build());
            }
            else if (packet.Length == 2)
            {
                ControllerStateBuilder state = new ControllerStateBuilder();

                state.SetAnalog("paddle", packet[0] / 256.0f, packet[0]);
                state.SetButton("1", packet[1] != 0);

                return(state.Build());
            }
            else if (packet.Length == 4)
            {
                ControllerStateBuilder state = new ControllerStateBuilder();

                sbyte x = (sbyte)packet[0];
                sbyte y = (sbyte)packet[1];

                if (Math.Abs(x) != 0 && Math.Abs(x) > maxX)
                {
                    maxX = Math.Abs(x);
                }

                if (Math.Abs(y) != 0 && Math.Abs(y) > maxY)
                {
                    maxY = Math.Abs(y);
                }

                SignalTool.SetMouseProperties(-1.0f * x / maxX, y / maxY, x, y, state);

                state.SetButton("1", packet[2] != 0);
                state.SetButton("2", packet[3] != 0);

                return(state.Build());
            }
            else
            {
                return(null);
            }
        }
コード例 #26
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

            polishedPacket[0] = 0;
            byte j = 8;

            for (byte i = 0; i < 8; ++i)
            {
                j--;
                polishedPacket[0] |= (byte)((packet[i] == 0 ? 0 : 1) << j);
            }

            if (polishedPacket[0] != 0x02 && polishedPacket[0] != 0x16 && polishedPacket[0] != 0xFF)
            {
                return(null);
            }

            var state = new ControllerStateBuilder();

            if (polishedPacket[0] != 0xFF)
            {
                for (byte i = 0; i < 16; ++i)
                {
                    polishedPacket[i + 1] = packet[i + 8] == 0 ? (byte)1 : (byte)0;
                }

                byte numExtraBytes = 0;
                if (polishedPacket[0] == 0x16)
                {
                    numExtraBytes = 4;
                }

                for (int i = 0; i < numExtraBytes; ++i)
                {
                    polishedPacket[17 + i] = 0;
                    j = 8;
                    for (byte k = 0; k < 8; ++k)
                    {
                        j--;
                        polishedPacket[17 + i] |= (byte)((packet[24 + (i * 8 + k)] == 0 ? 0 : 1) << j);
                    }
                }


                if (polishedPacket.Length < POLISHED_PACKET_SIZE)
                {
                    return(null);
                }

                for (int i = 0; i < BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS[i]))
                    {
                        continue;
                    }
                    state.SetButton(BUTTONS[i], polishedPacket[i] != 0x00);
                }

                if (polishedPacket[0] == 0x16)
                {
                    state.SetAnalog("lstick_x", readStick(polishedPacket[17]));
                    state.SetAnalog("lstick_y", readStick(polishedPacket[18]));
                    state.SetAnalog("trig_r", readTrigger(polishedPacket[19]));
                    state.SetAnalog("trig_l", readTrigger(polishedPacket[20]));
                }
                else
                {
                    state.SetAnalog("lstick_x", readStick(128));
                    state.SetAnalog("lstick_y", readStick(128));
                    state.SetAnalog("trig_r", readTrigger(0));
                    state.SetAnalog("trig_l", readTrigger(0));
                }
            }
            else
            {
                for (int i = 0; i < BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS[i]))
                    {
                        continue;
                    }
                    state.SetButton(BUTTONS[i], false);
                }

                for (int i = 0; i < MOUSE_BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(MOUSE_BUTTONS[i]))
                    {
                        continue;
                    }
                    state.SetButton(MOUSE_BUTTONS[i], packet[i + 8] != 0x00);
                }

                state.SetAnalog("lstick_x", readStick(128));
                state.SetAnalog("lstick_y", readStick(128));
                state.SetAnalog("trig_r", readTrigger(0));
                state.SetAnalog("trig_l", readTrigger(0));

                byte xVal = 0;
                j = 8;
                for (byte k = 0; k < 8; ++k)
                {
                    j--;
                    xVal |= (byte)((packet[16 + k] == 0 ? 0 : 1) << j);
                }
                byte yVal = 0;
                j = 8;
                for (byte k = 0; k < 8; ++k)
                {
                    j--;
                    yVal |= (byte)((packet[24 + k] == 0 ? 0 : 1) << j);
                }

                float x = readMouse(packet[11] != 0, packet[9] != 0, xVal);
                float y = readMouse(packet[10] != 0, packet[8] != 0, yVal);

                SignalTool.SetMouseProperties(x, y, state);
            }

            return(state.Build());
        }
コード例 #27
0
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length != 46 && packet.Length != 50)
            {
                return(null);
            }

            byte[] data            = new byte[1024];
            byte[] unencryptedData = new byte[1024];

            int j        = 2;
            int numBytes = 0;

            for (int i = 0; i < (packet.Length / 2) - 1; ++i)
            {
                numBytes++;
                data[i] = (byte)(packet[j] | (packet[j + 1] >> 4));
                j      += 2;
            }

            if (packet[1] != 0xFF && packet[1] != encryptionKeySet)
            {
                for (j = 0; j < 10; j++)
                {
                    wm_rand[9 - j] = data[j];
                }
                for (j = 0; j < 6; j++)
                {
                    wm_key[5 - j] = data[10 + j];
                }
                if (!Wm_gentabs())
                {
                    ControllerStateBuilder outState = new ControllerStateBuilder();

                    outState.SetButton("lock", true);
                    outState.SetButton("disconnect", false);

                    return(outState.Build());
                }
                encryptionKeySet = packet[1];
            }
            else if (packet[1] == 0xFF)
            {
                encryptionKeySet = 0xFF;
            }

            for (int i = 16; i < numBytes; ++i)
            {
                if (encryptionKeySet != 255)
                {
                    unencryptedData[i - 16] = (byte)((data[i] ^ wm_sb[(i - 16) % 8]) + wm_ft[(i - 16) % 8]);
                }
                else
                {
                    unencryptedData[i - 16] = data[i];
                }
            }

            if (packet[0] == 0)  // Nunchuck
            {
                ControllerStateBuilder outState = new ControllerStateBuilder();

                byte   stickX = unencryptedData[0];
                byte   stickY = unencryptedData[1];
                ushort aX     = (ushort)((unencryptedData[2] << 2) | ((unencryptedData[5] & 0b0000000000001100) >> 2));
                ushort ay     = (ushort)((unencryptedData[3] << 2) | ((unencryptedData[5] & 0b0000000000110000) >> 2));
                ushort az     = (ushort)((unencryptedData[4] << 2) | ((unencryptedData[5] & 0b0000000011000000) >> 2));

                outState.SetButton("c", (unencryptedData[5] & 0b00000010) == 0);
                outState.SetButton("z", (unencryptedData[5] & 0b00000001) == 0);

                outState.SetAnalog("stick_x", (stickX - 128.0f) / 128.0f);
                outState.SetAnalog("stick_y", (stickY - 128.0f) / 128.0f);

                outState.SetButton("disconnect", false);
                outState.SetButton("lock", false);

                return(outState.Build());
            }
            else if (packet[0] == 1) // Classic Controller
            {
                if ((unencryptedData[4] & 0b00000001) == 0)
                {
                    return(null);
                }

                ControllerStateBuilder outState = new ControllerStateBuilder();

                byte rightTrigger = (byte)(unencryptedData[3] & 0b00011111);
                byte leftTrigger  = (byte)(((unencryptedData[3] & 0b11100000) >> 5) | ((unencryptedData[2] & 0b01100000) >> 2));

                byte leftX = (byte)(unencryptedData[0] & 0b00111111);
                byte leftY = (byte)(unencryptedData[1] & 0b00111111);

                byte rightX = (byte)(((unencryptedData[2] & 0b10000000) >> 7) | ((unencryptedData[1] & 0b11000000) >> 5) | ((unencryptedData[0] & 0b11000000) >> 3));
                byte rightY = (byte)(unencryptedData[2] & 0b00011111);

                outState.SetButton("up", (unencryptedData[5] & ~0xFE) == 0);
                outState.SetButton("right", (unencryptedData[4] & ~0x7F) == 0);
                outState.SetButton("down", (unencryptedData[4] & ~0xBF) == 0);
                outState.SetButton("left", (unencryptedData[5] & ~0xFD) == 0);

                outState.SetButton("b", (unencryptedData[5] & ~0xBF) == 0);
                outState.SetButton("a", (unencryptedData[5] & ~0xEF) == 0);
                outState.SetButton("y", (unencryptedData[5] & 0b00100000) == 0);
                outState.SetButton("x", (unencryptedData[5] & 0b00001000) == 0);

                outState.SetButton("select", (unencryptedData[4] & ~0xEF) == 0);
                outState.SetButton("home", (unencryptedData[4] & 0b00001000) == 0);
                outState.SetButton("start", (unencryptedData[4] & ~0xFB) == 0);

                outState.SetButton("l", (unencryptedData[4] & 0b00100000) == 0);
                outState.SetButton("r", (unencryptedData[4] & 0b00000010) == 0);

                outState.SetAnalog("l_trig", leftTrigger / 31.0f);
                outState.SetAnalog("r_trig", rightTrigger / 31.0f);

                outState.SetButton("zl", (unencryptedData[5] & 0b10000000) == 0);
                outState.SetButton("zr", (unencryptedData[5] & 0b00000100) == 0);

                outState.SetAnalog("lstick_x", (leftX - 32.0f) / 32.0f);
                outState.SetAnalog("lstick_y", (leftY - 32.0f) / 32.0f);
                outState.SetAnalog("rstick_x", (rightX - 15.0f) / 15.0f);
                outState.SetAnalog("rstick_y", (rightY - 15.0f) / 15.0f);

                outState.SetButton("disconnect", false);
                outState.SetButton("lock", false);

                return(outState.Build());
            }
            else if (packet[0] == 2) // Unknown and its 6 bytes
            {
                ControllerStateBuilder outState = new ControllerStateBuilder();

                byte rightTrigger = (byte)(unencryptedData[3] & 0b00011111);
                byte leftTrigger  = (byte)(((unencryptedData[3] & 0b11100000) >> 5) | ((unencryptedData[2] & 0b01100000) >> 2));

                byte leftX = (byte)(unencryptedData[0] & 0b00111111);
                byte leftY = (byte)(unencryptedData[1] & 0b00111111);

                byte rightX = (byte)(((unencryptedData[2] & 0b10000000) >> 7) | ((unencryptedData[1] & 0b11000000) >> 5) | ((unencryptedData[0] & 0b11000000) >> 3));
                byte rightY = (byte)(unencryptedData[2] & 0b00011111);

                outState.SetButton("up", (unencryptedData[5] & ~0xFE) == 0);
                outState.SetButton("right", (unencryptedData[4] & ~0x7F) == 0);
                outState.SetButton("down", (unencryptedData[4] & ~0xBF) == 0);
                outState.SetButton("left", (unencryptedData[5] & ~0xFD) == 0);

                outState.SetButton("b", (unencryptedData[5] & ~0xBF) == 0);
                outState.SetButton("a", (unencryptedData[5] & ~0xEF) == 0);
                outState.SetButton("y", (unencryptedData[5] & 0b00100000) == 0);
                outState.SetButton("x", (unencryptedData[5] & 0b00001000) == 0);

                outState.SetButton("select", (unencryptedData[4] & ~0xEF) == 0);
                outState.SetButton("home", (unencryptedData[4] & 0b00001000) == 0);
                outState.SetButton("start", (unencryptedData[4] & ~0xFB) == 0);

                outState.SetButton("l", (unencryptedData[4] & 0b00100000) == 0);
                outState.SetButton("r", (unencryptedData[4] & 0b00000010) == 0);

                outState.SetAnalog("l_trig", leftTrigger / 31.0f);
                outState.SetAnalog("r_trig", rightTrigger / 31.0f);

                outState.SetButton("zl", (unencryptedData[5] & 0b10000000) == 0);
                outState.SetButton("zr", (unencryptedData[5] & 0b00000100) == 0);

                outState.SetAnalog("lstick_x", (leftX - 32.0f) / 32.0f);
                outState.SetAnalog("lstick_y", (leftY - 32.0f) / 32.0f);
                outState.SetAnalog("rstick_x", (rightX - 15.0f) / 15.0f);
                outState.SetAnalog("rstick_y", (rightY - 15.0f) / 15.0f);

                outState.SetButton("disconnect", false);
                outState.SetButton("lock", false);

                byte   stickX = unencryptedData[0];
                byte   stickY = unencryptedData[1];
                ushort aX     = (ushort)((unencryptedData[2] << 2) | ((unencryptedData[5] & 0b0000000000001100) >> 2));
                ushort ay     = (ushort)((unencryptedData[3] << 2) | ((unencryptedData[5] & 0b0000000000110000) >> 2));
                ushort az     = (ushort)((unencryptedData[4] << 2) | ((unencryptedData[5] & 0b0000000011000000) >> 2));

                outState.SetButton("c", (unencryptedData[5] & 0b00000010) == 0);
                outState.SetButton("z", (unencryptedData[5] & 0b00000001) == 0);

                outState.SetAnalog("stick_x", (stickX - 128.0f) / 128.0f);
                outState.SetAnalog("stick_y", (stickY - 128.0f) / 128.0f);

                return(outState.Build());
            }
            else if (packet[0] == 3) //8BitDo GBros. Adapter
            {
                if ((unencryptedData[6] & 0b00000001) == 0)
                {
                    return(null);
                }

                ControllerStateBuilder outState = new ControllerStateBuilder();

                byte rightTrigger = unencryptedData[5];
                byte leftTrigger  = unencryptedData[4];

                byte leftX = unencryptedData[0];
                byte leftY = unencryptedData[2];

                byte rightX = unencryptedData[1];
                byte rightY = unencryptedData[3];

                outState.SetButton("up", (unencryptedData[7] & ~0xFE) == 0);
                outState.SetButton("right", (unencryptedData[6] & ~0x7F) == 0);
                outState.SetButton("down", (unencryptedData[6] & ~0xBF) == 0);
                outState.SetButton("left", (unencryptedData[7] & ~0xFD) == 0);

                outState.SetButton("b", (unencryptedData[7] & ~0xBF) == 0);
                outState.SetButton("a", (unencryptedData[7] & ~0xEF) == 0);
                outState.SetButton("y", (unencryptedData[7] & 0b00100000) == 0);
                outState.SetButton("x", (unencryptedData[7] & 0b00001000) == 0);

                outState.SetButton("select", (unencryptedData[6] & ~0xEF) == 0);
                outState.SetButton("home", (unencryptedData[6] & 0b00001000) == 0);
                outState.SetButton("start", (unencryptedData[6] & ~0xFB) == 0);

                outState.SetButton("l", (unencryptedData[6] & 0b00100000) == 0);
                outState.SetButton("r", (unencryptedData[6] & 0b00000010) == 0);

                outState.SetAnalog("l_trig", leftTrigger / 255.0f);
                outState.SetAnalog("r_trig", rightTrigger / 255.0f);

                outState.SetButton("zl", (unencryptedData[7] & 0b10000000) == 0);
                outState.SetButton("zr", (unencryptedData[7] & 0b00000100) == 0);

                outState.SetAnalog("lstick_x", (leftX - 128.0f) / 128.0f);
                outState.SetAnalog("lstick_y", (leftY - 128.0f) / 128.0f);
                outState.SetAnalog("rstick_x", (rightX - 128.0f) / 128.0f);
                outState.SetAnalog("rstick_y", (rightY - 128.0f) / 128.0f);

                outState.SetButton("disconnect", false);
                outState.SetButton("lock", false);

                return(outState.Build());
            }

            return(null);
        }
コード例 #28
0
        public ControllerState Process(ControllerState state)
        {
            if (!ButtonEnabled && !AnalogEnabled && !MassEnabled)
            {
                return(state);
            }
            bool revert   = false;
            bool filtered = false;

            _states.RemoveAt(0);  // move by
            _states.Add(state);   // one frame
            ControllerStateBuilder filteredStateBuilder = new ControllerStateBuilder();

            {
                uint massCounter = 0;
                foreach (var button in _states[0].Buttons.Keys)
                {
                    filteredStateBuilder.SetButton(button, _states[2].Buttons[button]);

                    if (ButtonEnabled)
                    {
                        // previous previous frame    equals      current frame
                        if (_states[0].Buttons[button] == _states[2].Buttons[button] &&
                            // AND current frame       not equals    previous frame
                            _states[2].Buttons[button] != _states[1].Buttons[button])
                        {
                            filteredStateBuilder.SetButton(button, false); // if noisy, we turn the button off
                            filtered = true;
                        }
                    }
                    if (MassEnabled)
                    {
                        if (_states[2].Buttons[button])
                        {
                            massCounter++;
                        }
                    }
                }

                foreach (var button in _states[0].Analogs.Keys)
                {
                    filteredStateBuilder.SetAnalog(button, _states[2].Analogs[button]);
                    if (MassEnabled)
                    {
                        if (Math.Abs(Math.Abs(_states[2].Analogs[button]) - Math.Abs(_states[1].Analogs[button])) > 0.3)
                        {
                            massCounter++;
                        }
                    }
                    if (AnalogEnabled)
                    {
                        // If we traveled over 0.5 Analog between the last three frames
                        // but less than 0.1 in the frame before
                        // we drop the change for this input
                        if (Math.Abs(_states[2].Analogs[button] - _states[1].Analogs[button]) > .5f &&
                            Math.Abs(_states[1].Analogs[button] - _states[0].Analogs[button]) < 0.1f)
                        {
                            filteredStateBuilder.SetAnalog(button, _lastUnfiltered.Analogs[button]);
                            filtered = true;
                        }
                    }
                }
                // if over 80% of the buttons are used we revert (this is either a reset button combo or a blink)
                if (massCounter > (_states[0].Analogs.Count + _states[0].Buttons.Count) * 0.8)
                {
                    revert = true;
                }
            }

            if (revert)
            {
                return(_lastUnfiltered);
            }
            if (filtered)
            {
                return(filteredStateBuilder.Build());
            }
            _lastUnfiltered = _states[2];
            return(_states[2]);
        }
コード例 #29
0
ファイル: C64mini.cs プロジェクト: retrospy/RetroSpy
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

            for (int i = 40; i < 56; ++i)
            {
                polishedPacket[i - 40] = (byte)((packet[i] == 0x31) ? 1 : 0);
            }

            for (int i = 0; i < 2; ++i)
            {
                polishedPacket[16 + i] = 0;
                for (byte j = 0; j < 8; ++j)
                {
                    polishedPacket[16 + i] |= (byte)((packet[0 + (i * 8) + j] == 0x30 ? 0 : 1) << j);
                }
            }

            ControllerStateBuilder outState = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                outState.SetButton(BUTTONS[i], polishedPacket[i] != 0x00);
            }

            outState.SetButton("left", polishedPacket[16] < 0x7f);
            outState.SetButton("right", polishedPacket[16] > 0x7f);
            outState.SetButton("up", polishedPacket[17] < 0x7f);
            outState.SetButton("down", polishedPacket[17] > 0x7f);

            float x = 0;
            float y = 0;

            if (polishedPacket[16] > 0x7f)
            {
                x = 1;
            }
            else if (polishedPacket[16] < 0x7f)
            {
                x = -1;
            }

            if (polishedPacket[17] > 0x7f)
            {
                y = -1;
            }
            else if (polishedPacket[17] < 0x7f)
            {
                y = 1;
            }

            if (y != 0 || x != 0)
            {
                // point on the unit circle at the same angle
                double radian = Math.Atan2(y, x);
                float  x1     = (float)Math.Cos(radian);
                float  y1     = (float)Math.Sin(radian);

                // Don't let magnitude exceed the unit circle
                if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) > 1.0)
                {
                    x = x1;
                    y = y1;
                }
            }

            outState.SetAnalog("x", x, polishedPacket[16]);
            outState.SetAnalog("y", y, polishedPacket[17]);

            return(outState.Build());
        }
コード例 #30
0
ファイル: NeoGeoMini.cs プロジェクト: edru/RetroSpy
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder outState = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                outState.SetButton(BUTTONS[i], packet[i] == 0x31);
            }

            int position = 0;

            for (byte j = 0; j < 4; ++j)
            {
                position |= (byte)((packet[16 + j] == 0x30 ? 0 : 1) << j);
            }

            float x = 0;
            float y = 0;

            switch (position)
            {
            case 0:
                outState.SetButton("up", true);
                y = -1;
                outState.SetButton("down", false);
                outState.SetButton("left", false);
                outState.SetButton("right", false);
                break;

            case 1:
                outState.SetButton("up", true);
                y = -1;
                outState.SetButton("right", true);
                x = 1;
                outState.SetButton("down", false);
                outState.SetButton("left", false);
                break;

            case 2:
                outState.SetButton("right", true);
                x = 1;
                outState.SetButton("down", false);
                outState.SetButton("left", false);
                outState.SetButton("up", false);
                break;

            case 3:
                outState.SetButton("right", true);
                x = 1;
                outState.SetButton("down", true);
                y = 1;
                outState.SetButton("up", false);
                outState.SetButton("left", false);
                break;

            case 4:
                outState.SetButton("down", true);
                y = 1;
                outState.SetButton("up", false);
                outState.SetButton("left", false);
                outState.SetButton("right", false);
                break;

            case 5:
                outState.SetButton("left", true);
                x = -1;
                outState.SetButton("down", true);
                y = 1;
                outState.SetButton("right", false);
                outState.SetButton("up", false);
                break;

            case 6:
                outState.SetButton("right", false);
                outState.SetButton("down", false);
                outState.SetButton("up", false);
                outState.SetButton("left", true);
                x = -1;
                break;

            case 7:
                outState.SetButton("up", true);
                y = -1;
                outState.SetButton("left", true);
                x = -1;
                outState.SetButton("right", false);
                outState.SetButton("down", false);
                break;

            default:
                outState.SetButton("up", false);
                outState.SetButton("left", false);
                outState.SetButton("right", false);
                outState.SetButton("down", false);
                break;
            }

            if (y != 0 || x != 0)
            {
                // point on the unit circle at the same angle
                double radian = Math.Atan2(y, x);
                float  x1     = (float)Math.Cos(radian);
                float  y1     = (float)Math.Sin(radian);

                // Don't let magnitude exceed the unit circle
                if (Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2)) > 1.0)
                {
                    x = x1;
                    y = y1;
                }
            }

            outState.SetAnalog("lstick_x", x);
            outState.SetAnalog("lstick_y", y);

            return(outState.Build());
        }