hybridgroup.gobot/platforms/sphero/sphero_adaptor.go

55 lines
904 B
Go
Raw Normal View History

2014-04-27 09:07:04 +08:00
package sphero
import (
"github.com/hybridgroup/gobot"
"github.com/tarm/goserial"
"io"
)
type SpheroAdaptor struct {
gobot.Adaptor
2014-04-27 09:07:04 +08:00
sp io.ReadWriteCloser
connect func(*SpheroAdaptor)
}
2014-05-23 12:20:16 +08:00
func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
2014-04-27 09:07:04 +08:00
return &SpheroAdaptor{
2014-07-08 08:42:19 +08:00
Adaptor: *gobot.NewAdaptor(
name,
"SpheroAdaptor",
port,
),
2014-04-27 09:07:04 +08:00
connect: func(a *SpheroAdaptor) {
2014-07-08 08:42:19 +08:00
c := &serial.Config{Name: a.Port(), Baud: 115200}
2014-04-27 09:07:04 +08:00
s, err := serial.OpenPort(c)
if err != nil {
panic(err)
}
a.sp = s
},
}
}
2014-04-27 09:07:04 +08:00
func (a *SpheroAdaptor) Connect() bool {
a.connect(a)
2014-07-08 08:42:19 +08:00
a.SetConnected(true)
return true
}
2014-04-27 09:07:04 +08:00
func (a *SpheroAdaptor) Reconnect() bool {
2014-07-08 08:42:19 +08:00
if a.Connected() == true {
2014-04-27 09:07:04 +08:00
a.Disconnect()
}
2014-04-27 09:07:04 +08:00
return a.Connect()
}
2014-04-27 09:07:04 +08:00
func (a *SpheroAdaptor) Disconnect() bool {
a.sp.Close()
2014-07-08 08:42:19 +08:00
a.SetConnected(false)
return true
}
2014-04-27 09:07:04 +08:00
func (a *SpheroAdaptor) Finalize() bool {
return true
}