-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
232 lines (219 loc) · 5.57 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
package main
import (
mapset "github.com/deckarep/golang-set/v2"
"github.com/labstack/echo/v4"
log "github.com/sirupsen/logrus"
"github.com/tarm/serial"
"math"
"strconv"
"strings"
"sync"
"time"
)
var client *serial.Port
func InitUART() {
config := &serial.Config{
Baud: 19200,
Name: "COM5",
ReadTimeout: 1 * time.Second,
}
var err error
client, err = serial.OpenPort(config)
if err != nil {
log.Fatal(err)
}
log.Info("Connected to COM5")
if !syncUART() {
log.Fatal("Failed to sync")
}
log.Info("Synced")
if !sendCommand(BTN_A + DPAD_U_R + LSTICK_U + RSTICK_D_L) {
log.Fatal("Packet Error!")
}
time.Sleep(500 * time.Millisecond)
if !sendNoInput() {
log.Fatal("Packet Error!")
}
}
func matchNoOrder[T comparable](s1, s2, m1, m2 T) bool {
return (s1 == m1 && s2 == m2) || (s1 == m2 && s2 == m1)
}
const MouseMax = 5
const xAmp = 1.8
const yAmp = 1
func sendHoldingButtons() bool {
var buttons int64
for button := range holdingButtons.Iter() {
buttons += int64(button)
}
if holdingLSticks.Cardinality() == 1 {
buttons += int64(keyMap[holdingLSticks.ToSlice()[0]])
} else if holdingLSticks.Cardinality() > 1 {
s := holdingLSticks.ToSlice()
l1 := keyMap[s[0]]
l2 := keyMap[s[1]]
if matchNoOrder(l1, l2, LSTICK_U, LSTICK_L) {
buttons += LSTICK_U_L
} else if matchNoOrder(l1, l2, LSTICK_U, LSTICK_R) {
buttons += LSTICK_U_R
} else if matchNoOrder(l1, l2, LSTICK_D, LSTICK_L) {
buttons += LSTICK_D_L
} else if matchNoOrder(l1, l2, LSTICK_D, LSTICK_R) {
buttons += LSTICK_D_R
}
}
if holdingRSticks.Cardinality() == 1 {
buttons += int64(keyMap[holdingRSticks.ToSlice()[0]])
} else if holdingRSticks.Cardinality() > 1 {
s := holdingRSticks.ToSlice()
r1 := keyMap[s[0]]
r2 := keyMap[s[1]]
if matchNoOrder(r1, r2, RSTICK_U, RSTICK_L) {
buttons += RSTICK_U_L
} else if matchNoOrder(r1, r2, RSTICK_U, RSTICK_R) {
buttons += RSTICK_U_R
} else if matchNoOrder(r1, r2, RSTICK_D, RSTICK_L) {
buttons += RSTICK_D_L
} else if matchNoOrder(r1, r2, RSTICK_D, RSTICK_R) {
buttons += RSTICK_D_R
}
}
if mouseYDiff != 0 || mouseXDiff != 0 {
length := int64(math.Sqrt(float64(mouseXDiff*mouseXDiff + mouseYDiff*mouseYDiff)))
if length > MouseMax {
length = MouseMax
}
intensity := int64((float64(length) / MouseMax) * 255)
angle := math.Atan(float64(mouseYDiff/mouseXDiff)) * 180 / math.Pi
if mouseXDiff < 0 {
angle += 180
} else if mouseYDiff < 0 {
angle += 360
}
log.Debugf("Length: %d, Angle: %d, Intensity: %d", length, angle, intensity)
buttons += rstickAngle(int64(angle), intensity)
}
return sendCommand(buttons)
}
var keyMap = map[string]int{
"A": BTN_A,
"B": BTN_B,
"X": BTN_X,
"Y": BTN_Y,
"U": DPAD_U,
"R": DPAD_R,
"D": DPAD_D,
"L": DPAD_L,
"ZR": BTN_ZR,
"ZL": BTN_ZL,
"LR": BTN_R,
"LL": BTN_L,
"LClick": BTN_LCLICK,
"RClick": BTN_RCLICK,
"Plus": BTN_PLUS,
"Minus": BTN_MINUS,
"Home": BTN_HOME,
"Capture": BTN_CAPTURE,
"LUp": LSTICK_U,
"LDown": LSTICK_D,
"LLeft": LSTICK_L,
"LRight": LSTICK_R,
"RUp": RSTICK_U,
"RDown": RSTICK_D,
"RLeft": RSTICK_L,
"RRight": RSTICK_R,
}
var holdingButtons = mapset.NewSet[int]()
var holdingLSticks = mapset.NewSet[string]()
var holdingRSticks = mapset.NewSet[string]()
var mouseXDiff float32
var mouseYDiff float32
var mutex = sync.Mutex{}
func InitREST() {
app := echo.New()
app.GET("/:ac/:key", func(c echo.Context) error {
mutex.Lock()
defer mutex.Unlock()
action := c.Param("ac")
key := c.Param("key")
log.Infof("Action: %s | Key: %s", action, key)
mapped, ok := keyMap[key]
if !ok && action != "A" && action != "D" {
return nil
}
switch action {
case "D":
s := strings.Split(key, ",")
if len(s) == 2 {
x, _ := strconv.Atoi(s[0])
y, _ := strconv.Atoi(s[1])
mouseXDiff = float32(x)
mouseYDiff = float32(y)
mouseYDiff = -mouseYDiff
mouseXDiff = mouseXDiff * xAmp
mouseYDiff = mouseYDiff * yAmp
sendHoldingButtons()
}
case "A":
holdingButtons.Clear()
sendNoInput()
case "R":
prev := holdingButtons.Clone()
prevLSticks := holdingLSticks.Clone()
prevRSticks := holdingRSticks.Clone()
switch mapped {
case LSTICK_U, LSTICK_D, LSTICK_L, LSTICK_R:
holdingLSticks.Remove(key)
case RSTICK_U, RSTICK_D, RSTICK_L, RSTICK_R:
holdingRSticks.Remove(key)
default:
holdingButtons.Remove(mapped)
}
if !prev.Equal(holdingButtons) || !prevLSticks.Equal(holdingLSticks) || !prevRSticks.Equal(holdingRSticks) {
sendHoldingButtons()
}
case "H":
prev := holdingButtons.Clone()
prevLSticks := holdingLSticks.Clone()
prevRSticks := holdingRSticks.Clone()
switch key {
case "LUp":
holdingLSticks.Remove("LDown")
case "LDown":
holdingLSticks.Remove("LUp")
case "LLeft":
holdingLSticks.Remove("LRight")
case "LRight":
holdingLSticks.Remove("LLeft")
case "RUp":
holdingRSticks.Remove("RDown")
case "RDown":
holdingRSticks.Remove("RUp")
case "RLeft":
holdingRSticks.Remove("RRight")
case "RRight":
holdingRSticks.Remove("RLeft")
}
switch key {
case "LUp", "LDown", "LLeft", "LRight":
holdingLSticks.Add(key)
case "RUp", "RDown", "RLeft", "RRight":
holdingRSticks.Add(key)
default:
holdingButtons.Add(mapped)
}
if !prev.Equal(holdingButtons) || !prevLSticks.Equal(holdingLSticks) || !prevRSticks.Equal(holdingRSticks) {
sendHoldingButtons()
}
}
return nil
})
err := app.Start(":80")
if err != nil {
log.Fatal("Failed to start server")
}
}
func main() {
InitUART()
InitREST()
}