-
Notifications
You must be signed in to change notification settings - Fork 1
/
launch.go
286 lines (247 loc) · 6.7 KB
/
launch.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
package golaunch
import (
"context"
"errors"
"strings"
"time"
"github.com/go-ble/ble"
)
var (
// Bluetooth device name of the Launch
name = "Launch"
// The main service UUID for the Launch
serviceID = ble.MustParse("88f80580-0000-01e6-aace-0002a5d5c51b")
// Command characteristic (Write)
cmdCharID = ble.MustParse("88f80581-0000-01e6-aace-0002a5d5c51b")
// Touch events characteristic (Notifications)
touchCharID = ble.MustParse("88f80582-0000-01e6-aace-0002a5d5c51b")
// Mode? characteristic (Read/Write)
modeCharID = ble.MustParse("88f80583-0000-01e6-aace-0002a5d5c51b")
)
var (
// ErrDisconnected is the error returned when there is no connection.
ErrDisconnected = errors.New("disconnected")
// ErrDiscover is the error returned when there was an issue
// discovering the Launch.
ErrDiscover = errors.New("could not discover launch")
// ErrInit is the error returned when there was an issue initializing
// the Launch.
ErrInit = errors.New("initialization error")
// ErrUnknownMode is the error returned when someone wants to send a
// (yet) unknown mode.
ErrUnknownMode = errors.New("unknown mode")
)
var (
// Time for the device to get ready before sending data.
readyTime = time.Second * 2
// Minimum amount of time between write operations.
threshold = time.Millisecond * 100
// Amount of write events to buffer before blocking.
writeBufferSize = 10
)
const (
// modeReadValuesAsBytes will cause the Launch to read position/speed
// values as bytes instead of BCD. Also seems to fix it from having
// spasms.
modeReadValuesAsBytes = 0x00
)
// Launch interface represents a device that can move like a Launch.
type Launch interface {
// Connect will (re)connect to the Launch.
Connect(ctx context.Context) error
// Disconnect will disconnect to the Launch.
Disconnect()
// Move moves to position at speed in percent.
Move(position, speed int)
// HandleDisconnect registers a function to call when a device disconnects
HandleDisconnect(f func())
}
// NewLaunch creates and returns a Launch client that can be used to
// communicate.
func NewLaunch() Launch {
l := &launch{
disconnect: make(chan bool),
wbuffer: make(chan [2]byte, writeBufferSize),
limiter: time.Tick(threshold),
}
return l
}
// NewLaunchWithDevice creates and returns a Launch client that can be used to
// communicate over specified Bluetooth device.
func NewLaunchWithDevice(d ble.Device) Launch {
l, _ := NewLaunch().(*launch)
l.device = d
ble.SetDefaultDevice(d)
return l
}
// launch is the structure used to manage the connection to a Launch.
type launch struct {
device ble.Device
client ble.Client
cmd *ble.Characteristic
mode *ble.Characteristic
disconnect chan bool
wbuffer chan [2]byte
limiter <-chan time.Time
disconnectFunc func()
}
// Connect initializes configured Bluetooth device and creates a connection to
// a Launch.
func (l *launch) Connect(ctx context.Context) (err error) {
// Claim a Bluetooth device
if l.device == nil {
l.device, err = NewDefaultDevice()
if err != nil {
return err
}
ble.SetDefaultDevice(l.device)
}
// Still connected
if l.client != nil {
return nil
}
// Discover the Launch and it's characteristics
err = l.discover(ctx)
if err != nil {
return err
}
// Give the Launch a little bit of time to get ready
<-time.After(readyTime)
// Put the Launch into a mode that it will actually work well
err = l.writeMode(modeReadValuesAsBytes)
if err != nil {
l.client.CancelConnection()
l.cleanupClient()
return ErrInit
}
// Handle disconnects
stopWriting := make(chan bool)
go func() {
select {
case <-l.client.Disconnected():
case <-l.disconnect:
l.client.CancelConnection()
}
stopWriting <- true
l.cleanupClient()
if l.disconnectFunc != nil {
l.disconnectFunc()
}
}()
// Start the goroutine that will write commands
go l.writeFromBuffer(stopWriting)
return nil
}
// discover scans, connects and discovers characteristics of a Launch.
func (l *launch) discover(ctx context.Context) error {
// Connect to Launch
client, err := ble.Connect(ctx, launchAdvFilter)
if err != nil {
return err
}
// Discover Service
s, err := client.DiscoverServices([]ble.UUID{serviceID})
if err != nil {
return err
}
if len(s) != 1 {
return ErrDiscover
}
// Discover Launch characteristics
cs, err := client.DiscoverCharacteristics(
[]ble.UUID{cmdCharID, modeCharID}, s[0])
if err != nil {
return err
}
var cmd, mode *ble.Characteristic
for _, c := range cs {
switch {
case c.UUID.Equal(cmdCharID):
cmd = c
case c.UUID.Equal(modeCharID):
mode = c
}
}
if cmd == nil || mode == nil {
return ErrDiscover
}
// Store found device characteristics
l.client = client
l.cmd = cmd
l.mode = mode
return nil
}
// launchAdvFilter implements ble.AdvFilter and filters for a Launch.
func launchAdvFilter(a ble.Advertisement) bool {
if strings.ToUpper(a.LocalName()) == strings.ToUpper(name) {
return true
}
return false
}
// cleanupClient removes all references to a disconnected client.
func (l *launch) cleanupClient() {
if l.client != nil {
l.client = nil
l.mode = nil
l.cmd = nil
}
}
// Disconnect cancels the connection with the Launch.
func (l *launch) Disconnect() {
select {
case l.disconnect <- true:
default:
}
}
// writeMode will send specified mode to the Launch. Only support putting
// the Launch in "read values as bytes", so it interprets values on the command
// channel as bytes instead of binary coded decimals.
func (l *launch) writeMode(c byte) error {
if l.client == nil {
return ErrDisconnected
}
if c == modeReadValuesAsBytes {
err := l.client.WriteCharacteristic(l.mode, []byte{c}, false)
return err
}
return ErrUnknownMode
}
// writeFromBuffer sends commands to the Launch that are stored in the write
// buffer. To stop this function send true on the l.stopWriting channel.
func (l *launch) writeFromBuffer(stopchan <-chan bool) {
for {
select {
case stop := <-stopchan:
if stop == true {
return
}
case b := <-l.wbuffer:
// Limit amount of writes to avoid disconnects
<-l.limiter
l.client.WriteCharacteristic(l.cmd, b[:], true)
}
}
}
// Move will move to the specified position at the desired speed.
// Position and speed are specified in percent.
func (l *launch) Move(position, speed int) {
// Make sure we remain in the limits
if position < 0 {
position = 0
} else if position > 99 {
position = 99
}
// Don't go below 20% as very slow speeds seem to crash the Launch
if speed < 20 {
speed = 20
} else if speed > 99 {
speed = 99
}
data := [2]byte{byte(position), byte(speed)}
l.wbuffer <- data
}
// HandleDisconnect registers a function that is called when the Launch
// disconnects.
func (l *launch) HandleDisconnect(f func()) {
l.disconnectFunc = f
}