-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalkingpad.go
297 lines (249 loc) · 6.57 KB
/
walkingpad.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
287
288
289
290
291
292
293
294
295
296
297
package main
import (
"context"
"fmt"
"log/slog"
"strings"
"sync"
"time"
"tinygo.org/x/bluetooth"
)
var walkingPadUUIDs = []bluetooth.UUID{
mustUUID("00001800-0000-1000-8000-00805f9b34fb"),
mustUUID("0000180a-0000-1000-8000-00805f9b34fb"),
mustUUID("00010203-0405-0607-0809-0a0b0c0d1912"),
mustUUID("0000fe00-0000-1000-8000-00805f9b34fb"),
mustUUID("00002902-0000-1000-8000-00805f9b34fb"),
mustUUID("00010203-0405-0607-0809-0a0b0c0d1912"),
mustUUID("00002901-0000-1000-8000-00805f9b34fb"),
mustUUID("00002a00-0000-1000-8000-00805f9b34fb"),
mustUUID("00002a01-0000-1000-8000-00805f9b34fb"),
mustUUID("00002a04-0000-1000-8000-00805f9b34fb"),
mustUUID("00002a25-0000-1000-8000-00805f9b34fb"),
mustUUID("00002a26-0000-1000-8000-00805f9b34fb"),
mustUUID("00002a28-0000-1000-8000-00805f9b34fb"),
mustUUID("00002a24-0000-1000-8000-00805f9b34fb"),
mustUUID("00002a29-0000-1000-8000-00805f9b34fb"),
mustUUID("0000fe01-0000-1000-8000-00805f9b34fb"),
mustUUID("0000fe02-0000-1000-8000-00805f9b34fb"),
mustUUID("00010203-0405-0607-0809-0a0b0c0d2b12"),
}
func mustUUID(uuid string) bluetooth.UUID {
u, err := bluetooth.ParseUUID(uuid)
if err != nil {
panic(err)
}
return u
}
type WalkingPadCandidate struct {
Device bluetooth.ScanResult
}
func FindWalkingPadCandidates(adapter *bluetooth.Adapter, timeout time.Duration, targetAddr *string) ([]WalkingPadCandidate, error) {
go func() {
<-time.After(timeout)
_ = adapter.StopScan()
}()
var (
set = make(map[string]struct{})
devices []WalkingPadCandidate
)
err := adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
for _, uuid := range walkingPadUUIDs {
if device.HasServiceUUID(uuid) {
if _, ok := set[device.Address.String()]; ok {
return
}
set[device.Address.String()] = struct{}{}
devices = append(devices, WalkingPadCandidate{Device: device})
if targetAddr != nil && device.Address.String() == *targetAddr {
_ = adapter.StopScan()
return
}
}
}
})
if err != nil {
return nil, err
}
return devices, nil
}
func (candidate WalkingPadCandidate) Connect(adapter *bluetooth.Adapter, params bluetooth.ConnectionParams) (*WalkingPad, error) {
device, err := adapter.Connect(candidate.Device.Address, params)
if err != nil {
return nil, fmt.Errorf("connect: %w", err)
}
services, err := device.DiscoverServices(walkingPadUUIDs)
if err != nil {
return nil, fmt.Errorf("discover services: %w", err)
}
var (
rxFound, txFound bool
rx bluetooth.DeviceCharacteristic
tx bluetooth.DeviceCharacteristic
)
for _, service := range services {
characteristics, err := service.DiscoverCharacteristics(nil)
if err != nil {
return nil, fmt.Errorf("discover characteristics: %w", err)
}
for _, ch := range characteristics {
if strings.HasPrefix(ch.UUID().String(), "0000fe01") {
rx = ch
rxFound = true
}
if strings.HasPrefix(ch.UUID().String(), "0000fe02") {
tx = ch
txFound = true
}
}
}
if !rxFound || !txFound {
return nil, fmt.Errorf("missing characteristics")
}
pad := newWalkingPad(device, rx, tx)
_ = pad.rx.EnableNotifications(pad.onBufferReceive)
var ctx context.Context
ctx, pad.cancel = context.WithCancel(context.Background())
pad.wg.Add(2)
go pad.writeLoop(ctx)
go pad.askStatsLoop(ctx)
return pad, nil
}
type WalkingPad struct {
device bluetooth.Device
rx bluetooth.DeviceCharacteristic
tx bluetooth.DeviceCharacteristic
wg sync.WaitGroup
cancel context.CancelFunc
stopped bool
queue chan walkingPadCommand
LastStatus WalkingPadStatus
LastStatusTime time.Time
}
type walkingPadCommand struct {
timeout time.Duration
buffer []byte
}
func newWalkingPad(device bluetooth.Device, rx, tx bluetooth.DeviceCharacteristic) *WalkingPad {
return &WalkingPad{
device: device,
rx: rx,
tx: tx,
queue: make(chan walkingPadCommand, 50),
}
}
func (pad *WalkingPad) Disconnect() {
if pad.stopped {
return
}
pad.stopped = true
close(pad.queue)
pad.cancel()
pad.wg.Wait()
_ = pad.device.Disconnect()
}
func (pad *WalkingPad) pushCmd(cmd []byte, timeout time.Duration) {
fixCrc(cmd)
pad.queue <- walkingPadCommand{timeout: timeout, buffer: cmd}
}
func (pad *WalkingPad) ChangeMode(mode WalkingPadMode) {
pad.pushCmd([]byte{247, 162, 2, byte(mode), 0xFF, 253}, 0)
}
func (pad *WalkingPad) StartBelt() {
pad.pushCmd([]byte{247, 162, 4, 1, 0xFF, 253}, 0)
}
func (pad *WalkingPad) StopBelt() {
pad.ChangeSpeed(0.0)
}
func (pad *WalkingPad) ChangeSpeed(speed float64) {
if speed < 0 || speed > 6 {
panic("invalid speed")
}
cnv := byte(speed * 10.0)
pad.pushCmd([]byte{247, 162, 1, cnv, 0xFF, 253}, 0)
}
func (pad *WalkingPad) AskStats() {
pad.pushCmd([]byte{247, 162, 0, 0, 162, 253}, 0)
}
func (pad *WalkingPad) WaitCmd(timeout time.Duration) {
pad.pushCmd(nil, timeout)
}
func (pad *WalkingPad) onBufferReceive(buf []byte) {
if len(buf) < 2 {
return
}
if buf[0] == 248 && buf[1] == 162 {
status := readStatusBuffer(buf[2:])
pad.LastStatus = status
pad.LastStatusTime = time.Now()
return
}
}
func (pad *WalkingPad) writeLoop(ctx context.Context) {
defer pad.wg.Done()
for {
select {
case <-ctx.Done():
return
case cmd := <-pad.queue:
if cmd.timeout != 0 {
time.Sleep(cmd.timeout)
}
if cmd.buffer != nil {
_, err := pad.tx.WriteWithoutResponse(cmd.buffer)
if err != nil {
slog.Error("error writing to bluetooth device", "err", err)
}
time.Sleep(700 * time.Millisecond)
}
}
}
}
func (pad *WalkingPad) askStatsLoop(ctx context.Context) {
defer pad.wg.Done()
ticket := time.NewTicker(3 * time.Second)
defer ticket.Stop()
pad.AskStats()
for {
select {
case <-ctx.Done():
return
case <-ticket.C:
pad.AskStats()
}
}
}
func fixCrc(cmd []byte) {
if len(cmd) < 2 {
return
}
var sum byte
for i := 1; i < len(cmd)-2; i++ {
sum += cmd[i] // overflow intended
}
cmd[len(cmd)-2] = sum
}
type WalkingPadMode byte
const (
WalkingPadModeStandby WalkingPadMode = 2
WalkingPadModeManual WalkingPadMode = 1
WalkingPadModeAuto WalkingPadMode = 0
)
type WalkingPadStatus struct {
Speed float64
Mode WalkingPadMode
Time time.Duration
WalkedKM float64
Steps int
}
func readStatusBuffer(buf []byte) WalkingPadStatus {
timeS := int(buf[3])<<16 | int(buf[4])<<8 | int(buf[5])
dist := int(buf[6])<<16 | int(buf[7])<<8 | int(buf[8])
return WalkingPadStatus{
Speed: float64(buf[1]) / 10.0,
Mode: WalkingPadMode(buf[2]),
Time: time.Duration(timeS) * time.Second,
WalkedKM: float64(dist) / 100.0,
Steps: int(buf[9])<<16 | int(buf[10])<<8 | int(buf[11]),
}
}