Я пытаюсь использовать обратную связь по усилию с рулевым колесом Fanatec в своем проекте «Единство». Я использую SharpDX.DirectInput, чтобы попытаться решить проблему. Вот мой код, который пытается использовать принудительную обратную связь, я только что вызывал этот метод в функции запуска скрипта для целей тестирования.
private void ForceFeedback()
{
// set up direct input
DirectInput di = new DirectInput();
Device wheel = null;
// find wheel
foreach (DeviceInstance instance in di.GetDevices(
DeviceClass.GameControl,
DeviceEnumerationFlags.AttachedOnly | DeviceEnumerationFlags.ForceFeedback))
{
wheel = new Joystick(di, instance.InstanceGuid);
}
// set cooperative level
wheel.SetCooperativeLevel(GetWindowHandle(),
CooperativeLevel.Exclusive | CooperativeLevel.Background);
// set axis mode to absolute
wheel.Properties.AxisMode = DeviceAxisMode.Absolute;
// set buffer size
wheel.Properties.BufferSize = 128;
// acquire wheel for capturing
wheel.Acquire();
//Configure axes
int[] axis = null;
foreach (DeviceObjectInstance doi in wheel.GetObjects())
{
// set axes ranges
if (((int)doi.ObjectId & (int)DeviceObjectTypeFlags.Axis) != 0)
{
wheel.Properties.Range =
new InputRange(-5000, 5000);
}
int[] temp;
// get info about first two ff axes on device
if (doi.ObjectType.CompareTo(ToGuid((int)DeviceObjectTypeFlags.ForceFeedbackActuator)) != 0)
{
if (axis != null)
{
temp = new int[axis.Length + 1];
axis.CopyTo(temp, 0);
axis = temp;
}
else
{
axis = new int[1];
}
// store the offset of each axis.
axis[axis.Length - 1] = doi.Offset;
if (axis.Length == 2)
{
break;
}
}
}
// see if wheel supports ConstantForce and set it
Effect e;
foreach (EffectInfo ei in wheel.GetEffects(EffectType.All))
{
if ((ei.Type & EffectType.ConstantForce) != 0)
{
// fill in some generic values for the effect
EffectParameters param = new EffectParameters();
param.Directions = new int[axis.Length];
param.Axes = new int[axis.Length];
// -> param.conditionStruct ?
// -> param.effectType ?
param.Duration = int.MaxValue;
param.Gain = 10000;
param.SamplePeriod = 0;
param.Flags = EffectFlags.ObjectOffsets | EffectFlags.Cartesian;
param.Axes = axis;
e = new Effect(wheel, wheel.Information.ForceFeedbackDriverGuid, param);
e.Start();
}
}
}
/// <summary>
/// Convert int to guid. To be used in forcefeedback function
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static Guid ToGuid(int value)
{
byte[] bytes = new byte[16];
BitConverter.GetBytes(value).CopyTo(bytes, 0);
return new Guid(bytes);
}
Функция GetWindowHandle()
определена в начале класса:
// function using native dll to get active window handle (for sharpdx cooperation levels)
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern System.IntPtr GetActiveWindow();
public static System.IntPtr GetWindowHandle()
{
return GetActiveWindow();
}
Каждый раз, когда я запускаю код, я получаю следующую ошибку: SharpDXException: HRESULT: [0x80040154], Module: [SharpDX.DirectInput], ApiCode: [DIERR_DEVICENOTREG/DeviceNotRegistered], Message: Class not registered
Кто-нибудь знает, что может быть причиной этого и что я могу сделать, чтобы это исправить? Я провел немало исследований в Интернете, чтобы попытаться найти решение, но пока мне не повезло.