-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
423 lines (371 loc) · 10.4 KB
/
main.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
tos428 configures the Switchable 4-to-8-Way Restrictor for Sanwa compatible
Joysticks
*/
package main
import (
"bufio"
"bytes"
_ "embed"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/tarm/serial"
"github.com/thoas/go-funk"
)
var autoRom string
var devicePath string
var deviceRestrictor string
var exportFile string
var getInfo bool
var mergeListPath string
var rawComand string
var romListPath string
var roms []string
var setWay int
//go:embed roms4way.txt
var romsData []byte
// A GRSDevice is a connection to a tos428
type GRSDevice struct {
device *serial.Port
}
func (g *GRSDevice) sendCommand(cmd string) {
_, err := g.device.Write([]byte(cmd))
if err != nil {
log.Fatal(err)
}
}
func (g *GRSDevice) sendCommandWithOutput(cmd string) string {
g.sendCommand(cmd)
return g.getOutput()
}
func (g *GRSDevice) getOutput() string {
buf := make([]byte, 128)
n, err := g.device.Read(buf)
if err != nil {
log.Fatal(err)
}
return strings.TrimRight(string(buf[:n]), "\r\n")
}
// DumpEEPROM lists the actual static (EEPROM) memory where configurations are
// permanently stored.
func (g *GRSDevice) DumpEEPROM() {
g.sendCommand("dumpeeprom")
r := g.getOutput()
fmt.Println(r)
}
// GetColor retrieves the actual color code for the modes given in P1
// (4|8|keyboard)
func (g *GRSDevice) GetColor(mode string) (int, int, int) {
cmd := fmt.Sprintf("getcolor,%s", mode)
g.sendCommand(cmd)
r := g.getOutput()
rgb := strings.Split(r, ",")
if len(rgb) != 3 {
log.Fatalf("Invalid response from device %s\n", r)
}
var rgbInts []int
for _, c := range rgb {
i, err := strconv.Atoi(c)
if err == nil {
rgbInts = append(rgbInts, i)
}
}
return rgbInts[0], rgbInts[1], rgbInts[2]
}
func (g *GRSDevice) GetInfo() {
log.Printf("Device: %s", g.GetWelcome())
startupWay := g.GetStartupWay()
log.Printf("Startup Orientation: %d", startupWay)
red, green, blue := g.GetColor("4")
log.Printf("4-way Color: %d,%d,%d", red, green, blue)
red, green, blue = g.GetColor("8")
log.Printf("8-way Color: %d,%d,%d", red, green, blue)
red, green, blue = g.GetColor("keyboard")
log.Printf("Keyboard Color: %d,%d,%d", red, green, blue)
}
// GetKeyList provides a list of supported symbolic key names to the remote
// system (for ConfigTool). Those key names are useful as buttons can be
// configured to act as a USBkeyboard key and send emulated keystrokes for up
// to 3 simultaneously pressed keys
// (e.g. combination KEY_LEFT_CTRL,KEY_LEFT_ALT,KEY_DELETE would be possible.)
func (g *GRSDevice) GetKeyList() []string {
r := g.sendCommandWithOutput("getkeylist")
keys := strings.Split(r, "\r\n")
if len(keys) == 0 {
log.Fatalln("Unable to get key list")
}
return keys
}
// GetSilent retrieves the configuration regarding the behavior of the servos
// when not in motion. Returns true if silent mode is enabled.
func (g *GRSDevice) GetSilent() bool {
r := g.sendCommandWithOutput("getsilent")
silent, err := strconv.ParseBool(r)
if err != nil {
log.Fatalf("ERROR: invalid response: %s", r)
}
return silent
}
// GetStartupWay retrieves the actual configuration of restrictor orientation
// after power up.
func (g *GRSDevice) GetStartupWay() int {
cmd := "getstartupway"
g.sendCommand(cmd)
r := g.getOutput()
i, err := strconv.Atoi(r)
if err != nil {
log.Fatal("Unable to get Startup Orientation value")
}
return i
}
// GetWelcome provides the product name and actual firmware version, so remote
// system can check if connected to the right COM-port.
func (g *GRSDevice) GetWelcome() string {
g.sendCommand("getwelcome")
return g.getOutput()
}
// MakePermanent makes all temporary configuration permanent, so that they are
// automatically loaded after each power on.
func (g *GRSDevice) MakePermanent() {
r := g.sendCommandWithOutput("makepermanent")
if r != "ok" {
log.Fatalf("Error making temporary configuration permanent: %s\n", r)
}
}
// RawCommand sends a raw command to the device.
func (g *GRSDevice) RawCommand(command string) {
r := g.sendCommandWithOutput(command)
log.Println(r)
}
// RestoreFactory temporarily reverts to the original factory settings.
// Must be made explicitly permanent with *GRSDevice.MakePermanent() if wanted.
func (g *GRSDevice) RestoreFactory() {
r := g.sendCommandWithOutput("restorefactory")
if r != "ok" {
log.Fatalf("Error restoring factory settings: %s\n", r)
}
}
// SetColor adjusts the color of a button, depending on the mode.
// When button is used for restrictor control: 4 sets color for 4-way position,
// 8 sets color for 8-way position.
// When button is configured as keybord key, keyboard will set the color for
// that mode
func (g *GRSDevice) SetColor(mode string, red int, green int, blue int) {
if !isValidMode(mode) {
log.Fatalf("ERROR: Invalid mode: %s\n", mode)
}
if !isValidColor(red) {
log.Fatalf("ERROR: Invalid value for red: %d\n", red)
}
if !isValidColor(green) {
log.Fatalf("ERROR: Invalid value for green: %d\n", green)
}
if !isValidColor(blue) {
log.Fatalf("ERROR: Invalid value for blue: %d\n", blue)
}
cmd := fmt.Sprintf("setcolor,%s,%d,%d,%d")
r := g.sendCommandWithOutput(cmd)
if r != "ok" {
log.Fatalf("ERROR: error setting color: %s\n", r)
}
}
// SetPosition sets restrictor to position way
//
// Valid values for restrictor are (all, a, b, c, d)
func (g *GRSDevice) SetPosition(restrictor string, way int) {
validValues := []string{"all", "a", "b", "c", "d"}
if !funk.Contains(validValues, restrictor) {
log.Fatalf("ERROR: invalid restrictor value: %s\n", restrictor)
}
if !isValidWay(way) {
log.Fatalf("ERROR: invalid way: %d\n", way)
}
log.Printf("Setting restrictor %s position to %d-way", restrictor, way)
cmd := fmt.Sprintf("setway,%s,%d", restrictor, way)
g.sendCommand(cmd)
r := g.getOutput()
if r != "ok" {
log.Fatalf("ERROR: \"%q\"\n", r)
}
log.Printf("Command completed successfully")
}
// SetSilent configures behavior of servos when not in motion. If silent is on,
// the servos are unpowered (low power consumption, low noise but also low
// holding torque).
//
// Recommended setting is false
func (g *GRSDevice) SetSilent(silent bool) {
s := "off"
if silent {
s = "on"
}
cmd := fmt.Sprintf("setsilent,%s", s)
r := g.sendCommandWithOutput(cmd)
if r != "ok" {
log.Fatalf("ERROR: error setting silent mode: %s\n", r)
}
}
// SetStartupWay allows configuration to which position all restrictors will be
// initialized/moved after power up.
func (g *GRSDevice) SetStartupWay(way int) {
if way != 4 && way != 8 {
log.Fatalf("ERROR: invalid value %d\n", way)
}
cmd := fmt.Sprintf("setstartupway,%d", way)
r := g.sendCommandWithOutput(cmd)
if r != "ok" {
log.Fatalf("ERROR: unable to set startup way: %s\n", r)
}
g.MakePermanent()
}
// SetWayForRom sets the way based on rom.
func (g *GRSDevice) SetWayForRom(rom string) {
log.Printf("Checking ROM: %s", autoRom)
if funk.Contains(roms, filepath.Base(autoRom)) {
g.SetPosition(deviceRestrictor, 4)
} else {
g.SetPosition(deviceRestrictor, 8)
}
}
func (g *GRSDevice) Init() {
c := &serial.Config{Name: devicePath, Baud: 115200}
d, err := serial.OpenPort(c)
if err != nil {
log.Fatal(err)
}
g.device = d
}
func findDevice() {
if devicePath == "auto" {
ttyDir := "/sys/class/tty"
files, err := os.ReadDir(ttyDir)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
p, _ := filepath.EvalSymlinks(filepath.Join(ttyDir, file.Name()))
if strings.Contains(p, "usb") {
const productString = "PRODUCT=2341/8036/100"
ueventPath := filepath.Join(p, "..", "..", "uevent")
if _, err := os.Stat(ueventPath); err == nil {
body, _ := os.ReadFile(ueventPath)
if strings.Contains(string(body), productString) {
devicePath = filepath.Join("/dev", file.Name())
log.Printf("Found tos428: %s\n", devicePath)
}
}
}
}
}
}
func initRomList() {
if romListPath == "" {
readRomList(romsData)
} else {
data, err := os.ReadFile(romListPath)
if err != nil {
log.Fatalln(err)
}
readRomList(data)
}
if mergeListPath != "" {
data, err := os.ReadFile(mergeListPath)
if err != nil {
log.Fatalln(err)
}
readRomList(data)
}
}
func init() {
flag.StringVar(&autoRom, "rom", "", "auto-detect the way for the specified rom")
flag.StringVar(&exportFile, "exportromlist", "", "exports the built-in 4-way rom list to specified path")
flag.StringVar(&romListPath, "romlist", "", "file containing list of 4-way roms. Defaults to built-in list.")
flag.StringVar(&mergeListPath, "mergelist", "", "file containing list of 4-way roms to merge with built-in list.")
flag.StringVar(&devicePath, "d", "auto", "path to tos428 device. Set to auto to scan for device. On Windows use COM#")
flag.StringVar(&deviceRestrictor, "r", "all", "restrictor to apply setting to")
flag.StringVar(&rawComand, "raw", "", "raw command to send to the device. Used to support features not currently implemented.")
flag.BoolVar(&getInfo, "info", false, "display device info")
flag.IntVar(&setWay, "way", 0, "way to set the restrictor (4 or 8)")
flag.Parse()
findDevice()
initRomList()
}
func isValidColor(color int) bool {
if color >= 0 && color <= 255 {
return true
}
return false
}
func isValidMode(mode string) bool {
if mode != "4" && mode != "8" && mode != "keyboard" {
return false
}
return true
}
func isValidRestrictor(restrictor string) bool {
if restrictor == "all" {
return true
}
i, err := strconv.Atoi(restrictor)
if err != nil {
return false
}
return (i >= 1) && (i <= 4)
}
func isValidWay(way int) bool {
if way != 4 && way != 8 {
return false
}
return true
}
func readRomList(data []byte) {
reader := bytes.NewReader(data)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
rom := strings.TrimSpace(scanner.Text())
if rom != "" {
roms = append(roms, scanner.Text())
}
}
if err := scanner.Err(); err != nil {
log.Printf("Error parsing roms list: %s\n", err)
}
}
func main() {
if exportFile != "" {
err := os.WriteFile(exportFile, romsData, 0644)
if err != nil {
log.Fatalf("Error exporting roms list: %s\n", err)
}
return
}
device := new(GRSDevice)
device.Init()
if rawComand != "" {
device.RawCommand(rawComand)
return
}
if getInfo {
device.GetInfo()
return
}
if setWay != 0 {
if !isValidWay(setWay) {
log.Fatalf("invalid value for -way: %d\n", setWay)
}
if !isValidRestrictor(deviceRestrictor) {
log.Fatalf("invalid value for -r: %s\n", deviceRestrictor)
}
device.SetPosition(deviceRestrictor, setWay)
return
}
if autoRom != "" {
device.SetWayForRom(autoRom)
return
}
}