mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 23:13:40 +00:00
machine/usb: allow USB Endpoint settings to be changed externally
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"machine"
|
||||
"machine/usb"
|
||||
"machine/usb/descriptor"
|
||||
)
|
||||
|
||||
// from usb-hid.go
|
||||
@@ -32,7 +33,21 @@ var size int
|
||||
// calls machine.EnableHID for USB configuration
|
||||
func SetHandler(d hidDevicer) {
|
||||
if size == 0 {
|
||||
machine.EnableHID(handler, nil, setupHandler)
|
||||
machine.ConfigureUSBEndpoint(descriptor.CDCHID,
|
||||
[]usb.EndpointConfig{
|
||||
{
|
||||
No: usb.HID_ENDPOINT_IN,
|
||||
IsIn: true,
|
||||
Type: usb.ENDPOINT_TYPE_INTERRUPT,
|
||||
TxHandler: handler,
|
||||
},
|
||||
},
|
||||
[]usb.SetupConfig{
|
||||
{
|
||||
No: usb.HID_INTERFACE,
|
||||
Handler: setupHandler,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
devices[size] = d
|
||||
|
||||
@@ -3,6 +3,7 @@ package joystick
|
||||
import (
|
||||
"machine"
|
||||
"machine/usb"
|
||||
"machine/usb/descriptor"
|
||||
"machine/usb/hid"
|
||||
)
|
||||
|
||||
@@ -32,18 +33,50 @@ func UseSettings(def Definitions, rxHandlerFunc func(b []byte), setupFunc func(s
|
||||
if setupFunc == nil {
|
||||
setupFunc = hid.DefaultSetupHandler
|
||||
}
|
||||
machine.EnableJoystick(js.handler, rxHandlerFunc, setupFunc, hidDesc)
|
||||
if rxHandlerFunc == nil {
|
||||
rxHandlerFunc = js.rxHandlerFunc
|
||||
}
|
||||
if len(hidDesc) == 0 {
|
||||
hidDesc = descriptor.JoystickDefaultHIDReport
|
||||
}
|
||||
class, err := descriptor.FindClassHIDType(descriptor.CDCJoystick.Configuration, descriptor.ClassHIDJoystick.Bytes())
|
||||
if err != nil {
|
||||
// TODO: some way to notify about error
|
||||
return nil
|
||||
}
|
||||
|
||||
class.ClassLength(uint16(len(hidDesc)))
|
||||
descriptor.CDCJoystick.HID[2] = hidDesc
|
||||
|
||||
machine.ConfigureUSBEndpoint(descriptor.CDCJoystick,
|
||||
[]usb.EndpointConfig{
|
||||
{
|
||||
No: usb.HID_ENDPOINT_OUT,
|
||||
IsIn: false,
|
||||
Type: usb.ENDPOINT_TYPE_INTERRUPT,
|
||||
RxHandler: rxHandlerFunc,
|
||||
},
|
||||
{
|
||||
No: usb.HID_ENDPOINT_IN,
|
||||
IsIn: true,
|
||||
Type: usb.ENDPOINT_TYPE_INTERRUPT,
|
||||
TxHandler: js.handler,
|
||||
},
|
||||
},
|
||||
[]usb.SetupConfig{
|
||||
{
|
||||
No: usb.HID_INTERFACE,
|
||||
Handler: setupFunc,
|
||||
},
|
||||
},
|
||||
)
|
||||
Joystick = js
|
||||
return js
|
||||
}
|
||||
|
||||
func newDefaultJoystick() *joystick {
|
||||
def := DefaultDefinitions()
|
||||
js := &joystick{
|
||||
State: def.NewState(),
|
||||
buf: hid.NewRingBuffer(),
|
||||
}
|
||||
machine.EnableJoystick(js.handler, js.rxHandler, hid.DefaultSetupHandler, def.Descriptor())
|
||||
js := UseSettings(def, nil, nil, nil)
|
||||
return js
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user