core: Refactor Sphero platform for new Adaptor/Driver creation signatures

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2016-10-01 18:52:58 +02:00
parent db69c63056
commit 7136bfcba7
6 changed files with 41 additions and 35 deletions

View File

@ -2,9 +2,9 @@
Sphero is a sophisticated and programmable robot housed in a polycarbonate sphere shell.
The gobot-sphero adaptor makes it easy to interact with Sphero using Go, once you have your Sphero setup and connected to your computer you can start writing code to make Sphero move, change direction, speed and colors, or detect Sphero events and execute some code when they occur.
The Gobot Sphero Adaptor & Driver makes it easy to interact with Sphero using Go. Once you have your Sphero setup and connected to your computer you can start writing code to make Sphero move, change direction, speed and colors, or detect Sphero events and execute some code when they occur.
Learn more about the Sphero robot from Orbotix [here](http://www.gosphero.com/).
Learn more about the Sphero robot go here: http://www.gosphero.com/
## How to Install
```
@ -60,8 +60,8 @@ import (
func main() {
gbot := gobot.NewGobot()
adaptor := sphero.NewSpheroAdaptor("sphero", "/dev/rfcomm0")
driver := sphero.NewSpheroDriver(adaptor, "sphero")
adaptor := sphero.NewAdaptor("/dev/rfcomm0")
driver := sphero.NewSpheroDriver(adaptor)
work := func() {
gobot.Every(3*time.Second, func() {

View File

@ -20,8 +20,8 @@ Example:
func main() {
gbot := gobot.NewGobot()
adaptor := sphero.NewSpheroAdaptor("sphero", "/dev/rfcomm0")
driver := sphero.NewSpheroDriver(adaptor, "sphero")
adaptor := sphero.NewAdaptor("/dev/rfcomm0")
driver := sphero.NewSpheroDriver(adaptor)
work := func() {
gobot.Every(3*time.Second, func() {

View File

@ -7,7 +7,7 @@ import (
)
// Represents a Connection to a Sphero
type SpheroAdaptor struct {
type Adaptor struct {
name string
port string
sp io.ReadWriteCloser
@ -15,21 +15,24 @@ type SpheroAdaptor struct {
connect func(string) (io.ReadWriteCloser, error)
}
// NewSpheroAdaptor returns a new SpheroAdaptor given a name and port
func NewSpheroAdaptor(name string, port string) *SpheroAdaptor {
return &SpheroAdaptor{
name: name,
// NewAdaptor returns a new Sphero Adaptor given a port
func NewAdaptor(port string) *Adaptor {
return &Adaptor{
name: "Sphero",
port: port,
connect: func(port string) (io.ReadWriteCloser, error) {
return serial.OpenPort(&serial.Config{Name: port, Baud: 115200})
},
}
}
func (a *SpheroAdaptor) Name() string { return a.name }
func (a *SpheroAdaptor) Port() string { return a.port }
func (a *Adaptor) Name() string { return a.name }
func (a *Adaptor) SetName(n string) { a.name = n }
func (a *Adaptor) Port() string { return a.port }
func (a *Adaptor) SetPort(p string) { a.port = p }
// Connect initiates a connection to the Sphero. Returns true on successful connection.
func (a *SpheroAdaptor) Connect() (errs []error) {
func (a *Adaptor) Connect() (errs []error) {
if sp, err := a.connect(a.Port()); err != nil {
return []error{err}
} else {
@ -42,7 +45,7 @@ func (a *SpheroAdaptor) Connect() (errs []error) {
// 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 *SpheroAdaptor) Reconnect() (errs []error) {
func (a *Adaptor) Reconnect() (errs []error) {
if a.connected {
a.Disconnect()
}
@ -50,7 +53,7 @@ func (a *SpheroAdaptor) Reconnect() (errs []error) {
}
// Disconnect terminates the connection to the Sphero. Returns true on successful disconnect.
func (a *SpheroAdaptor) Disconnect() (errs []error) {
func (a *Adaptor) Disconnect() (errs []error) {
if a.connected {
if err := a.sp.Close(); err != nil {
return []error{err}
@ -60,7 +63,7 @@ func (a *SpheroAdaptor) Disconnect() (errs []error) {
return
}
// Finalize finalizes the SpheroAdaptor
func (a *SpheroAdaptor) Finalize() (errs []error) {
// Finalize finalizes the Sphero Adaptor
func (a *Adaptor) Finalize() (errs []error) {
return a.Disconnect()
}

View File

@ -9,7 +9,7 @@ import (
"github.com/hybridgroup/gobot/gobottest"
)
var _ gobot.Adaptor = (*SpheroAdaptor)(nil)
var _ gobot.Adaptor = (*Adaptor)(nil)
type nullReadWriteCloser struct{}
@ -37,8 +37,8 @@ func (nullReadWriteCloser) Close() error {
return testAdaptorClose()
}
func initTestSpheroAdaptor() *SpheroAdaptor {
a := NewSpheroAdaptor("bot", "/dev/null")
func initTestSpheroAdaptor() *Adaptor {
a := NewAdaptor("/dev/null")
a.connect = func(string) (io.ReadWriteCloser, error) {
return &nullReadWriteCloser{}, nil
}
@ -47,7 +47,7 @@ func initTestSpheroAdaptor() *SpheroAdaptor {
func TestSpheroAdaptor(t *testing.T) {
a := initTestSpheroAdaptor()
gobottest.Assert(t, a.Name(), "bot")
gobottest.Assert(t, a.Name(), "Sphero")
gobottest.Assert(t, a.Port(), "/dev/null")
}

View File

@ -10,11 +10,13 @@ import (
)
const (
// event when error encountered
// Error event when error encountered
Error = "error"
// event when sensor data is received
// SensorData event when sensor data is received
SensorData = "sensordata"
// event when collision is detected
// Collision event when collision is detected
Collision = "collision"
)
@ -24,7 +26,7 @@ type packet struct {
checksum uint8
}
// Represents a Sphero
// Represents a Sphero 2.0
type SpheroDriver struct {
name string
connection gobot.Connection
@ -37,7 +39,7 @@ type SpheroDriver struct {
gobot.Commander
}
// NewSpheroDriver returns a new SpheroDriver given a SpheroAdaptor and name.
// NewSpheroDriver returns a new SpheroDriver given a Sphero Adaptor.
//
// Adds the following API Commands:
// "ConfigureLocator" - See SpheroDriver.ConfigureLocator
@ -50,9 +52,9 @@ type SpheroDriver struct {
// "SetStabilization" - See SpheroDriver.SetStabilization
// "SetDataStreaming" - See SpheroDriver.SetDataStreaming
// "SetRotationRate" - See SpheroDriver.SetRotationRate
func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
func NewSpheroDriver(a *Adaptor) *SpheroDriver {
s := &SpheroDriver{
name: name,
name: "Sphero",
connection: a,
Eventer: gobot.NewEventer(),
Commander: gobot.NewCommander(),
@ -141,10 +143,11 @@ func NewSpheroDriver(a *SpheroAdaptor, name string) *SpheroDriver {
}
func (s *SpheroDriver) Name() string { return s.name }
func (s *SpheroDriver) SetName(n string) { s.name = n }
func (s *SpheroDriver) Connection() gobot.Connection { return s.connection }
func (s *SpheroDriver) adaptor() *SpheroAdaptor {
return s.Connection().(*SpheroAdaptor)
func (s *SpheroDriver) adaptor() *Adaptor {
return s.Connection().(*Adaptor)
}
// Start starts the SpheroDriver and enables Collision Detection.

View File

@ -12,9 +12,9 @@ import (
var _ gobot.Driver = (*SpheroDriver)(nil)
func initTestSpheroDriver() *SpheroDriver {
a := NewSpheroAdaptor("bot", "/dev/null")
a := NewAdaptor("/dev/null")
a.sp = nullReadWriteCloser{}
return NewSpheroDriver(a, "bot")
return NewSpheroDriver(a)
}
func TestSpheroDriver(t *testing.T) {
@ -70,8 +70,8 @@ func TestSpheroDriver(t *testing.T) {
ret = d.Command("ReadLocator")(nil)
gobottest.Assert(t, ret, []int16{})
gobottest.Assert(t, d.Name(), "bot")
gobottest.Assert(t, d.Connection().Name(), "bot")
gobottest.Assert(t, d.Name(), "Sphero")
gobottest.Assert(t, d.Connection().Name(), "Sphero")
}
func TestSpheroDriverStart(t *testing.T) {