Search Unity

Resolved How to output data to my custom HID device?

Discussion in 'Input System' started by zfszero, Feb 20, 2021.

  1. zfszero

    zfszero

    Joined:
    Feb 14, 2021
    Posts:
    6
    Hello,

    I made a BLE HID gamepad and it worked well on the PC. For the HID input report, I configured game controllers and 10 bytes custom data(1 more byte ID). The output report has 10 bytes data(1 more byte ID).
    I want to receive some datas from the PC, like LED control, player inf and so on.
    upload_2021-2-20_21-33-7.png

    In the input debugger, I can find the input and output report information.
    upload_2021-2-20_21-32-31.png

    But I don't know how to send data to device in the Unity with C# code.
    I had read the docs "Input System, HID Output", To use HID Output Reports, call InputDevice.ExecuteCommand to send a command struct with the typeStatic property set as "HIDO" to a Device. The command struct contains the Device-specific data sent out to the HID.

    I can't understand it without a demo. Someone can help me?

    Thanks a lot!
     
  2. zfszero

    zfszero

    Joined:
    Feb 14, 2021
    Posts:
    6
    I found the examples for Motor control of gamepad. Here is my solution:

    MyControlCommand.cs:
    Code (CSharp):
    1. using System.Runtime.InteropServices;
    2. using UnityEngine.InputSystem.Utilities;
    3.  
    4. namespace UnityEngine.InputSystem.LowLevel
    5. {
    6.     [StructLayout(LayoutKind.Explicit, Size = kSize)]
    7.     internal struct MyControlCommand : IInputDeviceCommandInfo
    8.     {
    9.         public static FourCC Type { get { return new FourCC('H', 'I', 'D', 'O'); } }
    10.         internal const int id = 0;
    11.         internal const int kSize = InputDeviceCommand.BaseCommandSize + 11;
    12.  
    13.         [FieldOffset(0)]
    14.         public InputDeviceCommand baseCommand;
    15.  
    16.         [FieldOffset(InputDeviceCommand.BaseCommandSize)]
    17.         public byte reportId;
    18.  
    19.         [FieldOffset(InputDeviceCommand.BaseCommandSize+1)]
    20.         public byte DataType;
    21.  
    22.         [FieldOffset(InputDeviceCommand.BaseCommandSize + 2)]
    23.         public byte Data;
    24.  
    25. [INDENT]//Add data here[/INDENT]
    26.  
    27.         public FourCC typeStatic
    28.         {
    29.             get { return Type; }
    30.         }
    31.  
    32.         public static MyControlCommand Create(byte dataType, byte data)
    33.         {
    34.             return new MyControlCommand
    35.             {
    36.                 baseCommand = new InputDeviceCommand(Type, kSize),
    37.                 reportId = id,
    38.                 DataType = dataType,
    39.                 Data = data
    40.             };
    41.         }
    42.     }
    43. }
    44.  

    usage:( m_Gamepad is my controller )

    var command = MyControlCommand.Create(1,3);
    m_Gamepad.device.ExecuteCommand(ref command);



    Finally, I received data on my gamepad by BLE.
     
    Last edited: Mar 31, 2021
    cp- likes this.
  3. zfszero

    zfszero

    Joined:
    Feb 14, 2021
    Posts:
    6
    But I had a new problem, I can use control elements like button, axis, dpad, but the custom data from the HID input report, I don't how to read it. :(:(:(:(
    The example for PS4 HID, I found the battery level defined data. Is there anyway to read it out?
     
    Last edited: Mar 31, 2021
  4. Rickmc3280

    Rickmc3280

    Joined:
    Jun 28, 2014
    Posts:
    189
    Does it show up in the Input Debugger when you are in playmode? You should be able to create you ActionMap and ref the data.
     
  5. cp-

    cp-

    Joined:
    Mar 29, 2018
    Posts:
    78
    @zfszero thanks for the output example, helped me to work out how it works. where did you get the info though? the docs seem to be lacking in this regard.
     
  6. XuDark

    XuDark

    Joined:
    Jul 26, 2021
    Posts:
    4
    dude,how did you define your HID vendorID and productID,i made a hid too,but the Vendorid and productid as below: upload_2021-12-2_15-10-12.png
     
  7. zfszero

    zfszero

    Joined:
    Feb 14, 2021
    Posts:
    6
    I searched the C# code in the "Input System" with motor control for gamepad, than I figured out how does it work.
     
  8. zfszero

    zfszero

    Joined:
    Feb 14, 2021
    Posts:
    6
    I think different MCU has the different code, here is my code for ESP32, fszhang/BLE_HID_Unity (github.com)
    BLE_HID_Unity/BleGamepad.cpp
    Code (CSharp):
    1.     BleGamepadInstance->hid->manufacturer()->setValue(BleGamepadInstance->deviceManufacturer);
    2.     BleGamepadInstance->hid->pnp(0x02, 0x045e, 0x028e, 0x0002);
    3.     BleGamepadInstance->hid->hidInfo(0x00, 0x02);
     
  9. zfszero

    zfszero

    Joined:
    Feb 14, 2021
    Posts:
    6