-
Notifications
You must be signed in to change notification settings - Fork 2
/
go-row.go
78 lines (65 loc) · 1.33 KB
/
go-row.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
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/go-ble/ble"
"github.com/go-ble/ble/linux"
"github.com/mrverrall/go-row/peripheral"
"github.com/mrverrall/go-row/pm5"
)
var (
deviceName = "go-row"
)
func main() {
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.Printf("signal received from os: %s", sig)
unsetBT()
done <- true
}()
go btWorker(done)
<-done
}
func btWorker(done chan bool) {
for {
unsetBT()
d, err := linux.NewDeviceWithName(deviceName)
if err != nil {
log.Printf("can't get BT device: %s", err)
done <- true
}
ble.SetDefaultDevice(d)
log.Printf("searching for PM5...")
rower, err := pm5.NewClient()
if err != nil {
log.Printf("PM5 error: %s", err)
continue
}
sensors := peripheral.Sensors{
peripheral.NewCyclePower(deviceName),
peripheral.NewRunningSpeed(deviceName),
peripheral.NewHRM(deviceName),
}
log.Println("advertising sensor services")
go ble.AdvertiseNameAndServices(context.Background(), deviceName, sensors.UUIDs()...)
for data := range rower.StatusCh {
for _, s := range sensors {
select {
case s.DataCh <- data:
default:
}
}
}
}
}
func unsetBT() {
ble.Stop()
time.Sleep(time.Second * 5)
}