@@ -67,6 +67,10 @@ func (c Definitions) Descriptor() []byte {
67
67
68
68
func (c Definitions ) NewState () State {
69
69
bufSize := 1
70
+ hatSwitches := make ([]HatDirection , c .HatSwitchCnt )
71
+ for i := range hatSwitches {
72
+ hatSwitches [i ] = HatCenter
73
+ }
70
74
axises := make ([]* AxisValue , 0 , len (c .AxisDefs ))
71
75
for _ , v := range c .AxisDefs {
72
76
@@ -77,16 +81,14 @@ func (c Definitions) NewState() State {
77
81
}
78
82
btnSize := (c .ButtonCnt + 7 ) / 8
79
83
bufSize += btnSize
80
- if c .HatSwitchCnt > 0 {
81
- bufSize ++
82
- }
84
+ bufSize += (len (hatSwitches ) + 1 ) / 2
83
85
bufSize += len (axises ) * 2
84
86
initBuf := make ([]byte , bufSize )
85
87
initBuf [0 ] = c .ReportID
86
88
return State {
87
89
buf : initBuf ,
88
90
Buttons : make ([]byte , btnSize ),
89
- HatSwitches : make ([] HatDirection , c . HatSwitchCnt ) ,
91
+ HatSwitches : hatSwitches ,
90
92
Axises : axises ,
91
93
}
92
94
}
@@ -103,7 +105,11 @@ func (s State) MarshalBinary() ([]byte, error) {
103
105
s .buf = append (s .buf , s .Buttons ... )
104
106
if len (s .HatSwitches ) > 0 {
105
107
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
+ }
107
113
hat <<= 4
108
114
hat |= byte (v & 0xf )
109
115
}
0 commit comments