private void initAxis(HID_USAGES axis)
        {
            long maxval = 0;
            long minval = 0;

            joystick.GetVJDAxisMax(id, axis, ref maxval);
            joystick.GetVJDAxisMin(id, axis, ref minval);
            setAxis(axis, (int)(maxval - minval) / 2);
        }
Exemple #2
0
        public long GetMinForAxis(HID_USAGES Axis)
        {
            long minval = 0;

            if (bDeviceAcquired)
            {
                m_joystick.GetVJDAxisMin(m_vJoyID, Axis, ref minval);
            }
            return(minval);
        }
Exemple #3
0
        private void Button1_Click(object sender, EventArgs e)
        {
            //System.Diagnostics.Debug.Print("breakpoint passed");
            System.Diagnostics.Debug.Print(": serial.IsOpen == " + serial.IsOpen.ToString());
            long minax = 0, maxax = 0;

            //joy.AcquireVJD((uint)joyIDSelector.Value);
            joy.GetVJDAxisMin((uint)joyIDSelector.Value, HID_USAGES.HID_USAGE_X, ref minax);
            joy.GetVJDAxisMax((uint)joyIDSelector.Value, HID_USAGES.HID_USAGE_X, ref maxax);
            //joy.RelinquishVJD((uint)joyIDSelector.Value);
            System.Diagnostics.Debug.Print("x axis min == " + minax + ", max == " + maxax);
        }
        int ConvertAxisRatioToAxisInt(float axisRatio, HID_USAGES hid)
        {
            long min = 0, max = 0;
            var  gotMin = vjoy.GetVJDAxisMin(deviceId, HID_USAGES.HID_USAGE_X, ref min);
            var  gotMax = vjoy.GetVJDAxisMax(deviceId, HID_USAGES.HID_USAGE_X, ref max);

            if (!gotMin || !gotMax)
            {
                Debug.LogWarningFormat("Error getting min/max of HID axis {0}", hid.ToString());
                return(0);
            }

            // Get an absolute ratio where 0 is -Max, .5 is 0, and 1 is +Max
            float absRatio = axisRatio / 2f + .5f;
            long  range    = max - min;

            return((int)((long)(range * absRatio) + min));
        }
Exemple #5
0
        /// <summary>Scale value m from any reference scale to the HID axis scale.</summary>
        /// <param name="m">the value to scale</param>
        /// <param name="hid">the HID axis type</param>
        /// <param name="rMin">minimum value of reference scale</param>
        /// <param name="rMax">maximum value of reference scale</param>
        private int ScaleAxis(float m, HID_USAGES hid, float rMin, float rMax)
        {
            // target range
            long tMin = 0, tMax = 0;
            // todo: no reason to keep requesting the min max, make these constants
            var gotMin = vjoy.GetVJDAxisMin(deviceId, hid, ref tMin);
            var gotMax = vjoy.GetVJDAxisMax(deviceId, hid, ref tMax);

            if (!gotMin || !gotMax)
            {
                Debug.LogWarning($"Error getting min/max of HID axis {hid}");
                return(0);
            }

            // scale m to target scale
            var result = (m - rMin) / (rMax - rMin) * (tMax - tMin) + tMin;

            return((int)result);
        }
        private xState[] m_hasBtn = new xState[1 + 128]; // [0] is not used

        private xState GetState(HID_USAGES usage, int pindex = 0)
        {
            var ret = new xState {
                usage = usage, current = 0
            };

            if (usage == HID_USAGES.HID_USAGE_POV)
            {
                ret.has     = (pindex > 0 && pindex <= m_nPov);
                ret.pindex  = pindex;
                ret.current = (int)POVType.Nil;
            }
            else if (usage == HID_USAGES.HID_USAGE_WHL) // button
            {
                ret.has     = (pindex > 0 && pindex <= m_nPov);
                ret.pindex  = pindex;
                ret.current = 0; // == false...
            }
            else
            {
                ret.has = m_joy.GetVJDAxisExist(Index, usage);
                if (ret.has)
                {
                    long val = 0;
                    if (m_joy.GetVJDAxisMin(Index, usage, ref val))
                    {
                        ret.min = (int)val;
                    }
                    if (m_joy.GetVJDAxisMax(Index, usage, ref val))
                    {
                        ret.max = (int)val;
                    }
                    ret.range = (double)(ret.max - ret.min) / xState.MAXOUT;
                }
            }

            return(ret);
        }
        /// <summary>
        /// Acquire a vJoy device ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Acquire(uint id)
        {
            // Device ID can only be in the range 1-16
            vJoyDevID      = id;
            Report.bDevice = (byte)vJoyDevID;
            if (vJoyDevID <= 0 || vJoyDevID > 16)
            {
                LogFormat(LogLevels.ERROR, "Illegal device ID {0}\nExit!", vJoyDevID);
                return(-1);
            }

            // Get the state of the requested device
            VjdStat status = Joystick.GetVJDStatus(vJoyDevID);

            switch (status)
            {
            case VjdStat.VJD_STAT_OWN:
                LogFormat(LogLevels.DEBUG, "vJoy Device {0} is already owned by this feeder", vJoyDevID);
                break;

            case VjdStat.VJD_STAT_FREE:
                LogFormat(LogLevels.DEBUG, "vJoy Device {0} is free", vJoyDevID);
                break;

            case VjdStat.VJD_STAT_BUSY:
                LogFormat(LogLevels.ERROR, "vJoy Device {0} is already owned by another feeder\nCannot continue", vJoyDevID);
                return(-3);

            case VjdStat.VJD_STAT_MISS:
                LogFormat(LogLevels.ERROR, "vJoy Device {0} is not installed or disabled\nCannot continue", vJoyDevID);
                return(-4);

            default:
                LogFormat(LogLevels.ERROR, "vJoy Device {0} general error\nCannot continue", vJoyDevID);
                return(-1);
            }
            ;


            // Get the number of buttons and POV Hat switchessupported by this vJoy device
            this.NbButtons = Joystick.GetVJDButtonNumber(vJoyDevID);
            int ContPovNumber = Joystick.GetVJDContPovNumber(vJoyDevID);
            int DiscPovNumber = Joystick.GetVJDDiscPovNumber(vJoyDevID);

            // Print results
            LogFormat(LogLevels.DEBUG, "vJoy Device {0} capabilities:", vJoyDevID);
            LogFormat(LogLevels.DEBUG, "Numner of buttons\t\t{0}", this.NbButtons);
            LogFormat(LogLevels.DEBUG, "Numner of Continuous POVs\t{0}", ContPovNumber);
            LogFormat(LogLevels.DEBUG, "Numner of Descrete POVs\t\t{0}", DiscPovNumber);

            // Check which axes are supported. Follow enum HID_USAGES
            ConfiguredvJoyAxes.Clear();
            int i = 0;

            foreach (HID_USAGES toBeTested in Enum.GetValues(typeof(HID_USAGES)))
            {
                // Skip POV
                if (toBeTested == HID_USAGES.HID_USAGE_POV)
                {
                    continue;
                }
                var present = Joystick.GetVJDAxisExist(vJoyDevID, toBeTested);
                LogFormat(LogLevels.DEBUG, "Axis " + HIDAxesInfo[i].Name + " \t\t{0}", present ? "Yes" : "No");
                if (present)
                {
                    HIDAxesInfo[i].IsPresent = present;
                    // Retrieve min/max from vJoy
                    if (!Joystick.GetVJDAxisMin(vJoyDevID, toBeTested, ref HIDAxesInfo[i].MinValue))
                    {
                        Log("Failed getting min value!");
                    }
                    if (!Joystick.GetVJDAxisMax(vJoyDevID, toBeTested, ref HIDAxesInfo[i].MaxValue))
                    {
                        Log("Failed getting min value!");
                    }
                    Log(" Min= " + HIDAxesInfo[i].MinValue + " Max=" + HIDAxesInfo[i].MaxValue);

                    // Add to indexed list of configured axes
                    ConfiguredvJoyAxes.Add(HIDAxesInfo[i]);
                }
                i++;
            }

            // Acquire the target
            if ((status == VjdStat.VJD_STAT_OWN) || ((status == VjdStat.VJD_STAT_FREE) && (!Joystick.AcquireVJD(vJoyDevID))))
            {
                LogFormat(LogLevels.ERROR, "Failed to acquire vJoy device number {0}.", vJoyDevID);
                return(-1);
            }
            else
            {
                LogFormat(LogLevels.DEBUG, "Acquired: vJoy device number {0}.", vJoyDevID);
            }
            return((int)status);
        }
        /// <summary>
        /// vJoyの初期化
        /// </summary>
        public void Initialize()
        {
            // vJoyドライバーのテスト
            // 失敗した場合、falseを返す
            bool result = joystick.vJoyEnabled();

            if (result == false)
            {
                throw new DeviceControlException(Properties.Resources.VJOY_ENABLE);
            }
            // vJoy仮想デバイスのテスト
            VjdStat status = joystick.GetVJDStatus(rID);

            switch (status)
            {
            case VjdStat.VJD_STAT_BUSY:     // 〃
                throw new DeviceControlException(Properties.Resources.VJOY_STAT_OWN);

            case VjdStat.VJD_STAT_MISS:     // インストールされていない or 無効な状態
                throw new DeviceControlException(Properties.Resources.VJOY_ENABLE);

            case VjdStat.VJD_STAT_OWN:
                return;

            case VjdStat.VJD_STAT_FREE:     // 利用可能
                break;
            }

            // vJoy仮想デバイスの設定値確認
            // 今回、仕様としては、
            //    12ボタン
            //    +字キー
            //    アナログスティック 2本
            // としています
            // ※将来的には自動的に設定するよう変更

            // ボタン数のチェック
            Int32 nBtn = joystick.GetVJDButtonNumber(rID);

            if (nBtn != 12)
            {
                throw new DeviceControlException(Properties.Resources.VJOY_BUTTON_NUMBER_FATAL);
            }
            // +字キーの有無チェック
            /* Returns the number of discrete-type POV hats in the specified device Discrete-type POV Hat values may be  North, East, South, West or neutral Valid values are 0 to 4  (from version 2.0.1)*/
            Int32 nDPov = joystick.GetVJDDiscPovNumber(rID);

            if (nDPov != 1)
            {
                throw new DeviceControlException(Properties.Resources.VJOY_POV_FATAL);
            }
            Int32 nCPov = joystick.GetVJDContPovNumber(rID);

            if (nCPov != 0)
            {
                throw new DeviceControlException(Properties.Resources.VJOY_POV_FATAL);
            }
            // アナログスティックのチェック
            // 全ての機能の有無はチェックせず必要条件のみ確認
            // 左アナログスティック←→
            result = joystick.GetVJDAxisExist(rID, HID_USAGES.HID_USAGE_X);
            if (result == false)
            {
                throw new DeviceControlException(Properties.Resources.VJOY_STICK_LEFT_FATAL);
            }
            // 左アナログスティック↑↓
            result = joystick.GetVJDAxisExist(rID, HID_USAGES.HID_USAGE_Y);
            if (result == false)
            {
                throw new DeviceControlException(Properties.Resources.VJOY_STICK_LEFT_FATAL);
            }
            // 右アナログスティック←→
            result = joystick.GetVJDAxisExist(rID, HID_USAGES.HID_USAGE_Z);
            if (result == false)
            {
                throw new DeviceControlException(Properties.Resources.VJOY_STICK_RIGHT_FATAL);
            }
            // 右アナログスティック↑↓
            result = joystick.GetVJDAxisExist(rID, HID_USAGES.HID_USAGE_RZ);
            if (result == false)
            {
                throw new DeviceControlException(Properties.Resources.VJOY_STICK_RIGHT_FATAL);
            }

            joystick.GetVJDAxisMax(rID, HID_USAGES.HID_USAGE_X, ref m_nAxisMax);
            joystick.GetVJDAxisMin(rID, HID_USAGES.HID_USAGE_X, ref m_nAxisMin);

            // 接続
            joystick.AcquireVJD(rID);

            joystick.ResetVJD(rID);
        }
Exemple #9
0
        public Form1()
        {
            InitializeComponent();


            // ******************************************* **** *********************************************
            // **************************************** start_Vjoy ******************************************
            // ******************************************* **** *********************************************


            // Create one joystick object and a position structure.
            joystick = new vJoy();
            iReport  = new vJoy.JoystickState();


            // 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 AxisRY = joystick.GetVJDAxisExist(id, HID_USAGES.HID_USAGE_RY);
            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("Number of buttons\t\t{0}\n", nButtons);
            Console.WriteLine("Number of Continuous POVs\t{0}\n", ContPovNumber);
            Console.WriteLine("Number 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", AxisY ? "Yes" : "No");
            Console.WriteLine("Axis Z\t\t{0}\n", AxisZ ? "Yes" : "No");
            Console.WriteLine("Axis Rx\t\t{0}\n", AxisRX ? "Yes" : "No");
            Console.WriteLine("Axis Rx\t\t{0}\n", AxisRY ? "Yes" : "No");
            Console.WriteLine("Axis Rz\t\t{0}\n", AxisRZ ? "Yes" : "No");

            // Test if DLL matches the driver
            UInt32 DllVer = 0, DrvVer = 0;
            bool   match = joystick.DriverMatch(ref DllVer, ref DrvVer);

            if (match)
            {
                Console.WriteLine("Version of Driver Matches DLL Version ({0:X})\n", DllVer);
            }
            else
            {
                Console.WriteLine("Version of Driver ({0:X}) does NOT match DLL Version ({1:X})\n", DrvVer, DllVer);
            }


            // 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);
            }

            joystick.GetVJDAxisMax(id, HID_USAGES.HID_USAGE_X, ref maxvalue);
            Console.WriteLine("Max value X: {0}.\n", maxvalue);
            joystick.GetVJDAxisMax(id, HID_USAGES.HID_USAGE_Y, ref maxvalue);
            Console.WriteLine("Max value Y: {0}.\n", maxvalue);
            joystick.GetVJDAxisMax(id, HID_USAGES.HID_USAGE_RX, ref maxvalue);
            Console.WriteLine("Max value RX: {0}.\n", maxvalue);
            joystick.GetVJDAxisMax(id, HID_USAGES.HID_USAGE_RY, ref maxvalue);
            Console.WriteLine("Max value RY: {0}.\n", maxvalue);

            joystick.GetVJDAxisMin(id, HID_USAGES.HID_USAGE_X, ref minvalue);
            Console.WriteLine("Min value X: {0}.\n", minvalue);
            joystick.GetVJDAxisMin(id, HID_USAGES.HID_USAGE_Y, ref minvalue);
            Console.WriteLine("Min value Y: {0}.\n", minvalue);
            joystick.GetVJDAxisMin(id, HID_USAGES.HID_USAGE_RX, ref minvalue);
            Console.WriteLine("Min value RX: {0}.\n", minvalue);
            joystick.GetVJDAxisMin(id, HID_USAGES.HID_USAGE_RY, ref minvalue);
            Console.WriteLine("Min value RY: {0}.\n", minvalue);

            this.minValJOY.Text = minvalue.ToString();
            this.maxValJOY.Text = maxvalue.ToString();

            // Reset this device to default values
            joystick.ResetVJD(id);

            yawProg.Minimum    = (int)minvalue;
            yawProg.Maximum    = (int)maxvalue;
            rollProg.Minimum   = (int)minvalue;
            rollProg.Maximum   = (int)maxvalue;
            pitchProg.Minimum  = (int)minvalue;
            pitchProg.Maximum  = (int)maxvalue;
            heightProg.Minimum = (int)minvalue;
            heightProg.Maximum = (int)maxvalue;

            rollChart.ChartAreas[0].AxisY.Maximum   = maxvalue;
            rollChart.ChartAreas[0].AxisY.Minimum   = minvalue;
            pitchChart.ChartAreas[0].AxisY.Maximum  = maxvalue;
            pitchChart.ChartAreas[0].AxisY.Minimum  = minvalue;
            heightChart.ChartAreas[0].AxisY.Maximum = maxvalue;
            heightChart.ChartAreas[0].AxisY.Minimum = minvalue;
            yawChart.ChartAreas[0].AxisY.Maximum    = maxvalue;
            yawChart.ChartAreas[0].AxisY.Minimum    = minvalue;

            myCircularBuff = new CircularBuffer <HandValue>(2000);

            myLog = new Logger();

            t = new Thread(joyThread);
            t.Start();

            tS = new Thread(serialThread);
            tS.Start();

            // ******************************************* **** *********************************************
            // ***************************************** end_Vjoy *******************************************
            // ******************************************* **** *********************************************

            controller.EventContext        = WindowsFormsSynchronizationContext.Current;
            controller.FrameReady         += newFrameHandler;
            controller.ImageReady         += onImageReady;
            controller.ImageRequestFailed += onImageRequestFailed;

            //set greyscale palette for image Bitmap object
            ColorPalette grayscale = bitmap.Palette;

            for (int i = 0; i < 256; i++)
            {
                grayscale.Entries[i] = Color.FromArgb((int)255, i, i, i);
            }
            bitmap.Palette = grayscale;
        }
        public TesterForm()
        {
            InitializeComponent();
            joystick = new vJoy();
            position = new vJoy.JoystickState();


            /////	General driver data
            short  iVer    = joystick.GetvJoyVersion();
            bool   enabled = joystick.vJoyEnabled();
            string Prd     = joystick.GetvJoyProductString();
            string Mnf     = joystick.GetvJoyManufacturerString();
            string Srl     = joystick.GetvJoySerialNumberString();
            string prt     = String.Format("Product: {0}; Version {1:X}; Manuf: {2}; Serial:{3}", Prd, iVer, Mnf, Srl);

            label1.Text   = prt;
            Enbld.Checked = enabled;

            /////	vJoy Device properties
            int  nBtn     = joystick.GetVJDButtonNumber(id);
            int  nDPov    = joystick.GetVJDDiscPovNumber(id);
            int  nCPov    = joystick.GetVJDContPovNumber(id);
            bool X_Exist  = joystick.GetVJDAxisExist(id, HID_USAGES.HID_USAGE_X);
            bool Y_Exist  = joystick.GetVJDAxisExist(id, HID_USAGES.HID_USAGE_Y);
            bool Z_Exist  = joystick.GetVJDAxisExist(id, HID_USAGES.HID_USAGE_Z);
            bool RX_Exist = joystick.GetVJDAxisExist(id, HID_USAGES.HID_USAGE_RX);

            prt         = String.Format("Device[{0}]: Buttons={1}; DiscPOVs:{2}; ContPOVs:{3}", id, nBtn, nDPov, nCPov);
            label2.Text = prt;

            UInt32 DllVer = 0, DrvVer = 0;
            bool   match = joystick.DriverMatch(ref DllVer, ref DrvVer);

            if (match)
            {
                prt = String.Format("Version of Driver Matches DLL Version {0:X}", DllVer);
            }
            else
            {
                prt = String.Format("Version of Driver ({0:X}) does NOT match DLL Version ({1:X})", DrvVer, DllVer);
            }
            label7.Text = prt;

            long max = 10, min = 10;
            bool ok;

            ok = joystick.GetVJDAxisMax(id, HID_USAGES.HID_USAGE_X, ref max);
            ok = joystick.GetVJDAxisMin(id, HID_USAGES.HID_USAGE_X, ref min);

            /////	Write access to vJoy Device - Basic
            VjdStat status;

            status = joystick.GetVJDStatus(id);
            bool acq = joystick.AcquireVJD(id);

            status = joystick.GetVJDStatus(id);

            position.AxisX      = 1000;
            position.AxisY      = 5000;
            position.AxisZ      = 10000;
            position.AxisXRot   = 20000;
            position.Buttons    = 0xA5A5A5A5;
            position.ButtonsEx1 = 0;
            bool upd = joystick.UpdateVJD(id, ref position);

            status = joystick.GetVJDStatus(id);

            //// Reset functions
            joystick.ResetButtons(id);

            // Register callback function
            // Function to register:     Removal()
            // User data to pass:        label2
            joystick.RegisterRemovalCB(Removal, label2);
        }
Exemple #11
0
        public void Evaluate(int SpreadMax)
        {
            if (init)
            {
                VJInstance = new vJoy();
                VJState    = new vJoy.JoystickState();
                if (!VJInstance.vJoyEnabled())
                {
                    FEnabled[0] = false;
                    return;
                }
                Status     = VJInstance.GetVJDStatus(FID[0]);
                FStatus[0] = Status.ToString();

                ButtonCount = VJInstance.GetVJDButtonNumber(FID[0]);
                PovCount    = VJInstance.GetVJDContPovNumber(FID[0]);
                if (Status != VjdStat.VJD_STAT_FREE && Status != VjdStat.VJD_STAT_OWN)
                {
                    return;
                }
                if (!VJInstance.AcquireVJD(FID[0]))
                {
                    return;
                }

                Axes = new[] {
                    HID_USAGES.HID_USAGE_X,
                    HID_USAGES.HID_USAGE_Y,
                    HID_USAGES.HID_USAGE_Z,
                    HID_USAGES.HID_USAGE_RX,
                    HID_USAGES.HID_USAGE_RY,
                    HID_USAGES.HID_USAGE_RZ,
                    HID_USAGES.HID_USAGE_SL0,
                    HID_USAGES.HID_USAGE_SL1,
                    HID_USAGES.HID_USAGE_WHL,
                    HID_USAGES.HID_USAGE_ACCELERATOR,
                    HID_USAGES.HID_USAGE_BRAKE,
                    HID_USAGES.HID_USAGE_CLUTCH,
                    HID_USAGES.HID_USAGE_STEERING,
                    HID_USAGES.HID_USAGE_AILERON,
                    HID_USAGES.HID_USAGE_RUDDER,
                    HID_USAGES.HID_USAGE_THROTTLE
                };
                FAxisPresent.SliceCount = Axes.Length;
                AxisMaxVal = new long[Axes.Length];
                AxisMinVal = new long[Axes.Length];

                for (int i = 0; i < Axes.Length; i++)
                {
                    FAxisPresent[i] = VJInstance.GetVJDAxisExist(FID[0], Axes[i]);
                    if (FAxisPresent[i])
                    {
                        long minv = 0, maxv = 0;
                        VJInstance.GetVJDAxisMin(FID[0], Axes[i], ref minv);
                        VJInstance.GetVJDAxisMax(FID[0], Axes[i], ref maxv);
                        AxisMinVal[i] = minv;
                        AxisMaxVal[i] = maxv;
                    }
                }
                init = false;
            }
            FEnabled[0] = true;
            FStatus[0]  = Status.ToString();
            for (int i = 0; i < Math.Min(FAxesIn.SliceCount, Axes.Length); i++)
            {
                if (FAxisPresent[i])
                {
                    VJInstance.SetAxis(
                        (int)(FAxesIn[i] * AxisMaxVal[i] + (1 - FAxesIn[i]) * AxisMinVal[i]),
                        FID[0],
                        Axes[i]);
                }
            }
            for (int i = 0; i < Math.Min(FButtonsIn.SliceCount, ButtonCount); i++)
            {
                VJInstance.SetBtn(FButtonsIn[i], FID[0], (uint)i + 1);
            }
            for (int i = 0; i < Math.Min(FPovPosIn.SliceCount, PovCount); i++)
            {
                if (!FPovSetIn[i])
                {
                    VJInstance.SetContPov(-1, FID[0], (uint)i + 1);
                }
                else
                {
                    VJInstance.SetContPov((int)(FPovPosIn[i] * 35999), FID[0], (uint)i + 1);
                }
            }
        }
Exemple #12
0
        public void Connect(string deviceId)
        {
            if (!UInt32.TryParse(deviceId, out _deviceId))
            {
                throw new PTGenericException($"Error parsing device ID '{deviceId}'. Must be a number.");
            }

            if (_deviceId < 1 || _deviceId > 16)
            {
                throw new PTGenericException($"Illegal device ID '{deviceId}'. Must be between 1 and 16, inclusive.");
            }

            // Initialize the joystick and position structure.
            _joystick = new vJoy();

            // Get the driver attributes (Vendor ID, Product ID, Version Number)
            if (!_joystick.vJoyEnabled())
            {
                throw new PTGenericException("vJoy driver not enabled: Failed Getting vJoy attributes.");
            }
            else
            {
                _logger.Debug($"Vendor: {_joystick.GetvJoyManufacturerString()}");
                _logger.Debug($"Product: {_joystick.GetvJoyProductString()}");
                _logger.Debug($"Version: {_joystick.GetvJoySerialNumberString()}");
            }

            // Test if DLL matches the driver
            uint dllVer = 0, drvVer = 0;
            bool isDriverMatch = _joystick.DriverMatch(ref dllVer, ref drvVer);

            if (isDriverMatch)
            {
                _logger.Debug($"Version of Driver Matches DLL Version ({dllVer})");
            }
            else
            {
                _logger.Warn($"Version of Driver ({drvVer}) does NOT match DLL Version ({dllVer})");
            }

            // Get the state of the requested device
            var status = _joystick.GetVJDStatus(_deviceId);

            switch (status)
            {
            case VjdStat.VJD_STAT_OWN:
                _logger.Debug($"vJoy Device '{_deviceId}' is already owned by this feeder.");
                break;

            case VjdStat.VJD_STAT_FREE:
                _logger.Debug($"vJoy Device '{_deviceId}' is free\n");
                break;

            case VjdStat.VJD_STAT_BUSY:
                throw new PTGenericException($"vJoy Device '{_deviceId}' is already owned by another feeder. Cannot connect.");

            case VjdStat.VJD_STAT_MISS:
                throw new PTGenericException($"vJoy Device '{_deviceId}' is not installed or disabled. Cannot connect.");

            default:
                throw new PTGenericException($"vJoy Device '{_deviceId}' general error. Cannot connect.");
            }
            ;

            // Acquire the target
            if ((status == VjdStat.VJD_STAT_OWN) || ((status == VjdStat.VJD_STAT_FREE) && (!_joystick.AcquireVJD(_deviceId))))
            {
                throw new PTGenericException($"Failed to acquire vJoy device number '{_deviceId}'.");
            }
            else
            {
                _logger.Info($"Acquired: vJoy device number '{_deviceId}'.");
            }

            _axisX  = _joystick.GetVJDAxisExist(_deviceId, HID_USAGES.HID_USAGE_X);
            _axisY  = _joystick.GetVJDAxisExist(_deviceId, HID_USAGES.HID_USAGE_Y);
            _axisZ  = _joystick.GetVJDAxisExist(_deviceId, HID_USAGES.HID_USAGE_Z);
            _axisRX = _joystick.GetVJDAxisExist(_deviceId, HID_USAGES.HID_USAGE_RX);
            _axisRY = _joystick.GetVJDAxisExist(_deviceId, HID_USAGES.HID_USAGE_RY);
            _axisRZ = _joystick.GetVJDAxisExist(_deviceId, HID_USAGES.HID_USAGE_RZ);
            // Get the number of buttons and POV Hat switchessupported by this vJoy device
            _nButtons      = _joystick.GetVJDButtonNumber(_deviceId);
            _contPovNumber = _joystick.GetVJDContPovNumber(_deviceId);
            _discPovNumber = _joystick.GetVJDDiscPovNumber(_deviceId);

            _joystick.GetVJDAxisMin(_deviceId, HID_USAGES.HID_USAGE_X, ref _axisMinX);
            _joystick.GetVJDAxisMax(_deviceId, HID_USAGES.HID_USAGE_X, ref _axisMaxX);
            _joystick.GetVJDAxisMin(_deviceId, HID_USAGES.HID_USAGE_Y, ref _axisMinY);
            _joystick.GetVJDAxisMax(_deviceId, HID_USAGES.HID_USAGE_Y, ref _axisMaxY);
            _joystick.GetVJDAxisMin(_deviceId, HID_USAGES.HID_USAGE_Z, ref _axisMinZ);
            _joystick.GetVJDAxisMax(_deviceId, HID_USAGES.HID_USAGE_Z, ref _axisMaxZ);
            _joystick.GetVJDAxisMin(_deviceId, HID_USAGES.HID_USAGE_RX, ref _axisMinRX);
            _joystick.GetVJDAxisMax(_deviceId, HID_USAGES.HID_USAGE_RX, ref _axisMaxRX);
            _joystick.GetVJDAxisMin(_deviceId, HID_USAGES.HID_USAGE_RY, ref _axisMinRY);
            _joystick.GetVJDAxisMax(_deviceId, HID_USAGES.HID_USAGE_RY, ref _axisMaxRY);
            _joystick.GetVJDAxisMin(_deviceId, HID_USAGES.HID_USAGE_RZ, ref _axisMinRZ);
            _joystick.GetVJDAxisMax(_deviceId, HID_USAGES.HID_USAGE_RZ, ref _axisMaxRZ);

            PrintDeviceSupport();
        }
        public VJoyGlobalHolder(uint index)
        {
            Index              = index;
            Global             = new VJoyGlobal(this);
            setPressedStrategy = new SetPressedStrategy <int>(b => SetButton(b, true), b => SetButton(b, false));

            _joystick = new vJoy();
            report    = new vJoy.JoystickState();//tkz
            if (index < 1 || index > 16)
            {
                throw new ArgumentException($"Illegal joystick device id: {index}");
            }

            if (!_joystick.vJoyEnabled())
            {
                throw new Exception("vJoy driver not enabled: Failed Getting vJoy attributes");
            }

            uint apiVersion    = 0;
            uint driverVersion = 0;
            bool match         = _joystick.DriverMatch(ref apiVersion, ref driverVersion);

            if (!match)
            {
                Console.WriteLine("vJoy version of Driver ({0:X}) does NOT match DLL Version ({1:X})", driverVersion, apiVersion);
            }

            Version = new VjoyVersionGlobal(apiVersion, driverVersion);

            var status = _joystick.GetVJDStatus(index);


            string error = null;

            switch (status)
            {
            case VjdStat.VJD_STAT_BUSY:
                error = "vJoy Device {0} is already owned by another feeder";
                break;

            case VjdStat.VJD_STAT_MISS:
                error = "vJoy Device {0} is not installed or disabled";
                break;

            case VjdStat.VJD_STAT_UNKN:
                error = ("vJoy Device {0} general error");
                break;
            }

            if (error == null && !_joystick.AcquireVJD(index))
            {
                error = "Failed to acquire vJoy device number {0}";
            }

            if (error != null)
            {
                throw new Exception(string.Format(error, index));
            }

            long max = 0, min = 0;

            _joystick.GetVJDAxisMax(index, HID_USAGES.HID_USAGE_X, ref max);
            _joystick.GetVJDAxisMin(index, HID_USAGES.HID_USAGE_X, ref min);
            AxisMax     = (int)max / 2 + 1;//modif TKZ erreur -1 -> +1
            AxisMin     = (int)min;
            enabledAxis = new Dictionary <HID_USAGES, bool>();
            foreach (HID_USAGES axis in Enum.GetValues(typeof(HID_USAGES)))
            {
                enabledAxis[axis] = _joystick.GetVJDAxisExist(index, axis);
            }

            maxButtons       = _joystick.GetVJDButtonNumber(index);
            maxDirPov        = _joystick.GetVJDDiscPovNumber(index);
            maxContinuousPov = _joystick.GetVJDContPovNumber(index);

            currentAxisValue = new Dictionary <HID_USAGES, int>();

            _joystick.ResetVJD(index);
        }
        public int Acquire(uint id)
        {
            // Device ID can only be in the range 1-16
            joyID          = id;
            Report.bDevice = (byte)joyID;
            if (joyID <= 0 || joyID > 16)
            {
                LogFormat(LogLevels.ERROR, "Illegal device ID {0}\nExit!", joyID);
                return(-1);
            }

            // Get the state of the requested device
            VjdStat status = Joystick.GetVJDStatus(joyID);

            switch (status)
            {
            case VjdStat.VJD_STAT_OWN:
                LogFormat(LogLevels.DEBUG, "vJoy Device {0} is already owned by this feeder", joyID);
                break;

            case VjdStat.VJD_STAT_FREE:
                LogFormat(LogLevels.DEBUG, "vJoy Device {0} is free", joyID);

                break;

            case VjdStat.VJD_STAT_BUSY:
                LogFormat(LogLevels.ERROR, "vJoy Device {0} is already owned by another feeder\nCannot continue", joyID);
                return(-3);

            case VjdStat.VJD_STAT_MISS:
                LogFormat(LogLevels.ERROR, "vJoy Device {0} is not installed or disabled\nCannot continue", joyID);
                return(-4);

            default:
                LogFormat(LogLevels.ERROR, "vJoy Device {0} general error\nCannot continue", joyID);
                return(-1);
            }
            ;


            // Get the number of buttons and POV Hat switchessupported by this vJoy device
            int nButtons      = Joystick.GetVJDButtonNumber(joyID);
            int ContPovNumber = Joystick.GetVJDContPovNumber(joyID);
            int DiscPovNumber = Joystick.GetVJDDiscPovNumber(joyID);

            // Print results
            LogFormat(LogLevels.DEBUG, "vJoy Device {0} capabilities:", joyID);
            LogFormat(LogLevels.DEBUG, "Numner of buttons\t\t{0}", nButtons);
            LogFormat(LogLevels.DEBUG, "Numner of Continuous POVs\t{0}", ContPovNumber);
            LogFormat(LogLevels.DEBUG, "Numner of Descrete POVs\t\t{0}", DiscPovNumber);

            // Check which axes are supported. Follow enum HID_USAGES, up to 8
            for (int i = 0; i < AxesInfo.Count; i++)
            {
                HID_USAGES toBeTested = AxesInfo[i].HID_Usage;
                var        present    = Joystick.GetVJDAxisExist(joyID, toBeTested);
                LogFormat(LogLevels.DEBUG, "Axis " + AxesInfo[i].Name + " \t\t{0}", present ? "Yes" : "No");
                if (present)
                {
                    AxesInfo[i].IsPresent = present;
                    if (AxesInfo[i].AxisCorrection.ControlPoints.Count < 2)
                    {
                        AxesInfo[i].ResetCorrectionFactors();
                    }
                    // Retrieve min/max from vJoy
                    if (!Joystick.GetVJDAxisMin(joyID, toBeTested, ref AxesInfo[i].MinValue))
                    {
                        Log("Failed getting min value!");
                    }
                    if (!Joystick.GetVJDAxisMax(joyID, toBeTested, ref AxesInfo[i].MaxValue))
                    {
                        Log("Failed getting min value!");
                    }
                    Log(" Min= " + AxesInfo[i].MinValue + " Max=" + AxesInfo[i].MaxValue);
                }
            }


            // Acquire the target
            if ((status == VjdStat.VJD_STAT_OWN) || ((status == VjdStat.VJD_STAT_FREE) && (!Joystick.AcquireVJD(joyID))))
            {
                LogFormat(LogLevels.ERROR, "Failed to acquire vJoy device number {0}.", joyID);
                return(-1);
            }
            else
            {
                LogFormat(LogLevels.DEBUG, "Acquired: vJoy device number {0}.", joyID);
            }
            return((int)status);
        }
Exemple #15
0
        private void serialConnect_Click(object sender, EventArgs e)
        {
            if (serialConnect.Text == "Connect")
            {
                serialPort1.BaudRate      = int.Parse(serialBaud.Text);
                serialPort1.PortName      = list_SerialPorts.SelectedItem.ToString();
                serialPort1.NewLine       = "\n";
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                }
                try
                {
                    serialPort1.Open();
                    timer1.Enabled = true;
                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }

                if (serialPort1.IsOpen)
                {
                    list_SerialPorts.Enabled = false;
                    serialBaud.Enabled       = false;


                    // Create one joystick object and a position structure.

                    axis_e_x.Text  = ujoyStick.GetVJDAxisExist(uJoyId, HID_USAGES.HID_USAGE_X).ToString();
                    axis_e_y.Text  = ujoyStick.GetVJDAxisExist(uJoyId, HID_USAGES.HID_USAGE_Y).ToString();
                    axis_e_rx.Text = ujoyStick.GetVJDAxisExist(uJoyId, HID_USAGES.HID_USAGE_RX).ToString();
                    axis_e_ry.Text = ujoyStick.GetVJDAxisExist(uJoyId, HID_USAGES.HID_USAGE_RY).ToString();
                    axis_e_rz.Text = ujoyStick.GetVJDAxisExist(uJoyId, HID_USAGES.HID_USAGE_RZ).ToString();
                    axis_e_s1.Text = ujoyStick.GetVJDAxisExist(uJoyId, HID_USAGES.HID_USAGE_SL0).ToString();
                    axis_e_s2.Text = ujoyStick.GetVJDAxisExist(uJoyId, HID_USAGES.HID_USAGE_SL1).ToString();
                    button_e.Text  = ujoyStick.GetVJDButtonNumber(uJoyId).ToString();
                    cpov_e.Text    = ujoyStick.GetVJDContPovNumber(uJoyId).ToString();
                    dpov_e.Text    = ujoyStick.GetVJDDiscPovNumber(uJoyId).ToString();

                    ujoyStick.GetVJDAxisMax(uJoyId, HID_USAGES.HID_USAGE_X, ref joyMax);
                    ujoyStick.GetVJDAxisMin(uJoyId, HID_USAGES.HID_USAGE_X, ref joyMin);
                    MessageBox.Show("Min:" + joyMin.ToString() + ", Max:" + joyMax.ToString());


                    // Acquire the target
                    if ((vjoy_status.Text == VjdStat.VJD_STAT_OWN.ToString()) || ((vjoy_status.Text == VjdStat.VJD_STAT_FREE.ToString()) && (!ujoyStick.AcquireVJD(uJoyId))))
                    {
                        vjoy_status.Text = "Failed to acquire vJoy device";
                        Console.WriteLine(vjoy_status.Text);
                        return;
                    }
                    else
                    {
                        vjoy_status.Text = "Acquired: vJoy device";
                        Console.WriteLine(vjoy_status.Text);
                    }


                    ujoyStick.ResetVJD(uJoyId);
                    serialConnect.Text = "Disconnect";
                }
            }
            else
            {
                serialConnect.Enabled = true;
                serialPort1.Close();
                if (serialPort1.IsOpen == false)
                {
                    timer1.Enabled           = false;
                    serialConnect.Enabled    = true;
                    list_SerialPorts.Enabled = true;
                    serialBaud.Enabled       = true;
                    serialConnect.Text       = "Connect";
                }
                else
                {
                    MessageBox.Show("Error disconnecting");
                }
            }
        }