Search Unity

Discussion Custom HID Value Keeps on Repeating! Need Help!!!

Discussion in 'Input System' started by pctesting, Jul 11, 2023.

  1. pctesting

    pctesting

    Joined:
    Jun 5, 2023
    Posts:
    1
    Hi, I created a Custom HID Input for my FANATEC CSL Pedal because it is listed as unsupported and noticed an annoying bug. The value of all of the CSL Pedal Input keeps on repeating by themself, it will go from 0 to -1 on and on, as you can see here:

    https://www.awesomescreenshot.com/video/18995698?key=be25a10b9fe639422331a48723d79635

    The Clutch value also got called when the Brake is going down or up, even though on the Input Action Map, the binding only recognizes one of them.

    I have also created a question in Unity Discussion which you can see here:

    Custom HID Input Error! Input Value Keeps on Repeating by Themself! - Questions & Answers - Unity Discussions

    Here's my code:

    Code (CSharp):
    1. public class JoystickTesting : MonoBehaviour
    2. {
    3.     [Header("Input Action Asset")]
    4.     [SerializeField] private InputActionAsset inputActionAsset;
    5.     private InputActionMap t16000M;
    6.     private InputAction leftXStick;
    7.     private InputAction leftYStick;
    8.     private InputAction rightXStick;
    9.     private InputAction rightYStick;
    10.     private InputActionMap fanatecCSLPedalKit;
    11.     private InputAction cslThrottle;
    12.     private InputAction cslBrake;
    13.     private InputAction cslClutch;
    14.  
    15.     [Header("T.16000M")]
    16.     [SerializeField] private float leftX;
    17.     [SerializeField] private float leftY;
    18.     [SerializeField] private float rightX;
    19.     [SerializeField] private float rightY;
    20.  
    21.     [Header("FANATEC CSL Pedal Kit")]
    22.     [SerializeField] private float throttle;
    23.     [SerializeField] private float brake;
    24.     [SerializeField] private float clutch;
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.         var joysticks = new List<InputDevice>();
    29.         foreach (var device in InputSystem.devices)
    30.         {
    31.             Debug.Log(device.description.product);
    32.             if (device.description.product == "T.16000M")
    33.                 joysticks.Add(device);
    34.         }
    35.  
    36.         if (joysticks.Count > 0)
    37.         {
    38.             InputSystem.SetDeviceUsage(joysticks[0], CommonUsages.LeftHand);
    39.             if (joysticks.Count > 1)
    40.                 InputSystem.SetDeviceUsage(joysticks[1], CommonUsages.RightHand);
    41.         }
    42.  
    43.         t16000M = inputActionAsset.FindActionMap("T.16000M");
    44.         leftXStick = t16000M.FindAction("leftHandStickX");
    45.         leftYStick = t16000M.FindAction("leftHandStickY");
    46.         rightXStick = t16000M.FindAction("rightHandStickX");
    47.         rightYStick = t16000M.FindAction("rightHandStickY");
    48.  
    49.         leftXStick.Enable();
    50.         leftYStick.Enable();
    51.         rightXStick.Enable();
    52.         rightYStick.Enable();
    53.  
    54.         t16000M = inputActionAsset.FindActionMap("Fanatec CSL Pedal Kit");
    55.         cslThrottle = t16000M.FindAction("throttle");
    56.         cslBrake = t16000M.FindAction("brake");
    57.         cslClutch = t16000M.FindAction("clutch");
    58.    
    59.         cslThrottle.Enable();
    60.         cslBrake.Enable();
    61.         cslClutch.Enable();
    62.     }
    63.  
    64.     // Update is called once per frame
    65.     void Update()
    66.     {
    67.         leftX = leftXStick.ReadValue<float>();
    68.         leftY = leftYStick.ReadValue<float>();
    69.         rightX = rightXStick.ReadValue<float>();
    70.         rightY = rightYStick.ReadValue<float>();
    71.  
    72.         throttle = cslThrottle.ReadValue<float>();
    73.         brake = cslBrake.ReadValue<float>();
    74.         clutch = cslClutch.ReadValue<float>();
    75.     }
    76. }
    Here's the HIDInputReport:

    Code (CSharp):
    1. [StructLayout(LayoutKind.Explicit, Size = 32)]
    2. struct FanatecCSLHIDInputReport : IInputStateTypeInfo
    3. {
    4.     // Because all HID input reports are tagged with the 'HID ' FourCC,
    5.     // this is the format we need to use for this state struct.
    6.     public FourCC format => new FourCC('H', 'I', 'D');
    7.  
    8.     // HID input reports can start with an 8-bit report ID. It depends on the device
    9.     // whether this is present or not. On the PS4 DualShock controller, it is
    10.     // present. We don't really need to add the field, but let's do so for the sake of
    11.     // completeness. This can also help with debugging.
    12.     [FieldOffset(0)] public byte reportId;
    13.  
    14.     // The InputControl annotations here probably look a little scary, but what we do
    15.     // here is relatively straightforward. The fields we add we annotate with
    16.     // [FieldOffset] to force them to the right location, and then we add InputControl
    17.     // to attach controls to the fields. Each InputControl attribute can only do one of
    18.     // two things: either it adds a new control or it modifies an existing control.
    19.     // Given that our layout is based on Gamepad, almost all the controls here are
    20.     // inherited from Gamepad, and we just modify settings on them.
    21.  
    22.     [InputControl(name = "Throttle", layout = "Stick")]
    23.     [InputControl(name = "Throttle/x", offset = 0, format = "BYTE")]
    24.     [InputControl(name = "Throttle/left", offset = 0, format = "BYTE")]
    25.     [InputControl(name = "Throttle/right", offset = 0, format = "BYTE")]
    26.     [FieldOffset(1)] public byte throttle;
    27.  
    28.     [InputControl(name = "Brake", layout = "Stick")]
    29.     [InputControl(name = "Brake/y", offset = 1, format = "BYTE")]
    30.     [InputControl(name = "Brake/up", offset = 1, format = "BYTE")]
    31.     [InputControl(name = "Brake/down", offset = 1, format = "BYTE")]
    32.     [FieldOffset(2)] public byte brake;
    33.  
    34.     [InputControl(name = "Clutch", layout = "Axis", bit = 0)]
    35.     [FieldOffset(3)] public byte clutch;
    36. }