USB HID多点触控串行报告

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了USB HID多点触控串行报告相关的知识,希望对你有一定的参考价值。

我正在创建一个连接到Windows计算机的Arduino设备,并模拟计算机上的触摸屏。屏幕不属于Arduino,而是Arduino通过USB将HID触摸消息转发到Windows 10计算机。

我做的第一件事是转发AbsoluteMouse事件而不是Digitizer事件。这对我来说很好。但是,由于我的设备支持多次触摸,我想使用多点触控HID消息而不是AbsoluteMouse。作为一个概念证明,我开始只传递一个手指的数据。 (我计划在我开始工作后添加更多手指)。我采用SerialReport方法,因为它似乎是最容易实现的:

我发现了很多使用AbsoluteMouse的例子,所以基本上我没有做比复制粘贴HID报告更多的事情了。但是,对于触摸屏,代码样本很难得到。 (我只找到了不是触摸屏的突触触摸板样本)。因此,我尝试实现自己的代码来报告触摸消息。但这失败了。我想我理解如何解释HID描述符,但这是我的第一次,所以我不确定...我的消息结构有什么问题吗?

#define REPORTID_MTOUCH                 1

static const uint8_t _hidSerialMultiTouchDescriptor[] PROGMEM = {
    //https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/sample-report-descriptor--serial-reporting-mode-
    //linked from: https://docs.microsoft.com/en-us/windows-hardware/design/component-guidelines/selecting-packet-reporting-modes-in-multitouch-drivers
    0x05, 0x0d,                         // USAGE_PAGE (Digitizers)
    0x09, 0x04,                         // USAGE (Touch Screen)
    0xa1, 0x01,                         // COLLECTION (Application)
    0x85, REPORTID_MTOUCH,              //   REPORT_ID (Touch)
    0x09, 0x22,                         //   USAGE (Finger)
    0xa1, 0x00,                         //   COLLECTION (Physical)
    0x09, 0x42,                         //     USAGE (Tip Switch)
    0x15, 0x00,                         //     LOGICAL_MINIMUM (0)
    0x25, 0x01,                         //     LOGICAL_MAXIMUM (1)
    0x75, 0x01,                         //     REPORT_SIZE (1)
    0x95, 0x01,                         //     REPORT_COUNT (1)
    0x81, 0x02,                         //     INPUT (Data,Var,Abs)
    0x95, 0x03,                         //     REPORT_COUNT (3)
    0x81, 0x03,                         //     INPUT (Cnst,Ary,Abs)
    0x09, 0x32,                         //     USAGE (In Range)
    0x09, 0x47,                         //     USAGE (Confidence)
    0x95, 0x02,                         //     REPORT_COUNT (2)
    0x81, 0x02,                         //     INPUT (Data,Var,Abs)
    0x95, 0x0a,                         //     REPORT_COUNT (10)
    0x81, 0x03,                         //     INPUT (Cnst,Ary,Abs)
    0x05, 0x01,                         //     USAGE_PAGE (Generic Desk..
    0x26, 0xff, 0x7f,                   //     LOGICAL_MAXIMUM (32767)
    0x75, 0x10,                         //     REPORT_SIZE (16)
    0x95, 0x01,                         //     REPORT_COUNT (1)
    0x65, 0x00,                         //     UNIT (None)
    0x09, 0x30,                         //     USAGE (X)
    0x81, 0x02,                         //     INPUT (Data,Var,Abs)
    0x09, 0x31,                         //     USAGE (Y)
    0x46, 0x00, 0x00,                   //     PHYSICAL_MAXIMUM (0)
    0x81, 0x02,                         //     INPUT (Data,Var,Abs)
    0x05, 0x0d,                         //     USAGE PAGE (Digitizers)
    0x09, 0x48,                         //     USAGE (Width)
    0x09, 0x49,                         //     USAGE (Height)
    0x95, 0x01,                         //     REPORT_COUNT (2)
    0x81, 0x02,                         //     INPUT (Data,Var,Abs)
    0x81, 0x03,                         //     INPUT (Cnst,Ary,Abs)
    0x09, 0x51,                         //     USAGE (Contact Identifier)
    0x75, 0x10,                         //     REPORT_SIZE (16) 
    0x95, 0x02,                         //     REPORT_COUNT (1)
    0x81, 0x02,                         //     INPUT (Data,Var,Abs)
    0x09, 0x55,                         //    USAGE(Maximum Count)
    0x15, 0x00,                         //   LOGICAL_MINIMUM (0)
    0x25, 0x08,                         //   LOGICAL_MAXIMUM (255)
    0x75, 0x08,                         //   REPORT_SIZE (8)
    0x95, 0x01,                         //   REPORT_COUNT (1)
    0xb1, 0x02,                         //   FEATURE (Data,Var,Abs)
    0xc0,                               //   END_COLLECTION
    0xc0                               // END_COLLECTION
};

这是我对所述消息的实现:

typedef union {
  struct{
    uint8_t TipSwitchLsb4Bits_InRange_Confidence_Empty2Bits;
    uint8_t Padding8_10Minus2;
    int16_t xAxis;
    int16_t yAxis;
    int16_t width;
    int16_t height;
    int16_t ContactId;
    uint8_t MaximumCount;
  };
} HID_SerialTouchReport;

void reportTouch(unsigned int x, unsigned int y) {
  HID_SerialTouchReport report;
  report.TipSwitchLsb4Bits_InRange_Confidence_Empty2Bits=0xff;
  report.xAxis=x;
  report.yAxis=y;
  report.width=4;
  report.height=4;
  report.ContactId=0;
  report.MaximumCount=1;
  HID().SendReport(REPORTID_MTOUCH,&report,sizeof(report));
}

...
  static HIDSubDescriptor node(_hidSerialMultiTouchDescriptor, sizeof(_hidSerialMultiTouchDescriptor));
HID().AppendDescriptor(&node);
...

//Move the pointer diagonally across the screen:
for(unsigned int x=0;x<32767;x+=1000) {
  reportTouch(x,x);
  delay(50);
}

测试代码应该在MSPaint上从左上角到右下角画一条对角线。

当我使用AbsoluteMouse描述符/消息测试时,一切正常。所以我认为问题出在我对HID_SerialTouchReport的定义中。

一些引起我注意的事情:

  1. 为什么“PHYSICAL_MAXIMUM(0)”在定义“USAGE(Y)”时出现。它是否与Y有关而不是X?为什么它是“0”?
  2. “Ary”在“INPUT(Cnst,Ary,Abs)”中对于身高和宽度有什么意义?
  3. 我有点“捏造”东西:“report.TipSwitchLsb4Bits_InRange_Confidence_Empty2Bits = 0xff;”。然而,这对我来说似乎没问题,因为我认为所有非填充位都需要设置为“1”。
  4. 我不确定应该设置report.MaximumCount是什么。我尝试过尝试不同的Count&Id值,但都没有效果。
  5. 我的Arduino确实在设备管理器中显示为“复合设备”,所以我认为并非一切都是错的。
  6. 在文章我链接“串行数据包”模式是为旧版Windows 7,我使用Windows 10.但我认为这应该不是一个问题。
  7. 虽然我直接从微软网站复制HID描述符,但我不确定我是否正确构建了消息结构。 (我忘记了几位吗?)

或者是否是导致我的代码失败的其他原因?

答案

很难说肯定,但看起来HID报告描述符与您的HID_SerialTouchReport结构不匹配。以下是我解码你的描述符的方法:

//--------------------------------------------------------------------------------
// Decoded Application Collection
//--------------------------------------------------------------------------------

/*
05 0D        (GLOBAL) USAGE_PAGE         0x000D Digitizer Device Page 
09 04        (LOCAL)  USAGE              0x000D0004 Touch Screen (CA=Application Collection) 
A1 01        (MAIN)   COLLECTION         0x00000001 Application (Usage=0x000D0004: Page=Digitizer Device Page, Usage=Touch Screen, Type=CA)
85 01          (GLOBAL) REPORT_ID          0x01 (1) 
09 22          (LOCAL)  USAGE              0x000D0022 Finger (CL=Logical Collection) 
A1 00          (MAIN)   COLLECTION         0x00000000 Physical (Usage=0x000D0022: Page=Digitizer Device Page, Usage=Finger, Type=CL) <-- Warning: USAGE type should be CP (Physical)
09 42            (LOCAL)  USAGE              0x000D0042 Tip Switch (MC=Momentary Control) 
15 00            (GLOBAL) LOGICAL_MINIMUM    0x00 (0) <-- Redundant: LOGICAL_MINIMUM is already 0 <-- Info: Consider replacing 15 00 with 14
25 01            (GLOBAL) LOGICAL_MAXIMUM    0x01 (1)  
75 01            (GLOBAL) REPORT_SIZE        0x01 (1) Number of bits per field  
95 01            (GLOBAL) REPORT_COUNT       0x01 (1) Number of fields  
81 02            (MAIN)   INPUT              0x00000002 (1 field x 1 bit) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap 
95 03            (GLOBAL) REPORT_COUNT       0x03 (3) Number of fields  
81 03            (MAIN)   INPUT              0x00000003 (3 fields x 1 bit) 1=Constant 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap 
09 32            (LOCAL)  USAGE              0x000D0032 In Range (MC=Momentary Control) 
09 47            (LOCAL)  USAGE              0x000D0047 Confidence (DV=Dynamic Value) 
95 02            (GLOBAL) REPORT_COUNT       0x02 (2) Number of fields  
81 02            (MAIN)   INPUT              0x00000002 (2 fields x 1 bit) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap 
95 0A            (GLOBAL) REPORT_COUNT       0x0A (10) Number of fields  
81 03            (MAIN)   INPUT              0x00000003 (10 fields x 1 bit) 1=Constant 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap 
05 01            (GLOBAL) USAGE_PAGE         0x0001 Generic Desktop Page 
26 FF7F          (GLOBAL) LOGICAL_MAXIMUM    0x7FFF (32767)  
75 10            (GLOBAL) REPORT_SIZE        0x10 (16) Number of bits per field  
95 01            (GLOBAL) REPORT_COUNT       0x01 (1) Number of fields  
65 00            (GLOBAL) UNIT               0x00000000 No unit (0=None) <-- Redundant: UNIT is already 0x00000000
09 30            (LOCAL)  USAGE              0x00010030 X (DV=Dynamic Value) 
81 02            (MAIN)   INPUT              0x00000002 (1 field x 16 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap 
09 31            (LOCAL)  USAGE              0x00010031 Y (DV=Dynamic Value) 
46 0000          (GLOBAL) PHYSICAL_MAXIMUM   0x0000 (0) <-- Redundant: PHYSICAL_MAXIMUM is already 0 <-- Info: Consider replacing 46 0000 with 44
81 02            (MAIN)   INPUT              0x00000002 (1 field x 16 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap 
05 0D            (GLOBAL) USAGE_PAGE         0x000D Digitizer Device Page 
09 48            (LOCAL)  USAGE              0x000D0048 Width (DV=Dynamic Value) 
09 49            (LOCAL)  USAGE              0x000D0049 Height (DV=Dynamic Value) 
95 01            (GLOBAL) REPORT_COUNT       0x01 (1) Number of fields <-- Redundant: REPORT_COUNT is already 1 
81 02            (MAIN)   INPUT              0x00000002 (1 field x 16 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap 
81 03            (MAIN)   INPUT              0x00000003 (1 field x 16 bits) 1=Constant 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap 
09 51            (LOCAL)  USAGE              0x000D0051 Contact Identifier (DV=Dynamic Value) 
75 10            (GLOBAL) REPORT_SIZE        0x10 (16) Number of bits per field <-- Redundant: REPORT_SIZE is already 16 
95 02            (GLOBAL) REPORT_COUNT       0x02 (2) Number of fields  
81 02            (MAIN)   INPUT              0x00000002 (2 fields x 16 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap 
09 55            (LOCAL)  USAGE              0x000D0055 Contact Count Maximum (SV=Static Value) 
15 00            (GLOBAL) LOGICAL_MINIMUM    0x00 (0) <-- Redundant: LOGICAL_MINIMUM is already 0 <-- Info: Consider replacing 15 00 with 14
25 08            (GLOBAL) LOGICAL_MAXIMUM    0x08 (8)  
75 08            (GLOBAL) REPORT_SIZE        0x08 (8) Number of bits per field  
95 01            (GLOBAL) REPORT_COUNT       0x01 (1) Number of fields  
B1 02            (MAIN)   FEATURE            0x00000002 (1 field x 8 bits) 0=Data 1=Variable 0=Absolute 0=NoWrap 0=Linear 0=PrefState 0=NoNull 0=NonVolatile 0=Bitmap 
C0             (MAIN)   END_COLLECTION     Physical 
C0           (MAIN)   END_COLLECTION     Application 
*/

//--------------------------------------------------------------------------------
// Digitizer Device Page featureReport 01 (Device <-> Host)
//--------------------------------------------------------------------------------

typedef struct
{
  uint8_t  reportId;                                 // Report ID = 0x01 (1)
                                                     // Collection: TouchScreen Finger
  uint8_t  DIG_TouchScreenFingerContactCountMaximum; // Usage 0x000D0055: Contact Count Maximum, Value = 0 to 8
} featureReport01_t;


//--------------------------------------------------------------------------------
// Digitizer Device Page inputReport 01 (Device --> Host)
//--------------------------------------------------------------------------------

typedef struct
{
  uint8_t  reportId;                                 // Report ID = 0x01 (1)
                                                     // Collection: TouchScreen Finger
  uint8_t  DIG_TouchScreenFingerTipSwitch : 1;       // Usage 0x000D0042: Tip Switch, Value = 0 to 1
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint8_t  DIG_TouchScreenFingerInRange : 1;         // Usage 0x000D0032: In Range, Value = 0 to 1
  uint8_t  DIG_TouchScreenFingerConfidence : 1;      // Usage 0x000D0047: Confidence, Value = 0 to 1
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint8_t  : 1;                                      // Pad
  uint16_t GD_TouchScreenFingerX;                    // Usage 0x00010030: X, Value = 0 to 32767
  uint16_t GD_TouchScreenFingerY;                    // Usage 0x00010031: Y, Value = 0 to 32767
  uint16_t DIG_TouchScreenFingerWidth;               // Usage 0x000D0048: Width, Value = 0 to 32767
                                                     // Usage 0x000D0049 Height (DV=Dynamic Value) Value = 0 to 32767 <-- Ignored: REPORT_COUNT (1) is too small
  uint16_t pad_8;                                    // Pad
  uint16_t DIG_TouchScreenFingerContactIdentifier[2]; // Usage 0x0

以上是关于USB HID多点触控串行报告的主要内容,如果未能解决你的问题,请参考以下文章

gtkmm 多点触控设备无法识别 (Windows)

我应该使用IOKit还是DriverKIt(或HIDDriverKit)为macOS中的USB或蓝牙多点触控设备编写驱动程序?

将串行从 Raspberry 传递到 Arduino USB HID

USB鼠标实现——HID 报告的返回

浅析USB HID ReportDesc (HID报告描述符)

USB HID描述符