コード例 #1
0
        private static void ProcessInputMapping(RawInputHidData data, InputMapping mapping)
        {
            var allValues = data.ValueSetStates.SelectMany(x => x).ToArray();

            // TODO: This relies on a complex process of encountering errors, encoding those errors into exceptions, and throwing exceptions
            //  On the whole, this is probably very slow.
            // We can speed up the whole thing by forgoing RawInput.Sharp and implementing it ourselves, caching device data and checking
            //  what reports each axis is under.

            foreach (var axis in mapping.Axes)
            {
                var maybeValue = TryGetNormalizedValue(allValues, axis.AxisUsage);
                if (!maybeValue.HasValue)
                {
                    continue;
                }

                var value = maybeValue.Value;
                if (axis.Invert)
                {
                    value = -value;
                }

                var inputId = new InputIdentifier(data.Device.VendorId, data.Device.ProductId, axis.GameAxis);
                sInputs[inputId] = value;

                SixAxisPlugin.Instance.ReceivedControllerInput();
            }
        }
コード例 #2
0
ファイル: MainWindow.cs プロジェクト: deshi-dw/PichuBOT
        private void OnControllerInput(RawInputData data)
        {
            if (isStarted == true && hidDropdown.SelectedIndex >= 0 && Program.Hids.ConnectedHids[hidDropdown.SelectedIndex] != null && data.Header.Type == RawInputDeviceType.Hid)
            {
                RawInputHidData hid = (RawInputHidData)data;

                Program.Gamepad.Update(hid.Hid.RawData);
                // PrintLine($"Left({Program.Gamepad.AxisLeft.rawX},{Program.Gamepad.AxisLeft.rawY})::Right({Program.Gamepad.AxisRight.rawX},{Program.Gamepad.AxisRight.rawY})");
            }
        }
コード例 #3
0
        public void Setup()
        {
            var devices   = RawInputDevice.GetDevices();
            var hid       = devices.OfType <RawInputHid>();
            var keyboards = devices.OfType <RawInputKeyboard>();
            //RawInputKeyboard keyboard1=keyboards.ElementAt(0);


            // var window1 = new RawInputReceiverWindow();
            var window2 = new RawInputReceiverWindow();


            window2.Input += (sender, e) =>
            {
                // Catch your input here!
                var data1 = e.Data;

                RawInputKeyboardData data_2 = (RawInputKeyboardData)data1;
                // messageBx.Text = data_2.Device.DeviceType.ToString();
                // X_axis.Text = data_2.Keyboard.ScanCode.ToString();
                // messageBx.Text = data.ToString();
            };
            // Register the HidUsageAndPage to watch any device.
            window1.Input += (sender, e) =>
            {
                // Catch your input here!
                data       = e.Data;
                data_1     = (RawInputHidData)data;
                joy_x      = data_1.Hid.RawData.ElementAt(4) - 128;
                joy_y      = data_1.Hid.RawData.ElementAt(5) - 128;
                joy_z      = data_1.Hid.RawData.ElementAt(2) - 128;
                axis_speed = 0.05f;
                if (data_1.Hid.RawData.ElementAt(7) == 4 || data_1.Hid.RawData.ElementAt(7) == 8)
                {
                    axis_speed = 0.1f;
                }

                // if (data_1.Hid.RawData.ElementAt(4) != 128)
                // x_axis_var += joy_x * axis_speed;


                //port.WriteLine("G1 " + "X" + (joy_x * axis_speed) + "F" + fBox.Text);
                // if (data_1.Hid.RawData.ElementAt(5) != 128)
                //  y_axis_var += joy_y * axis_speed;
                //Y_axis.Text = y_axis_var.ToString("F3");
                // if (data_1.Hid.RawData.ElementAt(2) != 128)
                //   z_axis_var += joy_z * axis_speed;
                // Z_axis.Text = z_axis_var.ToString("F3");
                //Z_axis.Text = data_1.Hid.RawData.ElementAt(6).ToString();
                if (port != null && port.IsOpen)
                {
                    int z_max = 50;
                    int y_max = 50;
                    int x_max = 50;

                    //MessageBox.Show("Joystick is online!");
                    //v.LeftMotorSpeed = (ushort)(controller.GetState().Gamepad.LeftTrigger * 255);
                    // v.RightMotorSpeed = (ushort)(controller.GetState().Gamepad.RightTrigger * 255);

                    if (joy_x != 0)
                    {
                        if (x_axis_var < x_max)
                        {
                            if (joy_x < 0)
                            {
                                axis_speed = -axis_speed;
                            }
                            //X_axis.Text = x_axis_var.ToString("F3");
                            //x_axis_var += joy_x * axis_speed;
                            x = axis_speed;
                            //x = speed_xy * x;
                            port.WriteLine("G91");
                            port.WriteLine("G0 X" + x.ToString("F3") + "F" + fBox.Text);
                            //port.WriteLine("G90");
                            x_axis_var += x;
                            X_axis.Text = x_axis_var.ToString("F3");
                        }
                    }

                    if (joy_y != 0)
                    {
                        if (y_axis_var < y_max)
                        {
                            if (joy_y > 0)
                            {
                                axis_speed = -axis_speed;
                            }
                            //y = speed_xy * y;
                            y = axis_speed;
                            port.WriteLine("G91");
                            port.WriteLine("G0 Y" + y.ToString() + "F" + fBox.Text);
                            //port.WriteLine("G90");
                            y_axis_var += y;
                            Y_axis.Text = y_axis_var.ToString("F3");
                        }
                    }

                    if (joy_z != 0)
                    {
                        if (z_axis_var < z_max)
                        {
                            // z = speed_z * z;
                            if (joy_z > 0)
                            {
                                axis_speed = -axis_speed;
                            }
                            z = axis_speed;
                            port.WriteLine("G91");
                            port.WriteLine("G0 Z" + z.ToString() + "F" + fBox.Text);
                            //port.WriteLine("G90");
                            z_axis_var += z;
                            Z_axis.Text = z_axis_var.ToString("F3");
                        }
                    }
                    // controller.SetVibration(v);
                    if (data_1.Hid.RawData.ElementAt(6) == 143) //X buttun

                    {
                        //messageBx.Text = controller.GetState().Gamepad.Buttons + "\r\n";
                        x_axis_var  = 0;
                        X_axis.Text = x_axis_var.ToString("F3");
                        port.WriteLine("G92 X0");
                    }
                    if (data_1.Hid.RawData.ElementAt(6) == 31) //Y buttun

                    {
                        //messageBx.Text = controller.GetState().Gamepad.Buttons + "\r\n";
                        y_axis_var  = 0;
                        Y_axis.Text = y_axis_var.ToString("F3");
                        port.WriteLine("G92 Y0");
                    }

                    if (data_1.Hid.RawData.ElementAt(6) == 47) //Z buttun
                    {
                        //  messageBx.Text = controller.GetState().Gamepad.Buttons + "\r\n";
                        z_axis_var  = 0;
                        Z_axis.Text = z_axis_var.ToString("F3");
                        port.WriteLine("G92 Z0");
                    }


                    if (data_1.Hid.RawData.ElementAt(6) == 79)     //A buttun

                    {
                        // messageBx.Text = controller.GetState().Gamepad.Buttons + "\r\n";
                        x_axis_var  = 0;
                        X_axis.Text = x_axis_var.ToString("F3");
                        y_axis_var  = 0;
                        Y_axis.Text = y_axis_var.ToString("F3");
                        z_axis_var  = 0;
                        Z_axis.Text = z_axis_var.ToString("F3");
                        port.WriteLine("G92 X0 Y0 Z0");
                    }
                }


                // set ports
                //port.WriteLine("G91");
                //messageBx.Text += "G91 \r\n";
                //port.WriteLine("G1 " + sendBx.Text + "F" + fBox.Text);
                // messageBx.Text += "G1 " + sendBx.Text + "\r\n";
                //port.WriteLine("G90");
                //messageBx.Text += "G90 \r\n";
                // move the axis
            };


            // RawInputDevice.RegisterDevice(HidUsageAndPage.Keyboard,  RawInputDeviceFlags.ExInputSink | RawInputDeviceFlags.NoLegacy, window2.Handle);
            //RawInputDevice.RegisterDevice(HidUsageAndPage.Joystick, RawInputDeviceFlags.ExInputSink, window1.Handle);


            //RawInputDevice.UnregisterDevice(HidUsageAndPage.Joystick);
            //RawInputDevice.UnregisterDevice(HidUsageAndPage.Keyboard);

            foreach (var device in hid)
            {
                if (device.UsageAndPage.Usage == 0x04 || device.UsageAndPage.Usage == 0x05)
                {
                    string productid = device.ProductId.ToString("X4");
                    string vendorid  = device.VendorId.ToString("X4");
                    // messageBx.Text += device.ProductName + " ::" + productid + "::" + vendorid + "\r\n";// +device.VendorId:X4+device.ProductId:X4+device.ProductName+device.ManufacturerName;
                }
            }
        }