2014-04-27 09:07:04 +08:00
|
|
|
package sphero
|
2014-04-26 18:11:51 +08:00
|
|
|
|
|
|
|
import (
|
2014-09-29 11:32:31 +08:00
|
|
|
"io"
|
|
|
|
|
2014-04-26 18:11:51 +08:00
|
|
|
"github.com/hybridgroup/gobot"
|
|
|
|
"github.com/tarm/goserial"
|
|
|
|
)
|
|
|
|
|
2014-11-17 04:25:48 +08:00
|
|
|
var _ gobot.AdaptorInterface = (*SpheroAdaptor)(nil)
|
|
|
|
|
2014-09-29 11:32:31 +08:00
|
|
|
// Represents a Connection to a Sphero
|
2014-04-26 18:11:51 +08:00
|
|
|
type SpheroAdaptor struct {
|
|
|
|
gobot.Adaptor
|
2014-04-27 09:07:04 +08:00
|
|
|
sp io.ReadWriteCloser
|
2014-11-20 08:17:14 +08:00
|
|
|
connect func(*SpheroAdaptor) (err error)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-09-29 11:32:31 +08:00
|
|
|
// NewSpheroAdaptor returns a new SpheroAdaptor given a name and port
|
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-11-20 08:17:14 +08:00
|
|
|
connect: func(a *SpheroAdaptor) (err error) {
|
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 {
|
2014-11-20 08:17:14 +08:00
|
|
|
return err
|
2014-04-27 09:07:04 +08:00
|
|
|
}
|
|
|
|
a.sp = s
|
2014-11-20 08:17:14 +08:00
|
|
|
return
|
2014-04-27 09:07:04 +08:00
|
|
|
},
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 11:32:31 +08:00
|
|
|
// Connect initiates a connection to the Sphero. Returns true on successful connection.
|
2014-11-20 15:21:19 +08:00
|
|
|
func (a *SpheroAdaptor) Connect() (errs []error) {
|
|
|
|
if err := a.connect(a); err != nil {
|
|
|
|
return []error{err}
|
2014-11-20 08:17:14 +08:00
|
|
|
}
|
2014-07-08 08:42:19 +08:00
|
|
|
a.SetConnected(true)
|
2014-11-20 08:17:14 +08:00
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-09-29 11:32:31 +08:00
|
|
|
// Reconnect attempts to reconnect to the Sphero. If the Sphero has an active connection
|
|
|
|
// it will first close that connection and then establish a new connection.
|
|
|
|
// Returns true on Successful reconnection
|
2014-11-20 15:21:19 +08:00
|
|
|
func (a *SpheroAdaptor) Reconnect() (errs []error) {
|
2014-07-08 08:42:19 +08:00
|
|
|
if a.Connected() == true {
|
2014-04-27 09:07:04 +08:00
|
|
|
a.Disconnect()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
2014-04-27 09:07:04 +08:00
|
|
|
return a.Connect()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-09-29 11:32:31 +08:00
|
|
|
// Disconnect terminates the connection to the Sphero. Returns true on successful disconnect.
|
2014-11-20 15:21:19 +08:00
|
|
|
func (a *SpheroAdaptor) Disconnect() (errs []error) {
|
|
|
|
if err := a.sp.Close(); err != nil {
|
|
|
|
return []error{err}
|
2014-11-20 08:17:14 +08:00
|
|
|
}
|
2014-07-08 08:42:19 +08:00
|
|
|
a.SetConnected(false)
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2014-09-29 11:32:31 +08:00
|
|
|
// Finalize finalizes the SpheroAdaptor
|
2014-11-20 15:21:19 +08:00
|
|
|
func (a *SpheroAdaptor) Finalize() (errs []error) {
|
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|