adafruit / Adafruit_TinyUSB_Arduino

Arduino library for TinyUSB
MIT License
477 stars 127 forks source link

Support 'Multi-Touch' for USB Touchscreen devices #332

Closed Joewawaw closed 1 year ago

Joewawaw commented 1 year ago

Hi, I am looking to get driver support for Ilitek Multi-Touch drivers. These are capacitive touchscreen drivers from AliExpress (https://www.aliexpress.com/item/4001166217016.html) and similar retailers. These touch drivers usually come with two interfaces in their usb descriptor, one with multi-touch support and a "mouse" mode. Under a linux host with evtest, the device works fine. I have been able to interface it with python succesfully. I am looking to use a feather RP2040 USB Host instead to talk to it. The test examples work, which is promising (see below for a printout of hid_device_report sketch). The issue is the device reports seems to only comes from a single interface, the one without multi-touch support.

examples/DualRole/hid/get_device_report.ino prints that both instance = 0 and instance = 1 are mounted. The HID report data though is only from the mouse mode. I believe that tinyusb is currently always mounting the "mouse mode" interface for the ILITEK Multi-Touch-V3000 device. I would like it to mount the multi-touch interface instead. See below for the device descriptor

In src/class/hid/hid_host.c i have tried to modify hidh_open() by adding this line at the start of the function, which will skip mounting if bInterfaceNumber == 1. This causes the microcontroller to mount/unmount the device continuously in a loop. if(desc_itf->bInterfaceNumber == 1) {return true;} I am not experienced enough to write my own driver for this device, I think a new CFG_TUH_XX definition under usbc.c needs to be made for this type of device (which is technically HID). Any help for adding support for this device to tinyusb is much appreciated. I also own 3 of these devices for testing.

Output of lsusb on a Linux host:

└─$ sudo lsusb                
Bus 001 Device 027: ID 222a:0001 ILI Technology Corp. Multi-Touch Screen
└─$ sudo lsusb -v -d 222a:0001

Bus 001 Device 027: ID 222a:0001 ILI Technology Corp. Multi-Touch Screen
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x222a ILI Technology Corp.
  idProduct          0x0001 Multi-Touch Screen
  bcdDevice            0.00
  iManufacturer           1 ILITEK
  iProduct                2 Multi-Touch-V3000
  iSerial                 0 
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x003b
    bNumInterfaces          2
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0x80
      (Bus Powered)
    MaxPower              200mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      0 
      bInterfaceProtocol      0 
      iInterface              0 
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.10
          bCountryCode            0 Not supported
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength     733
         Report Descriptors: 
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x82  EP 2 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        1
      bAlternateSetting       0
      bNumEndpoints           1
      bInterfaceClass         3 Human Interface Device
      bInterfaceSubClass      0 
      bInterfaceProtocol      0 
      iInterface              0 
        HID Device Descriptor:
          bLength                 9
          bDescriptorType        33
          bcdHID               1.10
          bCountryCode            0 Not supported
          bNumDescriptors         1
          bDescriptorType        34 Report
          wDescriptorLength     109
         Report Descriptors: 
           ** UNAVAILABLE **
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               1
Device Status:     0x0000
  (Bus Powered) 

Output of evtest (under Linux):

└─$ sudo evtest               
Available devices:
/dev/input/event6:      ILITEK Multi-Touch-V3000
/dev/input/event7:      ILITEK Multi-Touch-V3000 Mouse

Select the device event number [0-7]: 6   
Input driver version is 1.0.1
Input device ID: bus 0x3 vendor 0x222a product 0x1 version 0x110
Input device name: "ILITEK Multi-Touch-V3000"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 330 (BTN_TOUCH)
  Event type 3 (EV_ABS)
    Event code 0 (ABS_X)
      Value      0
      Min        0
      Max    16384
      Resolution      31
    Event code 1 (ABS_Y)
      Value      0
      Min        0
      Max     9600
      Resolution      32
    Event code 47 (ABS_MT_SLOT)
      Value      0
      Min        0
      Max        9
    Event code 53 (ABS_MT_POSITION_X)
      Value      0
      Min        0
      Max    16384
      Resolution      31
    Event code 54 (ABS_MT_POSITION_Y)
      Value      0
      Min        0
      Max     9600
      Resolution      32
    Event code 57 (ABS_MT_TRACKING_ID)
      Value      0
      Min        0
      Max    65535
  Event type 4 (EV_MSC)
    Event code 5 (MSC_TIMESTAMP)
Properties:
  Property type 1 (INPUT_PROP_DIRECT)
Testing ... (interrupt to exit)

Select the device event number [0-7]: 7
Input driver version is 1.0.1
Input device ID: bus 0x3 vendor 0x222a product 0x1 version 0x110
Input device name: "ILITEK Multi-Touch-V3000 Mouse"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 272 (BTN_LEFT)
    Event code 273 (BTN_RIGHT)
    Event code 274 (BTN_MIDDLE)
    Event code 275 (BTN_SIDE)
    Event code 276 (BTN_EXTRA)
  Event type 3 (EV_ABS)
    Event code 0 (ABS_X)
      Value      0
      Min        1
      Max    16384
      Resolution      31
    Event code 1 (ABS_Y)
      Value      0
      Min        1
      Max     9600
      Resolution      32
  Event type 4 (EV_MSC)
    Event code 4 (MSC_SCAN)
Properties:
Testing ... (interrupt to exit)

Output of examples/DualRole/Simple/device_info/device_info.ino:

Device attached, address = 1
Device 1: ID 222a:0001
Device Descriptor:
  bLength             18
  bDescriptorType     1
  bcdUSB              0200
  bDeviceClass        0
  bDeviceSubClass     0
  bDeviceProtocol     0
  bMaxPacketSize0     64
  idVendor            0x222a
  idProduct           0x0001
  bcdDevice           0000
  iManufacturer       1     ILITEK
  iProduct            2     Multi-Touch-V3000
  iSerialNumber       0     
  bNumConfigurations  1
Device 1: ID 222a:0001 ILITEK Multi-Touch-V3000

Output of examples/DualRole/HID/hid_device_report/hid_device_report.ino:

HID device address = 1, instance = 0 is mounted
VID = 222a, PID = 0001
HID device address = 1, instance = 1 is mounted
VID = 222a, PID = 0001
HID device address = 1, instance = 0 is unmounted
HID device address = 1, instance = 1 is unmounted
HIDreport :  0x0F 0x01 0x65 0x18 0x24 0x09 
HIDreport :  0x0F 0x01 0x65 0x18 0x24 0x09 
HIDreport :  0x0F 0x01 0x65 0x18 0x24 0x09 
HIDreport :  0x0F 0x01 0x65 0x18 0x24 0x09 
HIDreport :  0x0F 0x01 0x65 0x18 0x24 0x09 
HIDreport :  0x0F 0x01 0x65 0x18 0x24 0x09 
HIDreport :  0x0F 0x01 0x65 0x18 0x24 0x09 
HIDreport :  0x0F 0x01 0x56 0x18 0x3A 0x09