/
manager.go
262 lines (220 loc) · 5.81 KB
/
manager.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
package main
import (
"fmt"
"sync"
"time"
"github.com/aj3423/joy-typing/joycon"
"github.com/aj3423/joy-typing/mode"
"github.com/gen2brain/beeep"
log "github.com/sirupsen/logrus"
"github.com/sstallion/go-hid"
)
type Manager struct {
mu sync.Mutex
connected map[joycon.Controller]joycon.RemoveListenerFn
}
func NewManager() *Manager {
return &Manager{
connected: make(map[joycon.Controller]joycon.RemoveListenerFn),
}
}
func (m *Manager) monitorNewDevice(chExit_Ctrl_d chan struct{}) {
// scan for newly connected device every second
tickNewDevice := time.NewTicker(1 * time.Second)
for {
select {
case <-tickNewDevice.C:
m.CheckNewDevice()
case <-chExit_Ctrl_d: // ctrl-d in console, exit app
m.removeAll()
return
}
}
}
func (m *Manager) OnReadWriteError(
jc joycon.Controller, err error,
) {
log.Errorf("%s: %s", jc.Side().String(), err.Error())
go beeep.Notify("R/W error", jc.Side().String(), "")
m.mu.Lock()
defer m.mu.Unlock()
// If any r/w error occurs, the connection must be broken,
// but the zombie connection still stays in system BT manager for a while,
// so forcely disconnect it to avoid that period of time.
go m.remove(jc, true)
}
func (m *Manager) OnButton(
jc joycon.Controller,
down, up, curr *joycon.ButtonState,
) {
log.Tracef("onButton, down: %v, up: %v", down, up)
mode.Manager.Handle(
&mode.Input{
Type: mode.InputType_Button,
Jc: jc,
ButtonInput: &mode.ButtonInput{
Up: up, Down: down, Curr: curr,
},
},
)
}
func isGoingNeutral(curr, prev *joycon.Ratio) bool {
return !prev.AtNeutral() && curr.AtNeutral()
}
func isLeavingNeutral(curr, prev *joycon.Ratio) bool {
return prev.AtNeutral() && !curr.AtNeutral()
}
func isGoingEdge(curr, prev *joycon.Ratio, dir joycon.SpinDirection) bool {
return !prev.AtEdge(dir) && curr.AtEdge(dir)
}
func isLeavingEdge(curr, prev *joycon.Ratio, dir joycon.SpinDirection) bool {
return prev.AtEdge(dir) && !curr.AtEdge(dir)
}
func (m *Manager) OnStick(
jc joycon.Controller, side joycon.JoyConSide, curr, prev *joycon.Ratio,
) {
go log.Tracef("onStick <%s>: %s", jc.Side().String(), curr.String())
// fire 2 events:
// 1. a movement event
// 2. a edge-movement event, e.g. Up, Upleave
// 1.
mode.Manager.Handle(
&mode.Input{
Type: mode.InputType_Stick,
Jc: jc,
StickInput: &mode.StickInput{
Side: side,
Ratio: curr,
Direction: joycon.SpinDirection_None,
},
},
)
// 2.
dir := joycon.SpinDirection_None
if isGoingEdge(curr, prev, joycon.Spin_Up) { // spin Up
dir = joycon.Spin_Up
} else if isLeavingEdge(curr, prev, joycon.Spin_Up) { // leave Up area
dir = joycon.Spin_Up_Leave
} else if isGoingEdge(curr, prev, joycon.Spin_Right) {
dir = joycon.Spin_Right
} else if isLeavingEdge(curr, prev, joycon.Spin_Right) {
dir = joycon.Spin_Right_Leave
} else if isGoingEdge(curr, prev, joycon.Spin_Down) {
dir = joycon.Spin_Down
} else if isLeavingEdge(curr, prev, joycon.Spin_Down) {
dir = joycon.Spin_Down_Leave
} else if isGoingEdge(curr, prev, joycon.Spin_Left) {
dir = joycon.Spin_Left
} else if isLeavingEdge(curr, prev, joycon.Spin_Left) {
dir = joycon.Spin_Left_Leave
} else if isGoingNeutral(curr, prev) {
dir = joycon.Spin_Neutral
} else if isLeavingNeutral(curr, prev) {
dir = joycon.Spin_Neutral_Leave
}
if dir != joycon.SpinDirection_None {
mode.Manager.Handle(
&mode.Input{
Type: mode.InputType_Stick,
Jc: jc,
StickInput: &mode.StickInput{
Side: side,
Ratio: curr,
Direction: dir,
},
},
)
}
}
func (m *Manager) OnStickCalib(
jc joycon.Controller, calib *[2]joycon.CalibrationData,
) {
log.Infof("🔧 <%s> Calibrated: %v", jc.Side().String(), calib)
go beeep.Notify("🔧 Calibrated", jc.Side().String(), "")
}
func (m *Manager) OnGyro(jc joycon.Controller, gyro *joycon.GyroFrame) {
log.Tracef("onGyro, %v", *gyro)
mode.Manager.Handle(
&mode.Input{
Type: mode.InputType_Gyro,
Gyro: &mode.Gyro{
Frame: gyro,
},
},
)
}
func renderBattery(level int8, charging bool) string {
if charging {
return fmt.Sprintf("🔋%d, ⚡ Charging", level)
} else {
return fmt.Sprintf("🔋%d", level)
}
}
func (m *Manager) OnBattery(
jc joycon.Controller, level int8, charging bool,
) {
name := jc.Side().String()
batt := renderBattery(level, charging)
log.Infof("Battery: %s %s", name, batt)
go beeep.Notify(name, batt, "")
}
func (m *Manager) remove(
jc joycon.Controller, disconnectBT bool,
) {
log.Warningf("Removing %s (%s) ...", jc.Side().String(), jc.Mac())
// unbind events
unbind := m.connected[jc]
unbind()
if disconnectBT {
jc.ShutdownBT()
}
jc.Disconnect()
delete(m.connected, jc)
}
func (m *Manager) removeAll() {
log.Warningf("Removing all controllers...")
m.mu.Lock()
defer m.mu.Unlock()
for jc := range m.connected {
m.remove(jc, false)
}
}
// find which side is not connected yet
func (m *Manager) findBySide(side joycon.JoyConSide) joycon.Controller {
for jc := range m.connected {
if jc.Side() == side {
return jc
}
}
return nil
}
// corresponding side <-> products
var sides = []joycon.JoyConSide{joycon.SideLeft, joycon.SideRight}
var products = []uint16{joycon.JOYCON_PRODUCT_L, joycon.JOYCON_PRODUCT_R}
func (m *Manager) CheckNewDevice() {
m.mu.Lock()
defer m.mu.Unlock()
for i, side := range sides {
if m.findBySide(side) != nil { // already exists
continue
}
dev, e := hid.OpenFirst(joycon.VENDOR_NINTENDO, products[i])
if e != nil {
continue
}
mac, e := dev.GetSerialNbr()
if e != nil {
dev.Close()
continue
}
jc := joycon.NewJoycon(dev, side, mac)
m.addNewDevice(jc)
}
}
func (m *Manager) addNewDevice(jc joycon.Controller) {
unbindFn := jc.SetListener(m)
m.connected[jc] = unbindFn
log.Infof("Connected to: <%s> %s", jc.Side(), jc.Mac())
go beeep.Notify("Connected", jc.Side().String(), "")
jc.Rumble(&joycon.RumbleFrequencySample)
}