-
Notifications
You must be signed in to change notification settings - Fork 1
/
tapDevice.go
207 lines (179 loc) · 4.81 KB
/
tapDevice.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
package tapgosdk
import (
"encoding/binary"
"reflect"
"github.com/GPeye/tapgosdk/events"
"tinygo.org/x/bluetooth"
)
type TapDevice struct {
tapDataValueChangedAssigned bool
mouseDataValueChangedAssigned bool
adapter *bluetooth.Adapter
device bluetooth.Device
rx *bluetooth.DeviceCharacteristic
tx *bluetooth.DeviceCharacteristic
tapData *bluetooth.DeviceCharacteristic
mousedata bluetooth.DeviceCharacteristic
uiCommands *bluetooth.DeviceCharacteristic
fwChar *bluetooth.DeviceCharacteristic
isReady bool
isConnected bool
fw int
inputMode TAPInputMode
supportsMouse bool
}
func NewTapDevice(mode TAPInputMode) TapDevice {
td := new(TapDevice)
td.fw = 0
td.isReady = false
td.isConnected = false
td.inputMode = mode
td.supportsMouse = false
td.tapDataValueChangedAssigned = false
td.mouseDataValueChangedAssigned = false
return *td
}
func (td TapDevice) InputMode() TAPInputMode {
return td.inputMode
}
func (td TapDevice) IsReady() bool {
return td.isReady
}
func (td TapDevice) SupportsMouse() bool {
return td.supportsMouse
}
func (td TapDevice) Identifier() bluetooth.MACAddress {
return td.device.Address.MACAddress
}
func (td TapDevice) Name() string {
return "name"
}
func (td TapDevice) FW() int {
return td.fw
}
func (td *TapDevice) FromBlueToothScanResult(adapter bluetooth.Adapter, device bluetooth.ScanResult) TapDevice {
td.adapter = &adapter
var err error = nil
td.device, err = adapter.Connect(device.Address, bluetooth.ConnectionParams{})
if err != nil {
panic(err.Error())
}
td.isConnected = true
println("discovering services/characteristics")
srvcs, err := td.device.DiscoverServices([]bluetooth.UUID{tapService, nusService, deviceInfoService})
must("discover services", err)
if len(srvcs) < 3 {
panic("Could not find all Tap Services")
}
tapServ := srvcs[0]
nusServ := srvcs[1]
infoServ := srvcs[2]
println("found Tap Service", tapServ.UUID().String())
println("found Nus Service", nusServ.UUID().String())
println("found Info Service", infoServ.UUID().String())
chars, err := tapServ.DiscoverCharacteristics([]bluetooth.UUID{tapDataCharacteristic, uiCmdCharacteristic})
if err != nil {
println(err)
}
if len(chars) < 2 {
panic("could not find tap data characteristic")
}
td.tapData = &chars[0]
td.uiCommands = &chars[1]
println("found Tap Data characteristic", td.tapData.UUID().String())
println("found Tap UI Command characteristic", td.uiCommands.UUID().String())
chars, err = nusServ.DiscoverCharacteristics([]bluetooth.UUID{tapModeCharacteristic, rawSensorsCharacteristic})
if err != nil {
println(err)
}
if len(chars) < 2 {
panic("could not find tap data characteristic")
}
td.rx = &chars[0]
td.tx = &chars[1]
println("found Tap Mode characteristic", td.rx.UUID().String())
println("found Raw Mode characteristic", td.tx.UUID().String())
chars, err = infoServ.DiscoverCharacteristics([]bluetooth.UUID{fwVersionCharacteristic})
if err != nil {
println(err)
}
if len(chars) == 0 {
panic("could not find tap data characteristic")
}
td.fwChar = &chars[0]
println("found Fw Version characteristic", td.fwChar.UUID().String())
var fwdata []byte = []byte{0}
_, err = td.fwChar.Read(fwdata)
if err != nil {
println(err)
}
println(int(fwdata[0]))
td.fw = int(fwdata[0])
return TapDevice{}
}
func (td *TapDevice) Vibrate(durations []byte) {
println("Sending Vibrate")
if td.uiCommands == nil || !td.isReady {
return
}
//byteDurations := IntsToBytesBE(durations)
//println(byteDurations)
var data [20]byte
data[0] = 0
data[1] = 2
for i := range len(data) - 2 {
if i < len(durations) {
data[i+2] = durations[i]
} else {
data[i+2] = 0
}
}
td.uiCommands.WriteWithoutResponse(data[:])
}
func IntsToBytesBE(i []int) []byte {
intSize := int(reflect.TypeOf(i).Elem().Size())
b := make([]byte, intSize*len(i))
for n, s := range i {
switch intSize {
case 64 / 8:
binary.BigEndian.PutUint64(b[intSize*n:], uint64(s))
case 32 / 8:
binary.BigEndian.PutUint32(b[intSize*n:], uint32(s))
default:
panic("unreachable")
}
}
return b
}
func (td *TapDevice) MakeReady() {
if td.isConnected {
switch td.inputMode {
case Controller:
td.tapData.EnableNotifications(func(buf []byte) {
events.TappedData.Trigger(uint8(buf[0]))
})
case RawSensors:
err := td.tx.EnableNotifications(func(buf []byte) {
println(buf)
events.RawData.Trigger(buf)
})
if err != nil {
println(err)
}
default:
}
td.isReady = true
}
}
func (td *TapDevice) SendMode() {
println("Sending Mode: ", byte(td.inputMode))
if td.isReady && td.isConnected {
var data []byte
if td.inputMode == RawSensors {
data = []byte{0x3, 0xc, 0x0, byte(td.inputMode), 0x1, 0x2, 0x1}
} else {
data = []byte{0x3, 0xc, 0x0, byte(td.inputMode)}
}
td.rx.WriteWithoutResponse(data)
}
}