Author Topic: Windows Sensor Support for Gamepad  (Read 7 times)

inteks

  • Newbie
  • *
  • Posts: 3
    • View Profile
Windows Sensor Support for Gamepad
« on: October 04, 2017, 03:46:13 am »
hi,

i like your software and i use touchmousepointer for a long time now 👍
last weekend i tested tablet pro gamepad. but i really missed 1 feature.
can you please add support for windows sensors ?

i want to play needforspeed like with other tablet/phone apps.# and use sensor for steering.

this is super easy to implement. i try it for my self. but only 1 app a time can feed vjoy. so i have to choose between onscreen buttons or  sensor  :(


you can use this assembly ->
Windows.Devices.Sensors.Inclinometer.GetDefault();
you get an Inclinometer object with eventsupport
"ReadingChanged" or just use "GetCurrentReading" to get the current values.
its super easy! not a big deal ;o)

please can you add this???

thx

Social Buttons


inteks

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Windows Sensor Support for Gamepad
« Reply #1 on: October 09, 2017, 03:18:32 am »
ok. i can use the gamepad set it to keyboard and now i can feed vjoy with sensor data.
i made a little prog to do it for me.
but it would still be nize to have it in one tool.
this code works ->

 
Code: [Select]
class JoyFeederDev {
        private vJoy joystick;
        static public uint id = 1;
        private long maxval;
        private Accelerometer accelerometer;

        public void Enable() {
            joystick = new vJoy();
            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;
            };


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


            accelerometer = Accelerometer.GetDefault();
            accelerometer.ReadingChanged += Accelerometer_ReadingChanged;

            accelerometer.ReportInterval = 5;
            lastState = true.ToString();

        }
       
        private void Accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args) {

            var X = (int)((maxval / 2) * ((args.Reading.AccelerationX * 4) + 1));
            X = FilterVJoyData(dx2, X, 3);
            joystick.SetAxis(X, id, HID_USAGES.HID_USAGE_X);

            var Y = (int)((maxval / 2) * (((args.Reading.AccelerationZ + 0.3) * 1.5) + 1));
            Y = FilterVJoyData(dy2, Y, 5);
            joystick.SetAxis(Y, id, HID_USAGES.HID_USAGE_Y);

            Status = $"X={X} Y={Y} {args.Reading.AccelerationX:0.00}/{args.Reading.AccelerationY:0.00}/{args.Reading.AccelerationZ:0.00}";

        }

        List<int> dx2 = new List<int>();
        List<int> dy2 = new List<int>();
        int FilterVJoyData(List<int> list, int newValue, int steps) {
            list.Add(newValue);
            if (list.Count > steps) list.RemoveAt(0);
            int x3 = 0;
            foreach (int i in list) {
                x3 += i;
            }
            var outval = x3 / steps;
            if (outval > maxval) outval = (int)maxval;
            if (outval < 0) outval = 0;
            return outval;
        }

        public void Dispose() {
            if (joystick != null) {
                joystick.RelinquishVJD(id);
                joystick = null;
            }

            if (accelerometer != null) {
                accelerometer.ReadingChanged -= Accelerometer_ReadingChanged;
                accelerometer = null;
            }
        }
}