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
|
|
|
)
|
|
|
|
|
2016-12-01 23:08:56 +08:00
|
|
|
// Adaptor 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
|
|
|
|
2016-12-01 23:08:56 +08:00
|
|
|
// Name returns the Adaptor's name
|
|
|
|
func (a *Adaptor) Name() string { return a.name }
|
|
|
|
|
|
|
|
// SetName sets the Adaptor's name
|
2016-10-02 00:52:58 +08:00
|
|
|
func (a *Adaptor) SetName(n string) { a.name = n }
|
2016-12-01 23:08:56 +08:00
|
|
|
|
|
|
|
// Port returns the Adaptor's port
|
|
|
|
func (a *Adaptor) Port() string { return a.port }
|
|
|
|
|
|
|
|
// SetPort sets the Adaptor's port
|
2016-10-02 00:52:58 +08:00
|
|
|
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-11-08 03:15:07 +08:00
|
|
|
func (a *Adaptor) Connect() (err error) {
|
2016-12-01 23:08:56 +08:00
|
|
|
sp, e := a.connect(a.Port())
|
|
|
|
if e != nil {
|
2016-11-08 03:15:07 +08:00
|
|
|
return e
|
2014-11-20 08:17:14 +08:00
|
|
|
}
|
2016-12-01 23:08:56 +08:00
|
|
|
|
|
|
|
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-11-08 03:15:07 +08:00
|
|
|
func (a *Adaptor) Reconnect() (err 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-11-08 03:15:07 +08:00
|
|
|
func (a *Adaptor) Disconnect() error {
|
2014-12-23 23:00:23 +08:00
|
|
|
if a.connected {
|
2016-11-08 03:15:07 +08:00
|
|
|
if e := a.sp.Close(); e != nil {
|
|
|
|
return e
|
2014-12-23 23:00:23 +08:00
|
|
|
}
|
|
|
|
a.connected = false
|
2014-11-20 08:17:14 +08:00
|
|
|
}
|
2016-11-08 03:15:07 +08:00
|
|
|
return nil
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|
|
|
|
|
2016-10-02 00:52:58 +08:00
|
|
|
// Finalize finalizes the Sphero Adaptor
|
2016-11-08 03:15:07 +08:00
|
|
|
func (a *Adaptor) Finalize() error {
|
2014-11-29 10:21:02 +08:00
|
|
|
return a.Disconnect()
|
2014-04-26 18:11:51 +08:00
|
|
|
}
|