-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.go
333 lines (261 loc) · 6.13 KB
/
controller.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package gods4
import (
"encoding/binary"
"fmt"
"hash/crc32"
"sync"
"github.com/pkg/errors"
"github.com/kpeu3i/gods4/hid"
"github.com/kpeu3i/gods4/led"
"github.com/kpeu3i/gods4/rumble"
)
var (
ErrInvalidConnectionType = errors.New("ds4: can't detect connection type")
ErrControllerIsConnected = errors.New("ds4: controller is already connected")
ErrControllerIsNotConnected = errors.New("ds4: controller is not connected")
ErrControllerIsListening = errors.New("ds4: controller is already listening for events")
)
const getFeatureReportCode0x04 = 0x04
type Device interface {
VendorID() uint16
ProductID() uint16
Path() string
Release() uint16
Serial() string
Manufacturer() string
Product() string
Open() error
Close() error
Read(b []byte) (int, error)
Write(b []byte) (int, error)
GetFeatureReport(code byte) ([]byte, error)
}
type Controller struct {
mutex sync.RWMutex
device Device
connectionType ConnectionType
emitter *emitter
inputOffset uint
inputCurrState *state
inputPrevState *state
outputOffset uint
outputState []byte
isListening bool
errors chan error
quit chan struct{}
}
type Callback func(data interface{}) error
func NewController(device Device) *Controller {
return &Controller{
device: device,
connectionType: ConnectionTypeNone,
emitter: newEmitter(),
errors: make(chan error),
quit: make(chan struct{}),
}
}
func (c *Controller) Connect() error {
c.mutex.Lock()
defer c.mutex.Unlock()
err := c.errorIfConnected()
if err != nil {
return err
}
err = c.device.Open()
if err != nil {
return err
}
connectionType, err := detectConnectionType(c.device)
if err != nil {
return err
}
c.connectionType = connectionType
switch c.connectionType {
case ConnectionTypeBluetooth:
_, err = c.device.GetFeatureReport(getFeatureReportCode0x04)
if err != nil {
return err
}
c.inputOffset = 2
c.outputOffset = 3
c.outputState = make([]byte, 79)
c.outputState[0] = 0xA2
c.outputState[1] = 0x11
c.outputState[2] = 0x80
c.outputState[4] = 0x0F
case ConnectionTypeUSB:
c.inputOffset = 0
c.outputOffset = 0
c.outputState = make([]byte, 79)
c.outputState[0] = 0x05
c.outputState[1] = 0x07
}
return nil
}
func (c *Controller) Disconnect() error {
c.mutex.Lock()
defer c.mutex.Unlock()
err := c.errorIfNotConnected()
if err != nil {
return err
}
c.quit <- struct{}{}
err = c.device.Close()
if err != nil {
return err
}
c.connectionType = ConnectionTypeNone
c.errors <- nil
return nil
}
func (c *Controller) ConnectionType() ConnectionType {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.connectionType
}
func (c *Controller) Listen() error {
c.mutex.Lock()
err := c.errorIfNotConnected()
if err != nil {
return err
}
err = c.errorIfListening()
if err != nil {
return err
}
c.isListening = true
defer func() {
c.isListening = false
}()
go c.handle()
c.mutex.Unlock()
return <-c.errors
}
func (c *Controller) On(event Event, fn Callback) {
c.emitter.setCallback(event, fn)
}
func (c *Controller) Off(event Event) {
c.emitter.unsetCallback(event)
}
func (c *Controller) Rumble(rumble *rumble.Rumble) error {
c.mutex.Lock()
defer c.mutex.Unlock()
err := c.errorIfNotConnected()
if err != nil {
return err
}
patch := make(map[uint]byte, 2)
patch[4+c.outputOffset] = rumble.Left()
patch[5+c.outputOffset] = rumble.Right()
return c.set(patch)
}
func (c *Controller) Led(led *led.Led) error {
c.mutex.Lock()
defer c.mutex.Unlock()
err := c.errorIfNotConnected()
if err != nil {
return err
}
patch := make(map[uint]byte, 3)
patch[6+c.outputOffset] = led.Red()
patch[7+c.outputOffset] = led.Green()
patch[8+c.outputOffset] = led.Blue()
patch[9+c.outputOffset] = led.FlashOn()
patch[10+c.outputOffset] = led.FlashOff()
return c.set(patch)
}
func (c *Controller) handle() {
bytes := make([]byte, 64)
bytes[0+c.inputOffset] = 1
bytes[1+c.inputOffset] = 128
bytes[2+c.inputOffset] = 128
bytes[3+c.inputOffset] = 128
bytes[4+c.inputOffset] = 128
bytes[5+c.inputOffset] = 8
c.inputPrevState = newState(bytes, c.inputOffset, nil)
for {
select {
case <-c.quit:
return
default:
_, err := c.device.Read(bytes)
if err != nil {
c.errors <- err
return
}
c.inputCurrState = newState(bytes, c.inputOffset, c.inputPrevState)
err = c.emitter.emit(c.inputCurrState, c.inputPrevState)
if err != nil {
c.errors <- err
return
}
c.inputPrevState = c.inputCurrState
}
}
}
func (c *Controller) VendorID() uint16 {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.device.VendorID()
}
func (c *Controller) ProductID() uint16 {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.device.ProductID()
}
func (c *Controller) Name() string {
c.mutex.RLock()
defer c.mutex.RUnlock()
return c.device.Product()
}
func (c *Controller) String() string {
c.mutex.RLock()
defer c.mutex.RUnlock()
return fmt.Sprintf("%s (vendor: %v, product: %v)", c.device.Product(), c.device.VendorID(), c.device.ProductID())
}
func (c *Controller) set(patch map[uint]byte) error {
for i, b := range patch {
c.outputState[i] = b
}
switch c.connectionType {
case ConnectionTypeBluetooth:
crc := crc32.ChecksumIEEE(c.outputState[0:75])
binary.LittleEndian.PutUint32(c.outputState[75:], crc)
_, err := c.device.Write(c.outputState[1:])
if err != nil {
return err
}
case ConnectionTypeUSB:
_, err := c.device.Write(c.outputState)
if err != nil {
return err
}
}
return nil
}
func (c *Controller) errorIfConnected() error {
if c.connectionType != ConnectionTypeNone {
return ErrControllerIsConnected
}
return nil
}
func (c *Controller) errorIfNotConnected() error {
if c.connectionType == ConnectionTypeNone {
return ErrControllerIsNotConnected
}
return nil
}
func (c *Controller) errorIfListening() error {
if c.isListening {
return ErrControllerIsListening
}
return nil
}
func Find() []*Controller {
devices := hid.Find()
controllers := make([]*Controller, 0, len(devices))
for _, device := range devices {
controllers = append(controllers, NewController(device))
}
return controllers
}