From 61e70f7571ed4bde14c33de348d12ee039e35f05 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Fri, 28 Nov 2014 18:21:02 -0800 Subject: [PATCH] Refactor sphero to use new driver and adaptor interfaces --- platforms/sphero/sphero_adaptor.go | 27 +++++++++++++------------ platforms/sphero/sphero_adaptor_test.go | 3 ++- platforms/sphero/sphero_driver.go | 21 +++++++++++-------- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/platforms/sphero/sphero_adaptor.go b/platforms/sphero/sphero_adaptor.go index 86163b36..e5c6fc66 100644 --- a/platforms/sphero/sphero_adaptor.go +++ b/platforms/sphero/sphero_adaptor.go @@ -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() } diff --git a/platforms/sphero/sphero_adaptor_test.go b/platforms/sphero/sphero_adaptor_test.go index 1df6e4b4..05fb3b4b 100644 --- a/platforms/sphero/sphero_adaptor_test.go +++ b/platforms/sphero/sphero_adaptor_test.go @@ -1,8 +1,9 @@ package sphero import ( - "github.com/hybridgroup/gobot" "testing" + + "github.com/hybridgroup/gobot" ) func initTestSpheroAdaptor() *SpheroAdaptor { diff --git a/platforms/sphero/sphero_driver.go b/platforms/sphero/sphero_driver.go index 1add47d5..b0dd117f 100644 --- a/platforms/sphero/sphero_driver.go +++ b/platforms/sphero/sphero_driver.go @@ -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.