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"
|
|
|
|
|
2016-10-03 14:38:24 +08:00
|
|
|
"github.com/tarm/serial"
|
2014-04-26 18:11:51 +08:00
|
|
|
)
|
|
|
|
|
2014-09-29 11:32:31 +08:00
|
|
|
// Represents a Connection to a Sphero
|
2016-10-02 00:52:58 +08:00
|
|
|
type Adaptor struct {
|
2014-11-29 10:21:02 +08:00
|
|
|
name string
|
|
|
|
port string
|
|
|
|
sp io.ReadWriteCloser
|
|
|
|
connected bool
|
2014-12-23 23:00:23 +08:00
|
|
|
connect func(string) (io.ReadWriteCloser, error)
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2016-10-02 00:52:58 +08:00
|
|
|
// NewAdaptor returns a new Sphero Adaptor given a port
|
|
|
|
func NewAdaptor(port string) *Adaptor {
|
|
|
|
return &Adaptor{
|
|
|
|
name: "Sphero",
|
2014-11-29 10:21:02 +08:00
|
|
|
port: port,
|
2014-12-23 23:00:23 +08:00
|
|
|
connect: func(port string) (io.ReadWriteCloser, error) {
|
|
|
|
return serial.OpenPort(&serial.Config{Name: port, Baud: 115200})
|
2014-04-27 09:07:04 +08:00
|
|
|
},
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-02 00:52:58 +08:00
|
|
|
|
|
|
|
func (a *Adaptor) Name() string { return a.name }
|
|
|
|
func (a *Adaptor) SetName(n string) { a.name = n }
|
|
|
|
func (a *Adaptor) Port() string { return a.port }
|
|
|
|
func (a *Adaptor) SetPort(p string) { a.port = p }
|
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.
|
2016-10-02 00:52:58 +08:00
|
|
|
func (a *Adaptor) Connect() (errs []error) {
|
2014-12-23 23:00:23 +08:00
|
|
|
if sp, err := a.connect(a.Port()); err != nil {
|
2014-11-20 15:21:19 +08:00
|
|
|
return []error{err}
|
2014-12-23 23:00:23 +08:00
|
|
|
} else {
|
|
|
|
a.sp = sp
|
|
|
|
a.connected = 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
|
2016-10-02 00:52:58 +08:00
|
|
|
func (a *Adaptor) Reconnect() (errs []error) {
|
2014-12-23 23:00:23 +08:00
|
|
|
if a.connected {
|
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.
|
2016-10-02 00:52:58 +08:00
|
|
|
func (a *Adaptor) Disconnect() (errs []error) {
|
2014-12-23 23:00:23 +08:00
|
|
|
if a.connected {
|
|
|
|
if err := a.sp.Close(); err != nil {
|
|
|
|
return []error{err}
|
|
|
|
}
|
|
|
|
a.connected = false
|
2014-11-20 08:17:14 +08:00
|
|
|
}
|
2014-11-20 15:21:19 +08:00
|
|
|
return
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2016-10-02 00:52:58 +08:00
|
|
|
// Finalize finalizes the Sphero Adaptor
|
|
|
|
func (a *Adaptor) Finalize() (errs []error) {
|
2014-11-29 10:21:02 +08:00
|
|
|
return a.Disconnect()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|