如何调试Windows USB HID控制器延迟?

时间:2019-01-27 17:04:29

标签: windows usb driver delay hid

我要做的是将Wii Nunchuk连接到Windows PC。我已经编写了处理控制器端的ARM固件。我还编写了一个Windows应用程序,用于轮询数据。一切正常,除了用户按下按钮和Windows注意到它之间有大约2秒的延迟。

我正在使用hidapi库。

不完全知道该如何调试。我敢肯定,如果应用未运行,Windows会继续轮询设备(已通过Wireshark检查)。启动应用程序时它还会轮询还是该应用程序是唯一可以访问该设备的进程?我也不想Windows也轮询该设备(但我仍然希望它是一个简单的HID设备)。

该设备是通用HID USB设备,因此默认情况下绑定了hidclass.syshidparse.sys hidusb.sys驱动程序(如果您可以调用这些驱动程序...不确定它是否是正确的术语sys个文件)。

我已经删除了部分代码,因此代码更短了。

我尝试过:轮询越来越慢,从非阻塞改为阻塞读取。

int main(int argc, char* argv[])
{
    signal(SIGINT, SigHandler);
    signal(SIGTERM, SigHandler);

#ifdef WIN32
    UNREFERENCED_PARAMETER(argc);
    UNREFERENCED_PARAMETER(argv);
#endif

    int result;
    hid_device* usb_handle;

    if (hid_init())
        return -1;

    unsigned char usb_data_buffer[256];

    // Open the device using the VID, PID,
    // and optionally the Serial number.
    usb_handle = hid_open(VID, PID, NULL);
    if (!usb_handle) {
        printf(" >> Unable to open device\n");
        return 1;
    }

    print_usb_info(usb_handle);
    // Lets block because why not
    hid_set_nonblocking(usb_handle, 0);    


    while (running)
    {
        result = hid_read(usb_handle, usb_data_buffer, sizeof(usb_data_buffer));

        controller.stick_dir = DIR_NONE;
        controller.stick_raw_x = usb_data_buffer[0];
        controller.stick_raw_y = usb_data_buffer[1];
        controller.btn_C = (usb_data_buffer[2] & 0b00000010) >> 1;
        controller.btn_Z =    usb_data_buffer[2] & 0b00000001;

        printf("%03d ", controller.stick_raw_x);
        printf("%03d ", controller.stick_raw_y);
        printf("%01d ", controller.btn_C);
        printf("%01d ", controller.btn_Z);
        printf("\n");

        ...

        MOUSEINPUT mouseInput = {};
        mouseInput.dwFlags = MOUSEEVENTF_MOVE;
        INPUT mInput;
        if (cursor_pie_offset(&mouseInput, controller.stick_dir, controller.stick_dir_prev))
        {
            mInput.type = 0;
            mInput.mi = mouseInput;
            SendInput(1, &mInput, sizeof(mInput));
        }

        KEYBDINPUT keybdInput = {};
        keybdInput.wVk = VK_CONTROL;
        INPUT kInput0;
        kInput0.type = 1;
        kInput0.ki = keybdInput;

        #ifdef WIN32
        Sleep(5);
        #else
        usleep(5*1000);
        #endif
    }
    hid_close(usb_handle);
    hid_exit();

    return 0;
}

0 个答案:

没有答案