Пример #1
0
 public void vJoySendButton(uint btn, uint cnt)
 {
     Console.WriteLine("vJoy: Sending " + btn + ", " + btn + " time(s)");
     if (this.InvokeRequired) // TODO macro this
     {
         for (int i = 0; i < cnt; i++)
         {
             this.Invoke(new Action(
                             () =>
             {
                 joystick.SetBtn(true, id, btn + 1);
             })
                         );
             Thread.Sleep(150);
             this.Invoke(new Action(
                             () =>
             {
                 joystick.SetBtn(false, id, btn + 1);
             })
                         );
             Thread.Sleep(500);
         }
     }
     else if (Program.IsMainThread)
     {
         for (int i = 0; i < cnt; i++)
         {
             joystick.SetBtn(true, id, btn + 1);
             Thread.Sleep(150);
             joystick.SetBtn(false, id, btn + 1);
             Thread.Sleep(500);
         }
     }
 }
Пример #2
0
 public override void HandleControl(string line, vJoy joystick, uint deviceId)
 {
     if (line == string.Format("{0}\r", PushMsg) || line == string.Format("{0}\r", ReleaseMsg))
     {
         joystick.SetBtn(true, deviceId, Button);
         joystick.SetBtn(false, deviceId, Button);
     }
 }
Пример #3
0
 /// <summary>
 /// Handle button push and release
 /// </summary>
 /// <param name="deviceId">Joystick (VJoy device) ID</param>
 /// <param name="line">Serial port message</param>
 /// <param name="button">Joystick button</param>
 /// <param name="pinId">Arduino prot id - used in simple protocol. Protocol message: id + state + line end symbol
 /// For example: "301\r"; pin id = 30, state = 1, line end \r</param>
 private void HandlePushButton(uint deviceId, string line, uint button, int pinId)
 {
     if (line == string.Format("{0}1\r", pinId))
     {
         _joystick.SetBtn(true, deviceId, button);
     }
     if (line == string.Format("{0}0\r", pinId))
     {
         _joystick.SetBtn(false, deviceId, button);
     }
 }
Пример #4
0
        static void SetPedalAngle()
        {
            int brakePressure = PedalDevice.GetBrakePressure();

            if (brakePressure > 5)
            {
                Joystick.SetBtn(true, JoystickID, 1);
            }
            else
            {
                Joystick.SetBtn(false, JoystickID, 1);
            }
        }
Пример #5
0
 public void SetButton(uint Index, bool Value)
 {
     if (bDeviceAcquired)
     {
         m_joystick.SetBtn(Value, m_vJoyID, Index);
     }
 }
Пример #6
0
 // Handles stream deck key presses.
 private void StreamDeckKeyPressed(object Sender, KeyEventArgs EventArgs)
 {
     lock (lockObj)
     {
         VirtualJoystick.SetBtn(EventArgs.IsDown, 1, (uint)EventArgs.Key + 1);
     }
 }
 internal void SetOutput(bool state)
 {
     if (_joystick != null)
     {
         _joystick.SetBtn(state, _vJoyId, _vJoyButtonNumber);
     }
 }
Пример #8
0
 public void NoteOn(NoteOnMessage msg)
 {
     if (msg.Velocity != 0)
     {
         Console.WriteLine("Channel: " + msg.Channel);
         Console.WriteLine("Note: " + msg.Pitch);
         Console.WriteLine("Velocity: " + msg.Velocity);
         Console.WriteLine("-------------------");
         SendNote(Channel.Channel1, msg.Pitch, (int)PadColor.FULL_GREEN);
         joystick.SetBtn(true, id, PitchToButtonNum(msg.Pitch));
     }
     else
     {
         SendNote(Channel.Channel1, msg.Pitch, (int)PadColor.FULL_ORANGE);
         joystick.SetBtn(false, id, PitchToButtonNum(msg.Pitch));
     }
 }
Пример #9
0
        private void ResetAll(uint player)
        {
            vJoy playa = player == 1 ? player1 : player2;
            bool code  = false;

            for (uint i = 1; i <= 2; i++)
            {
                code = playa.SetDiscPov(-1, player, i);
                code = playa.SetBtn(false, player, i);
            }
            for (uint i = 3; i <= 5; i++)
            {
                code = playa.SetBtn(false, player, i);
            }

            System.Threading.Thread.Sleep(50);
        }
 /// <summary>
 /// Set a button (vJoy 1..n)
 /// </summary>
 /// <param name="button">The button 1..n</param>
 /// <param name="value">true for Button Down, false for Button Up</param>
 public void SetButton(int button, bool value)
 {
     if (button < 1 || button > m_nButtons)
     {
         return;
     }
     if (value)
     {
         m_hasBtn[button].current = 1; // read back storage
         m_joy.SetBtn(true, Index, (uint)button);
     }
     else
     {
         m_hasBtn[button].current = 0; // read back storage
         m_joy.SetBtn(false, Index, (uint)button);
     }
 }
Пример #11
0
        public void SetButton(int button, bool pressed)
        {
            if (button >= maxButtons)
            {
                throw new Exception(string.Format("Maximum buttons are {0}. You need to increase number of buttons in vJoy config", maxButtons));
            }

            joystick.SetBtn(pressed, Index, (uint)button + 1);
        }
Пример #12
0
    string Joystick(dynamic input)
    {
        id       = (uint)input.pid;
        joystick = new vJoy();
        iReport  = new vJoy.JoystickState();

        if (!joystick.vJoyEnabled())
        {
            return("vJoy driver not enabled: Failed Getting vJoy attributes.\n");
        }
        if (started == 0)
        {
            status = joystick.GetVJDStatus(id);
            switch (status)
            {
            case VjdStat.VJD_STAT_OWN:
            case VjdStat.VJD_STAT_FREE:
                break;

            case VjdStat.VJD_STAT_MISS:
            case VjdStat.VJD_STAT_BUSY:
            default:
                return("vJoy error\n" + status);
            }
            ;
            if (!joystick.AcquireVJD(id))
            {
                return("Cannot acquire!");
            }
            joystick.ResetVJD(id);
        }
        started = 1;
        bool   btnStatus = false;
        uint   btnId     = 0;
        string teste     = "";

        foreach (dynamic btn in input.buttons)
        {
            foreach (dynamic btnP in btn.Value)
            {
                if (btnP.Key == "bid")
                {
                    btnId = (uint)btnP.Value;
                }
                if (btnP.Key == "status")
                {
                    btnStatus = (bool)btnP.Value;
                }
            }
            joystick.SetBtn(btnStatus, id, btnId);
            teste += " " + btnId + ":(" + btnStatus + ")";
        }
        joystick.SetAxis(((int)input.axis.X), id, HID_USAGES.HID_USAGE_X);
        joystick.SetAxis(((int)input.axis.Y), id, HID_USAGES.HID_USAGE_Y);
        return("vJoy Enabled! " + id + teste);
    }
Пример #13
0
        public void SetButton(int key, int state)
        {
            bool b = false;

            if (state == 1)
            {
                b = true;
            }
            joystick.SetBtn(b, (uint)id, (uint)key);
        }
Пример #14
0
        public void set_cntr(uint id, ushort btns)
        {
            uint nbut  = 16;
            var  array = Convert.ToString(btns, 2).Select(s => s.Equals('1')).ToArray();

            for (int ind = 0; ind < nbut; ind++)
            {
                var set = (array.Length - ind - 1) < 0 ?false:array[array.Length - ind - 1];
                joystick.SetBtn(set, id, nbut - (uint)ind);
            }
        }
Пример #15
0
        private void Reset(uint player, vJoy playa)
        {
            bool code = false;

            code = playa.SetDiscPov(-1, player, 2);
            for (uint i = 1; i <= 5; i++)
            {
                code = playa.SetBtn(false, player, i);
            }

            System.Threading.Thread.Sleep(50);
        }
Пример #16
0
        static public void Setjoystick(int X, int Y, int Z, int XR, int YR, int ZR, bool aButton)
        {
            joystick.SetAxis(X, id, HID_USAGES.HID_USAGE_X);
            joystick.SetAxis(Y, id, HID_USAGES.HID_USAGE_Y);
            joystick.SetAxis(Z, id, HID_USAGES.HID_USAGE_Z);
            joystick.SetAxis(XR, id, HID_USAGES.HID_USAGE_RX);
            joystick.SetAxis(YR, id, HID_USAGES.HID_USAGE_RY);
            joystick.SetAxis(ZR, id, HID_USAGES.HID_USAGE_RZ);
            joystick.SetBtn(aButton, id, 1);

            //string values = string.Format("X:{0,6:#####}  Y:{1,6:#####}  Z:{2,6:#####}  XR:{3,6:#####}  YR:{4,6:#####}  ZR:{5,6:#####}", X,Y,Z,XR,YR,ZR);
            //Console.WriteLine(values);
        }
Пример #17
0
        private void Button_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox chk = (CheckBox)sender;

            joystick.SetBtn(chk.Checked, id, uint.Parse(chk.Tag.ToString()));
            if (chk.Checked)
            {
                chk.BackColor = Color.DarkRed;
            }
            else
            {
                chk.BackColor = Color.Red;
            }
        }
Пример #18
0
 private void FeederForm_Load(object sender, EventArgs e)
 {
     r             = new Random(DateTime.Now.Millisecond);
     DiscPovNumber = joystick.GetVJDDiscPovNumber(id);
     ContPovNumber = joystick.GetVJDContPovNumber(id);
     joystick.GetVJDAxisMax(id, HID_USAGES.HID_USAGE_X, ref maxval);
     // Reset this device to default values
     joystick.ResetVJD(id);
     X           = 20;
     Y           = 30;
     Z           = 40;
     XR          = 60;
     ZR          = 80;
     cPitch      = cRoll = cYaw = 0.5;
     desPitch    = desRoll = desYaw = 0.5;
     cThr        = 0;
     desThr      = 0;
     cConv       = 0;
     state       = ERAState.Disarmed;
     label2.Text = "НЕ ВЗВЕДЕНО";
     joystick.SetBtn(false, id, 1);
     pParam   = 2;
     waitTime = 1.5;
 }
Пример #19
0
 public void SetButtonState(uint id, string button, int value)
 {
     if (button.Contains("Buttons"))
     {
         vjoy.SetBtn(value > 0, id, (uint)RLocalInput.MapButtonToId[button] + 1);
     }
     else if (button.Contains("PointOfViewControllers"))
     {
         vjoy.SetContPov(value, id, 1);
     }
     else
     {
         var axis = MapButtonToAxis[button];
         vjoy.SetAxis(value / 2, id, axis);
     }
 }
Пример #20
0
        private void timer_joystick_output_Tick(object sender, EventArgs e)
        {
            getGameInfo();       //get location/speed from the game
            updatebuttonstate(); //just sets the boxes next to the labels as red or green
            //control the virtual joystick

            res = joystick.SetAxis(tb_steering.Maximum - tb_steering.Value, id, HID_USAGES.HID_USAGE_X);
            //res = joystick.SetAxis(tb_speed.Maximum - tb_speed.Value, id, HID_USAGES.HID_USAGE_Y);//if you want to control speed, this could also be used
            //res = joystick.SetAxis(tb_speed.Maximum - tb_speed.Value, id, HID_USAGES.HID_USAGE_Z);
            //set button state
            res = joystick.SetBtn(b1, id, 1);
            res = joystick.SetBtn(b2, id, 2);
            res = joystick.SetBtn(b3, id, 3);
            res = joystick.SetBtn(b4, id, 4);
            res = joystick.SetBtn(b5, id, 5);
            res = joystick.SetBtn(b6, id, 6);
            res = joystick.SetBtn(b7, id, 7);
            res = joystick.SetBtn(b8, id, 8);
        }
Пример #21
0
        public void SetButton(int button, bool pressed)
        {
            if (button < 0)
            {
                return;
            }

            if (button > 999)
            {
                int numpov    = button / 1000 - 1;
                int direction = pressed ? button % 1000: -1;
                SetDirectionalPov(numpov, direction);
                return;
            }
            if (button >= maxButtons)
            {
                throw new Exception(
                          $"Maximum buttons are {maxButtons}. You need to increase number of buttons in vJoy config");
            }
            if (!modeDiffered)
            {
                _joystick.SetBtn(pressed, Index, (uint)(button) + 1);
            }
            else
            {
                switch (button / 32)
                {
                case 0:
                    setButtonBuffer(ref report.Buttons, button, pressed);
                    break;

                case 1:
                    setButtonBuffer(ref report.ButtonsEx1, button, pressed);
                    break;

                case 2:
                    setButtonBuffer(ref report.ButtonsEx2, button, pressed);
                    break;

                case 3:
                    setButtonBuffer(ref report.ButtonsEx3, button, pressed);
                    break;
                }
            }
        }
Пример #22
0
        // this code runs when a message was received
        void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            string ReceivedMessage = Encoding.UTF8.GetString(e.Message);

            writeMessage(e.Topic);
            writeMessage(ReceivedMessage);

            dynamic stuff = JsonConvert.DeserializeObject(ReceivedMessage);

            if (!(stuff is long) && joystick != null)
            {
                String type = stuff["type"];
                switch (type.ToLower())
                {
                case "b":
                    UInt32 bi = stuff["index"];
                    if (bi <= nButtons)
                    {
                        joystick.SetBtn(true, id, bi);
                    }
                    break;

                case "j":
                    HID_USAGES hv;
                    String     index = stuff["index"];
                    Int32      value = stuff["value"];
                    if (value > maxval)
                    {
                        //value = Convert.ToInt32(maxval);
                    }
                    if (Enum.TryParse(index, out hv))
                    {
                        if (Enum.IsDefined(typeof(HID_USAGES), hv))
                        {
                            joystick.SetAxis(value, id, hv);
                        }
                    }
                    break;

                default:
                    break;
                }
            }
        }
Пример #23
0
        public static void Feed(vJoy vjoy, Configuration config, EDStatusMonitor monitor)
        {
            // copy configuration and status to prevent changes from taking effect during the feed process
            uint deviceId = (uint)config.DeviceId;
            Dictionary <string, int>  buttonIds   = config.ButtonConfigurationCopy;
            Dictionary <string, bool> statusFlags = monitor.StatusCopy;

            if (!vjoy.AcquireVJD(deviceId))
            {
                throw new ButtonFeederException($"vJoy error: Unable to acquire vJoy device #{deviceId}");
            }

            foreach (string flag in statusFlags.Keys)
            {
                bool status   = statusFlags[flag];
                char buttonId = (char)buttonIds[flag];
                vjoy.SetBtn(status, deviceId, buttonId);
            }

            vjoy.RelinquishVJD(deviceId);
        }
Пример #24
0
        private static void Input_ChannelMessageReceived(object sender, ChannelMessageEventArgs e)
        {
            Console.WriteLine("Input: MIDI, Channel: " + e.Message.MidiChannel + " Data1: " + e.Message.Data1 + " Data2: " + e.Message.Data2);

            var target = stat_mapping.GetJoyTarget(e.Message.MidiChannel, e.Message.Data1);

            if (target != null)
            {
                if (target.Btn != 0)
                {
                    bool state = e.Message.Data2 != 0;
                    Console.WriteLine("Output: VJoy " + target.JoyId + ", set button " + target.Btn + " = " + state);
                    stat_vJoy.SetBtn(state, target.JoyId, target.Btn);
                }
                else                 // axis
                {
                    int hidValue = (int)Math.Round(e.Message.Data2 * MidiToAxisFactor);
                    Console.WriteLine("Output: VJoy " + target.JoyId + ", set axis " + target.Axis + " = " + hidValue);
                    stat_vJoy.SetAxis(hidValue, target.JoyId, target.Axis);
                }
            }
        }
Пример #25
0
        private void SetGamepadItem(byte[] values, int index, GamepadConfigurationItem config)
        {
            double value = values[index];

            if (config.Type == GamepadItemType.Axis)
            {
                long maxAxisValue = 0;
                var  targetAxis   = config.TargetUsage ?? HID_USAGES.HID_USAGE_X;
                _vJoy.GetVJDAxisMax(_gamepadId, targetAxis, ref maxAxisValue);
                value = NormalizeAxis((byte)value, config.ConvertAxis ?? false);

                if (config.InvertAxis ?? false)
                {
                    value = InvertNormalizedAxis(value);
                }

                _vJoy.SetAxis((int)(value * maxAxisValue), _gamepadId, targetAxis);
            }
            else if (config.Type == GamepadItemType.Button)
            {
                _vJoy.SetBtn(ConvertToButtonState((byte)value), _gamepadId, config.TargetButtonId ?? 0);
            }
        }
Пример #26
0
        public void ButtonStateReceived(List <State> states)
        {
            //When usb was disconnected it was losing the VJD
            if (joystick != null)
            {
                VjdStat status = joystick.GetVJDStatus(jID);

                if (status != VjdStat.VJD_STAT_OWN)
                {
                    joystick.AcquireVJD(jID);
                }

                //setAxis(states);

                for (uint i = AXIS_OFFSET; i < states.Count; i++)
                {
                    //if(states[(int)i] == State.KeyDown || states[(int)i] == State.KeyHold)
                    //{
                    //    Debug.WriteLine(i + "==" + states[(int)i].ToString());
                    //}
                    joystick.SetBtn(states[(int)i] == State.KeyDown || states[(int)i] == State.KeyHold, jID, i + 1 - AXIS_OFFSET);
                }
            }
        }
        private void OnKeyPressed(object sender, RawInputEventArg e)
        {
            if (detecting)
            {
                if (!keyboards.ContainsKey(e.KeyPressEvent.DeviceHandle))
                {
                    keyboards.Add(e.KeyPressEvent.DeviceHandle, new Keyboard(e.KeyPressEvent.DeviceHandle, e.KeyPressEvent.DeviceName));
                    comboBox1.Items.Add(e.KeyPressEvent.DeviceHandle);
                }
                vJoyBox.CheckedChanged -= vJoyBox_CheckedChanged;
                vJoyBox.Checked         = keyboards[e.KeyPressEvent.DeviceHandle].vJoyEnabled;
                vJoyBox.CheckedChanged += vJoyBox_CheckedChanged;

                LSAutorunBox.CheckedChanged -= LSAutorunBox_CheckedChanged;
                LSAutorunBox.Checked         = keyboards[e.KeyPressEvent.DeviceHandle].LSAutoRun;
                LSAutorunBox.CheckedChanged += LSAutorunBox_CheckedChanged;

                //labelCurrentKb.Text = $"Current Keyboard: {e.KeyPressEvent.DeviceHandle.ToString()}";
                currentHandle = e.KeyPressEvent.DeviceHandle;

                comboBox1.SelectedItem = currentHandle;

                /*
                 * inputBox_A.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.A) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.A] : "";
                 * inputBox_B.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.B) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.B] : "";
                 * inputBox_X.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.X) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.X] : "";
                 * inputBox_Y.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.Y) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.Y] : "";
                 * inputBox_L.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.Y) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.L] : "";
                 * inputBox_R.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.Y) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.R] : "";
                 * inputBox_Z.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.Y) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.Z] : "";
                 * inputBox_START.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.Y) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.START] : "";
                 *
                 * inputBox_LSUP.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.LSUP) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.LSUP] : "";
                 * inputBox_LSDOWN.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.LSDOWN) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.LSDOWN] : "";
                 * inputBox_LSLEFT.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.LSLEFT) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.LSLEFT] : "";
                 * inputBox_LSRIGHT.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.LSRIGHT) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.LSRIGHT] : "";
                 * inputBox_LSMOD.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.LSMOD) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.LSMOD] : "";
                 *
                 * inputBox_RSUP.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.RSUP) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.RSUP] : "";
                 * inputBox_RSDOWN.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.RSDOWN) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.RSDOWN] : "";
                 * inputBox_RSLEFT.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.RSLEFT) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.RSLEFT] : "";
                 * inputBox_RSRIGHT.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.RSRIGHT) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.RSRIGHT] : "";
                 *
                 * inputBox_DUP.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.DUP) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.DUP] : "";
                 * inputBox_DDOWN.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.DDOWN) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.DDOWN] : "";
                 * inputBox_DLEFT.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.DLEFT) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.DLEFT] : "";
                 * inputBox_DRIGHT.Text = keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsKey(ButtonEnum.DRIGHT) ? keyboards[e.KeyPressEvent.DeviceHandle].binds[ButtonEnum.DRIGHT] : "";
                 *
                 * LSAutorunBox.Checked = keyboards[e.KeyPressEvent.DeviceHandle].LSAutoRun;
                 * LSAutorunBox.Checked = keyboards[e.KeyPressEvent.DeviceHandle].RSAutoRun;
                 */
                detecting = false;
            }
            label14.Text = $"{e.KeyPressEvent.DeviceHandle.ToString()}_{e.KeyPressEvent.VKeyName}_{(e.KeyPressEvent.Message == 256 ? "PRESS" : "RELEASE") }";
            if (focusedBox != null)
            {
                if (focusedBox == inputBox_LSUP)
                {
                    keyboards[currentHandle].binds[ButtonEnum.LSUP] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_LSDOWN)
                {
                    keyboards[currentHandle].binds[ButtonEnum.LSDOWN] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_LSLEFT)
                {
                    keyboards[currentHandle].binds[ButtonEnum.LSLEFT] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_LSRIGHT)
                {
                    keyboards[currentHandle].binds[ButtonEnum.LSRIGHT] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_LSMOD)
                {
                    keyboards[currentHandle].binds[ButtonEnum.LSMOD] = e.KeyPressEvent.VKeyName;
                }

                else if (focusedBox == inputBox_RSUP)
                {
                    keyboards[currentHandle].binds[ButtonEnum.RSUP] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_RSDOWN)
                {
                    keyboards[currentHandle].binds[ButtonEnum.RSDOWN] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_RSLEFT)
                {
                    keyboards[currentHandle].binds[ButtonEnum.RSLEFT] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_RSRIGHT)
                {
                    keyboards[currentHandle].binds[ButtonEnum.RSRIGHT] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_A)
                {
                    keyboards[currentHandle].binds[ButtonEnum.A] = e.KeyPressEvent.VKeyName;
                }

                else if (focusedBox == inputBox_B)
                {
                    keyboards[currentHandle].binds[ButtonEnum.B] = e.KeyPressEvent.VKeyName;
                }

                else if (focusedBox == inputBox_X)
                {
                    keyboards[currentHandle].binds[ButtonEnum.X] = e.KeyPressEvent.VKeyName;
                }

                else if (focusedBox == inputBox_Y)
                {
                    keyboards[currentHandle].binds[ButtonEnum.Y] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_Z)
                {
                    keyboards[currentHandle].binds[ButtonEnum.Z] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_L)
                {
                    keyboards[currentHandle].binds[ButtonEnum.L] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_R)
                {
                    keyboards[currentHandle].binds[ButtonEnum.R] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_START)
                {
                    keyboards[currentHandle].binds[ButtonEnum.START] = e.KeyPressEvent.VKeyName;
                }

                else if (focusedBox == inputBox_DUP)
                {
                    keyboards[currentHandle].binds[ButtonEnum.DUP] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_DDOWN)
                {
                    keyboards[currentHandle].binds[ButtonEnum.DDOWN] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_DLEFT)
                {
                    keyboards[currentHandle].binds[ButtonEnum.DLEFT] = e.KeyPressEvent.VKeyName;
                }
                else if (focusedBox == inputBox_DRIGHT)
                {
                    keyboards[currentHandle].binds[ButtonEnum.DRIGHT] = e.KeyPressEvent.VKeyName;
                }

                focusedBox.Text = e.KeyPressEvent.VKeyName;
                focusedBox      = null;
                label1.Focus();
            }
            else
            {
                if (keyboards.ContainsKey(e.KeyPressEvent.DeviceHandle))
                {
                    if (keyboards[e.KeyPressEvent.DeviceHandle].binds.ContainsValue(e.KeyPressEvent.VKeyName))
                    {
                        var button = keyboards[e.KeyPressEvent.DeviceHandle].binds.FirstOrDefault(x => x.Value == e.KeyPressEvent.VKeyName).Key;
                        switch (button)
                        {
                        case ButtonEnum.A:
                            virtualJoy.SetBtn(e.KeyPressEvent.Message == 256, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, 1);
                            break;

                        case ButtonEnum.B:
                            virtualJoy.SetBtn(e.KeyPressEvent.Message == 256, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, 2);
                            break;

                        case ButtonEnum.X:
                            virtualJoy.SetBtn(e.KeyPressEvent.Message == 256, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, 3);
                            break;

                        case ButtonEnum.Y:
                            virtualJoy.SetBtn(e.KeyPressEvent.Message == 256, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, 4);
                            break;

                        case ButtonEnum.L:
                            virtualJoy.SetAxis(e.KeyPressEvent.Message == 256 ? 32768 : 0, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_Z);
                            break;

                        case ButtonEnum.R:
                            virtualJoy.SetAxis(e.KeyPressEvent.Message == 256 ? 32768 : 0, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_RZ);
                            break;

                        case ButtonEnum.Z:
                            virtualJoy.SetBtn(e.KeyPressEvent.Message == 256, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, 5);
                            break;

                        case ButtonEnum.START:
                            virtualJoy.SetBtn(e.KeyPressEvent.Message == 256, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, 8);
                            break;

                        case ButtonEnum.LSUP:
                            if (e.KeyPressEvent.Message == 256)
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].LSUP = 1;
                            }
                            else
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].LSUP = 0;
                            }
                            virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetLSY(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_Y);
                            break;

                        case ButtonEnum.LSDOWN:
                            if (e.KeyPressEvent.Message == 256)
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].LSDOWN = 1;
                            }
                            else
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].LSDOWN = 0;
                            }
                            virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetLSY(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_Y);
                            break;

                        case ButtonEnum.LSLEFT:
                            if (e.KeyPressEvent.Message == 256)
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].LSLEFT = 1;
                            }
                            else
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].LSLEFT = 0;
                            }
                            virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetLSX(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_X);
                            break;

                        case ButtonEnum.LSRIGHT:
                            if (e.KeyPressEvent.Message == 256)
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].LSRIGHT = 1;
                            }
                            else
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].LSRIGHT = 0;
                            }
                            virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetLSX(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_X);
                            break;

                        case ButtonEnum.LSMOD:
                            if (keyboards[e.KeyPressEvent.DeviceHandle].LSAutoRun)
                            {
                                if (e.KeyPressEvent.Message == 256)
                                {
                                    keyboards[e.KeyPressEvent.DeviceHandle].LSMOD = 2;
                                }
                                else
                                {
                                    keyboards[e.KeyPressEvent.DeviceHandle].LSMOD = 1;
                                }
                            }
                            else
                            {
                                if (e.KeyPressEvent.Message == 256)
                                {
                                    keyboards[e.KeyPressEvent.DeviceHandle].LSMOD = 1;
                                }
                                else
                                {
                                    keyboards[e.KeyPressEvent.DeviceHandle].LSMOD = 2;
                                }
                            }
                            break;

                        case ButtonEnum.RSUP:
                            if (e.KeyPressEvent.Message == 256)
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].RSUP = 1;
                            }
                            else
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].RSUP = 0;
                            }
                            virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetRSY(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_RY);
                            break;

                        case ButtonEnum.RSDOWN:
                            if (e.KeyPressEvent.Message == 256)
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].RSDOWN = 1;
                            }
                            else
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].RSDOWN = 0;
                            }
                            virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetRSY(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_RY);
                            break;

                        case ButtonEnum.RSLEFT:
                            if (e.KeyPressEvent.Message == 256)
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].RSLEFT = 1;
                            }
                            else
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].RSLEFT = 0;
                            }
                            virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetRSX(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_RX);
                            break;

                        case ButtonEnum.RSRIGHT:
                            if (e.KeyPressEvent.Message == 256)
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].RSRIGHT = 1;
                            }
                            else
                            {
                                keyboards[e.KeyPressEvent.DeviceHandle].RSRIGHT = 0;
                            }
                            virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetRSX(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_RX);
                            break;

                        case ButtonEnum.DUP:
                            /*
                             * if (e.KeyPressEvent.Message == 256)
                             * {
                             *  keyboards[e.KeyPressEvent.DeviceHandle].DUP = 1;
                             * }
                             * else
                             * {
                             *  keyboards[e.KeyPressEvent.DeviceHandle].DUP = 0;
                             * }
                             * virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetDY(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_SL0);
                             * break;
                             */
                            virtualJoy.SetBtn(e.KeyPressEvent.Message == 256, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, 9);
                            break;

                        case ButtonEnum.DDOWN:
                            /*
                             * if (e.KeyPressEvent.Message == 256)
                             * {
                             *  keyboards[e.KeyPressEvent.DeviceHandle].DDOWN = 1;
                             * }
                             * else
                             * {
                             *  keyboards[e.KeyPressEvent.DeviceHandle].DDOWN = 0;
                             * }
                             * virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetDY(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_SL0);
                             * break;
                             */
                            virtualJoy.SetBtn(e.KeyPressEvent.Message == 256, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, 10);
                            break;

                        case ButtonEnum.DLEFT:
                            /*
                             * if (e.KeyPressEvent.Message == 256)
                             * {
                             *  keyboards[e.KeyPressEvent.DeviceHandle].DLEFT = 1;
                             * }
                             * else
                             * {
                             *  keyboards[e.KeyPressEvent.DeviceHandle].DLEFT = 0;
                             * }
                             * virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetDX(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_SL1);
                             * break;
                             */
                            virtualJoy.SetBtn(e.KeyPressEvent.Message == 256, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, 11);
                            break;

                        case ButtonEnum.DRIGHT:
                            /*
                             * if (e.KeyPressEvent.Message == 256)
                             * {
                             *  keyboards[e.KeyPressEvent.DeviceHandle].DRIGHT = 1;
                             * }
                             * else
                             * {
                             *  keyboards[e.KeyPressEvent.DeviceHandle].DRIGHT = 0;
                             * }
                             * virtualJoy.SetAxis(keyboards[e.KeyPressEvent.DeviceHandle].GetDX(), keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, HID_USAGES.HID_USAGE_SL1);
                             */
                            virtualJoy.SetBtn(e.KeyPressEvent.Message == 256, keyboards[e.KeyPressEvent.DeviceHandle].vJoyId, 12);
                            break;
                        }
                    }
                }
            }
        }
Пример #28
0
        static void Main(string[] args)
        {
            // Create one joystick object and a position structure.
            joystick = new vJoy();
            iReport  = new vJoy.JoystickState();


            // Device ID can only be in the range 1-16
            if (args.Length > 0 && !String.IsNullOrEmpty(args[0]))
            {
                id = Convert.ToUInt32(args[0]);
            }
            if (id <= 0 || id > 16)
            {
                Console.WriteLine("Illegal device ID {0}\nExit!", id);
                return;
            }

            // Get the driver attributes (Vendor ID, Product ID, Version Number)
            if (!joystick.vJoyEnabled())
            {
                Console.WriteLine("vJoy driver not enabled: Failed Getting vJoy attributes.\n");
                return;
            }
            else
            {
                Console.WriteLine("Vendor: {0}\nProduct :{1}\nVersion Number:{2}\n", joystick.GetvJoyManufacturerString(), joystick.GetvJoyProductString(), joystick.GetvJoySerialNumberString());
            }

            // Get the state of the requested device
            VjdStat status = joystick.GetVJDStatus(id);

            switch (status)
            {
            case VjdStat.VJD_STAT_OWN:
                Console.WriteLine("vJoy Device {0} is already owned by this feeder\n", id);
                break;

            case VjdStat.VJD_STAT_FREE:
                Console.WriteLine("vJoy Device {0} is free\n", id);
                break;

            case VjdStat.VJD_STAT_BUSY:
                Console.WriteLine("vJoy Device {0} is already owned by another feeder\nCannot continue\n", id);
                return;

            case VjdStat.VJD_STAT_MISS:
                Console.WriteLine("vJoy Device {0} is not installed or disabled\nCannot continue\n", id);
                return;

            default:
                Console.WriteLine("vJoy Device {0} general error\nCannot continue\n", id);
                return;
            }
            ;

            // Check which axes are supported
            bool AxisX  = joystick.GetVJDAxisExist(id, HID_USAGES.HID_USAGE_X);
            bool AxisY  = joystick.GetVJDAxisExist(id, HID_USAGES.HID_USAGE_Y);
            bool AxisZ  = joystick.GetVJDAxisExist(id, HID_USAGES.HID_USAGE_Z);
            bool AxisRX = joystick.GetVJDAxisExist(id, HID_USAGES.HID_USAGE_RX);
            bool AxisRZ = joystick.GetVJDAxisExist(id, HID_USAGES.HID_USAGE_RZ);
            // Get the number of buttons and POV Hat switchessupported by this vJoy device
            int nButtons      = joystick.GetVJDButtonNumber(id);
            int ContPovNumber = joystick.GetVJDContPovNumber(id);
            int DiscPovNumber = joystick.GetVJDDiscPovNumber(id);

            // Print results
            Console.WriteLine("\nvJoy Device {0} capabilities:\n", id);
            Console.WriteLine("Numner of buttons\t\t{0}\n", nButtons);
            Console.WriteLine("Numner of Continuous POVs\t{0}\n", ContPovNumber);
            Console.WriteLine("Numner of Descrete POVs\t\t{0}\n", DiscPovNumber);
            Console.WriteLine("Axis X\t\t{0}\n", AxisX ? "Yes" : "No");
            Console.WriteLine("Axis Y\t\t{0}\n", AxisX ? "Yes" : "No");
            Console.WriteLine("Axis Z\t\t{0}\n", AxisX ? "Yes" : "No");
            Console.WriteLine("Axis Rx\t\t{0}\n", AxisRX ? "Yes" : "No");
            Console.WriteLine("Axis Rz\t\t{0}\n", AxisRZ ? "Yes" : "No");

            // Acquire the target
            if ((status == VjdStat.VJD_STAT_OWN) || ((status == VjdStat.VJD_STAT_FREE) && (!joystick.AcquireVJD(id))))
            {
                Console.WriteLine("Failed to acquire vJoy device number {0}.\n", id);
                return;
            }
            else
            {
                Console.WriteLine("Acquired: vJoy device number {0}.\n", id);
            }

            Console.WriteLine("\npress enter to stat feeding");
            Console.ReadKey(true);

            int  X, Y, Z, ZR, XR;
            uint count  = 0;
            long maxval = 0;

            X  = 20;
            Y  = 30;
            Z  = 40;
            XR = 60;
            ZR = 80;

            joystick.GetVJDAxisMax(id, HID_USAGES.HID_USAGE_X, ref maxval);

#if ROBUST
            bool res;
            // Reset this device to default values
            joystick.ResetVJD(id);

            // Feed the device in endless loop
            while (true)
            {
                // Set position of 4 axes
                res = joystick.SetAxis(X, id, HID_USAGES.HID_USAGE_X);
                res = joystick.SetAxis(Y, id, HID_USAGES.HID_USAGE_Y);
                res = joystick.SetAxis(Z, id, HID_USAGES.HID_USAGE_Z);
                res = joystick.SetAxis(XR, id, HID_USAGES.HID_USAGE_RX);
                res = joystick.SetAxis(ZR, id, HID_USAGES.HID_USAGE_RZ);

                // Press/Release Buttons
                res = joystick.SetBtn(true, id, count / 50);
                res = joystick.SetBtn(false, id, 1 + count / 50);

                // If Continuous POV hat switches installed - make them go round
                // For high values - put the switches in neutral state
                if (ContPovNumber > 0)
                {
                    if ((count * 70) < 30000)
                    {
                        res = joystick.SetContPov(((int)count * 70), id, 1);
                        res = joystick.SetContPov(((int)count * 70) + 2000, id, 2);
                        res = joystick.SetContPov(((int)count * 70) + 4000, id, 3);
                        res = joystick.SetContPov(((int)count * 70) + 6000, id, 4);
                    }
                    else
                    {
                        res = joystick.SetContPov(-1, id, 1);
                        res = joystick.SetContPov(-1, id, 2);
                        res = joystick.SetContPov(-1, id, 3);
                        res = joystick.SetContPov(-1, id, 4);
                    };
                }
                ;

                // If Discrete POV hat switches installed - make them go round
                // From time to time - put the switches in neutral state
                if (DiscPovNumber > 0)
                {
                    if (count < 550)
                    {
                        joystick.SetDiscPov((((int)count / 20) + 0) % 4, id, 1);
                        joystick.SetDiscPov((((int)count / 20) + 1) % 4, id, 2);
                        joystick.SetDiscPov((((int)count / 20) + 2) % 4, id, 3);
                        joystick.SetDiscPov((((int)count / 20) + 3) % 4, id, 4);
                    }
                    else
                    {
                        joystick.SetDiscPov(-1, id, 1);
                        joystick.SetDiscPov(-1, id, 2);
                        joystick.SetDiscPov(-1, id, 3);
                        joystick.SetDiscPov(-1, id, 4);
                    };
                }
                ;

                System.Threading.Thread.Sleep(20);
                X += 150; if (X > maxval)
                {
                    X = 0;
                }
                Y += 250; if (Y > maxval)
                {
                    Y = 0;
                }
                Z += 350; if (Z > maxval)
                {
                    Z = 0;
                }
                XR += 220; if (XR > maxval)
                {
                    XR = 0;
                }
                ZR += 200; if (ZR > maxval)
                {
                    ZR = 0;
                }
                count++;

                if (count > 640)
                {
                    count = 0;
                }
            } // While (Robust)
#endif // ROBUST
#if EFFICIENT
            byte[] pov = new byte[4];

            while (true)
            {
                iReport.bDevice  = (byte)id;
                iReport.AxisX    = X;
                iReport.AxisY    = Y;
                iReport.AxisZ    = Z;
                iReport.AxisZRot = ZR;
                iReport.AxisXRot = XR;

                // Set buttons one by one
                iReport.Buttons = (uint)(0x1 << (int)(count / 20));

                if (ContPovNumber > 0)
                {
                    // Make Continuous POV Hat spin
                    iReport.bHats    = (count * 70);
                    iReport.bHatsEx1 = (count * 70) + 3000;
                    iReport.bHatsEx2 = (count * 70) + 5000;
                    iReport.bHatsEx3 = 15000 - (count * 70);
                    if ((count * 70) > 36000)
                    {
                        iReport.bHats    = 0xFFFFFFFF; // Neutral state
                        iReport.bHatsEx1 = 0xFFFFFFFF; // Neutral state
                        iReport.bHatsEx2 = 0xFFFFFFFF; // Neutral state
                        iReport.bHatsEx3 = 0xFFFFFFFF; // Neutral state
                    }
                    ;
                }
                else
                {
                    // Make 5-position POV Hat spin

                    pov[0] = (byte)(((count / 20) + 0) % 4);
                    pov[1] = (byte)(((count / 20) + 1) % 4);
                    pov[2] = (byte)(((count / 20) + 2) % 4);
                    pov[3] = (byte)(((count / 20) + 3) % 4);

                    iReport.bHats = (uint)(pov[3] << 12) | (uint)(pov[2] << 8) | (uint)(pov[1] << 4) | (uint)pov[0];
                    if ((count) > 550)
                    {
                        iReport.bHats = 0xFFFFFFFF;         // Neutral state
                    }
                };

                /*** Feed the driver with the position packet - is fails then wait for input then try to re-acquire device ***/
                if (!joystick.UpdateVJD(id, ref iReport))
                {
                    Console.WriteLine("Feeding vJoy device number {0} failed - try to enable device then press enter\n", id);
                    Console.ReadKey(true);
                    joystick.AcquireVJD(id);
                }

                System.Threading.Thread.Sleep(20);
                count++;
                if (count > 640)
                {
                    count = 0;
                }

                X += 150; if (X > maxval)
                {
                    X = 0;
                }
                Y += 250; if (Y > maxval)
                {
                    Y = 0;
                }
                Z += 350; if (Z > maxval)
                {
                    Z = 0;
                }
                XR += 220; if (XR > maxval)
                {
                    XR = 0;
                }
                ZR += 200; if (ZR > maxval)
                {
                    ZR = 0;
                }
            }
            ; // While
#endif // EFFICIENT
        } // Main
Пример #29
0
        static void Main(string[] args)
        {
            SerialPort _port;

            float[] Thetas;
            joystick  = new vJoy();
            Thetas    = new float[3];
            Thetas[0] = 0;
            Thetas[1] = 0;
            Thetas[2] = 0;
            VJoyActivation();

            Console.WriteLine("insert COM port name: ");
            string COMportName = Console.ReadLine();

            Console.WriteLine("\ninsert baud rate: ");
            int baudRate = Int32.Parse(Console.ReadLine());

            _port              = new SerialPort();
            _port.PortName     = COMportName;
            _port.BaudRate     = baudRate;
            _port.Parity       = Parity.None;
            _port.DataBits     = 8;
            _port.StopBits     = StopBits.One;
            _port.ReadTimeout  = 500;
            _port.WriteTimeout = 500;
            _port.Open();

            long maxval = 0;

            joystick.GetVJDAxisMax(id, HID_USAGES.HID_USAGE_X, ref maxval);


            while (true)
            {
                try
                {
                    string   a          = _port.ReadLine();
                    string[] substrings = a.Split('|');
                    if (substrings.Length == 3)
                    {
                        Thetas[0] = clamp(float.Parse(substrings[0]), -50, 50);
                        Thetas[1] = clamp(float.Parse(substrings[1]), -50, 50);
                        Thetas[2] = float.Parse(substrings[2]);
                        Console.WriteLine("ThetaX={0} ThetaY={1} ThetaZ={2}", Thetas[0], Thetas[1], Thetas[2]);
                    }
                }
                catch { }

                //normalizing xrot and mapping to full range ans letting x maxRot=50deg
                joystick.SetAxis(Convert.ToInt32(((Thetas[0] / 100) + 0.5) * maxval), id, HID_USAGES.HID_USAGE_X);
                joystick.SetAxis(Convert.ToInt32(((Thetas[1] / 100) + 0.5) * maxval), id, HID_USAGES.HID_USAGE_Y);


                //threshold of btn=20 deg  (btn1=R1 btn2=L1)
                if (Thetas[2] > 20)
                {
                    joystick.SetBtn(true, id, 1);
                    joystick.SetBtn(false, id, 2);
                }
                else if (Thetas[2] < -20)
                {
                    joystick.SetBtn(true, id, 2);
                    joystick.SetBtn(false, id, 1);
                }
                else
                {
                    joystick.SetBtn(false, id, 1);
                    joystick.SetBtn(false, id, 2);
                }

                Thread.Sleep(20);
            }
        }
Пример #30
0
        } // Main

        public static IEnumerator TestCoroutine()
        {
            int ContPovNumber = joystick.GetVJDContPovNumber(id);
            int DiscPovNumber = joystick.GetVJDDiscPovNumber(id);

            int  X, Y, Z, ZR, XR;
            uint count  = 0;
            long maxval = 0;

            X  = 20;
            Y  = 30;
            Z  = 40;
            XR = 60;
            ZR = 80;

            joystick.GetVJDAxisMax(id, HID_USAGES.HID_USAGE_X, ref maxval);

            bool res;

            // Reset this device to default values
            joystick.ResetVJD(id);
            // Feed the device in endless loop

            Debug.Log("start");

            while (true)
            {
                // Set position of 4 axes
                res = joystick.SetAxis(X, id, HID_USAGES.HID_USAGE_X);
                res = joystick.SetAxis(Y, id, HID_USAGES.HID_USAGE_Y);
                res = joystick.SetAxis(Z, id, HID_USAGES.HID_USAGE_Z);
                res = joystick.SetAxis(XR, id, HID_USAGES.HID_USAGE_RX);
                res = joystick.SetAxis(ZR, id, HID_USAGES.HID_USAGE_RZ);

                // Press/Release Buttons
                res = joystick.SetBtn(true, id, count / 50);
                res = joystick.SetBtn(false, id, 1 + count / 50);

                // If Continuous POV hat switches installed - make them go round
                // For high values - put the switches in neutral state
                if (ContPovNumber > 0)
                {
                    if ((count * 70) < 30000)
                    {
                        res = joystick.SetContPov(((int)count * 70), id, 1);
                        res = joystick.SetContPov(((int)count * 70) + 2000, id, 2);
                        res = joystick.SetContPov(((int)count * 70) + 4000, id, 3);
                        res = joystick.SetContPov(((int)count * 70) + 6000, id, 4);
                    }
                    else
                    {
                        res = joystick.SetContPov(-1, id, 1);
                        res = joystick.SetContPov(-1, id, 2);
                        res = joystick.SetContPov(-1, id, 3);
                        res = joystick.SetContPov(-1, id, 4);
                    };
                }
                ;

                // If Discrete POV hat switches installed - make them go round
                // From time to time - put the switches in neutral state
                if (DiscPovNumber > 0)
                {
                    if (count < 550)
                    {
                        joystick.SetDiscPov((((int)count / 20) + 0) % 4, id, 1);
                        joystick.SetDiscPov((((int)count / 20) + 1) % 4, id, 2);
                        joystick.SetDiscPov((((int)count / 20) + 2) % 4, id, 3);
                        joystick.SetDiscPov((((int)count / 20) + 3) % 4, id, 4);
                    }
                    else
                    {
                        joystick.SetDiscPov(-1, id, 1);
                        joystick.SetDiscPov(-1, id, 2);
                        joystick.SetDiscPov(-1, id, 3);
                        joystick.SetDiscPov(-1, id, 4);
                    };
                }
                ;

                System.Threading.Thread.Sleep(20);
                X += 150; if (X > maxval)
                {
                    X = 0;
                }
                Y += 250; if (Y > maxval)
                {
                    Y = 0;
                }
                Z += 350; if (Z > maxval)
                {
                    Z = 0;
                }
                XR += 220; if (XR > maxval)
                {
                    XR = 0;
                }
                ZR += 200; if (ZR > maxval)
                {
                    ZR = 0;
                }
                count++;

                if (count > 640)
                {
                    count = 0;
                }
                yield return(new WaitForFixedUpdate());
            } // While (Robust)
        }