1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00

Refactor sphero to use new driver and adaptor interfaces

This commit is contained in:
Adrian Zankich 2014-11-28 18:21:02 -08:00
parent 57978d87e9
commit 61e70f7571
3 changed files with 29 additions and 22 deletions

View File

@ -7,41 +7,42 @@ import (
"github.com/tarm/goserial" "github.com/tarm/goserial"
) )
var _ gobot.AdaptorInterface = (*SpheroAdaptor)(nil) var _ gobot.Adaptor = (*SpheroAdaptor)(nil)
// Represents a Connection to a Sphero // Represents a Connection to a Sphero
type SpheroAdaptor struct { type SpheroAdaptor struct {
gobot.Adaptor name string
sp io.ReadWriteCloser port string
connect func(*SpheroAdaptor) (err error) sp io.ReadWriteCloser
connected bool
connect func(*SpheroAdaptor) (err error)
} }
// NewSpheroAdaptor returns a new SpheroAdaptor given a name and port // NewSpheroAdaptor returns a new SpheroAdaptor given a name and port
func NewSpheroAdaptor(name string, port string) *SpheroAdaptor { func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
return &SpheroAdaptor{ return &SpheroAdaptor{
Adaptor: *gobot.NewAdaptor( name: name,
name, port: port,
"SpheroAdaptor",
port,
),
connect: func(a *SpheroAdaptor) (err error) { connect: func(a *SpheroAdaptor) (err error) {
c := &serial.Config{Name: a.Port(), Baud: 115200} c := &serial.Config{Name: a.Port(), Baud: 115200}
s, err := serial.OpenPort(c) s, err := serial.OpenPort(c)
if err != nil { if err != nil {
return err return err
} }
a.connected = true
a.sp = s a.sp = s
return 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. // Connect initiates a connection to the Sphero. Returns true on successful connection.
func (a *SpheroAdaptor) Connect() (errs []error) { func (a *SpheroAdaptor) Connect() (errs []error) {
if err := a.connect(a); err != nil { if err := a.connect(a); err != nil {
return []error{err} return []error{err}
} }
a.SetConnected(true)
return return
} }
@ -49,7 +50,7 @@ func (a *SpheroAdaptor) Connect() (errs []error) {
// it will first close that connection and then establish a new connection. // it will first close that connection and then establish a new connection.
// Returns true on Successful reconnection // Returns true on Successful reconnection
func (a *SpheroAdaptor) Reconnect() (errs []error) { func (a *SpheroAdaptor) Reconnect() (errs []error) {
if a.Connected() == true { if a.connected == true {
a.Disconnect() a.Disconnect()
} }
return a.Connect() return a.Connect()
@ -60,11 +61,11 @@ func (a *SpheroAdaptor) Disconnect() (errs []error) {
if err := a.sp.Close(); err != nil { if err := a.sp.Close(); err != nil {
return []error{err} return []error{err}
} }
a.SetConnected(false) a.connected = false
return return
} }
// Finalize finalizes the SpheroAdaptor // Finalize finalizes the SpheroAdaptor
func (a *SpheroAdaptor) Finalize() (errs []error) { func (a *SpheroAdaptor) Finalize() (errs []error) {
return return a.Disconnect()
} }

View File

@ -1,8 +1,9 @@
package sphero package sphero
import ( import (
"github.com/hybridgroup/gobot"
"testing" "testing"
"github.com/hybridgroup/gobot"
) )
func initTestSpheroAdaptor() *SpheroAdaptor { func initTestSpheroAdaptor() *SpheroAdaptor {

View File

@ -9,7 +9,7 @@ import (
"github.com/hybridgroup/gobot" "github.com/hybridgroup/gobot"
) )
var _ gobot.DriverInterface = (*SpheroDriver)(nil) var _ gobot.Driver = (*SpheroDriver)(nil)
type packet struct { type packet struct {
header []uint8 header []uint8
@ -19,12 +19,15 @@ type packet struct {
// Represents a Sphero // Represents a Sphero
type SpheroDriver struct { type SpheroDriver struct {
gobot.Driver name string
connection gobot.Connection
seq uint8 seq uint8
asyncResponse [][]uint8 asyncResponse [][]uint8
syncResponse [][]uint8 syncResponse [][]uint8
packetChannel chan *packet packetChannel chan *packet
responseChannel chan []uint8 responseChannel chan []uint8
gobot.Eventer
gobot.Commander
} }
type Collision struct { type Collision struct {
@ -51,11 +54,10 @@ type Collision struct {
// "SetStabilization" - See SpheroDriver.SetStabilization // "SetStabilization" - See SpheroDriver.SetStabilization
func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver { func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
s := &SpheroDriver{ s := &SpheroDriver{
Driver: *gobot.NewDriver( name: name,
name, connection: a,
"SpheroDriver", Eventer: gobot.NewEventer(),
a, Commander: gobot.NewCommander(),
),
packetChannel: make(chan *packet, 1024), packetChannel: make(chan *packet, 1024),
responseChannel: make(chan []uint8, 1024), responseChannel: make(chan []uint8, 1024),
} }
@ -106,8 +108,11 @@ func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
return s return s
} }
func (s *SpheroDriver) Name() string { return s.name }
func (s *SpheroDriver) Connection() gobot.Connection { return s.connection }
func (s *SpheroDriver) adaptor() *SpheroAdaptor { func (s *SpheroDriver) adaptor() *SpheroAdaptor {
return s.Adaptor().(*SpheroAdaptor) return s.Connection().(*SpheroAdaptor)
} }
// Start starts the SpheroDriver and enables Collision Detection. // Start starts the SpheroDriver and enables Collision Detection.