|
- import codecs
- import struct
-
- from usb_protocol.types import USBTransferType
- from usb_protocol.emitters.descriptors import DeviceDescriptorCollection, get_string_descriptor
-
-
- # Imported defines from usbd_def.h
- USBD_IDX_LANGID_STR = 0x00
- USBD_IDX_MFC_STR = 0x01
- USBD_IDX_PRODUCT_STR = 0x02
- USBD_IDX_SERIAL_STR = 0x03
- USBD_IDX_CONFIG_STR = 0x04
- USBD_IDX_INTERFACE_STR = 0x05
-
- USB_CLASS_HID = 0x03
- USB_CLASS_HID_NO_BOOT = 0x00
- USB_CLASS_HID_BOOT = 0x01
- USB_PROTOCOL_HID_NONE = 0x00
- USB_PROTOCOL_HID_KEYBOARD = 0x01
- USB_PROTOCOL_HID_MOUSE = 0x02
-
- collection = DeviceDescriptorCollection()
-
- with collection.DeviceDescriptor() as d:
- # https://pid.codes/1209/
- d.idVendor = 0x1209
- d.idProduct = 0x001
- d.bNumConfigurations = 1
-
- # Hack for ST reference code
- d.iProduct = USBD_IDX_PRODUCT_STR
- collection.add_descriptor(get_string_descriptor('RS-485 to HID'), d.iProduct)
-
- with collection.ConfigurationDescriptor() as c:
- with c.InterfaceDescriptor() as i:
- i.bInterfaceNumber = 0
- i.bInterfaceClass = USB_CLASS_HID
- i.bInterfaceSubclass = USB_CLASS_HID_BOOT
- i.bInterfaceProtocol = USB_PROTOCOL_HID_KEYBOARD
-
- report_desc = codecs.decode('05010906a101050719e029e71500250175019508810295017508810195057501050819012905910295017503910195067508150025650507190029658100c0', 'hex')
- collection.add_descriptor(report_desc, descriptor_type=0x22)
-
- hid_desc = codecs.decode('09211101000122', 'hex') + struct.pack('<H', len(report_desc))
-
- # add HID Descriptor after interface, but before
- # endpoint descriptors per HID v1.11 ยง 7.1
- i.add_subordinate_descriptor(hid_desc)
-
- # No OUT endpoint. Use the SET_REPORT via the control EP
-
- with i.EndpointDescriptor() as e:
- e.bEndpointAddress = 0x81
-
- e.bmAttributes = USBTransferType.INTERRUPT
-
- # it'd be good to adjust based upon FS vs LS
- e.bInterval = 10 # polling interval (LS/FS in ms)
-
- # Full-speed max size
- e.wMaxPacketSize = 64
|