-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlego.go
executable file
·55 lines (43 loc) · 1.17 KB
/
lego.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
package lego
import (
"errors"
"tinygo.org/x/bluetooth"
)
type Hub struct {
device *bluetooth.Device
svc *bluetooth.DeviceService
chr *bluetooth.DeviceCharacteristic
buf []byte
}
var (
// BLE service
// 00001623-1212-EFDE-1623-785FEABCD123
hubService = bluetooth.NewUUID([16]byte{0x00, 0x00, 0x16, 0x23, 0x12, 0x12, 0xef, 0xde, 0x16, 0x23, 0x78, 0x5f, 0xea, 0xbc, 0xd1, 0x23})
hubCharacteristic = bluetooth.NewUUID([16]byte{0x00, 0x00, 0x16, 0x24, 0x12, 0x12, 0xef, 0xde, 0x16, 0x23, 0x78, 0x5f, 0xea, 0xbc, 0xd1, 0x23})
)
// NewHub creates a new LEGO hub.
func NewHub(dev *bluetooth.Device) *Hub {
h := &Hub{
device: dev,
buf: make([]byte, 255),
}
return h
}
func (h *Hub) Start() (err error) {
srvcs, err := r.device.DiscoverServices([]bluetooth.UUID{
hubService,
})
if err != nil || len(srvcs) == 0 {
return errors.New("could not find services")
}
h.svc = &srvcs[0]
println("found LEGO hub service", h.srv.UUID().String())
chars, err := h.svc.DiscoverCharacteristics([]bluetooth.UUID{
hubCharacteristic,
})
if err != nil || len(chars) == 0 {
return errors.New("could not find LEGO hub characteristic")
}
h.chr = &chars[0]
return
}