forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphero_adaptor.go
54 lines (47 loc) · 904 Bytes
/
sphero_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
package sphero
import (
"github.com/hybridgroup/gobot"
"github.com/tarm/goserial"
"io"
)
type SpheroAdaptor struct {
gobot.Adaptor
sp io.ReadWriteCloser
connect func(*SpheroAdaptor)
}
func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
return &SpheroAdaptor{
Adaptor: *gobot.NewAdaptor(
name,
"SpheroAdaptor",
port,
),
connect: func(a *SpheroAdaptor) {
c := &serial.Config{Name: a.Port(), Baud: 115200}
s, err := serial.OpenPort(c)
if err != nil {
panic(err)
}
a.sp = s
},
}
}
func (a *SpheroAdaptor) Connect() bool {
a.connect(a)
a.SetConnected(true)
return true
}
func (a *SpheroAdaptor) Reconnect() bool {
if a.Connected() == true {
a.Disconnect()
}
return a.Connect()
}
func (a *SpheroAdaptor) Disconnect() bool {
a.sp.Close()
a.SetConnected(false)
return true
}
func (a *SpheroAdaptor) Finalize() bool {
return true
}