-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (109 loc) · 2.59 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"os/signal"
"strings"
"syscall"
"time"
"github.com/hypebeast/go-osc/osc"
"tinygo.org/x/bluetooth"
)
var (
mac = ""
host = "localhost"
port = 9000
adapter = bluetooth.DefaultAdapter
)
func init() {
flag.StringVar(&mac, "mac", "", "过滤的MAC地址")
flag.StringVar(&host, "addr", "10.0.3.115", "VRChat OSC 地址")
flag.IntVar(&port, "port", 9000, "VRChat OSC 端口")
}
func main() {
flag.Parse()
mac = strings.ToUpper(mac)
if mac == "" {
log.Fatal("请指定蓝牙 MAC 地址")
}
_, err := bluetooth.ParseMAC(mac)
if err != nil {
log.Fatal("MAC 地址格式错误")
}
oscClient := osc.NewClient(host, port)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGHUP, syscall.SIGTERM)
defer stop()
if err := adapter.Enable(); err != nil {
log.Fatal("蓝牙开启失败", err)
}
atomicBpm := ""
go func() {
for {
if atomicBpm == "" {
continue
}
message := osc.NewMessage("/chatbox/input")
message.Append(strings.TrimSpace(atomicBpm))
message.Append(true)
message.Append(true)
_ = oscClient.Send(message)
time.Sleep(2 * time.Second)
}
}()
go func() {
<-ctx.Done()
_ = adapter.StopScan()
}()
log.Printf("开始监听来自 %s 的广播数据", mac)
maxBPM := 0
if err := adapter.Scan(func(adapter *bluetooth.Adapter, device bluetooth.ScanResult) {
if device.Address.MAC.String() == mac {
for _, data := range device.ManufacturerData() {
if data.CompanyID == 0x0157 {
bpm := int(data.Data[3])
if bpm == 255 {
log.Printf("当前心率不正常 ( == 255 ),可能未开启小米手环运动模式")
atomicBpm = fmt.Sprintf("[%d dBm] 小米手环寄了", device.RSSI)
continue
} else {
log.Printf("[ %d dBm] 当前手环心率为 %d BPM", device.RSSI, bpm)
}
if maxBPM < bpm {
maxBPM = bpm
}
note := "休息一下吧"
if bpm < 70 {
note = "我是废物"
}
if bpm < 40 {
note = "快猝死了"
}
if bpm < 20 {
note = "猝死了"
}
if 130 >= bpm && bpm > 100 {
note = "小小的运动一下"
}
if 150 >= bpm && bpm > 130 {
note = "呼呼呼 火力全开"
}
if bpm > 150 {
note = "感觉快寄了"
}
if bpm > 180 {
note = "真的快寄了"
}
if bpm > 200 {
note = "寄了"
}
atomicBpm = fmt.Sprintf("BLE RSSI: %d dBm\nHeart Rate: %03d / %03d BPM\n\n%s", device.RSSI, bpm, maxBPM, note)
}
}
}
}); err != nil {
log.Printf("%v", err)
}
log.Printf("任务结束")
}