3
3
package machine
4
4
5
5
import (
6
- "bytes"
7
- "encoding/binary"
8
- "errors"
9
6
"machine/usb"
7
+ "machine/usb/descriptor"
8
+
9
+ "errors"
10
10
)
11
11
12
12
type USBDevice struct {
@@ -29,7 +29,7 @@ type Serialer interface {
29
29
RTS () bool
30
30
}
31
31
32
- var usbDescriptor = usb . DescriptorCDC
32
+ var usbDescriptor = descriptor . CDC
33
33
34
34
var usbDescriptorConfig uint8 = usb .DescriptorConfigCDC
35
35
@@ -72,7 +72,7 @@ func usbProduct() string {
72
72
// binary.
73
73
func strToUTF16LEDescriptor (in string , out []byte ) {
74
74
out [0 ] = byte (len (out ))
75
- out [1 ] = usb . STRING_DESCRIPTOR_TYPE
75
+ out [1 ] = descriptor . TypeString
76
76
for i , rune := range in {
77
77
out [(i << 1 )+ 2 ] = byte (rune )
78
78
out [(i << 1 )+ 3 ] = 0
88
88
)
89
89
90
90
var (
91
- usbEndpointDescriptors [usb .NumberOfEndpoints ]usb. DeviceDescriptor
91
+ usbEndpointDescriptors [usb .NumberOfEndpoints ]descriptor. Device
92
92
93
93
isEndpointHalt = false
94
94
isRemoteWakeUpEnabled = false
@@ -134,27 +134,27 @@ var (
134
134
// can be requested by the host.
135
135
func sendDescriptor (setup usb.Setup ) {
136
136
switch setup .WValueH {
137
- case usb . CONFIGURATION_DESCRIPTOR_TYPE :
137
+ case descriptor . TypeConfiguration :
138
138
sendUSBPacket (0 , usbDescriptor .Configuration , setup .WLength )
139
139
return
140
- case usb . DEVICE_DESCRIPTOR_TYPE :
140
+ case descriptor . TypeDevice :
141
141
// composite descriptor
142
142
switch {
143
143
case (usbDescriptorConfig & usb .DescriptorConfigHID ) > 0 :
144
- usbDescriptor = usb . DescriptorCDCHID
144
+ usbDescriptor = descriptor . CDCHID
145
145
case (usbDescriptorConfig & usb .DescriptorConfigMIDI ) > 0 :
146
- usbDescriptor = usb . DescriptorCDCMIDI
146
+ usbDescriptor = descriptor . CDCMIDI
147
147
case (usbDescriptorConfig & usb .DescriptorConfigJoystick ) > 0 :
148
- usbDescriptor = usb . DescriptorCDCJoystick
148
+ usbDescriptor = descriptor . CDCJoystick
149
149
default :
150
- usbDescriptor = usb . DescriptorCDC
150
+ usbDescriptor = descriptor . CDC
151
151
}
152
152
153
153
usbDescriptor .Configure (usbVendorID (), usbProductID ())
154
154
sendUSBPacket (0 , usbDescriptor .Device , setup .WLength )
155
155
return
156
156
157
- case usb . STRING_DESCRIPTOR_TYPE :
157
+ case descriptor . TypeString :
158
158
switch setup .WValueL {
159
159
case 0 :
160
160
usb_trans_buffer [0 ] = 0x04
@@ -178,12 +178,12 @@ func sendDescriptor(setup usb.Setup) {
178
178
SendZlp ()
179
179
}
180
180
return
181
- case usb . HID_REPORT_TYPE :
181
+ case descriptor . TypeHIDReport :
182
182
if h , ok := usbDescriptor .HID [setup .WIndex ]; ok {
183
183
sendUSBPacket (0 , h , setup .WLength )
184
184
return
185
185
}
186
- case usb . DEVICE_QUALIFIER :
186
+ case descriptor . TypeDeviceQualifier :
187
187
// skip
188
188
default :
189
189
}
@@ -302,11 +302,10 @@ func EnableMIDI(txHandler func(), rxHandler func([]byte), setupHandler func(usb.
302
302
303
303
// EnableJoystick enables HID. This function must be executed from the init().
304
304
func EnableJoystick (txHandler func (), rxHandler func ([]byte ), setupHandler func (usb.Setup ) bool , hidDesc []byte ) {
305
- idx := bytes .Index (usb .DescriptorCDCJoystick .Configuration , []byte {
306
- 0x09 , 0x21 , 0x11 , 0x01 , 0x00 , 0x01 , 0x22 ,
307
- })
308
- binary .LittleEndian .PutUint16 (usb .DescriptorCDCJoystick .Configuration [idx + 7 :idx + 9 ], uint16 (len (hidDesc )))
309
- usb .DescriptorCDCJoystick .HID [2 ] = hidDesc
305
+ class := descriptor .FindClassHIDType (descriptor .CDCJoystick .Configuration , descriptor .ClassHIDJoystick .Bytes ())
306
+ class .ClassLength (uint16 (len (hidDesc )))
307
+ descriptor .CDCJoystick .HID [2 ] = hidDesc
308
+
310
309
usbDescriptorConfig |= usb .DescriptorConfigJoystick
311
310
endPoints [usb .HID_ENDPOINT_OUT ] = (usb .ENDPOINT_TYPE_INTERRUPT | usb .EndpointOut )
312
311
usbRxHandler [usb .HID_ENDPOINT_OUT ] = rxHandler
0 commit comments