2020-10-29 04:02:59 +08:00
|
|
|
// +build !windows
|
2020-10-27 03:04:51 +08:00
|
|
|
|
2017-01-17 04:45:37 +08:00
|
|
|
package firmata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
2017-02-02 23:10:09 +08:00
|
|
|
"gobot.io/x/gobot"
|
2017-01-17 04:45:37 +08:00
|
|
|
"gobot.io/x/gobot/platforms/ble"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-01-17 04:50:54 +08:00
|
|
|
// ReceiveID is the BLE characteristic ID for receiving serial data
|
|
|
|
ReceiveID = "6e400003b5a3f393e0a9e50e24dcca9e"
|
|
|
|
|
|
|
|
// TransmitID is the BLE characteristic ID for transmitting serial data
|
2017-01-17 04:45:37 +08:00
|
|
|
TransmitID = "6e400002b5a3f393e0a9e50e24dcca9e"
|
|
|
|
)
|
|
|
|
|
2017-01-17 21:09:24 +08:00
|
|
|
// BLEAdaptor represents a Bluetooth LE based connection to a
|
|
|
|
// microcontroller running FirmataBLE
|
2017-01-17 04:45:37 +08:00
|
|
|
type BLEAdaptor struct {
|
|
|
|
*Adaptor
|
|
|
|
}
|
|
|
|
|
2017-01-17 21:09:24 +08:00
|
|
|
// NewBLEAdaptor opens and uses a BLE connection to a
|
|
|
|
// microcontroller running FirmataBLE
|
2017-01-17 04:45:37 +08:00
|
|
|
func NewBLEAdaptor(args ...interface{}) *BLEAdaptor {
|
|
|
|
address := args[0].(string)
|
|
|
|
rid := ReceiveID
|
|
|
|
wid := TransmitID
|
|
|
|
|
|
|
|
if len(args) >= 3 {
|
|
|
|
rid = args[1].(string)
|
|
|
|
wid = args[2].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
a := NewAdaptor(address)
|
2017-02-02 23:10:09 +08:00
|
|
|
a.SetName(gobot.DefaultName("BLEFirmata"))
|
2017-01-17 04:45:37 +08:00
|
|
|
a.PortOpener = func(port string) (io.ReadWriteCloser, error) {
|
2017-01-18 19:17:49 +08:00
|
|
|
sp := ble.NewSerialPort(address, rid, wid)
|
2017-01-17 04:45:37 +08:00
|
|
|
sp.Open()
|
|
|
|
return sp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BLEAdaptor{
|
2017-01-18 19:17:49 +08:00
|
|
|
Adaptor: a,
|
2017-01-17 04:45:37 +08:00
|
|
|
}
|
|
|
|
}
|