-
Notifications
You must be signed in to change notification settings - Fork 2
/
state.go
301 lines (241 loc) · 6.73 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package gods4
import (
"encoding/binary"
"math"
)
const analogSticksSmoothing = 4
type state struct {
cross bool
circle bool
square bool
triangle bool
l1 bool
l2 byte
l3 bool
r1 bool
r2 byte
r3 bool
dPadUp bool
dPadDown bool
dPadLeft bool
dPadRight bool
share bool
options bool
ps bool
leftStick Stick
rightStick Stick
touchpad Touchpad
accelerometer Accelerometer
gyroscope Gyroscope
battery Battery
}
type Stick struct {
X byte
Y byte
}
type Touchpad struct {
Press bool
Swipe []Touch
}
type Touch struct {
IsActive bool
X byte
Y byte
}
type Accelerometer struct {
X int16
Y int16
Z int16
}
type Gyroscope struct {
Roll int16
Yaw int16
Pitch int16
}
type Battery struct {
Capacity byte
IsCharging bool
IsCableConnected bool
}
func newState(bytes []byte, offset uint, prevState *state) *state {
s := &state{
cross: buttonCrossState(bytes, offset),
circle: buttonCircleState(bytes, offset),
square: buttonSquareState(bytes, offset),
triangle: buttonTriangleState(bytes, offset),
l1: buttonL1State(bytes, offset),
l2: buttonL2State(bytes, offset),
l3: buttonL3State(bytes, offset),
r1: buttonR1State(bytes, offset),
r2: buttonR2State(bytes, offset),
r3: buttonR3State(bytes, offset),
dPadUp: buttonDPadUpState(bytes, offset),
dPadDown: buttonDPadDownState(bytes, offset),
dPadLeft: buttonDPadLeftState(bytes, offset),
dPadRight: buttonDPadRightState(bytes, offset),
share: buttonShareState(bytes, offset),
options: buttonOptionsState(bytes, offset),
ps: buttonPSState(bytes, offset),
leftStick: buttonLeftStickState(bytes, offset, prevState),
rightStick: buttonRightStickState(bytes, offset, prevState),
touchpad: touchpadState(bytes, offset),
accelerometer: accelerometerState(bytes, offset),
gyroscope: gyroscopeState(bytes, offset),
battery: batteryState(bytes, offset),
}
return s
}
func buttonCrossState(bytes []byte, offset uint) bool {
return bytes[5+offset]&32 != 0
}
func buttonCircleState(bytes []byte, offset uint) bool {
return bytes[5+offset]&64 != 0
}
func buttonSquareState(bytes []byte, offset uint) bool {
return bytes[5+offset]&16 != 0
}
func buttonTriangleState(bytes []byte, offset uint) bool {
return bytes[5+offset]&128 != 0
}
func buttonL1State(bytes []byte, offset uint) bool {
return bytes[6+offset]&1 != 0
}
func buttonL2State(bytes []byte, offset uint) byte {
if bytes[6+offset]&4 != 0 {
return bytes[8+offset]
}
return 0
}
func buttonL3State(bytes []byte, offset uint) bool {
return bytes[6+offset]&64 != 0
}
func buttonR1State(bytes []byte, offset uint) bool {
return bytes[6+offset]&2 != 0
}
func buttonR2State(bytes []byte, offset uint) byte {
if bytes[6+offset]&8 != 0 {
return bytes[9+offset]
}
return 0
}
func buttonR3State(bytes []byte, offset uint) bool {
return bytes[6+offset]&128 != 0
}
func buttonDPadUpState(bytes []byte, offset uint) bool {
v := bytes[5+offset] & 15
return v == 0 || v == 1 || v == 7
}
func buttonDPadDownState(bytes []byte, offset uint) bool {
v := bytes[5+offset] & 15
return v == 3 || v == 4 || v == 5
}
func buttonDPadLeftState(bytes []byte, offset uint) bool {
v := bytes[5+offset] & 15
return v == 5 || v == 6 || v == 7
}
func buttonDPadRightState(bytes []byte, offset uint) bool {
v := bytes[5+offset] & 15
return v == 1 || v == 2 || v == 3
}
func buttonShareState(bytes []byte, offset uint) bool {
return bytes[6+offset]&16 != 0
}
func buttonOptionsState(bytes []byte, offset uint) bool {
return bytes[6+offset]&32 != 0
}
//func buttonTouchpadState(bytes []byte, offset uint) bool {
// return bytes[7+offset]&2 != 0
//}
func buttonPSState(bytes []byte, offset uint) bool {
return bytes[7+offset]&1 != 0
}
func buttonLeftStickState(bytes []byte, offset uint, prevState *state) Stick {
var prevX, prevY byte
if prevState == nil {
prevX, prevY = bytes[1+offset], bytes[2+offset]
} else {
prevX, prevY = prevState.leftStick.X, prevState.leftStick.Y
}
if math.Abs(float64(bytes[1+offset])-float64(prevX)) >= float64(analogSticksSmoothing) ||
math.Abs(float64(bytes[2+offset])-float64(prevY)) >= float64(analogSticksSmoothing) {
return Stick{X: bytes[1+offset], Y: bytes[2+offset]}
}
return Stick{X: prevX, Y: prevY}
}
func buttonRightStickState(bytes []byte, offset uint, prevState *state) Stick {
var prevX, prevY byte
if prevState == nil {
prevX, prevY = bytes[3+offset], bytes[4+offset]
} else {
prevX, prevY = prevState.rightStick.X, prevState.rightStick.Y
}
if math.Abs(float64(bytes[3+offset])-float64(prevX)) >= float64(analogSticksSmoothing) ||
math.Abs(float64(bytes[4+offset])-float64(prevY)) >= float64(analogSticksSmoothing) {
return Stick{X: bytes[3+offset], Y: bytes[4+offset]}
}
return Stick{X: prevX, Y: prevY}
}
func touchpadState(bytes []byte, offset uint) Touchpad {
var (
touches []Touch
touchOffset uint
)
for i := 1; i <= 2; i++ {
touch := Touch{
IsActive: (bytes[35+touchOffset+offset] >> 7) == 0,
X: ((bytes[37+touchOffset+offset] & 0x0F) << 8) | bytes[36+touchOffset+offset],
Y: bytes[38+touchOffset+offset]<<4 | ((bytes[37+touchOffset+offset] & 0xF0) >> 4),
}
touches = append(touches, touch)
touchOffset += 4
}
t := Touchpad{
Press: bytes[7+offset]&2 != 0,
Swipe: touches,
}
return t
}
func accelerometerState(bytes []byte, offset uint) Accelerometer {
a := Accelerometer{
X: int16(binary.LittleEndian.Uint16(bytes[13+offset:])),
Y: -int16(binary.LittleEndian.Uint16(bytes[15+offset:])),
Z: -int16(binary.LittleEndian.Uint16(bytes[17+offset:])),
}
return a
}
func gyroscopeState(bytes []byte, offset uint) Gyroscope {
g := Gyroscope{
Roll: -int16(binary.LittleEndian.Uint16(bytes[19+offset:])),
Yaw: int16(binary.LittleEndian.Uint16(bytes[21+offset:])),
Pitch: int16(binary.LittleEndian.Uint16(bytes[23+offset:])),
}
return g
}
func batteryState(bytes []byte, offset uint) Battery {
var (
isCharging bool
maxCapacity byte
)
capacity := bytes[30+offset] & 0x0F
isCableConnected := ((bytes[30+offset] >> 4) & 0x01) == 1
if !isCableConnected || capacity > 10 {
isCharging = false
} else {
isCharging = true
}
if isCableConnected {
maxCapacity = 10
} else {
maxCapacity = 9
}
if capacity > maxCapacity {
capacity = maxCapacity
}
capacity = byte(float64(float64(capacity) / float64(maxCapacity) * 100))
battery := Battery{
Capacity: capacity,
IsCharging: isCharging,
IsCableConnected: isCableConnected,
}
return battery
}