Skip to content

Commit c38bf27

Browse files
Unruddeadprogram
authored andcommitted
machine/usb/hid/joystick: Allow more hat switches
1 parent 9a2fab8 commit c38bf27

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/machine/usb/hid/joystick/state.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ func (c Definitions) Descriptor() []byte {
6767

6868
func (c Definitions) NewState() State {
6969
bufSize := 1
70+
hatSwitches := make([]HatDirection, c.HatSwitchCnt)
71+
for i := range hatSwitches {
72+
hatSwitches[i] = HatCenter
73+
}
7074
axises := make([]*AxisValue, 0, len(c.AxisDefs))
7175
for _, v := range c.AxisDefs {
7276

@@ -77,16 +81,14 @@ func (c Definitions) NewState() State {
7781
}
7882
btnSize := (c.ButtonCnt + 7) / 8
7983
bufSize += btnSize
80-
if c.HatSwitchCnt > 0 {
81-
bufSize++
82-
}
84+
bufSize += (len(hatSwitches) + 1) / 2
8385
bufSize += len(axises) * 2
8486
initBuf := make([]byte, bufSize)
8587
initBuf[0] = c.ReportID
8688
return State{
8789
buf: initBuf,
8890
Buttons: make([]byte, btnSize),
89-
HatSwitches: make([]HatDirection, c.HatSwitchCnt),
91+
HatSwitches: hatSwitches,
9092
Axises: axises,
9193
}
9294
}
@@ -103,7 +105,11 @@ func (s State) MarshalBinary() ([]byte, error) {
103105
s.buf = append(s.buf, s.Buttons...)
104106
if len(s.HatSwitches) > 0 {
105107
hat := byte(0)
106-
for _, v := range s.HatSwitches {
108+
for i, v := range s.HatSwitches {
109+
if i != 0 && i%2 == 0 {
110+
s.buf = append(s.buf, hat)
111+
hat = 0
112+
}
107113
hat <<= 4
108114
hat |= byte(v & 0xf)
109115
}

0 commit comments

Comments
 (0)