Refactor sphero to use new driver and adaptor interfaces
This commit is contained in:
parent
57978d87e9
commit
61e70f7571
|
@ -7,41 +7,42 @@ import (
|
|||
"github.com/tarm/goserial"
|
||||
)
|
||||
|
||||
var _ gobot.AdaptorInterface = (*SpheroAdaptor)(nil)
|
||||
var _ gobot.Adaptor = (*SpheroAdaptor)(nil)
|
||||
|
||||
// Represents a Connection to a Sphero
|
||||
type SpheroAdaptor struct {
|
||||
gobot.Adaptor
|
||||
sp io.ReadWriteCloser
|
||||
connect func(*SpheroAdaptor) (err error)
|
||||
name string
|
||||
port string
|
||||
sp io.ReadWriteCloser
|
||||
connected bool
|
||||
connect func(*SpheroAdaptor) (err error)
|
||||
}
|
||||
|
||||
// NewSpheroAdaptor returns a new SpheroAdaptor given a name and port
|
||||
func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
|
||||
return &SpheroAdaptor{
|
||||
Adaptor: *gobot.NewAdaptor(
|
||||
name,
|
||||
"SpheroAdaptor",
|
||||
port,
|
||||
),
|
||||
name: name,
|
||||
port: port,
|
||||
connect: func(a *SpheroAdaptor) (err error) {
|
||||
c := &serial.Config{Name: a.Port(), Baud: 115200}
|
||||
s, err := serial.OpenPort(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.connected = true
|
||||
a.sp = s
|
||||
return
|
||||
},
|
||||
}
|
||||
}
|
||||
func (a *SpheroAdaptor) Name() string { return a.name }
|
||||
func (a *SpheroAdaptor) Port() string { return a.port }
|
||||
|
||||
// Connect initiates a connection to the Sphero. Returns true on successful connection.
|
||||
func (a *SpheroAdaptor) Connect() (errs []error) {
|
||||
if err := a.connect(a); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
a.SetConnected(true)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -49,7 +50,7 @@ func (a *SpheroAdaptor) Connect() (errs []error) {
|
|||
// it will first close that connection and then establish a new connection.
|
||||
// Returns true on Successful reconnection
|
||||
func (a *SpheroAdaptor) Reconnect() (errs []error) {
|
||||
if a.Connected() == true {
|
||||
if a.connected == true {
|
||||
a.Disconnect()
|
||||
}
|
||||
return a.Connect()
|
||||
|
@ -60,11 +61,11 @@ func (a *SpheroAdaptor) Disconnect() (errs []error) {
|
|||
if err := a.sp.Close(); err != nil {
|
||||
return []error{err}
|
||||
}
|
||||
a.SetConnected(false)
|
||||
a.connected = false
|
||||
return
|
||||
}
|
||||
|
||||
// Finalize finalizes the SpheroAdaptor
|
||||
func (a *SpheroAdaptor) Finalize() (errs []error) {
|
||||
return
|
||||
return a.Disconnect()
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package sphero
|
||||
|
||||
import (
|
||||
"github.com/hybridgroup/gobot"
|
||||
"testing"
|
||||
|
||||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
func initTestSpheroAdaptor() *SpheroAdaptor {
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"github.com/hybridgroup/gobot"
|
||||
)
|
||||
|
||||
var _ gobot.DriverInterface = (*SpheroDriver)(nil)
|
||||
var _ gobot.Driver = (*SpheroDriver)(nil)
|
||||
|
||||
type packet struct {
|
||||
header []uint8
|
||||
|
@ -19,12 +19,15 @@ type packet struct {
|
|||
|
||||
// Represents a Sphero
|
||||
type SpheroDriver struct {
|
||||
gobot.Driver
|
||||
name string
|
||||
connection gobot.Connection
|
||||
seq uint8
|
||||
asyncResponse [][]uint8
|
||||
syncResponse [][]uint8
|
||||
packetChannel chan *packet
|
||||
responseChannel chan []uint8
|
||||
gobot.Eventer
|
||||
gobot.Commander
|
||||
}
|
||||
|
||||
type Collision struct {
|
||||
|
@ -51,11 +54,10 @@ type Collision struct {
|
|||
// "SetStabilization" - See SpheroDriver.SetStabilization
|
||||
func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
|
||||
s := &SpheroDriver{
|
||||
Driver: *gobot.NewDriver(
|
||||
name,
|
||||
"SpheroDriver",
|
||||
a,
|
||||
),
|
||||
name: name,
|
||||
connection: a,
|
||||
Eventer: gobot.NewEventer(),
|
||||
Commander: gobot.NewCommander(),
|
||||
packetChannel: make(chan *packet, 1024),
|
||||
responseChannel: make(chan []uint8, 1024),
|
||||
}
|
||||
|
@ -106,8 +108,11 @@ func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
|
|||
return s
|
||||
}
|
||||
|
||||
func (s *SpheroDriver) Name() string { return s.name }
|
||||
func (s *SpheroDriver) Connection() gobot.Connection { return s.connection }
|
||||
|
||||
func (s *SpheroDriver) adaptor() *SpheroAdaptor {
|
||||
return s.Adaptor().(*SpheroAdaptor)
|
||||
return s.Connection().(*SpheroAdaptor)
|
||||
}
|
||||
|
||||
// Start starts the SpheroDriver and enables Collision Detection.
|
||||
|
|
Loading…
Reference in New Issue