-
Notifications
You must be signed in to change notification settings - Fork 1
/
tapManager.go
137 lines (114 loc) · 3.17 KB
/
tapManager.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
package tapgosdk
import (
"strings"
"time"
"github.com/GPeye/tapgosdk/events"
"tinygo.org/x/bluetooth"
)
type TappedEventArgs struct {
id string
tapCode int
}
type TapManager struct {
defaultInputMode TAPInputMode
inputModeWhenDeactivated TAPInputMode
activated bool
modeTimer *time.Ticker
deviceWatcher *DeviceWatcher
started bool
restartWatcher bool
deviceFoundHandler deviceFoundNotifier
deviceScanStoppedHandler deviceScanStoppedNotifier
desiredDevices int
taps []TapDevice
}
func NewTapManager() *TapManager {
tm := new(TapManager)
tm.deviceWatcher = NewDeviceWatcher()
tm.restartWatcher = false
tm.deviceFoundHandler = deviceFoundNotifier{tm}
tm.deviceScanStoppedHandler = deviceScanStoppedNotifier{tm}
tm.desiredDevices = 1
tm.defaultInputMode = Controller
tm.taps = []TapDevice{}
return tm
}
type deviceFoundNotifier struct {
tm *TapManager
}
func (d *deviceFoundNotifier) Handle(device bluetooth.ScanResult) {
println("found device:", device.Address.String(), device.RSSI, device.LocalName())
println("checking if it is a Tap device")
if strings.Contains(device.LocalName(), "Tap") && device.AdvertisementPayload.HasServiceUUID(tapService) && device.AdvertisementPayload.HasServiceUUID(nusService) {
println("Found a Tap Device: Connecting...")
d.tm.deviceWatcher.Stop()
var newTapDevice = NewTapDevice(d.tm.defaultInputMode)
newTapDevice.FromBlueToothScanResult(*d.tm.deviceWatcher.adapter, device)
d.tm.addDevice(newTapDevice)
//d.tm.taps = append(d.tm.taps, newTapDevice)
println(d.tm.taps[0].fw)
d.tm.taps[0].MakeReady()
d.tm.taps[0].SendMode()
} else {
println("Not a Tap Device: Skipping...")
}
}
type deviceScanStoppedNotifier struct {
tm *TapManager
}
func (d deviceScanStoppedNotifier) Handle() {
println("device scan stopped")
if d.tm.restartWatcher {
d.tm.restartWatcher = false
d.tm.deviceWatcher.Start()
}
}
func (tm *TapManager) HasMultipleTapConnections() bool {
return len(tm.taps) > 1
}
func (tm *TapManager) addDevice(tapDevice TapDevice) {
tm.taps = append(tm.taps, tapDevice)
}
func (tm *TapManager) Start() {
if !tm.started {
tm.started = true
tm.activated = true
tm.modeTimer = time.NewTicker(10 * time.Second)
events.DeviceFound.Register(&tm.deviceFoundHandler)
events.DeviceScanStopped.Register(&tm.deviceScanStoppedHandler)
tm.deviceWatcher.Start()
go func() {
for {
select {
case <-tm.modeTimer.C:
if len(tm.taps) > 0 {
tm.taps[0].SendMode()
//println("sending controller byte")
}
}
}
}()
}
}
func (tm TapManager) RestartDeviceWatcher() {
if tm.started {
tm.restartWatcher = true
if tm.deviceWatcher.status == Created || tm.deviceWatcher.status == Stopped {
tm.restartWatcher = false
println("Device Watcher Starting")
tm.deviceWatcher.Start()
} else if tm.deviceWatcher.status == Started {
tm.restartWatcher = true
tm.deviceWatcher.Stop()
}
}
}
func (tm *TapManager) SetDefaultIputMode(inputMode TAPInputMode) {
tm.defaultInputMode = inputMode
}
func (tm *TapManager) Vibrate(durations []byte) {
tm.taps[0].Vibrate(durations)
// for i := range len(tm.taps) {
// tm.taps[i].Vibrate(durations)
// }
}