-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnscon.go
287 lines (256 loc) · 7.44 KB
/
nscon.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
// SPDX-License-Identifier: GPL-3.0-only
package nscon
import (
"encoding/hex"
"errors"
"log"
"math"
"os"
"time"
)
var SPI_ROM_DATA = map[byte][]byte{
0x60: []byte{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0x03, 0xa0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0xff, 0xff, 0xff, 0xff,
0xf0, 0xff, 0x89, 0x00, 0xf0, 0x01, 0x00, 0x40, 0x00, 0x40, 0x00, 0x40, 0xf9, 0xff, 0x06, 0x00,
0x09, 0x00, 0xe7, 0x3b, 0xe7, 0x3b, 0xe7, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xba, 0x15, 0x62,
0x11, 0xb8, 0x7f, 0x29, 0x06, 0x5b, 0xff, 0xe7, 0x7e, 0x0e, 0x36, 0x56, 0x9e, 0x85, 0x60, 0xff,
0x32, 0x32, 0x32, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0x50, 0xfd, 0x00, 0x00, 0xc6, 0x0f, 0x0f, 0x30, 0x61, 0x96, 0x30, 0xf3, 0xd4, 0x14, 0x54, 0x41,
0x15, 0x54, 0xc7, 0x79, 0x9c, 0x33, 0x36, 0x63, 0x0f, 0x30, 0x61, 0x96, 0x30, 0xf3, 0xd4, 0x14,
0x54, 0x41, 0x15, 0x54, 0xc7, 0x79, 0x9c, 0x33, 0x36, 0x63, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
},
0x80: []byte{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xb2, 0xa1, 0xbe, 0xff, 0x3e, 0x00, 0xf0, 0x01, 0x00, 0x40,
0x00, 0x40, 0x00, 0x40, 0xfe, 0xff, 0xfe, 0xff, 0x08, 0x00, 0xe7, 0x3b, 0xe7, 0x3b, 0xe7, 0x3b,
},
}
type ControllerInput struct {
Dpad struct {
Up, Down, Left, Right uint8
}
Button struct {
A, B, X, Y, R, ZR, L, ZL uint8
Home, Plus, Minus, Capture uint8
}
Stick struct {
Left, Right struct {
X, Y float64
Press uint8
}
}
}
type Controller struct {
path string
fp *os.File
count uint8
stopCounter chan struct{}
stopInput chan struct{}
stopCommunicate chan struct{}
Input ControllerInput
LogLevel int
}
// NewController creates an instance of Controller with device path
func NewController(path string) *Controller {
return &Controller{
path: path,
}
}
// Close closes all channel and device file
func (c *Controller) Close() {
if c.fp == nil {
if c.LogLevel > 0 {
log.Println("Already closed.")
}
return
}
close(c.stopCounter)
close(c.stopInput)
close(c.stopCommunicate)
// TODO: Send close magic packet
c.fp.Close()
c.fp = nil
}
func (c *Controller) startCounter() {
ticker := time.NewTicker(time.Millisecond * 5)
go func() {
defer ticker.Stop()
for {
select {
case <-ticker.C:
c.count++
case <-c.stopCounter:
return
}
}
}()
}
func packShorts(short1, short2 uint16) (data []byte) {
data = make([]byte, 3)
data[0] = uint8(short1 & 0xff)
data[1] = uint8(((short2 << 4) & 0xf0) | ((short1 >> 8) & 0x0f))
data[2] = uint8((short2 >> 4) & 0xff)
return data
}
func bitInput(input, offset uint8) uint8 {
if input == 0 {
return 0
}
return 1 << offset
}
func (c *Controller) getInputBuffer() []byte {
left := bitInput(c.Input.Button.Y, 0) |
bitInput(c.Input.Button.X, 1) |
bitInput(c.Input.Button.B, 2) |
bitInput(c.Input.Button.A, 3) |
bitInput(c.Input.Button.R, 6) |
bitInput(c.Input.Button.ZR, 7)
center := bitInput(c.Input.Button.Minus, 0) |
bitInput(c.Input.Button.Plus, 1) |
bitInput(c.Input.Stick.Right.Press, 2) |
bitInput(c.Input.Stick.Left.Press, 3) |
bitInput(c.Input.Button.Home, 4) |
bitInput(c.Input.Button.Capture, 5)
right := bitInput(c.Input.Dpad.Down, 0) |
bitInput(c.Input.Dpad.Up, 1) |
bitInput(c.Input.Dpad.Right, 2) |
bitInput(c.Input.Dpad.Left, 3) |
bitInput(c.Input.Button.L, 6) |
bitInput(c.Input.Button.ZL, 7)
lx := uint16(math.Round((1 + c.Input.Stick.Left.X) * 2047.5))
ly := uint16(math.Round((1 + c.Input.Stick.Left.Y) * 2047.5))
rx := uint16(math.Round((1 + c.Input.Stick.Right.X) * 2047.5))
ry := uint16(math.Round((1 + c.Input.Stick.Right.Y) * 2047.5))
leftStick := packShorts(lx, ly)
rightStick := packShorts(rx, ry)
return []byte{0x81, left, center, right, leftStick[0], leftStick[1],
leftStick[2], rightStick[0], rightStick[1], rightStick[2], 0x00}
}
func (c *Controller) startInputReport() {
ticker := time.NewTicker(time.Millisecond * 30)
go func() {
defer ticker.Stop()
for {
select {
case <-ticker.C:
c.write(0x30, c.count, c.getInputBuffer())
case <-c.stopInput:
return
}
}
}()
}
func (c *Controller) uart(ack bool, subCmd byte, data []byte) {
ackByte := byte(0x00)
if ack {
ackByte = 0x80
if len(data) > 0 {
ackByte |= subCmd
}
}
c.write(0x21, c.count, append(append(c.getInputBuffer(), []byte{ackByte, subCmd}...), data...))
}
func (c *Controller) write(ack byte, cmd byte, buf []byte) {
data := append(append([]byte{ack, cmd}, buf...), make([]byte, 62-len(buf))...)
c.fp.Write(data)
if c.LogLevel > 0 {
if ack == 0x30 {
if c.LogLevel > 2 {
log.Println("write:", hex.EncodeToString(data))
}
} else {
log.Println("write:", hex.EncodeToString(data))
}
}
}
// Connect begins connection to device
func (c *Controller) Connect() error {
var err error
if c.fp != nil {
return errors.New("Already connected.")
}
c.fp, err = os.OpenFile(c.path, os.O_RDWR|os.O_SYNC, os.ModeDevice)
if err != nil {
return err
}
c.stopCounter = make(chan struct{})
c.stopInput = make(chan struct{})
c.stopCommunicate = make(chan struct{})
c.startCounter()
// Reset magic packet
c.write(0x81, 0x03, []byte{})
c.write(0x81, 0x01, []byte{0x00, 0x03})
go func() {
buf := make([]byte, 128)
for {
select {
case <-c.stopCommunicate:
return
default:
}
n, err := c.fp.Read(buf)
if c.LogLevel > 0 {
log.Println("read:", hex.EncodeToString(buf[:n]), err)
}
switch buf[0] {
case 0x80:
switch buf[1] {
case 0x01:
c.write(0x81, buf[1], []byte{0x00, 0x03, 0x00, 0x00, 0x5e, 0x00, 0x53, 0x5e})
case 0x02, 0x03:
c.write(0x81, buf[1], []byte{})
case 0x04:
c.startInputReport()
case 0x05:
close(c.stopInput)
c.stopInput = make(chan struct{})
}
case 0x01:
switch buf[10] {
case 0x01: // Bluetooth manual pairing
c.uart(true, buf[10], []byte{0x03, 0x01})
case 0x02: // Request device info
c.uart(true, buf[10], []byte{0x03, 0x48, 0x03,
0x02, 0x5e, 0x53, 0x00, 0x5e, 0x00, 0x00, 0x03, 0x01})
case 0x03, 0x08, 0x30, 0x38, 0x40, 0x41, 0x48: // Empty response
c.uart(true, buf[10], []byte{})
case 0x04: // Empty response
c.uart(true, buf[10], []byte{})
case 0x10: // Read SPI ROM
data, ok := SPI_ROM_DATA[buf[12]]
if ok {
c.uart(true, buf[10], append(buf[11:16],
data[buf[11]:buf[11]+buf[15]]...))
if c.LogLevel > 1 {
log.Printf("Read SPI address: %02x%02x[%d] %v\n",
buf[12], buf[11], buf[15], data[buf[11]:buf[11]+buf[15]])
}
} else {
c.uart(false, buf[10], []byte{})
if c.LogLevel > 1 {
log.Printf("Unknown SPI address: %02x[%d]\n", buf[12], buf[15])
}
}
case 0x21:
// FIXME: Check ack value
c.uart(true, buf[10], []byte{0x01, 0x00, 0xff, 0x00, 0x03, 0x00, 0x05, 0x01})
default:
if c.LogLevel > 1 {
log.Println("UART unknown request", buf[10], buf)
}
}
case 0x00:
case 0x10:
default:
if c.LogLevel > 1 {
log.Println("unknown request", buf[0])
}
}
}
}()
return nil
}