82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package sphero
|
|
|
|
import (
|
|
"io"
|
|
|
|
"go.bug.st/serial"
|
|
|
|
"gobot.io/x/gobot/v2"
|
|
)
|
|
|
|
// Adaptor represents a Connection to a Sphero
|
|
type Adaptor struct {
|
|
name string
|
|
port string
|
|
sp io.ReadWriteCloser
|
|
connected bool
|
|
connect func(string) (io.ReadWriteCloser, error)
|
|
}
|
|
|
|
// NewAdaptor returns a new Sphero Adaptor given a port
|
|
func NewAdaptor(port string) *Adaptor {
|
|
return &Adaptor{
|
|
name: gobot.DefaultName("Sphero"),
|
|
port: port,
|
|
connect: func(port string) (io.ReadWriteCloser, error) {
|
|
return serial.Open(port, &serial.Mode{BaudRate: 115200})
|
|
},
|
|
}
|
|
}
|
|
|
|
// Name returns the Adaptor's name
|
|
func (a *Adaptor) Name() string { return a.name }
|
|
|
|
// SetName sets the Adaptor's name
|
|
func (a *Adaptor) SetName(n string) { a.name = n }
|
|
|
|
// Port returns the Adaptor's port
|
|
func (a *Adaptor) Port() string { return a.port }
|
|
|
|
// SetPort sets the Adaptor's port
|
|
func (a *Adaptor) SetPort(p string) { a.port = p }
|
|
|
|
// Connect initiates a connection to the Sphero. Returns true on successful connection.
|
|
func (a *Adaptor) Connect() error {
|
|
sp, err := a.connect(a.Port())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
a.sp = sp
|
|
a.connected = true
|
|
return nil
|
|
}
|
|
|
|
// 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
|
|
func (a *Adaptor) Reconnect() error {
|
|
if a.connected {
|
|
if err := a.Disconnect(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return a.Connect()
|
|
}
|
|
|
|
// Disconnect terminates the connection to the Sphero. Returns true on successful disconnect.
|
|
func (a *Adaptor) Disconnect() error {
|
|
if a.connected {
|
|
if err := a.sp.Close(); err != nil {
|
|
return err
|
|
}
|
|
a.connected = false
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Finalize finalizes the Sphero Adaptor
|
|
func (a *Adaptor) Finalize() error {
|
|
return a.Disconnect()
|
|
}
|