mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-11 19:29:20 +08:00
Refactor ardrone to use new driver and adaptor interfaces
This commit is contained in:
parent
88db6be3ac
commit
9a0f3b46f7
@ -5,7 +5,7 @@ import (
|
|||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ gobot.AdaptorInterface = (*ArdroneAdaptor)(nil)
|
var _ gobot.Adaptor = (*ArdroneAdaptor)(nil)
|
||||||
|
|
||||||
// drone defines expected drone behaviour
|
// drone defines expected drone behaviour
|
||||||
type drone interface {
|
type drone interface {
|
||||||
@ -23,7 +23,7 @@ type drone interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ArdroneAdaptor struct {
|
type ArdroneAdaptor struct {
|
||||||
gobot.Adaptor
|
name string
|
||||||
drone drone
|
drone drone
|
||||||
connect func(*ArdroneAdaptor) (err error)
|
connect func(*ArdroneAdaptor) (err error)
|
||||||
}
|
}
|
||||||
@ -31,10 +31,7 @@ type ArdroneAdaptor struct {
|
|||||||
// NewArdroneAdaptor creates a new ardrone and connects with default configuration
|
// NewArdroneAdaptor creates a new ardrone and connects with default configuration
|
||||||
func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
|
func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
|
||||||
return &ArdroneAdaptor{
|
return &ArdroneAdaptor{
|
||||||
Adaptor: *gobot.NewAdaptor(
|
name: name,
|
||||||
name,
|
|
||||||
"ArdroneAdaptor",
|
|
||||||
),
|
|
||||||
connect: func(a *ArdroneAdaptor) (err error) {
|
connect: func(a *ArdroneAdaptor) (err error) {
|
||||||
config := client.DefaultConfig()
|
config := client.DefaultConfig()
|
||||||
if len(v) > 0 {
|
if len(v) > 0 {
|
||||||
@ -50,6 +47,8 @@ func NewArdroneAdaptor(name string, v ...string) *ArdroneAdaptor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *ArdroneAdaptor) Name() string { return a.name }
|
||||||
|
|
||||||
// Connect returns true when connection to ardrone is established correclty
|
// Connect returns true when connection to ardrone is established correclty
|
||||||
func (a *ArdroneAdaptor) Connect() (errs []error) {
|
func (a *ArdroneAdaptor) Connect() (errs []error) {
|
||||||
if err := a.connect(a); err != nil {
|
if err := a.connect(a); err != nil {
|
||||||
@ -59,6 +58,4 @@ func (a *ArdroneAdaptor) Connect() (errs []error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Finalize returns true when connection is finalized correctly
|
// Finalize returns true when connection is finalized correctly
|
||||||
func (a *ArdroneAdaptor) Finalize() (errs []error) {
|
func (a *ArdroneAdaptor) Finalize() (errs []error) { return }
|
||||||
return
|
|
||||||
}
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package ardrone
|
package ardrone
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hybridgroup/gobot"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hybridgroup/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initTestArdroneAdaptor() *ArdroneAdaptor {
|
func initTestArdroneAdaptor() *ArdroneAdaptor {
|
||||||
|
@ -4,31 +4,34 @@ import (
|
|||||||
"github.com/hybridgroup/gobot"
|
"github.com/hybridgroup/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
var _ gobot.DriverInterface = (*ArdroneDriver)(nil)
|
var _ gobot.Driver = (*ArdroneDriver)(nil)
|
||||||
|
|
||||||
type ArdroneDriver struct {
|
type ArdroneDriver struct {
|
||||||
gobot.Driver
|
name string
|
||||||
|
connection gobot.Connection
|
||||||
|
gobot.Eventer
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewArdroneDriver creates an ArdroneDriver with specified name.
|
// NewArdroneDriver creates an ArdroneDriver with specified name.
|
||||||
//
|
//
|
||||||
// It add the following events:
|
// It add the following events:
|
||||||
// 'flying' - Sent when the device has taken off.
|
// 'flying' - Sent when the device has taken off.
|
||||||
func NewArdroneDriver(adaptor *ArdroneAdaptor, name string) *ArdroneDriver {
|
func NewArdroneDriver(connection *ArdroneAdaptor, name string) *ArdroneDriver {
|
||||||
d := &ArdroneDriver{
|
d := &ArdroneDriver{
|
||||||
Driver: *gobot.NewDriver(
|
name: name,
|
||||||
name,
|
connection: connection,
|
||||||
"ArdroneDriver",
|
Eventer: gobot.NewEventer(),
|
||||||
adaptor,
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
d.AddEvent("flying")
|
d.AddEvent("flying")
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *ArdroneDriver) Name() string { return a.name }
|
||||||
|
func (a *ArdroneDriver) Connection() gobot.Connection { return a.connection }
|
||||||
|
|
||||||
// adaptor returns ardrone adaptor
|
// adaptor returns ardrone adaptor
|
||||||
func (a *ArdroneDriver) adaptor() *ArdroneAdaptor {
|
func (a *ArdroneDriver) adaptor() *ArdroneAdaptor {
|
||||||
return a.Adaptor().(*ArdroneAdaptor)
|
return a.Connection().(*ArdroneAdaptor)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start returns true if driver is started succesfully
|
// Start returns true if driver is started succesfully
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package ardrone
|
package ardrone
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hybridgroup/gobot"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hybridgroup/gobot"
|
||||||
)
|
)
|
||||||
|
|
||||||
func initTestArdroneDriver() *ArdroneDriver {
|
func initTestArdroneDriver() *ArdroneDriver {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user