aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorUnrud <[email protected]>2023-08-05 15:23:20 +0200
committerRon Evans <[email protected]>2023-08-07 14:00:32 +0200
commit0ef86e1534d15f2b291dc2512c9c877138c0887f (patch)
tree196f6c25050a760e43ad93eb04da44e0c33c402b
parent72270f90529a4f138517ce6c5a15095b0c625079 (diff)
downloadtinygo-0ef86e1534d15f2b291dc2512c9c877138c0887f.tar.gz
tinygo-0ef86e1534d15f2b291dc2512c9c877138c0887f.zip
Add support for HID Keyboard LEDs
-rw-r--r--src/machine/usb/descriptor/hid.go14
-rw-r--r--src/machine/usb/descriptor/hidreport.go7
-rw-r--r--src/machine/usb/hid/hid.go6
-rw-r--r--src/machine/usb/hid/keyboard/keyboard.go15
4 files changed, 40 insertions, 2 deletions
diff --git a/src/machine/usb/descriptor/hid.go b/src/machine/usb/descriptor/hid.go
index 8135c2a60..cdd4fc7e5 100644
--- a/src/machine/usb/descriptor/hid.go
+++ b/src/machine/usb/descriptor/hid.go
@@ -26,7 +26,7 @@ var interfaceHID = [interfaceTypeLen]byte{
TypeInterface,
0x02, // InterfaceNumber
0x00, // AlternateSetting
- 0x01, // NumEndpoints
+ 0x02, // NumEndpoints
0x03, // InterfaceClass
0x00, // InterfaceSubClass
0x00, // InterfaceProtocol
@@ -103,7 +103,7 @@ var classHID = [ClassHIDTypeLen]byte{
0x00, // CountryCode
0x01, // NumDescriptors
0x22, // ClassType
- 0x7E, // ClassLength L
+ 0x90, // ClassLength L
0x00, // ClassLength H
}
@@ -128,6 +128,7 @@ var CDCHID = Descriptor{
InterfaceHID.Bytes(),
ClassHID.Bytes(),
EndpointEP4IN.Bytes(),
+ EndpointEP5OUT.Bytes(),
}),
HID: map[uint16][]byte{
2: Append([][]byte{
@@ -147,6 +148,15 @@ var CDCHID = Descriptor{
HIDReportCount(1),
HIDReportSize(8),
HIDInputConstVarAbs,
+ HIDReportCount(3),
+ HIDReportSize(1),
+ HIDUsagePageLED,
+ HIDUsageMinimum(1),
+ HIDUsageMaximum(3),
+ HIDOutputDataVarAbs,
+ HIDReportCount(5),
+ HIDReportSize(1),
+ HIDOutputConstVarAbs,
HIDReportCount(6),
HIDReportSize(8),
HIDLogicalMinimum(0),
diff --git a/src/machine/usb/descriptor/hidreport.go b/src/machine/usb/descriptor/hidreport.go
index 0d90d47c0..5819f6ad6 100644
--- a/src/machine/usb/descriptor/hidreport.go
+++ b/src/machine/usb/descriptor/hidreport.go
@@ -13,6 +13,7 @@ const (
hidUnit = 0x65
hidCollection = 0xa1
hidInput = 0x81
+ hidOutput = 0x91
hidReportSize = 0x75
hidReportCount = 0x95
hidReportID = 0x85
@@ -121,6 +122,12 @@ var (
// Input (Data, Variable, Relative), 2 position bytes (X & Y)
HIDInputDataVarRel = []byte{hidInput, 0x06}
+
+ // Output (Data, Variable, Absolute), Modifier byte
+ HIDOutputDataVarAbs = []byte{hidOutput, 0x02}
+
+ // Output (Const, Variable, Absolute), Modifier byte
+ HIDOutputConstVarAbs = []byte{hidOutput, 0x03}
)
func HIDReportSize(size int) []byte {
diff --git a/src/machine/usb/hid/hid.go b/src/machine/usb/hid/hid.go
index f7f1ff66b..791fd06a6 100644
--- a/src/machine/usb/hid/hid.go
+++ b/src/machine/usb/hid/hid.go
@@ -37,6 +37,12 @@ func SetHandler(d hidDevicer) {
machine.ConfigureUSBEndpoint(descriptor.CDCHID,
[]usb.EndpointConfig{
{
+ Index: usb.HID_ENDPOINT_OUT,
+ IsIn: false,
+ Type: usb.ENDPOINT_TYPE_INTERRUPT,
+ RxHandler: rxHandler,
+ },
+ {
Index: usb.HID_ENDPOINT_IN,
IsIn: true,
Type: usb.ENDPOINT_TYPE_INTERRUPT,
diff --git a/src/machine/usb/hid/keyboard/keyboard.go b/src/machine/usb/hid/keyboard/keyboard.go
index 237d79419..9f5f420ed 100644
--- a/src/machine/usb/hid/keyboard/keyboard.go
+++ b/src/machine/usb/hid/keyboard/keyboard.go
@@ -92,6 +92,9 @@ func (kb *keyboard) TxHandler() bool {
}
func (kb *keyboard) RxHandler(b []byte) bool {
+ if len(b) >= 2 && b[0] == 2 /* ReportID */ {
+ kb.led = b[1]
+ }
return false
}
@@ -106,6 +109,18 @@ func (kb *keyboard) tx(b []byte) {
}
}
+func (kb *keyboard) NumLockLed() bool {
+ return kb.led&1 != 0
+}
+
+func (kb *keyboard) CapsLockLed() bool {
+ return kb.led&2 != 0
+}
+
+func (kb *keyboard) ScrollLockLed() bool {
+ return kb.led&4 != 0
+}
+
func (kb *keyboard) ready() bool {
return true
}