-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ble_firmata_adaptor.go
55 lines (45 loc) · 1.23 KB
/
ble_firmata_adaptor.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
//go:build !windows
// +build !windows
package firmata
import (
"io"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/ble"
"gobot.io/x/gobot/v2/platforms/bleclient"
)
const (
// ReceiveID is the BLE characteristic ID for receiving serial data
ReceiveID = "6e400003b5a3f393e0a9e50e24dcca9e"
// TransmitID is the BLE characteristic ID for transmitting serial data
TransmitID = "6e400002b5a3f393e0a9e50e24dcca9e"
)
// BLEAdaptor represents a Bluetooth LE based connection to a
// microcontroller running FirmataBLE
type BLEAdaptor struct {
*Adaptor
}
// NewBLEAdaptor opens and uses a BLE connection to a
// microcontroller running FirmataBLE
func NewBLEAdaptor(args ...interface{}) *BLEAdaptor {
address := args[0].(string) //nolint:forcetypeassert // ok here
rid := ReceiveID
wid := TransmitID
//nolint:forcetypeassert // ok here
if len(args) >= 3 {
rid = args[1].(string)
wid = args[2].(string)
}
a := NewAdaptor(address)
a.SetName(gobot.DefaultName("BLEFirmata"))
a.PortOpener = func(port string) (io.ReadWriteCloser, error) {
a := bleclient.NewAdaptor(address)
sp := ble.NewSerialPortDriver(a, rid, wid)
if err := sp.Open(); err != nil {
return sp, err
}
return sp, nil
}
return &BLEAdaptor{
Adaptor: a,
}
}