1
1
package descriptor
2
2
3
+ import "math"
4
+
3
5
const (
4
6
hidUsagePage = 0x05
5
7
hidUsage = 0x09
@@ -130,6 +132,28 @@ var (
130
132
HIDOutputConstVarAbs = []byte {hidOutput , 0x03 }
131
133
)
132
134
135
+ func hidShortItem (tag byte , value uint32 ) []byte {
136
+ switch {
137
+ case value <= math .MaxUint8 :
138
+ return []byte {tag | hidSizeValue1 , byte (value )}
139
+ case value <= math .MaxUint16 :
140
+ return []byte {tag | hidSizeValue2 , byte (value ), byte (value >> 8 )}
141
+ default :
142
+ return []byte {tag | hidSizeValue4 , byte (value ), byte (value >> 8 ), byte (value >> 16 ), byte (value >> 24 )}
143
+ }
144
+ }
145
+
146
+ func hidShortItemSigned (tag byte , value int32 ) []byte {
147
+ switch {
148
+ case math .MinInt8 <= value && value <= math .MaxInt8 :
149
+ return []byte {tag | hidSizeValue1 , byte (value )}
150
+ case math .MinInt16 <= value && value <= math .MaxInt16 :
151
+ return []byte {tag | hidSizeValue2 , byte (value ), byte (value >> 8 )}
152
+ default :
153
+ return []byte {tag | hidSizeValue4 , byte (value ), byte (value >> 8 ), byte (value >> 16 ), byte (value >> 24 )}
154
+ }
155
+ }
156
+
133
157
func HIDReportSize (size int ) []byte {
134
158
return []byte {hidReportSize , byte (size )}
135
159
}
@@ -143,69 +167,27 @@ func HIDReportID(id int) []byte {
143
167
}
144
168
145
169
func HIDLogicalMinimum (min int ) []byte {
146
- switch {
147
- case min < - 32767 || 65535 < min :
148
- return []byte {hidLogicalMinimum + hidSizeValue4 , uint8 (min ), uint8 (min >> 8 ), uint8 (min >> 16 ), uint8 (min >> 24 )}
149
- case min < - 127 || 255 < min :
150
- return []byte {hidLogicalMinimum + hidSizeValue2 , uint8 (min ), uint8 (min >> 8 )}
151
- default :
152
- return []byte {hidLogicalMinimum + hidSizeValue1 , byte (min )}
153
- }
170
+ return hidShortItemSigned (hidLogicalMinimum , int32 (min ))
154
171
}
155
172
156
173
func HIDLogicalMaximum (max int ) []byte {
157
- switch {
158
- case max < - 32767 || 65535 < max :
159
- return []byte {hidLogicalMaximum + hidSizeValue4 , uint8 (max ), uint8 (max >> 8 ), uint8 (max >> 16 ), uint8 (max >> 24 )}
160
- case max < - 127 || 255 < max :
161
- return []byte {hidLogicalMaximum + hidSizeValue2 , uint8 (max ), uint8 (max >> 8 )}
162
- default :
163
- return []byte {hidLogicalMaximum + hidSizeValue1 , byte (max )}
164
- }
174
+ return hidShortItemSigned (hidLogicalMaximum , int32 (max ))
165
175
}
166
176
167
177
func HIDUsageMinimum (min int ) []byte {
168
- switch {
169
- case min < - 32767 || 65535 < min :
170
- return []byte {hidUsageMinimum + hidSizeValue4 , uint8 (min ), uint8 (min >> 8 ), uint8 (min >> 16 ), uint8 (min >> 24 )}
171
- case min < - 127 || 255 < min :
172
- return []byte {hidUsageMinimum + hidSizeValue2 , uint8 (min ), uint8 (min >> 8 )}
173
- default :
174
- return []byte {hidUsageMinimum + hidSizeValue1 , byte (min )}
175
- }
178
+ return hidShortItem (hidUsageMinimum , uint32 (min ))
176
179
}
177
180
178
181
func HIDUsageMaximum (max int ) []byte {
179
- switch {
180
- case max < - 32767 || 65535 < max :
181
- return []byte {hidUsageMaximum + hidSizeValue4 , uint8 (max ), uint8 (max >> 8 ), uint8 (max >> 16 ), uint8 (max >> 24 )}
182
- case max < - 127 || 255 < max :
183
- return []byte {hidUsageMaximum + hidSizeValue2 , uint8 (max ), uint8 (max >> 8 )}
184
- default :
185
- return []byte {hidUsageMaximum + hidSizeValue1 , byte (max )}
186
- }
182
+ return hidShortItem (hidUsageMaximum , uint32 (max ))
187
183
}
188
184
189
185
func HIDPhysicalMinimum (min int ) []byte {
190
- switch {
191
- case min < - 32767 || 65535 < min :
192
- return []byte {hidPhysicalMinimum + hidSizeValue4 , uint8 (min ), uint8 (min >> 8 ), uint8 (min >> 16 ), uint8 (min >> 24 )}
193
- case min < - 127 || 255 < min :
194
- return []byte {hidPhysicalMinimum + hidSizeValue2 , uint8 (min ), uint8 (min >> 8 )}
195
- default :
196
- return []byte {hidPhysicalMinimum + hidSizeValue1 , byte (min )}
197
- }
186
+ return hidShortItemSigned (hidPhysicalMinimum , int32 (min ))
198
187
}
199
188
200
189
func HIDPhysicalMaximum (max int ) []byte {
201
- switch {
202
- case max < - 32767 || 65535 < max :
203
- return []byte {hidPhysicalMaximum + hidSizeValue4 , uint8 (max ), uint8 (max >> 8 ), uint8 (max >> 16 ), uint8 (max >> 24 )}
204
- case max < - 127 || 255 < max :
205
- return []byte {hidPhysicalMaximum + hidSizeValue2 , uint8 (max ), uint8 (max >> 8 )}
206
- default :
207
- return []byte {hidPhysicalMaximum + hidSizeValue1 , byte (max )}
208
- }
190
+ return hidShortItemSigned (hidPhysicalMaximum , int32 (max ))
209
191
}
210
192
211
193
func HIDUnitExponent (exp int ) []byte {
0 commit comments